def mouseMoveEvent(self, event): deltaX = event.pos().x() - self.realStartDragpos.x() deltaY = event.pos().y() - self.realStartDragpos.y() newPos = QtCore.QPointF() modif = [QtCore.Qt.ControlModifier, QtCore.Qt.ShiftModifier, QtCore.Qt.ControlModifier | QtCore.Qt.ShiftModifier] modif_ctl_shift = QtCore.Qt.ControlModifier | QtCore.Qt.ShiftModifier if event.modifiers() in modif: if event.modifiers() == QtCore.Qt.ControlModifier: newPos.setX(self.startDragpos.x() + deltaX / 2) newPos.setY(self.startDragpos.y() + deltaY / 2) elif event.modifiers() == QtCore.Qt.ShiftModifier: newPos.setX(self.startDragpos.x() + deltaX / 4) newPos.setY(self.startDragpos.y() + deltaY / 4) elif event.modifiers() == modif_ctl_shift: newPos.setX(self.startDragpos.x() + deltaX / 8) newPos.setY(self.startDragpos.y() + deltaY / 8) nevent = QtGui.QMouseEvent(event.type(), newPos, event.button(), event.buttons(), event.modifiers()) super(slider, self).mouseMoveEvent(nevent) self.setValue(self.value() - self.deltaValue) else: super(slider, self).mouseMoveEvent(event)
def mousePressEvent(self, event): self.prevValue = self.value() self.startDragpos = event.pos() emodif = event.modifiers() modif = [ QtCore.Qt.ControlModifier, QtCore.Qt.ShiftModifier, QtCore.Qt.ControlModifier | QtCore.Qt.ShiftModifier ] if event.button() == QtCore.Qt.MidButton: if self.draggers is None: self.draggers = draggers(self, self.isFloat, draggerSteps=self.draggerSteps) self.draggers.increment.connect(self.valueIncremented.emit) self.draggers.show() if self.isFloat: self.draggers.move( self.mapToGlobal( QtCore.QPoint( event.pos().x() - 1, event.pos().y() - self.draggers.height() / 2))) else: self.draggers.move( self.mapToGlobal( QtCore.QPoint( event.pos().x() - 1, event.pos().y() - (self.draggers.height() - self.draggers.height() / 6)))) elif event.button() == self.LeftButton and emodif not in modif: butts = QtCore.Qt.MouseButtons(self.MidButton) nevent = QtGui.QMouseEvent(event.type(), event.pos(), self.MidButton, butts, event.modifiers()) super(slider, self).mousePressEvent(nevent) elif emodif in modif: st_slider = QtWidgets.QStyleOptionSlider() st_slider.initFrom(self) st_slider.orientation = self.orientation() available = self.style().pixelMetric( QtWidgets.QStyle.PM_SliderSpaceAvailable, st_slider, self) xloc = QtWidgets.QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), super(slider, self).value(), available) butts = QtCore.Qt.MouseButtons(self.MidButton) newPos = QtCore.QPointF() newPos.setX(xloc) nevent = QtGui.QMouseEvent(event.type(), newPos, self.MidButton, butts, event.modifiers()) self.startDragpos = newPos self.realStartDragpos = event.pos() super(slider, self).mousePressEvent(nevent) self.deltaValue = self.value() - self.prevValue self.setValue(self.prevValue) else: super(slider, self).mousePressEvent(event)
def __init__(self, parent=None, draggerSteps=INT_SLIDER_DRAG_STEPS, sliderRange=[-100, 100], *args, **kwargs): super(slider, self).__init__(parent, **kwargs) self.sliderRange = sliderRange self.setFocusPolicy(QtCore.Qt.StrongFocus) self.setOrientation(QtCore.Qt.Horizontal) self.draggerSteps = draggerSteps self.isFloat = False self.deltaValue = 0 self.startDragpos = QtCore.QPointF() self.realStartDragpos = QtCore.QPointF() self.LeftButton = QtCore.Qt.LeftButton self.MidButton = QtCore.Qt.MidButton self.draggers = None # if SessionDescriptor().software == "maya": self.LeftButton = QtCore.Qt.MidButton self.MidButton = QtCore.Qt.LeftButton self.defaultValue = 0 self.setRange(self.sliderRange[0], self.sliderRange[1])