From 63f83224f474c2c191e8cac3cb6bda8f2cb81b7d Mon Sep 17 00:00:00 2001 From: Denis Jacquerye Date: Mon, 19 Oct 2015 10:24:34 +0100 Subject: Use PEP8 code style --- Lib/defconQt/glyphCollectionView.py | 230 +++++++++++++++++++++++------------- 1 file changed, 145 insertions(+), 85 deletions(-) (limited to 'Lib/defconQt/glyphCollectionView.py') diff --git a/Lib/defconQt/glyphCollectionView.py b/Lib/defconQt/glyphCollectionView.py index 5f4cadb..c4ee9e4 100644 --- a/Lib/defconQt/glyphCollectionView.py +++ b/Lib/defconQt/glyphCollectionView.py @@ -1,9 +1,12 @@ from defconQt.util import platformSpecific from PyQt5.QtCore import QMimeData, QRectF, QSize, Qt -from PyQt5.QtGui import (QBrush, QColor, QCursor, QDrag, QFont, QFontMetrics, - QKeySequence, QLinearGradient, QPainter, QPen) +from PyQt5.QtGui import ( + QBrush, QColor, QCursor, QDrag, QFont, QFontMetrics, QKeySequence, + QLinearGradient, QPainter, QPen) from PyQt5.QtWidgets import QApplication, QMessageBox, QScrollArea, QWidget -import math, time, unicodedata +import math +import time +import unicodedata cellGridColor = QColor(130, 130, 130) cellHeaderBaseColor = QColor(230, 230, 230) @@ -14,8 +17,8 @@ cellSelectionColor = QColor.fromRgbF(.2, .3, .7, .15) GlyphCellBufferHeight = .2 GlyphCellHeaderHeight = 14 -# TODO: consider extracting each platform-specific thing (fonts, shortcuts) in a -# purposed folder +# TODO: consider extracting each platform-specific thing (fonts, shortcuts) in +# a autpurposed folder headerFont = QFont() headerFont.setFamily('Lucida Sans Unicode') headerFont.insertSubstitution('Lucida Sans Unicode', 'Lucida Grande') @@ -27,9 +30,10 @@ metrics = QFontMetrics(headerFont) arrowKeys = (Qt.Key_Up, Qt.Key_Down, Qt.Key_Left, Qt.Key_Right) + def proceedWithDeletion(self): closeDialog = QMessageBox(QMessageBox.Question, "", "Delete glyphs", - QMessageBox.Yes | QMessageBox.No, self) + QMessageBox.Yes | QMessageBox.No, self) closeDialog.setInformativeText("Are you sure you want to delete them?") closeDialog.setModal(True) ret = closeDialog.exec_() @@ -40,7 +44,10 @@ def proceedWithDeletion(self): """ A widget that presents a list of glyphs in cells. """ + + class GlyphCollectionWidget(QWidget): + def __init__(self, parent=None): super(GlyphCollectionWidget, self).__init__(parent) self.setAttribute(Qt.WA_KeyCompression) @@ -78,10 +85,11 @@ class GlyphCollectionWidget(QWidget): self._glyphs = glyphs self.adjustSize() self.selection = set() - #self.update() # self.selection changed will do it + # self.update() # self.selection changed will do it - glyphs = property(_get_glyphs, _set_glyphs, doc="A list of glyphs \ - displayed. Clears selection and schedules display refresh when set.") + glyphs = property( + _get_glyphs, _set_glyphs, doc="A list of glyphs displayed. Clears " + "selection and schedules display refresh when set.") def _get_selection(self): return self._selection @@ -91,8 +99,9 @@ class GlyphCollectionWidget(QWidget): self.computeCharacterSelected() self.update() - selection = property(_get_selection, _set_selection, doc="A set that contains \ - indexes of selected glyphs. Schedules display refresh when set.") + selection = property( + _get_selection, _set_selection, doc="A set that contains indexes of " + "selected glyphs. Schedules display refresh when set.") def getSelectedGlyphs(self): return [self._glyphs[key] for key in sorted(self._selection)] @@ -109,7 +118,8 @@ class GlyphCollectionWidget(QWidget): if index is not None: self.scrollToCell(index) - lastSelectedCell = property(_get_lastSelectedCell, _set_lastSelectedCell, + lastSelectedCell = property( + _get_lastSelectedCell, _set_lastSelectedCell, doc="The current lastSelectedCell in selection.") def lastSelectedGlyph(self): @@ -122,7 +132,8 @@ class GlyphCollectionWidget(QWidget): def scrollToCell(self, index): x = (.5 + index % self._columns) * self.squareSize y = (.5 + index // self._columns) * self.squareSize - self._scrollArea.ensureVisible(x, y, .5*self.squareSize, .5*self.squareSize) + self._scrollArea.ensureVisible( + x, y, .5 * self.squareSize, .5 * self.squareSize) def _get_currentDropIndex(self): return self._currentDropIndex @@ -141,15 +152,16 @@ class GlyphCollectionWidget(QWidget): def pipeDragMoveEvent(self, event): if event.source() == self: pos = event.posF() - self.currentDropIndex = int(self._columns * (pos.y() // self.squareSize) \ - + (pos.x() + .5*self.squareSize) // self.squareSize) + self.currentDropIndex = int( + self._columns * (pos.y() // self.squareSize) + + (pos.x() + .5 * self.squareSize) // self.squareSize) def pipeDragLeaveEvent(self, event): self.currentDropIndex = None def pipeDropEvent(self, event): - # TODO: consider dropping this check, maybe only subclasses should do it - # so as to dispatch but here we presumably don't need it + # TODO: consider dropping this check, maybe only subclasses should do + # it so as to dispatch but here we presumably don't need it if event.source() == self: insert = self.currentDropIndex newGlyphNames = event.mimeData().text().split(" ") @@ -163,27 +175,34 @@ class GlyphCollectionWidget(QWidget): self._glyphs[index] = None # insert newGlyphs into the list lst = self._glyphs[:insert] - lst.extend(newGlyphs+self._glyphs[insert:]) + lst.extend(newGlyphs + self._glyphs[insert:]) self._glyphs = lst # now, elide None self.currentDropIndex = None - self.glyphs = [glyph for glyph in self._glyphs if glyph != None] + self.glyphs = [glyph + for glyph in self._glyphs if glyph is not None] # TODO: break this down into set width/set square # TODO: see whether scrollArea gets resizeEvents def _sizeEvent(self, width, squareSize=None): - sw = self._scrollArea.verticalScrollBar().width() + self._scrollArea.contentsMargins().right() - if squareSize is not None: self.squareSize = squareSize + sw = self._scrollArea.verticalScrollBar().width( + ) + self._scrollArea.contentsMargins().right() + if squareSize is not None: + self.squareSize = squareSize columns = (width - sw) // self.squareSize - if not columns > 0: return + if not columns > 0: + return self._columns = columns self.adjustSize() def sizeHint(self): - # Calculate sizeHint with max(height, _scrollArea.height()) because if scrollArea is - # bigger than widget height after an update, we risk leaving old painted content on screen - return QSize(self._columns * self.squareSize, - max(math.ceil(len(self._glyphs) / self._columns) * self.squareSize, self._scrollArea.height())) + # Calculate sizeHint with max(height, _scrollArea.height()) because + # if scrollArea is bigger than widget height after an update, we risk + # leaving old painted content on screen + return QSize( + self._columns * self.squareSize, + max(math.ceil(len(self._glyphs) / self._columns) * self.squareSize, + self._scrollArea.height())) def computeCharacterSelected(self): if self.characterSelectedCallback is None: @@ -210,7 +229,7 @@ class GlyphCollectionWidget(QWidget): delta = -1 elif key == Qt.Key_Right: delta = 1 - newSel = self._lastSelectedCell + delta*count + newSel = self._lastSelectedCell + delta * count if newSel < 0 or newSel >= len(self._glyphs): return if modifiers & Qt.ShiftModifier: @@ -229,16 +248,17 @@ class GlyphCollectionWidget(QWidget): elif key == Qt.Key_Return: index = self._lastSelectedCell if index is not None and self.doubleClickCallback is not None: - # TODO: does it still make sense to call this doubleClickCallback? + # TODO: does it still make sense to call this + # doubleClickCallback? self.doubleClickCallback(self._glyphs[index]) elif event.matches(QKeySequence.SelectAll): self.selection = set(range(len(self._glyphs))) elif key == Qt.Key_D and modifiers & Qt.ControlModifier: self.selection = set() - # XXX: this is specific to fontView so should be done thru subclassing of a base widget, - # as is done in groupsView + # XXX: this is specific to fontView so should be done thru subclassing + # of a base widget, as is done in groupsView elif key == platformSpecific.deleteKey: - #if self.characterDeletionCallback is not None: + # if self.characterDeletionCallback is not None: if proceedWithDeletion(self) and self.selection: # we need to del in reverse order to keep key references valid for key in sorted(self._selection, reverse=True): @@ -252,8 +272,8 @@ class GlyphCollectionWidget(QWidget): # XXX: have template setter clear glyph content glyph.template = True self.selection = set() - elif modifiers in (Qt.NoModifier, Qt.ShiftModifier) and \ - unicodedata.category(event.text()) != "Cc": + elif (modifiers in (Qt.NoModifier, Qt.ShiftModifier) and + unicodedata.category(event.text()) != "Cc"): # adapted from defconAppkit # get the current time rightNow = time.time() @@ -274,22 +294,26 @@ class GlyphCollectionWidget(QWidget): lastResortIndex = None for index, glyph in enumerate(self._glyphs): item = glyph.name - # if the item starts with the input string, it is considered a match + # if the item starts with the input string, it is considered + # a match if item.startswith(self._inputString): if match is None: match = item matchIndex = index continue - # only if the item is less than the previous match is it a more relevant match + # only if the item is less than the previous match is it + # a more relevant match # example: # given this order: sys, signal # and this input string: s - # sys will be the first match, but signal is the more accurate match + # sys will be the first match, but signal is the more + # accurate match if item < match: match = item matchIndex = index continue - # if the item is greater than the input string,it can be used as a last resort + # if the item is greater than the input string,it can be used + # as a last resort # example: # given this order: vanilla, zipimport # and this input string: x @@ -319,7 +343,8 @@ class GlyphCollectionWidget(QWidget): event.accept() def _findEventIndex(self, event): - index = (event.y() // self.squareSize) * self._columns + event.x() // self.squareSize + index = (event.y() // self.squareSize) * \ + self._columns + event.x() // self.squareSize if index >= len(self._glyphs): return None return index @@ -331,9 +356,11 @@ class GlyphCollectionWidget(QWidget): newSelection = {index} else: if index < self._lastSelectedCell: - newSelection = self._selection | set(range(index, self._lastSelectedCell + 1)) + newSelection = self._selection | set( + range(index, self._lastSelectedCell + 1)) else: - newSelection = self._selection | set(range(self._lastSelectedCell, index + 1)) + newSelection = self._selection | set( + range(self._lastSelectedCell, index + 1)) return newSelection # TODO: in mousePressEvent and mouseMoveEvent below, self._lastSelectedCell @@ -345,7 +372,8 @@ class GlyphCollectionWidget(QWidget): modifiers = event.modifiers() event.accept() if index is None: - if not (modifiers & Qt.ControlModifier or modifiers & Qt.ShiftModifier): + if not (modifiers & Qt.ControlModifier or + modifiers & Qt.ShiftModifier): self.selection = set() self._lastSelectedCell = index return @@ -363,7 +391,7 @@ class GlyphCollectionWidget(QWidget): newSelection = self._linearSelection(index) if newSelection is not None: self.selection = newSelection - elif not index in self._selection: + elif index not in self._selection: self.selection = {index} else: self._maybeDragPosition = event.pos() @@ -375,10 +403,12 @@ class GlyphCollectionWidget(QWidget): if event.buttons() & Qt.LeftButton: index = self._findEventIndex(event) if self._maybeDragPosition is not None: - if ((event.pos() - self._maybeDragPosition).manhattanLength() \ - < QApplication.startDragDistance()): return + if ((event.pos() - self._maybeDragPosition).manhattanLength() + < QApplication.startDragDistance()): + return # TODO: needs ordering or not? - glyphList = " ".join(glyph.name for glyph in self.getSelectedGlyphs()) + glyphList = " ".join( + glyph.name for glyph in self.getSelectedGlyphs()) drag = QDrag(self) mimeData = QMimeData() mimeData.setText(glyphList) @@ -394,7 +424,8 @@ class GlyphCollectionWidget(QWidget): modifiers = event.modifiers() event.accept() if index is None: - if not (modifiers & Qt.ControlModifier or modifiers & Qt.ShiftModifier): + if not (modifiers & Qt.ControlModifier or + modifiers & Qt.ShiftModifier): self.selection = set() self._lastSelectedCell = index return @@ -403,7 +434,8 @@ class GlyphCollectionWidget(QWidget): selection = self.selection selection.remove(index) self.selection = selection - elif index not in self._selection and index not in self._oldSelection: + elif (index not in self._selection and + index not in self._oldSelection): selection = self.selection selection.add(index) self.selection = selection @@ -453,76 +485,93 @@ class GlyphCollectionWidget(QWidget): dirtyGradient = QLinearGradient(0, 0, 0, GlyphCellHeaderHeight) dirtyGradient.setColorAt(0.0, cellHeaderBaseColor.darker(125)) dirtyGradient.setColorAt(1.0, cellHeaderLineColor.darker(125)) - markGradient = QLinearGradient(0, 0, 0, self.squareSize-GlyphCellHeaderHeight) + markGradient = QLinearGradient( + 0, 0, 0, self.squareSize - GlyphCellHeaderHeight) for row in range(beginRow, endRow + 1): for column in range(beginColumn, endColumn + 1): key = row * self._columns + column - if key >= len(self._glyphs): break + if key >= len(self._glyphs): + break glyph = self._glyphs[key] painter.save() - painter.translate(column * self.squareSize, row * self.squareSize) - painter.fillRect(0, 0, self.squareSize, self.squareSize, Qt.white) + painter.translate(column * self.squareSize, + row * self.squareSize) + painter.fillRect(0, 0, self.squareSize, + self.squareSize, Qt.white) # prepare header colors brushColor = gradient linesColor = cellHeaderHighlightLineColor # mark color - if not glyph.template: - if glyph.markColor is not None: - markColor = QColor.fromRgbF(*tuple(glyph.markColor)) - markGradient.setColorAt(1.0, markColor) - markGradient.setColorAt(0.0, markColor.lighter(125)) - painter.fillRect(0, GlyphCellHeaderHeight, self.squareSize, - self.squareSize - GlyphCellHeaderHeight, QBrush(markGradient)) - if glyph.dirty: - brushColor = dirtyGradient - linesColor = cellHeaderHighlightLineColor.darker(110) + if not glyph.template and glyph.markColor is not None: + markColor = QColor.fromRgbF(*tuple(glyph.markColor)) + markGradient.setColorAt(1.0, markColor) + markGradient.setColorAt(0.0, markColor.lighter(125)) + painter.fillRect(0, GlyphCellHeaderHeight, self.squareSize, + self.squareSize - GlyphCellHeaderHeight, + QBrush(markGradient)) + if not glyph.template and glyph.dirty: + brushColor = dirtyGradient + linesColor = cellHeaderHighlightLineColor.darker(110) # header gradient painter.fillRect(0, 0, self.squareSize, GlyphCellHeaderHeight, - QBrush(brushColor)) + QBrush(brushColor)) # header lines painter.setPen(linesColor) minOffset = painter.pen().width() # disable antialiasing to avoid lines bleeding over background painter.setRenderHint(QPainter.Antialiasing, False) painter.drawLine(0, 0, 0, GlyphCellHeaderHeight - 1) - painter.drawLine(self.squareSize - 2, 0, self.squareSize - 2, GlyphCellHeaderHeight -1) + painter.drawLine(self.squareSize - 2, 0, + self.squareSize - 2, + GlyphCellHeaderHeight - 1) painter.setPen(QColor(170, 170, 170)) - painter.drawLine(0, GlyphCellHeaderHeight, self.squareSize, GlyphCellHeaderHeight) + painter.drawLine(0, GlyphCellHeaderHeight, + self.squareSize, GlyphCellHeaderHeight) painter.setRenderHint(QPainter.Antialiasing) # header text painter.setFont(headerFont) painter.setPen(QColor(80, 80, 80)) - name = metrics.elidedText(glyph.name, Qt.ElideRight, self.squareSize - 2) - painter.drawText(1, 0, self.squareSize - 2, GlyphCellHeaderHeight - minOffset, - Qt.TextSingleLine | Qt.AlignCenter, name) + name = metrics.elidedText( + glyph.name, Qt.ElideRight, self.squareSize - 2) + painter.drawText(1, 0, self.squareSize - 2, + GlyphCellHeaderHeight - minOffset, + Qt.TextSingleLine | Qt.AlignCenter, + name) painter.restore() painter.setPen(cellGridColor) rightEdgeX = column * self.squareSize + self.squareSize bottomEdgeY = row * self.squareSize + self.squareSize - painter.drawLine(rightEdgeX, row * self.squareSize + 1, rightEdgeX, bottomEdgeY) - painter.drawLine(rightEdgeX, bottomEdgeY, column * self.squareSize + 1, bottomEdgeY) + painter.drawLine(rightEdgeX, row * + self.squareSize + 1, rightEdgeX, bottomEdgeY) + painter.drawLine(rightEdgeX, bottomEdgeY, + column * self.squareSize + 1, bottomEdgeY) if self._currentDropIndex is not None: painter.setPen(Qt.green) if self._currentDropIndex == key: - painter.drawLine(column * self.squareSize, row * self.squareSize, + painter.drawLine( + column * self.squareSize, row * self.squareSize, column * self.squareSize, bottomEdgeY) # special-case the end-column - elif column == endColumn and self._currentDropIndex == key+1: + elif (column == endColumn and + self._currentDropIndex == key + 1): yPos = self.mapFromGlobal(QCursor.pos()).y() if row == yPos // self.squareSize: - painter.drawLine(rightEdgeX - 1, row * self.squareSize, + painter.drawLine( + rightEdgeX - 1, row * self.squareSize, rightEdgeX - 1, bottomEdgeY) # selection code if key in self._selection: painter.setRenderHint(QPainter.Antialiasing, False) painter.fillRect(column * self.squareSize + 1, - row * self.squareSize + 1, self.squareSize - 3, - self.squareSize - 3, cellSelectionColor) + row * self.squareSize + 1, + self.squareSize - 3, + self.squareSize - 3, + cellSelectionColor) painter.setRenderHint(QPainter.Antialiasing) if not glyph.template: @@ -534,18 +583,25 @@ class GlyphCollectionWidget(QWidget): descender = font.info.descender if descender is None or not descender < 0: descender = -250 - factor = (self.squareSize-GlyphCellHeaderHeight) / (uPM*(1+2*GlyphCellBufferHeight)) - x_offset = (self.squareSize-glyph.width*factor)/2 - # If the glyph overflows horizontally we need to adjust the scaling factor + factor = (self.squareSize - GlyphCellHeaderHeight) / \ + (uPM * (1 + 2 * GlyphCellBufferHeight)) + x_offset = (self.squareSize - glyph.width * factor) / 2 + # If the glyph overflows horizontally we need to adjust the + # scaling factor if x_offset < 0: - factor *= 1+2*x_offset/(glyph.width*factor) + factor *= 1 + 2 * x_offset / (glyph.width * factor) x_offset = 0 # TODO: the * 1.8 below is somewhat artificial - y_offset = descender*factor * 1.8 + y_offset = descender * factor * 1.8 painter.save() - painter.setClipRect(column * self.squareSize, row * self.squareSize+GlyphCellHeaderHeight, - self.squareSize, self.squareSize-GlyphCellHeaderHeight) - painter.translate(column * self.squareSize + x_offset, row * self.squareSize + self.squareSize + y_offset) + painter.setClipRect( + column * self.squareSize, + row * self.squareSize + GlyphCellHeaderHeight, + self.squareSize, + self.squareSize - GlyphCellHeaderHeight) + painter.translate( + column * self.squareSize + x_offset, + row * self.squareSize + self.squareSize + y_offset) painter.scale(factor, -factor) painter.fillPath(outline, Qt.black) painter.restore() @@ -553,9 +609,13 @@ class GlyphCollectionWidget(QWidget): painter.save() painter.setFont(voidFont) painter.setPen(QPen(Qt.lightGray)) - rect = QRectF(column * self.squareSize, row * self.squareSize+GlyphCellHeaderHeight, - self.squareSize, self.squareSize-GlyphCellHeaderHeight) - # TODO: need to flag template glyphs as to whether they have unicodings or not + rect = QRectF( + column * self.squareSize, + row * self.squareSize + GlyphCellHeaderHeight, + self.squareSize, + self.squareSize - GlyphCellHeaderHeight) + # TODO: need to flag template glyphs as to whether they + # have unicodings or not if glyph.unicode is not None: text = chr(glyph.unicode) else: -- cgit v1.2.3