Fix: TickSliderItem method uses function from subclass GradientEditorItem (#1439)

* Fix: TickSliderItem method uses function from subclass GradientEditorItem

* Improved code layout

Previously, creating a `GradientEditorItem` lead to 34 executions
of `updateGradient`, with these improvements, only 2 are needed.
Further, removed duplicate code and used signals whenever possible.

Co-authored-by: 2xB <2xB@users.noreply.github.com>
This commit is contained in:
2xB 2020-11-16 19:56:58 +01:00 committed by GitHub
parent b21967d20d
commit 01aee93a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,6 +45,9 @@ class TickSliderItem(GraphicsWidget):
A rectangular item with tick marks along its length that can (optionally) be moved by the user."""
sigTicksChanged = QtCore.Signal(object)
sigTicksChangeFinished = QtCore.Signal(object)
def __init__(self, orientation='bottom', allowAdd=True, **kargs):
"""
============== =================================================================================
@ -147,7 +150,7 @@ class TickSliderItem(GraphicsWidget):
self.translate(self.tickSize/2., 0)
def addTick(self, x, color=None, movable=True):
def addTick(self, x, color=None, movable=True, finish=True):
## public
"""
Add a tick to the item.
@ -163,12 +166,22 @@ class TickSliderItem(GraphicsWidget):
if color is None:
color = QtGui.QColor(255,255,255)
tick = Tick(self, [x*self.length, 0], color, movable, self.tickSize, pen=self.tickPen)
tick = Tick([x*self.length, 0], color, movable, self.tickSize, pen=self.tickPen)
self.ticks[tick] = x
tick.setParentItem(self)
tick.sigMoving.connect(self.tickMoved)
tick.sigMoved.connect(self.tickMoveFinished)
tick.sigClicked.connect(self.tickClicked)
self.sigTicksChanged.emit(self)
if finish:
self.sigTicksChangeFinished.emit(self)
return tick
def removeTick(self, tick):
def removeTick(self, tick, finish=True):
## public
"""
Removes the specified tick.
@ -178,6 +191,11 @@ class TickSliderItem(GraphicsWidget):
if self.scene() is not None:
self.scene().removeItem(tick)
self.sigTicksChanged.emit(self)
if finish:
self.sigTicksChangeFinished.emit(self)
def tickMoved(self, tick, pos):
#print "tick changed"
## Correct position of tick if it has left bounds.
@ -186,8 +204,10 @@ class TickSliderItem(GraphicsWidget):
tick.setPos(pos)
self.ticks[tick] = float(newX) / self.length
self.sigTicksChanged.emit(self)
def tickMoveFinished(self, tick):
pass
self.sigTicksChangeFinished.emit(self)
def tickClicked(self, tick, ev):
if ev.button() == QtCore.Qt.RightButton:
@ -295,6 +315,9 @@ class TickSliderItem(GraphicsWidget):
tick.update()
#tick.setBrush(QtGui.QBrush(QtGui.QColor(tick.color)))
self.sigTicksChanged.emit(self)
self.sigTicksChangeFinished.emit(self)
def setTickValue(self, tick, val):
## public
"""
@ -316,7 +339,9 @@ class TickSliderItem(GraphicsWidget):
pos.setX(x)
tick.setPos(pos)
self.ticks[tick] = val
self.updateGradient()
self.sigTicksChanged.emit(self)
self.sigTicksChangeFinished.emit(self)
def tickValue(self, tick):
## public
@ -463,6 +488,9 @@ class GradientEditorItem(TickSliderItem):
self.updateGradient()
self.linkedGradients = {}
self.sigTicksChanged.connect(lambda *args, **kwargs: self.updateGradient())
self.sigTicksChangeFinished.connect(self.sigGradientChangeFinished.emit)
def showTicks(self, show=True):
for tick in self.ticks.keys():
if show:
@ -529,7 +557,9 @@ class GradientEditorItem(TickSliderItem):
self.rgbAction.blockSignals(False)
self.hsvAction.blockSignals(False)
self.colorMode = cm
self.updateGradient()
self.sigTicksChanged.emit(self)
self.sigGradientChangeFinished.emit(self)
def colorMap(self):
"""Return a ColorMap object representing the current state of the editor."""
@ -554,18 +584,16 @@ class GradientEditorItem(TickSliderItem):
TickSliderItem.setLength(self, newLen)
self.backgroundRect.setRect(1, -self.rectSize, newLen, self.rectSize)
self.gradRect.setRect(1, -self.rectSize, newLen, self.rectSize)
self.updateGradient()
self.sigTicksChanged.emit(self)
def currentColorChanged(self, color):
#private
if color.isValid() and self.currentTick is not None:
self.setTickColor(self.currentTick, color)
self.updateGradient()
def currentColorRejected(self):
#private
self.setTickColor(self.currentTick, self.currentTickColor)
self.updateGradient()
def currentColorAccepted(self):
self.sigGradientChangeFinished.emit(self)
@ -589,15 +617,9 @@ class GradientEditorItem(TickSliderItem):
self.tickMenu = TickMenu(tick, self)
self.tickMenu.popup(ev.screenPos().toQPoint())
def tickMoved(self, tick, pos):
#private
TickSliderItem.tickMoved(self, tick, pos)
self.updateGradient()
def tickMoveFinished(self, tick):
self.sigGradientChangeFinished.emit(self)
def getGradient(self):
"""Return a QLinearGradient object."""
g = QtGui.QLinearGradient(QtCore.QPointF(0,0), QtCore.QPointF(self.length,0))
@ -727,12 +749,6 @@ class GradientEditorItem(TickSliderItem):
return False
return True
def mouseReleaseEvent(self, ev):
#private
TickSliderItem.mouseReleaseEvent(self, ev)
self.updateGradient()
def addTick(self, x, color=None, movable=True, finish=True):
"""
Add a tick to the gradient. Return the tick.
@ -746,25 +762,14 @@ class GradientEditorItem(TickSliderItem):
============== ==================================================================
"""
if color is None:
color = self.getColor(x)
t = TickSliderItem.addTick(self, x, color=color, movable=movable)
t = TickSliderItem.addTick(self, x, color=color, movable=movable, finish=finish)
t.colorChangeAllowed = True
t.removeAllowed = True
if finish:
self.sigGradientChangeFinished.emit(self)
return t
def removeTick(self, tick, finish=True):
TickSliderItem.removeTick(self, tick)
if finish:
self.updateGradient()
self.sigGradientChangeFinished.emit(self)
def saveState(self):
"""
Return a dictionary with parameters for rebuilding the gradient. Keys will include:
@ -798,6 +803,10 @@ class GradientEditorItem(TickSliderItem):
============== ====================================================================
"""
## public
# Mass edit ticks without graphics update
signalsBlocked = self.blockSignals(True)
self.setColorMode(state['mode'])
for t in list(self.ticks.keys()):
self.removeTick(t, finish=False)
@ -806,10 +815,16 @@ class GradientEditorItem(TickSliderItem):
self.addTick(t[0], c, finish=False)
self.showTicks( state.get('ticksVisible',
next(iter(self.ticks)).isVisible()) )
self.updateGradient()
# Close with graphics update
self.blockSignals(signalsBlocked)
self.sigTicksChanged.emit(self)
self.sigGradientChangeFinished.emit(self)
def setColorMap(self, cm):
# Mass edit ticks without graphics update
signalsBlocked = self.blockSignals(True)
self.setColorMode('rgb')
for t in list(self.ticks.keys()):
self.removeTick(t, finish=False)
@ -818,7 +833,10 @@ class GradientEditorItem(TickSliderItem):
x = cm.pos[i]
c = colors[i]
self.addTick(x, c, finish=False)
self.updateGradient()
# Close with graphics update
self.blockSignals(signalsBlocked)
self.sigTicksChanged.emit(self)
self.sigGradientChangeFinished.emit(self)
def linkGradient(self, slaveGradient, connect=True):
@ -842,13 +860,13 @@ class Tick(QtGui.QGraphicsWidget): ## NOTE: Making this a subclass of GraphicsO
# ..GraphicsScene.items(self, *args) will get Tick object as a
# class of QtGui.QMultimediaWidgets.QGraphicsVideoItem in python2.7-PyQt5(5.4.0)
sigMoving = QtCore.Signal(object)
sigMoving = QtCore.Signal(object, object)
sigMoved = QtCore.Signal(object)
sigClicked = QtCore.Signal(object, object)
def __init__(self, view, pos, color, movable=True, scale=10, pen='w'):
def __init__(self, pos, color, movable=True, scale=10, pen='w'):
self.movable = movable
self.moving = False
self.view = weakref.ref(view)
self.scale = scale
self.color = color
self.pen = fn.mkPen(pen)
@ -895,23 +913,20 @@ class Tick(QtGui.QGraphicsWidget): ## NOTE: Making this a subclass of GraphicsO
newPos.setY(self.pos().y())
self.setPos(newPos)
self.view().tickMoved(self, newPos)
self.sigMoving.emit(self)
self.sigMoving.emit(self, newPos)
if ev.isFinish():
self.moving = False
self.sigMoved.emit(self)
self.view().tickMoveFinished(self)
def mouseClickEvent(self, ev):
ev.accept()
if ev.button() == QtCore.Qt.RightButton and self.moving:
self.setPos(self.startPosition)
self.view().tickMoved(self, self.startPosition)
self.moving = False
self.sigMoving.emit(self)
self.sigMoving.emit(self, self.startPosition)
self.sigMoved.emit(self)
else:
self.view().tickClicked(self, ev)
self.sigClicked.emit(self, ev)
def hoverEvent(self, ev):
if (not ev.isExit()) and ev.acceptDrags(QtCore.Qt.LeftButton):