class PaletteWidget(QtGui.QWidget): """ widget containing palette """ def __init__(self, project): QtGui.QWidget.__init__(self) self.project = project ### palette ### self.paletteCanvas = PaletteCanvas(self) self.paletteV = Viewer() self.paletteV.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.paletteV.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.paletteV.setWidget(self.paletteCanvas) ### buttons ### self.project.updatePaletteSign.connect(self.paletteCanvas.update) addColorB = Button("add color", "icons/color_add.png", self.addColor) delColorB = Button("delete color", "icons/color_del.png", self.delColor) moveLeftColorB = Button("move color left", "icons/color_move_left.png", self.moveColorLeft) moveRightColorB = Button("move color right", "icons/color_move_right.png", self.moveColorRight) ### layout ### colorButtons = QtGui.QHBoxLayout() colorButtons.setSpacing(0) colorButtons.addWidget(addColorB) colorButtons.addWidget(delColorB) colorButtons.addWidget(moveLeftColorB) colorButtons.addWidget(moveRightColorB) self.layout = QtGui.QVBoxLayout() self.layout.setSpacing(0) self.layout.addWidget(self.paletteV) self.layout.addLayout(colorButtons) self.layout.setContentsMargins(6, 0, 6, 0) self.setLayout(self.layout) def showEvent(self, event): self.paletteV.setFixedWidth(self.paletteCanvas.width() + self.paletteV.verticalScrollBar().width() + 2) def editColor(self, n): col = self.project.colorTable[self.project.color] ok, color = ColorDialog(False, col).getRgb() if not ok: return self.project.saveToUndo("colorTable") self.project.colorTable[n] = color for i in self.project.timeline.getAllCanvas(): i.setColorTable(self.project.colorTable) self.project.updateViewSign.emit() self.paletteCanvas.update() self.project.colorChangedSign.emit() def addColor(self): """ choose a color and add it to the palette """ if not len(self.project.colorTable) >= 256: col = self.project.colorTable[self.project.color] ok, color = ColorDialog(False, col).getRgb() if not ok: return self.project.saveToUndo("colorTable_frames") self.project.colorTable.append(color) self.project.changeColor(len(self.project.colorTable)-1) for i in self.project.timeline.getAllCanvas(): i.setColorTable(self.project.colorTable) self.project.updateViewSign.emit() def delColor(self): """ delete a color, replace with color 0 on canvas """ col, table = self.project.color, self.project.colorTable if col != 0: self.project.saveToUndo("colorTable_frames") table.pop(col) for i in self.project.timeline.getAllCanvas(): i.delColor(col) i.setColorTable(table) self.project.changeColor(col-1) self.project.updateViewSign.emit() def moveColorLeft(self): col, table = self.project.color, self.project.colorTable if col != 0 and col != 1: self.project.saveToUndo("colorTable_frames") table[col], table[col-1] = table[col-1], table[col] for i in self.project.timeline.getAllCanvas(): i.swapColor(col, col-1) i.setColorTable(table) self.project.changeColor(col-1) def moveColorRight(self): col, table = self.project.color, self.project.colorTable if col != len(table)-1 and col != 0: self.project.saveToUndo("colorTable_frames") table[col], table[col+1] = table[col+1], table[col] for i in self.project.timeline.getAllCanvas(): i.swapColor(col, col+1) i.setColorTable(table) self.project.changeColor(col+1)
class TimelineWidget(QWidget): """ widget containing timeline, layers and all their buttons """ def __init__(self, project): QWidget.__init__(self) self.project = project self.selection = False self.toPaste = False ### viewer ### self.layersCanvas = LayersCanvas(self) self.layersV = Viewer() self.layersV.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.layersV.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) self.layersV.setWidget(self.layersCanvas) self.timelineCanvas = TimelineCanvas(self) self.timelineV = Viewer() self.timelineV.setWidget(self.timelineCanvas) self.timelineV.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn) self.timelineV.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.timeVSize = (0, 0) self.timelineV.resyzing.connect(self.adjustSize) self.layersV.verticalScrollBar().valueChanged.connect( lambda v: self.timelineV.verticalScrollBar().setValue(v)) self.timelineV.verticalScrollBar().valueChanged.connect( lambda v: self.layersV.verticalScrollBar().setValue(v)) self.project.updateTimelineSign.connect(self.timelineCanvas.update) self.project.updateTimelineSign.connect(self.layersCanvas.update) self.project.updateTimelineSizeSign.connect(self.adjustSize) ### adding and deleting layers ### self.addLayerB = Button("add layer", "icons/layer_add.png", self.addLayerClicked) self.dupLayerB = Button("duplicate layer", "icons/layer_dup.png", self.duplicateLayerClicked) self.delLayerB = Button("delete layer", "icons/layer_del.png", self.deleteLayerClicked) self.mergeLayerB = Button("merge layer", "icons/layer_merge.png", self.mergeLayerClicked) self.upLayerB = Button("move up layer", "icons/layer_up.png", self.upLayerClicked) self.downLayerB = Button("move down layer", "icons/layer_down.png", self.downLayerClicked) ### adding and deleting images ### self.addFrameB = Button("add frame", "icons/frame_add.png", self.addFrameClicked) self.dupFrameB = Button("duplicate frame", "icons/frame_dup.png", self.duplicateFrameClicked) self.delFrameB = Button("delete frame", "icons/frame_del.png", self.deleteFrameClicked) self.clearFrameB = Button("clear frame", "icons/frame_clear.png", self.clearFrameClicked) ### ponionskin ### self.onionSkinPrevB = Button("onion skin - previous frame", "icons/onionskin_prev.png", self.onionskinPrevClicked, True) self.onionSkinNextB = Button("onion skin - next frame", "icons/onionskin_next.png", self.onionskinNextClicked, True) ### play the animation ### self.fpsW = QSpinBox(self) self.fpsW.setValue(self.project.fps) self.fpsW.setRange(1, 60) self.fpsW.setSuffix(" fps") self.project.updateFpsSign.connect(lambda: self.fpsW.setValue(self.project.fps)) self.fpsW.valueChanged.connect(self.fpsChanged) self.repeatB = Button("no repeat / repeat", "icons/play_no_repeat.png", self.repeatClicked) self.playFrameB = Button("play / pause", "icons/play_play.png", self.playPauseClicked) self.playFrameB.state = "play" ### layout ### layerTools = QVBoxLayout() layerTools.setSpacing(0) layerTools.addWidget(self.addLayerB) layerTools.addWidget(self.dupLayerB) layerTools.addWidget(self.delLayerB) layerTools.addWidget(self.mergeLayerB) layerTools.addStretch() layerTools2 = QHBoxLayout() layerTools2.setSpacing(0) layerTools2.addWidget(self.upLayerB) layerTools2.addWidget(self.downLayerB) canvasTools = QHBoxLayout() canvasTools.setSpacing(0) canvasTools.addWidget(self.addFrameB) canvasTools.addWidget(self.dupFrameB) canvasTools.addWidget(self.delFrameB) canvasTools.addWidget(self.clearFrameB) canvasTools.addStretch() canvasTools.addWidget(self.onionSkinPrevB) canvasTools.addWidget(self.onionSkinNextB) canvasTools.addWidget(self.fpsW) canvasTools.addWidget(self.repeatB) canvasTools.addWidget(self.playFrameB) layout = QGridLayout() layout.setSpacing(4) layout.addLayout(layerTools, 0, 0) layout.addWidget(self.layersV, 0, 1) layout.addWidget(self.timelineV, 0, 2) layout.addLayout(layerTools2, 1, 1) layout.addLayout(canvasTools, 1, 2) self.setLayout(layout) ######## Size adjust ############################################### def showEvent(self, event): self.timelineCanvas.setMinimumHeight(len(self.project.timeline) * 20 + 25) self.timelineCanvas.update() self.layersCanvas.setMinimumHeight(self.timelineCanvas.height()) self.layersCanvas.update() self.layersV.setViewportMargins(0, 0, 0, self.timelineV.horizontalScrollBar().height()) self.layersV.setMinimumWidth(self.layersCanvas.width() + self.layersV.verticalScrollBar().width() + 2) self.layersV.setMaximumWidth(self.layersCanvas.width() + self.layersV.verticalScrollBar().width() + 2) def adjustSize(self, timeVSize=False): if timeVSize: self.timeVSize = timeVSize else: timeVSize = self.timeVSize wW = timeVSize[0] wH = timeVSize[1] - self.timelineV.horizontalScrollBar().height() timeMin = self.timelineCanvas.getMiniSize() self.timelineCanvas.setFixedWidth(timeMin[0] + wW - self.timelineCanvas.frameWidth) if timeMin[1] > wH - 2: self.timelineCanvas.setFixedHeight(timeMin[1]) self.layersCanvas.setFixedHeight(timeMin[1]) else: self.timelineCanvas.setFixedHeight(wH - 2) self.layersCanvas.setFixedHeight(wH - 2) self.timelineCanvas.update() self.layersCanvas.update() ######## Copy ###################################################### def cut(self): if self.selection: self.project.saveToUndo("frames") layer = self.project.timeline[self.selection[0]] f1, f2 = self.selection[1], self.selection[2] if f2 < f1: f1, f2, = f2, f1 # copy frames self.toPaste = layer[f1:f2 + 1] # check if first frame is a real canvas if not self.toPaste[0]: self.toPaste[0] = layer.getCanvas(f1).copy_() # check if frame next to selection is a real canvas nex = layer.getCanvas(f2 + 1) if nex and nex != layer.getCanvas(f1): layer[f2 + 1] = nex.copy_() # delete cutted frames del layer[f1:f2 + 1] self.timelineCanvas.update() def copy(self): if self.selection: layer = self.project.timeline[self.selection[0]] f1, f2 = self.selection[1], self.selection[2] if f2 < f1: f1, f2, = f2, f1 # copy frames self.toPaste = layer[f1:f2 + 1] # check if first frame is a real canvas if not self.toPaste[0]: self.toPaste[0] = layer.getCanvas(f1).copy_() # make a real copy of all canvas for n, canvas in enumerate(self.toPaste): if canvas: self.toPaste[n] = canvas.copy_() def paste(self): if self.toPaste: self.project.saveToUndo("frames") f = self.project.curFrame l = self.project.curLayer while f > len(self.project.timeline[l]): self.project.timeline[l].append(0) for n, canvas in enumerate(self.toPaste): self.project.timeline[l].insert(f + n, canvas) self.timelineCanvas.update() self.project.updateViewSign.emit() ######## Buttons ################################################### def addFrameClicked(self): self.project.saveToUndo("frames") self.project.timeline[self.project.curLayer].insertCanvas( self.project.curFrame, self.project.makeCanvas()) self.adjustSize() self.project.updateViewSign.emit() def duplicateFrameClicked(self): self.project.saveToUndo("frames") layer = self.project.timeline[self.project.curLayer] layer.insertCanvas(self.project.curFrame, layer.getCanvas(self.project.curFrame).copy_()) self.adjustSize() self.project.updateViewSign.emit() def deleteFrameClicked(self): self.project.saveToUndo("frames") layer = self.project.timeline[self.project.curLayer] frame = self.project.curFrame if frame >= len(layer): return if len(layer) == 1 and frame == 0: layer[frame].clear() elif layer[frame] and not frame + 1 >= len(layer) and not layer[frame + 1]: layer.pop(frame + 1) else: layer.pop(frame) self.adjustSize() self.project.updateViewSign.emit() def clearFrameClicked(self): f = self.project.timeline.getCanvas() if f: f.clear() self.project.updateViewSign.emit() def addLayerClicked(self): self.project.saveToUndo("frames") self.project.timeline.insert(self.project.curLayer, self.project.makeLayer()) self.adjustSize() self.project.updateViewSign.emit() def duplicateLayerClicked(self): self.project.saveToUndo("frames") layer = self.project.timeline[self.project.curLayer].deepCopy() layer.name = "%s copy" % (layer.name) self.project.timeline.insert(self.project.curLayer, layer) self.adjustSize() self.project.updateViewSign.emit() def deleteLayerClicked(self): self.project.saveToUndo("frames") del self.project.timeline[self.project.curLayer] self.project.curLayer = 0 if not self.project.timeline: self.project.timeline.append(self.project.makeLayer()) self.adjustSize() self.project.updateViewSign.emit() def mergeLayerClicked(self): if not self.project.curLayer < len(self.project.timeline) - 1: return self.project.saveToUndo("frames") layer1 = self.project.timeline[self.project.curLayer + 1] layer2 = self.project.timeline[self.project.curLayer] layer3 = self.project.makeLayer(False, True) layer3.name = layer1.name for i in range(max(len(layer1), len(layer2))): if i == len(layer1): layer3.append(layer2.getCanvas(i).copy_()) elif i == len(layer2): layer3.append(layer1.getCanvas(i).copy_()) elif i > len(layer1): if layer2[i]: layer3.append(layer2[i].copy_()) else: layer3.append(0) elif i > len(layer2): if layer1[i]: layer3.append(layer1[i].copy_()) else: layer3.append(0) elif layer1[i] or layer2[i]: c = layer1.getCanvas(i).copy_() c.mergeCanvas(layer2.getCanvas(i)) layer3.append(c) else: layer3.append(0) del self.project.timeline[self.project.curLayer] self.project.timeline[self.project.curLayer] = layer3 self.adjustSize() self.project.updateViewSign.emit() def upLayerClicked(self): self.project.saveToUndo("frames") l = self.project.curLayer f = self.project.timeline if l > 0: f[l], f[l - 1] = f[l - 1], f[l] self.project.curLayer = l - 1 self.project.updateViewSign.emit() self.project.updateTimelineSign.emit() def downLayerClicked(self): self.project.saveToUndo("frames") l = self.project.curLayer f = self.project.timeline if l < len(f) - 1: f[l], f[l + 1] = f[l + 1], f[l] self.project.curLayer = l + 1 self.project.updateViewSign.emit() self.project.updateTimelineSign.emit() def renameLayer(self, l): name = self.project.timeline[l].name nName = RenameLayerDialog(name).getReturn() if nName: self.project.saveToUndo("frames") self.project.timeline[l].name = str(nName) self.project.updateTimelineSign.emit() ######## Onionskin ################################################# def onionskinPrevClicked(self): self.project.onionSkinPrev = self.onionSkinPrevB.isChecked() self.project.updateViewSign.emit() def onionskinNextClicked(self): self.project.onionSkinNext = self.onionSkinNextB.isChecked() self.project.updateViewSign.emit() ######## Play ###################################################### def fpsChanged(self): if 0 < self.fpsW.value() < 161: self.project.fps = self.fpsW.value() self.timelineCanvas.update() def repeatClicked(self): if self.project.loop: self.repeatB.setIcon(QIcon(QPixmap("icons/play_no_repeat.png"))) self.project.loop = False else: self.repeatB.setIcon(QIcon(QPixmap("icons/play_repeat.png"))) self.project.loop = True self.project.updateViewSign.emit() def playPauseClicked(self): """play the animation""" if self.playFrameB.state == 'play': self.playFrameB.setIcon(QIcon(QPixmap("icons/play_pause.png"))) self.playFrameB.state = "stop" self.timer = QTimer() self.timer.timeout.connect(self.animate) self.f = self.project.curFrame self.fps = self.project.fps self.project.playing = True maxF = max([len(l) for l in self.project.timeline]) if self.project.curFrame + 1 >= maxF: self.project.curFrame = 0 self.timelineCanvas.update() self.project.updateViewSign.emit() self.timer.start(1000 // self.fps) elif self.playFrameB.state == 'stop': self.playEnd() def playEnd(self): self.timer.stop() self.playFrameB.state = "play" self.playFrameB.setIcon(QIcon(QPixmap("icons/play_play.png"))) self.project.playing = False self.project.updateViewSign.emit() def animate(self): if self.fps != self.project.fps: self.fps = self.project.fps self.timer.setInterval(1000 // self.fps) maxF = max([len(l) for l in self.project.timeline]) self.f = self.project.curFrame + 1 if self.f < maxF: self.project.curFrame = self.f self.timelineCanvas.update() self.project.updateViewSign.emit() else: if self.project.loop: self.project.curFrame = 0 self.timelineCanvas.update() self.project.updateViewSign.emit() else: self.playEnd()
class TimelineWidget(QtGui.QWidget): """ widget containing timeline, layers and all their buttons """ def __init__(self, project): QtGui.QWidget.__init__(self) self.project = project self.selection = False self.toPaste = False ### viewer ### self.layersCanvas = LayersCanvas(self) self.layersV = Viewer() self.layersV.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.layersV.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.layersV.setWidget(self.layersCanvas) self.timelineCanvas = TimelineCanvas(self) self.timelineV = Viewer() self.timelineV.setWidget(self.timelineCanvas) self.timelineV.setHorizontalScrollBarPolicy( QtCore.Qt.ScrollBarAlwaysOn) self.timelineV.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.timeVSize = (0, 0) self.timelineV.resyzing.connect(self.adjustSize) self.layersV.verticalScrollBar().valueChanged.connect( lambda v: self.timelineV.verticalScrollBar().setValue(v)) self.timelineV.verticalScrollBar().valueChanged.connect( lambda v: self.layersV.verticalScrollBar().setValue(v)) self.project.updateTimelineSign.connect(self.timelineCanvas.update) self.project.updateTimelineSign.connect(self.layersCanvas.update) self.project.updateTimelineSizeSign.connect(self.adjustSize) ### adding and deleting layers ### self.addLayerB = Button("add layer", "icons/layer_add.png", self.addLayerClicked) self.dupLayerB = Button("duplicate layer", "icons/layer_dup.png", self.duplicateLayerClicked) self.delLayerB = Button("delete layer", "icons/layer_del.png", self.deleteLayerClicked) self.mergeLayerB = Button("merge layer", "icons/layer_merge.png", self.mergeLayerClicked) self.upLayerB = Button("move up layer", "icons/layer_up.png", self.upLayerClicked) self.downLayerB = Button("move down layer", "icons/layer_down.png", self.downLayerClicked) ### adding and deleting images ### self.addFrameB = Button("add frame", "icons/frame_add.png", self.addFrameClicked) self.dupFrameB = Button("duplicate frame", "icons/frame_dup.png", self.duplicateFrameClicked) self.delFrameB = Button("delete frame", "icons/frame_del.png", self.deleteFrameClicked) self.clearFrameB = Button("clear frame", "icons/frame_clear.png", self.clearFrameClicked) ### play the animation ### self.fpsW = QtGui.QSpinBox(self) self.fpsW.setValue(self.project.fps) self.fpsW.setRange(1, 60) self.fpsW.setSuffix(" fps") self.project.updateFpsSign.connect( lambda: self.fpsW.setValue(self.project.fps)) self.fpsW.valueChanged.connect(self.fpsChanged) self.repeatB = Button("no repeat / repeat", "icons/play_no_repeat.png", self.repeatClicked) self.playFrameB = Button("play / pause", "icons/play_play.png", self.playPauseClicked) self.playFrameB.state = "play" ### layout ### layerTools = QtGui.QVBoxLayout() layerTools.setSpacing(0) layerTools.addWidget(self.addLayerB) layerTools.addWidget(self.dupLayerB) layerTools.addWidget(self.delLayerB) layerTools.addWidget(self.mergeLayerB) layerTools.addStretch() layerTools2 = QtGui.QHBoxLayout() layerTools2.setSpacing(0) layerTools2.addWidget(self.upLayerB) layerTools2.addWidget(self.downLayerB) canvasTools = QtGui.QHBoxLayout() canvasTools.setSpacing(0) canvasTools.addWidget(self.addFrameB) canvasTools.addWidget(self.dupFrameB) canvasTools.addWidget(self.delFrameB) canvasTools.addWidget(self.clearFrameB) canvasTools.addStretch() canvasTools.addWidget(self.fpsW) canvasTools.addWidget(self.repeatB) canvasTools.addWidget(self.playFrameB) layout = QtGui.QGridLayout() layout.setSpacing(4) layout.addLayout(layerTools, 0, 0) layout.addWidget(self.layersV, 0, 1) layout.addWidget(self.timelineV, 0, 2) layout.addLayout(layerTools2, 1, 1) layout.addLayout(canvasTools, 1, 2) self.setLayout(layout) ######## Size adjust ############################################### def showEvent(self, event): self.timelineCanvas.setMinimumHeight( len(self.project.timeline) * 20 + 25) self.timelineCanvas.update() self.layersCanvas.setMinimumHeight(self.timelineCanvas.height()) self.layersCanvas.update() self.layersV.setViewportMargins( 0, 0, 0, self.timelineV.horizontalScrollBar().height()) self.layersV.setMinimumWidth(self.layersCanvas.width() + self.layersV.verticalScrollBar().width() + 2) self.layersV.setMaximumWidth(self.layersCanvas.width() + self.layersV.verticalScrollBar().width() + 2) def adjustSize(self, timeVSize=False): if timeVSize: self.timeVSize = timeVSize else: timeVSize = self.timeVSize wW = timeVSize[0] wH = timeVSize[1] - self.timelineV.horizontalScrollBar().height() timeMin = self.timelineCanvas.getMiniSize() self.timelineCanvas.setFixedWidth(timeMin[0] + wW - self.timelineCanvas.frameWidth) if timeMin[1] > wH - 2: self.timelineCanvas.setFixedHeight(timeMin[1]) self.layersCanvas.setFixedHeight(timeMin[1]) else: self.timelineCanvas.setFixedHeight(wH - 2) self.layersCanvas.setFixedHeight(wH - 2) self.timelineCanvas.update() self.layersCanvas.update() ######## Copy ###################################################### def cut(self): if self.selection: self.project.saveToUndo("frames") layer = self.project.timeline[self.selection[0]] f1, f2 = self.selection[1], self.selection[2] if f2 < f1: f1, f2, = f2, f1 # copy frames self.toPaste = layer[f1:f2 + 1] # check if first frame is a real canvas if not self.toPaste[0]: self.toPaste[0] = layer.getCanvas(f1).copy_() # check if frame next to selection is a real canvas nex = layer.getCanvas(f2 + 1) if nex and nex != layer.getCanvas(f1): layer[f2 + 1] = nex.copy_() # delete cutted frames del layer[f1:f2 + 1] self.timelineCanvas.update() def copy(self): if self.selection: layer = self.project.timeline[self.selection[0]] f1, f2 = self.selection[1], self.selection[2] if f2 < f1: f1, f2, = f2, f1 # copy frames self.toPaste = layer[f1:f2 + 1] # check if first frame is a real canvas if not self.toPaste[0]: self.toPaste[0] = layer.getCanvas(f1).copy_() # make a real copy of all canvas for n, canvas in enumerate(self.toPaste): if canvas: self.toPaste[n] = canvas.copy_() def paste(self): if self.toPaste: self.project.saveToUndo("frames") f = self.project.curFrame l = self.project.curLayer while f > len(self.project.timeline[l]): self.project.timeline[l].append(0) for n, canvas in enumerate(self.toPaste): self.project.timeline[l].insert(f + n, canvas) self.timelineCanvas.update() self.project.updateViewSign.emit() ######## Buttons ################################################### def addFrameClicked(self): self.project.saveToUndo("frames") self.project.timeline[self.project.curLayer].insertCanvas( self.project.curFrame, self.project.makeCanvas()) self.adjustSize() self.project.updateViewSign.emit() def duplicateFrameClicked(self): self.project.saveToUndo("frames") layer = self.project.timeline[self.project.curLayer] layer.insertCanvas(self.project.curFrame, layer.getCanvas(self.project.curFrame).copy_()) self.adjustSize() self.project.updateViewSign.emit() def deleteFrameClicked(self): self.project.saveToUndo("frames") layer = self.project.timeline[self.project.curLayer] frame = self.project.curFrame if frame >= len(layer): return if len(layer) == 1 and frame == 0: layer[frame].clear() elif layer[frame] and not frame + 1 >= len(layer) and not layer[frame + 1]: layer.pop(frame + 1) else: layer.pop(frame) self.adjustSize() self.project.updateViewSign.emit() def clearFrameClicked(self): f = self.project.timeline.getCanvas() if f: f.clear() self.project.updateViewSign.emit() def addLayerClicked(self): self.project.saveToUndo("frames") self.project.timeline.insert(self.project.curLayer, self.project.makeLayer()) self.adjustSize() self.project.updateViewSign.emit() def duplicateLayerClicked(self): self.project.saveToUndo("frames") layer = self.project.timeline[self.project.curLayer].deepCopy() layer.name = "%s copy" % (layer.name) self.project.timeline.insert(self.project.curLayer, layer) self.adjustSize() self.project.updateViewSign.emit() def deleteLayerClicked(self): self.project.saveToUndo("frames") del self.project.timeline[self.project.curLayer] self.project.curLayer = 0 if not self.project.timeline: self.project.timeline.append(self.project.makeLayer()) self.adjustSize() self.project.updateViewSign.emit() def mergeLayerClicked(self): if not self.project.curLayer < len(self.project.timeline) - 1: return self.project.saveToUndo("frames") layer1 = self.project.timeline[self.project.curLayer + 1] layer2 = self.project.timeline[self.project.curLayer] layer3 = self.project.makeLayer(False, True) layer3.name = layer1.name for i in range(max(len(layer1), len(layer2))): if i == len(layer1): layer3.append(layer2.getCanvas(i).copy_()) elif i == len(layer2): layer3.append(layer1.getCanvas(i).copy_()) elif i > len(layer1): if layer2[i]: layer3.append(layer2[i].copy_()) else: layer3.append(0) elif i > len(layer2): if layer1[i]: layer3.append(layer1[i].copy_()) else: layer3.append(0) elif layer1[i] or layer2[i]: c = layer1.getCanvas(i).copy_() c.mergeCanvas(layer2.getCanvas(i)) layer3.append(c) else: layer3.append(0) del self.project.timeline[self.project.curLayer] self.project.timeline[self.project.curLayer] = layer3 self.adjustSize() self.project.updateViewSign.emit() def upLayerClicked(self): self.project.saveToUndo("frames") l = self.project.curLayer f = self.project.timeline if l > 0: f[l], f[l - 1] = f[l - 1], f[l] self.project.curLayer = l - 1 self.project.updateViewSign.emit() self.project.updateTimelineSign.emit() def downLayerClicked(self): self.project.saveToUndo("frames") l = self.project.curLayer f = self.project.timeline if l < len(f) - 1: f[l], f[l + 1] = f[l + 1], f[l] self.project.curLayer = l + 1 self.project.updateViewSign.emit() self.project.updateTimelineSign.emit() def renameLayer(self, l): name = self.project.timeline[l].name nName = RenameLayerDialog(name).getReturn() if nName: self.project.saveToUndo("frames") self.project.timeline[l].name = str(nName) self.project.updateTimelineSign.emit() ######## Play ###################################################### def fpsChanged(self): if 0 < self.fpsW.value() < 161: self.project.fps = self.fpsW.value() self.timelineCanvas.update() def repeatClicked(self): if self.project.loop: self.repeatB.setIcon( QtGui.QIcon(QtGui.QPixmap("icons/play_no_repeat.png"))) self.project.loop = False else: self.repeatB.setIcon( QtGui.QIcon(QtGui.QPixmap("icons/play_repeat.png"))) self.project.loop = True self.project.updateViewSign.emit() def playPauseClicked(self): """play the animation""" if self.playFrameB.state == 'play': self.playFrameB.setIcon( QtGui.QIcon(QtGui.QPixmap("icons/play_pause.png"))) self.playFrameB.state = "stop" self.timer = QtCore.QTimer() self.timer.timeout.connect(self.animate) self.f = self.project.curFrame self.fps = self.project.fps self.project.playing = True if self.project.curFrame + 1 >= self.project.timeline.frameVisibleCount( ): self.project.curFrame = 0 self.timelineCanvas.update() self.project.updateViewSign.emit() self.timer.start(1000 // self.fps) elif self.playFrameB.state == 'stop': self.playEnd() def playEnd(self): self.timer.stop() self.playFrameB.state = "play" self.playFrameB.setIcon( QtGui.QIcon(QtGui.QPixmap("icons/play_play.png"))) self.project.playing = False self.project.updateViewSign.emit() def animate(self): if self.fps != self.project.fps: self.fps = self.project.fps self.timer.setInterval(1000 // self.fps) self.f = self.project.curFrame + 1 if self.f < self.project.timeline.frameVisibleCount(): self.project.curFrame = self.f self.timelineCanvas.update() self.project.updateViewSign.emit() else: if self.project.loop: self.project.curFrame = 0 self.timelineCanvas.update() self.project.updateViewSign.emit() else: self.playEnd()
class PaletteWidget(QtGui.QWidget): """ widget containing palette """ def __init__(self, project): QtGui.QWidget.__init__(self) self.project = project ### palette ### self.paletteCanvas = PaletteCanvas(self) self.paletteV = Viewer() self.paletteV.setHorizontalScrollBarPolicy( QtCore.Qt.ScrollBarAlwaysOff) self.paletteV.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.paletteV.setWidget(self.paletteCanvas) ### buttons ### self.project.updatePaletteSign.connect(self.paletteCanvas.update) addColorB = Button("add color", "icons/color_add.png", self.addColor) delColorB = Button("delete color", "icons/color_del.png", self.delColor) moveLeftColorB = Button("move color left", "icons/color_move_left.png", self.moveColorLeft) moveRightColorB = Button("move color right", "icons/color_move_right.png", self.moveColorRight) ### layout ### colorButtons = QtGui.QHBoxLayout() colorButtons.setSpacing(0) colorButtons.addWidget(addColorB) colorButtons.addWidget(delColorB) colorButtons.addWidget(moveLeftColorB) colorButtons.addWidget(moveRightColorB) self.layout = QtGui.QVBoxLayout() self.layout.setSpacing(0) self.layout.addWidget(self.paletteV) self.layout.addLayout(colorButtons) self.layout.setContentsMargins(6, 0, 6, 0) self.setLayout(self.layout) def showEvent(self, event): self.paletteV.setFixedWidth(self.paletteCanvas.width() + self.paletteV.verticalScrollBar().width() + 2) def editColor(self, n): col = self.project.colorTable[self.project.color] ok, color = ColorDialog(False, col).getRgb() if not ok: return self.project.saveToUndo("colorTable") self.project.colorTable[n] = color for i in self.project.timeline.getAllCanvas(): i.setColorTable(self.project.colorTable) self.project.updateViewSign.emit() self.paletteCanvas.update() self.project.colorChangedSign.emit() def addColor(self): """ choose a color and add it to the palette """ if not len(self.project.colorTable) >= 256: col = self.project.colorTable[self.project.color] ok, color = ColorDialog(False, col).getRgb() if not ok: return self.project.saveToUndo("colorTable_frames") self.project.colorTable.append(color) self.project.changeColor(len(self.project.colorTable) - 1) for i in self.project.timeline.getAllCanvas(): i.setColorTable(self.project.colorTable) self.project.updateViewSign.emit() def delColor(self): """ delete a color, replace with color 0 on canvas """ col, table = self.project.color, self.project.colorTable if col != 0: self.project.saveToUndo("colorTable_frames") table.pop(col) for i in self.project.timeline.getAllCanvas(): i.delColor(col) i.setColorTable(table) self.project.changeColor(col - 1) self.project.updateViewSign.emit() def moveColorLeft(self): col, table = self.project.color, self.project.colorTable if col != 0 and col != 1: self.project.saveToUndo("colorTable_frames") table[col], table[col - 1] = table[col - 1], table[col] for i in self.project.timeline.getAllCanvas(): i.swapColor(col, col - 1) i.setColorTable(table) self.project.changeColor(col - 1) def moveColorRight(self): col, table = self.project.color, self.project.colorTable if col != len(table) - 1 and col != 0: self.project.saveToUndo("colorTable_frames") table[col], table[col + 1] = table[col + 1], table[col] for i in self.project.timeline.getAllCanvas(): i.swapColor(col, col + 1) i.setColorTable(table) self.project.changeColor(col + 1)
class PaletteWidget(QWidget): """ side widget containing palette """ def __init__(self, project): QWidget.__init__(self) self.project = project self.setContextMenuPolicy(Qt.ActionsContextMenu) ### context menu ### newAction = QAction("New", self) self.addAction(newAction) mergeAction = QAction("Merge", self) self.addAction(mergeAction) deleteAction = QAction("Delete", self) self.addAction(deleteAction) cutAction = QAction("Cut", self) self.addAction(cutAction) copyAction = QAction("Copy", self) self.addAction(copyAction) pasteAction = QAction("Paste", self) self.addAction(pasteAction) lockAction = QAction("Lock", self) lockAction.setCheckable(True) self.addAction(lockAction) ### palette ### self.paletteCanvas = PaletteCanvas(self) self.paletteV = Viewer() self.paletteV.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.paletteV.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) self.paletteV.setWidget(self.paletteCanvas) self.project.updatePaletteSign.connect(self.paletteCanvas.update) addColorB = Button("add color", "icons/color_add.png", self.addColor) delColorB = Button("delete color", "icons/color_del.png", self.delColor) ### Layout ### colorButtons = QHBoxLayout() colorButtons.setSpacing(0) colorButtons.addWidget(addColorB) colorButtons.addWidget(delColorB) paintOption = QHBoxLayout() paintOption.setSpacing(0) paintOption.addStretch() layout = QGridLayout() layout.setSpacing(0) layout.addLayout(paintOption, 1, 1) layout.addWidget(self.paletteV, 2, 1) layout.addLayout(colorButtons, 3, 1) layout.setContentsMargins(6, 0, 6, 0) self.setLayout(layout) def showEvent(self, event): self.paletteV.setFixedWidth(self.paletteCanvas.width() + self.paletteV.verticalScrollBar().width() + 2) ######## Color ##################################################### def updateColorTable(self): for i in self.project.timeline.getAllCanvas(): i.setColorTable(self.project.colorTable) self.project.updateViewSign.emit() self.paletteCanvas.update() self.project.colorChanged.emit() def colorDialog(self): current = self.project.colorTable[self.project.color] return QColorDialog.getColor(QColor(current), self) def editColor(self, n): color = self.colorDialog() if not color.isValid(): return self.project.saveToUndo("colorTable") self.project.colorTable[n] = color.rgba() self.updateColorTable() def addColor(self): """ select a color and add it to the palette""" if not len(self.project.colorTable) >= 256: color = self.colorDialog() if not color.isValid(): return self.project.saveToUndo("colorTable_frames") self.project.colorTable.append(color) self.project.setColorIndex(len(self.project.colorTable) - 1) for i in self.project.timeline.getAllCanvas(): i.setColorTable(self.project.colorTable) self.project.updateViewSign.emit() def delColor(self): col, table = self.project.color, self.project.colorTable if col != 0: self.project.saveToUndo("colorTable_frames") table.pop(col) for i in self.project.timeline.getAllCanvas(): i.delColor(col) i.setColorTable(table) self.project.setColorIndex(col - 1) self.project.updateViewSign.emit()