diff options
| author | Adrien Tétar | 2015-09-29 10:44:58 +0200 |
|---|---|---|
| committer | Adrien Tétar | 2015-09-29 11:48:28 +0200 |
| commit | 2675fa8275a60bdd613de40e5d61f9947b856152 (patch) | |
| tree | 65a367c80689f65043b079c5767a6e47875f11c6 /Lib/defconQt | |
| parent | 6b4593337cd4626b5b780d2774013063f537e793 (diff) | |
| download | trufont-2675fa8275a60bdd613de40e5d61f9947b856152.tar.bz2 | |
fontView,glyphView: generalize copy/paste and use app-wide clipboard
Diffstat (limited to 'Lib/defconQt')
| -rw-r--r-- | Lib/defconQt/__main__.py | 4 | ||||
| -rw-r--r-- | Lib/defconQt/fontView.py | 60 | ||||
| -rw-r--r-- | Lib/defconQt/glyphCollectionView.py | 3 | ||||
| -rw-r--r-- | Lib/defconQt/glyphView.py | 29 |
4 files changed, 62 insertions, 34 deletions
diff --git a/Lib/defconQt/__main__.py b/Lib/defconQt/__main__.py index 8678906..ffcef50 100644 --- a/Lib/defconQt/__main__.py +++ b/Lib/defconQt/__main__.py @@ -9,8 +9,8 @@ from PyQt5.QtWidgets import QApplication if len(sys.argv) < 2: share_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'share') ufoFile = os.path.join(share_dir, 'fonts', 'subsets', 'Merriweather-Bold-Subset-nop.ufo') - print ('Usage: %s <input.ufo>' % sys.argv[0]) - print ('Loading default sample font: "%s"' % ufoFile) + print('Usage: %s <input.ufo>' % sys.argv[0]) + print('Loading default sample font: "%s"' % ufoFile) else: ufoFile = sys.argv[1] print('Loading font file: "%s"' % ufoFile) diff --git a/Lib/defconQt/fontView.py b/Lib/defconQt/fontView.py index c3e4854..0ab05c7 100644 --- a/Lib/defconQt/fontView.py +++ b/Lib/defconQt/fontView.py @@ -3,7 +3,7 @@ from defconQt.fontInfo import TabDialog from defconQt.glyphCollectionView import GlyphCollectionWidget from defconQt.glyphView import MainGfxWindow from defconQt.groupsView import GroupsWindow -from defconQt.objects.defcon import CharacterSet, TFont +from defconQt.objects.defcon import CharacterSet, TFont, TGlyph from defcon import Component from defconQt.spaceCenter import MainSpaceWindow from fontTools.agl import AGL2UV @@ -11,8 +11,7 @@ from fontTools.agl import AGL2UV from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * -import os -import unicodedata +import os, pickle, unicodedata cannedDesign = [ dict(type="cannedDesign", allowPseudoUnicode=True) @@ -296,8 +295,9 @@ class MainWindow(QMainWindow): green.setIcon(QIcon(pixmap)) green.setData(QColor(Qt.green)) editMenu.addMenu(markColorMenu) - editMenu.addAction("Copy Reference", self.copyReference) - editMenu.addAction("Paste Into", self.pasteInto) + editMenu.addAction("Copy", self.copy, QKeySequence.Copy) + editMenu.addAction("Copy Reference", self.copyReference, "Ctrl+Alt+c") + editMenu.addAction("Paste", self.paste, QKeySequence.Paste) menuBar.addMenu(editMenu) fontMenu = QMenu("&Font", self) @@ -467,27 +467,43 @@ class MainWindow(QMainWindow): doc="The sortDescriptor. Takes glyphs from the font and sorts them \ when set.") - def copyReference(self): + def copy(self): + glyphs = self.collectionWidget.glyphs selection = self.collectionWidget.selection - if len(selection) == 0: - pass # XXX: error dialog: "you need to select a glyph first" - else: - self.selectedReferences = selection + pickled = [] + for index in sorted(self.collectionWidget.selection): + pickled.append(glyphs[index].serializeForUndo(False)) + clipboard = QApplication.clipboard() + mimeData = QMimeData() + mimeData.setData("application/x-defconQt-glyph-data", pickle.dumps(pickled)) + clipboard.setMimeData(mimeData) - def pasteInto(self): + def copyReference(self): glyphs = self.collectionWidget.glyphs selection = self.collectionWidget.selection - if len(selection) == 0: - pass # XXX: error dialog: "you need to select a glyph first" - else: - try: - for ref in self.selectedReferences: - component = Component() - component.baseGlyph = glyphs[ref].name - for key in selection: - glyphs[key].appendComponent(component) - except: - pass + pickled = [] + for index in sorted(self.collectionWidget.selection): + glyph = glyphs[index] + componentGlyph = TGlyph() + componentGlyph.width = glyph.width + component = Component() + component.baseGlyph = glyph.name + componentGlyph.appendComponent(component) + pickled.append(componentGlyph.serializeForUndo(False)) + clipboard = QApplication.clipboard() + mimeData = QMimeData() + mimeData.setData("application/x-defconQt-glyph-data", pickle.dumps(pickled)) + clipboard.setMimeData(mimeData) + + def paste(self): + clipboard = QApplication.clipboard() + mimeData = clipboard.mimeData() + if mimeData.hasFormat("application/x-defconQt-glyph-data"): + data = pickle.loads(mimeData.data("application/x-defconQt-glyph-data")) + glyphs = self.collectionWidget.getSelectedGlyphs() + if len(data) == len(glyphs): + for pickled, glyph in zip(data, glyphs): + glyph.deserializeFromUndo(pickled) def markColor(self): color = self.sender().data() diff --git a/Lib/defconQt/glyphCollectionView.py b/Lib/defconQt/glyphCollectionView.py index 6ced3d5..b0d4461 100644 --- a/Lib/defconQt/glyphCollectionView.py +++ b/Lib/defconQt/glyphCollectionView.py @@ -82,6 +82,9 @@ class GlyphCollectionWidget(QWidget): 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)] + def _get_lastSelectedCell(self): return self._lastSelectedCell diff --git a/Lib/defconQt/glyphView.py b/Lib/defconQt/glyphView.py index 8fd0f24..c85d43b 100644 --- a/Lib/defconQt/glyphView.py +++ b/Lib/defconQt/glyphView.py @@ -1,5 +1,6 @@ from enum import Enum from math import copysign +import pickle from defconQt.objects.defcon import TContour, TGlyph from defconQt.pens.copySelectionPen import CopySelectionPen from fontTools.misc import bezierTools @@ -326,7 +327,7 @@ class ComponentItem(QGraphicsPathItem): y = value.y() printf( "ItemPositionChange: X=%d Y=%d" % (x, y)) - + def setBounds(self, path): bounds_path = QPainterPath() region = path.boundingRegion(QTransform()).boundingRect().getCoords() @@ -340,7 +341,7 @@ class ComponentItem(QGraphicsPathItem): pen.setColor(Qt.green) self.setPen(pen) super(ComponentItem, self).paint(painter, option, widget) - + class OnCurvePointItem(QGraphicsPathItem): def __init__(self, x, y, isSmooth, contour, point, scale=1, parent=None): @@ -758,19 +759,27 @@ class GlyphScene(QGraphicsScene): return elif event.matches(QKeySequence.Copy): clipboard = QApplication.clipboard() - mimeData = clipboard.mimeData() + mimeData = QMimeData() pen = CopySelectionPen() self._glyphObject.drawPoints(pen) - # XXX: clipboard should outlive the widget window! - self._clipboardObject = pen.getGlyph().serializeForUndo() + copyGlyph = pen.getGlyph() + # TODO: somehow try to do this in the pen + # pass the glyph to a controller object that holds a self._pen + copyGlyph.width = self._glyphObject.width + mimeData.setData("application/x-defconQt-glyph-data", pickle.dumps([copyGlyph.serializeForUndo(False)])) + clipboard.setMimeData(mimeData) event.accept() return elif event.matches(QKeySequence.Paste): - if self._clipboardObject is None: return - pen = self._glyphObject.getPointPen() - pasteGlyph = TGlyph() - pasteGlyph.deserializeFromUndo(self._clipboardObject) - pasteGlyph.drawPoints(pen) + clipboard = QApplication.clipboard() + mimeData = clipboard.mimeData() + if mimeData.hasFormat("application/x-defconQt-glyph-data"): + data = pickle.loads(mimeData.data("application/x-defconQt-glyph-data")) + if len(data) == 1: + pen = self._glyphObject.getPointPen() + pasteGlyph = TGlyph() + pasteGlyph.deserializeFromUndo(data[0]) + pasteGlyph.drawPoints(pen) event.accept() return else: |
