--- a/eric7/IconEditor/IconEditorGrid.py Sat May 22 12:46:57 2021 +0200 +++ b/eric7/IconEditor/IconEditorGrid.py Sat May 22 12:54:57 2021 +0200 @@ -7,6 +7,7 @@ Module implementing the icon editor grid. """ +import enum import os from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QPoint, QRect, QSize @@ -61,6 +62,25 @@ self.__grid.setIconImage(self.__imageAfter, undoRedo=True) +class IconEditorTool(enum.IntEnum): + """ + Class defining the edit tools. + """ + PENCIL = 1 + RUBBER = 2 + LINE = 3 + RECTANGLE = 4 + FILLED_RECTANGLE = 5 + CIRCLE = 6 + FILLED_CIRCLE = 7 + ELLIPSE = 8 + FILLED_ELLIPSE = 9 + FILL = 10 + COLOR_PICKER = 11 + + SELECT_RECTANGLE = 100 + SELECT_CIRCLE = 101 + class IconEditorGrid(QWidget): """ Class implementing the icon editor grid. @@ -90,23 +110,6 @@ sizeChanged = pyqtSignal(int, int) zoomChanged = pyqtSignal(int) - # convert to Enum - Pencil = 1 - Rubber = 2 - Line = 3 - Rectangle = 4 - FilledRectangle = 5 - Circle = 6 - FilledCircle = 7 - Ellipse = 8 - FilledEllipse = 9 - Fill = 10 - ColorPicker = 11 - - # convert to Enum - RectangleSelection = 20 - CircleSelection = 21 - MarkColor = QColor(255, 255, 255, 255) NoMarkColor = QColor(0, 0, 0, 0) @@ -130,7 +133,7 @@ self.__curColor = Qt.GlobalColor.black self.__zoom = 12 - self.__curTool = self.Pencil + self.__curTool = IconEditorTool.PENCIL self.__startPos = QPoint() self.__endPos = QPoint() self.__dirty = False @@ -144,7 +147,7 @@ self.__currentUndoCmd = None self.__image = QImage(32, 32, QImage.Format.Format_ARGB32) - self.__image.fill(qRgba(0, 0, 0, 0)) + self.__image.fill(Qt.GlobalColor.transparent) self.__markImage = QImage(self.__image) self.__markImage.fill(self.NoMarkColor.rgba()) @@ -209,16 +212,16 @@ for the various drawing tools. """ self.__undoTexts = { - self.Pencil: self.tr("Set Pixel"), - self.Rubber: self.tr("Erase Pixel"), - self.Line: self.tr("Draw Line"), - self.Rectangle: self.tr("Draw Rectangle"), - self.FilledRectangle: self.tr("Draw Filled Rectangle"), - self.Circle: self.tr("Draw Circle"), - self.FilledCircle: self.tr("Draw Filled Circle"), - self.Ellipse: self.tr("Draw Ellipse"), - self.FilledEllipse: self.tr("Draw Filled Ellipse"), - self.Fill: self.tr("Fill Region"), + IconEditorTool.PENCIL: self.tr("Set Pixel"), + IconEditorTool.RUBBER: self.tr("Erase Pixel"), + IconEditorTool.LINE: self.tr("Draw Line"), + IconEditorTool.RECTANGLE: self.tr("Draw Rectangle"), + IconEditorTool.FILLED_RECTANGLE: self.tr("Draw Filled Rectangle"), + IconEditorTool.CIRCLE: self.tr("Draw Circle"), + IconEditorTool.FILLED_CIRCLE: self.tr("Draw Filled Circle"), + IconEditorTool.ELLIPSE: self.tr("Draw Ellipse"), + IconEditorTool.FILLED_ELLIPSE: self.tr("Draw Filled Ellipse"), + IconEditorTool.FILL: self.tr("Fill Region"), } def isDirty(self): @@ -292,28 +295,33 @@ Public method to set the current drawing tool. @param tool drawing tool to be used - (IconEditorGrid.Pencil ... IconEditorGrid.CircleSelection) + @type IconEditorTool """ self.__curTool = tool self.__lastPos = (-1, -1) - if self.__curTool in [self.RectangleSelection, self.CircleSelection]: + if self.__curTool in [ + IconEditorTool.SELECT_RECTANGLE, IconEditorTool.SELECT_CIRCLE + ]: self.__selecting = True else: self.__selecting = False - if self.__curTool in [self.RectangleSelection, self.CircleSelection, - self.Line, self.Rectangle, self.FilledRectangle, - self.Circle, self.FilledCircle, - self.Ellipse, self.FilledEllipse]: + if self.__curTool in [ + IconEditorTool.SELECT_RECTANGLE, IconEditorTool.SELECT_CIRCLE, + IconEditorTool.LINE, + IconEditorTool.RECTANGLE, IconEditorTool.FILLED_RECTANGLE, + IconEditorTool.CIRCLE, IconEditorTool.FILLED_CIRCLE, + IconEditorTool.ELLIPSE, IconEditorTool.FILLED_ELLIPSE + ]: self.setCursor(self.__aimCursor) - elif self.__curTool == self.Fill: + elif self.__curTool == IconEditorTool.FILL: self.setCursor(self.__fillCursor) - elif self.__curTool == self.ColorPicker: + elif self.__curTool == IconEditorTool.COLOR_PICKER: self.setCursor(self.__colorPickerCursor) - elif self.__curTool == self.Pencil: + elif self.__curTool == IconEditorTool.PENCIL: self.setCursor(self.__paintCursor) - elif self.__curTool == self.Rubber: + elif self.__curTool == IconEditorTool.RUBBER: self.setCursor(self.__rubberCursor) else: self.setCursor(self.__normalCursor) @@ -323,7 +331,7 @@ Public method to get the current drawing tool. @return current drawing tool - (IconEditorGrid.Pencil ... IconEditorGrid.CircleSelection) + @rtype IconEditorTool """ return self.__curTool @@ -482,21 +490,21 @@ self.__pasteRect = QRect() return - if self.__curTool == self.Pencil: + if self.__curTool == IconEditorTool.PENCIL: cmd = IconEditCommand(self, self.__undoTexts[self.__curTool], self.__image) self.__setImagePixel(evt.position().toPoint(), True) self.setDirty(True) self.__undoStack.push(cmd) self.__currentUndoCmd = cmd - elif self.__curTool == self.Rubber: + elif self.__curTool == IconEditorTool.RUBBER: cmd = IconEditCommand(self, self.__undoTexts[self.__curTool], self.__image) self.__setImagePixel(evt.position().toPoint(), False) self.setDirty(True) self.__undoStack.push(cmd) self.__currentUndoCmd = cmd - elif self.__curTool == self.Fill: + elif self.__curTool == IconEditorTool.FILL: i, j = self.__imageCoordinates(evt.position().toPoint()) col = QColor() col.setRgba(self.__image.pixel(i, j)) @@ -506,7 +514,7 @@ self.setDirty(True) self.__undoStack.push(cmd) cmd.setAfterImage(self.__image) - elif self.__curTool == self.ColorPicker: + elif self.__curTool == IconEditorTool.COLOR_PICKER: i, j = self.__imageCoordinates(evt.position().toPoint()) col = QColor() col.setRgba(self.__image.pixel(i, j)) @@ -522,7 +530,8 @@ @param evt reference to the mouse event object (QMouseEvent) """ - self.positionChanged.emit(*self.__imageCoordinates(evt.position().toPoint())) + self.positionChanged.emit( + *self.__imageCoordinates(evt.position().toPoint())) if ( self.__isPasting and @@ -532,13 +541,14 @@ return if evt.buttons() & Qt.MouseButton.LeftButton: - if self.__curTool == self.Pencil: + if self.__curTool == IconEditorTool.PENCIL: self.__setImagePixel(evt.position().toPoint(), True) self.setDirty(True) - elif self.__curTool == self.Rubber: + elif self.__curTool == IconEditorTool.RUBBER: self.__setImagePixel(evt.position().toPoint(), False) self.setDirty(True) - elif self.__curTool in [self.Fill, self.ColorPicker]: + elif self.__curTool in [IconEditorTool.FILL, + IconEditorTool.COLOR_PICKER]: pass # do nothing else: self.__drawTool(evt.position().toPoint(), True) @@ -551,16 +561,18 @@ """ if evt.button() == Qt.MouseButton.LeftButton: if ( - self.__curTool in [self.Pencil, self.Rubber] and + self.__curTool in [IconEditorTool.PENCIL, + IconEditorTool.RUBBER] and self.__currentUndoCmd ): self.__currentUndoCmd.setAfterImage(self.__image) self.__currentUndoCmd = None - if self.__curTool not in [self.Pencil, self.Rubber, - self.Fill, self.ColorPicker, - self.RectangleSelection, - self.CircleSelection]: + if self.__curTool not in [ + IconEditorTool.PENCIL, IconEditorTool.RUBBER, + IconEditorTool.FILL, IconEditorTool.COLOR_PICKER, + IconEditorTool.SELECT_RECTANGLE, IconEditorTool.SELECT_CIRCLE + ]: cmd = IconEditCommand(self, self.__undoTexts[self.__curTool], self.__image) if self.__drawTool(evt.position().toPoint(), False): @@ -661,18 +673,20 @@ painter.setPen(drawColor) painter.setCompositionMode(self.__compositingMode) - if self.__curTool == self.Line: + if self.__curTool == IconEditorTool.LINE: painter.drawLine(start, end) - elif self.__curTool in [self.Rectangle, self.FilledRectangle, - self.RectangleSelection]: + elif self.__curTool in [ + IconEditorTool.RECTANGLE, IconEditorTool.FILLED_RECTANGLE, + IconEditorTool.SELECT_RECTANGLE + ]: left = min(start.x(), end.x()) top = min(start.y(), end.y()) right = max(start.x(), end.x()) bottom = max(start.y(), end.y()) - if self.__curTool == self.RectangleSelection: + if self.__curTool == IconEditorTool.SELECT_RECTANGLE: painter.setBrush(QBrush(drawColor)) - if self.__curTool == self.FilledRectangle: + if self.__curTool == IconEditorTool.FILLED_RECTANGLE: for y in range(top, bottom + 1): painter.drawLine(left, y, right, y) else: @@ -683,12 +697,16 @@ self.__selectionAvailable = True self.selectionAvailable.emit(True) - elif self.__curTool in [self.Circle, self.FilledCircle, - self.CircleSelection]: + elif self.__curTool in [ + IconEditorTool.CIRCLE, IconEditorTool.FILLED_CIRCLE, + IconEditorTool.SELECT_CIRCLE + ]: deltaX = abs(start.x() - end.x()) deltaY = abs(start.y() - end.y()) r = max(deltaX, deltaY) - if self.__curTool in [self.FilledCircle, self.CircleSelection]: + if self.__curTool in [ + IconEditorTool.FILLED_CIRCLE, IconEditorTool.SELECT_CIRCLE + ]: painter.setBrush(QBrush(drawColor)) painter.drawEllipse(start, r, r) if self.__selecting: @@ -697,19 +715,23 @@ self.__selectionAvailable = True self.selectionAvailable.emit(True) - elif self.__curTool in [self.Ellipse, self.FilledEllipse]: + elif self.__curTool in [ + IconEditorTool.ELLIPSE, IconEditorTool.FILLED_ELLIPSE + ]: r1 = abs(start.x() - end.x()) r2 = abs(start.y() - end.y()) if r1 == 0 or r2 == 0: return False - if self.__curTool == self.FilledEllipse: + if self.__curTool == IconEditorTool.FILLED_ELLIPSE: painter.setBrush(QBrush(drawColor)) painter.drawEllipse(start, r1, r2) painter.end() - if self.__curTool in [self.Circle, self.FilledCircle, - self.Ellipse, self.FilledEllipse]: + if self.__curTool in [ + IconEditorTool.CIRCLE, IconEditorTool.FILLED_CIRCLE, + IconEditorTool.ELLIPSE, IconEditorTool.FILLED_ELLIPSE + ]: self.update() else: self.__updateRect(self.__startPos, pos) @@ -778,9 +800,11 @@ Private slot to remove the mark indicator. """ self.__markImage.fill(self.NoMarkColor.rgba()) - if self.__curTool in [self.Circle, self.FilledCircle, - self.Ellipse, self.FilledEllipse, - self.CircleSelection]: + if self.__curTool in [ + IconEditorTool.CIRCLE, IconEditorTool.FILLED_CIRCLE, + IconEditorTool.ELLIPSE, IconEditorTool.FILLED_ELLIPSE, + IconEditorTool.SELECT_CIRCLE + ]: self.update() else: self.__updateRect(self.__startPos, self.__endPos) @@ -859,7 +883,7 @@ self.__image) img = QImage(self.__selRect.size(), QImage.Format.Format_ARGB32) - img.fill(qRgba(0, 0, 0, 0)) + img.fill(Qt.GlobalColor.transparent) for i in range(0, self.__selRect.width()): for j in range(0, self.__selRect.height()): if ( @@ -873,7 +897,7 @@ if cut: self.__image.setPixel(self.__selRect.x() + i, self.__selRect.y() + j, - qRgba(0, 0, 0, 0)) + Qt.GlobalColor.transparent) if cut: self.__undoStack.push(cmd) @@ -988,7 +1012,7 @@ self.__unMark() cmd = IconEditCommand(self, self.tr("Clear Image"), self.__image) - self.__image.fill(qRgba(0, 0, 0, 0)) + self.__image.fill(Qt.GlobalColor.transparent) self.update() self.setDirty(True) self.__undoStack.push(cmd) @@ -1027,7 +1051,7 @@ if res == QDialog.DialogCode.Accepted: width, height = dlg.getData() img = QImage(width, height, QImage.Format.Format_ARGB32) - img.fill(qRgba(0, 0, 0, 0)) + img.fill(Qt.GlobalColor.transparent) self.setIconImage(img) def grayScale(self):