diff options
| author | Adrien Tétar | 2015-10-06 14:02:34 +0200 |
|---|---|---|
| committer | Adrien Tétar | 2015-10-06 14:02:34 +0200 |
| commit | 8af06af1cf3b3d4e554b67f6ef64c68219166fbf (patch) | |
| tree | 574f54fe383327576fe26b5e508c82e907b2e7e7 | |
| parent | b11dbffcf986b527cbb280e0dc6f655033e6aa2b (diff) | |
| download | trufont-8af06af1cf3b3d4e554b67f6ef64c68219166fbf.tar.bz2 | |
meta: CurrentGlyph() support, drop dead code, nits
| -rw-r--r-- | Lib/defconQt/fontView.py | 48 | ||||
| -rw-r--r-- | Lib/defconQt/glyphCollectionView.py | 9 | ||||
| -rw-r--r-- | Lib/defconQt/glyphView.py | 94 | ||||
| -rw-r--r-- | Lib/defconQt/scriptingWindow.py | 10 |
4 files changed, 68 insertions, 93 deletions
diff --git a/Lib/defconQt/fontView.py b/Lib/defconQt/fontView.py index b3c4757..d482050 100644 --- a/Lib/defconQt/fontView.py +++ b/Lib/defconQt/fontView.py @@ -12,7 +12,7 @@ from fontTools.agl import AGL2UV from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * -import os, pickle, unicodedata +import os, pickle, traceback cannedDesign = [ dict(type="cannedDesign", allowPseudoUnicode=True) @@ -47,6 +47,12 @@ latin1 = CharacterSet( "quoteright","minus"], "Latin-1") class Application(QApplication): + currentGlyphChanged = pyqtSignal() + + def __init__(self, *args, **kwargs): + super(Application, self).__init__(*args, **kwargs) + self._currentGlyph = None + def allFonts(self): fonts = [] for window in QApplication.topLevelWidgets(): @@ -57,6 +63,15 @@ class Application(QApplication): def currentFont(self): return self.currentMainWindow._font + def currentGlyph(self): + return self._currentGlyph + + def setCurrentGlyph(self, glyph): + if glyph == self._currentGlyph: + return + self._currentGlyph = glyph + self.currentGlyphChanged.emit() + MAX_RECENT_FILES = 6 # TODO: implement Frederik's Glyph Construction Builder @@ -278,6 +293,9 @@ class MainWindow(QMainWindow): self.font = font self.collectionWidget.characterSelectedCallback = self._selectionChanged self.collectionWidget.doubleClickCallback = self._glyphOpened + # XXX: should spaceCenter have this functionality as well? + # TODO: should default be True or False? + self.collectionWidget.updateCurrentGlyph = True self.collectionWidget.setFocus() menuBar = self.menuBar() @@ -287,7 +305,7 @@ class MainWindow(QMainWindow): fileMenu.addAction("&New…", self.newFile, QKeySequence.New) fileMenu.addAction("&Open…", self.openFile, QKeySequence.Open) # recent files - self.recentFilesMenu = QMenu("Open &Recent...", self) + self.recentFilesMenu = QMenu("Open &recent…", self) for i in range(MAX_RECENT_FILES): action = QAction(self.recentFilesMenu) action.setVisible(False) @@ -297,7 +315,7 @@ class MainWindow(QMainWindow): fileMenu.addMenu(self.recentFilesMenu) fileMenu.addSeparator() fileMenu.addAction("&Save", self.saveFile, QKeySequence.Save) - fileMenu.addAction("Save &As…", self.saveFileAs, QKeySequence.SaveAs) + fileMenu.addAction("Save &as…", self.saveFileAs, QKeySequence.SaveAs) fileMenu.addAction("Reload from disk", self.reload) fileMenu.addAction("E&xit", self.close, QKeySequence.Quit) menuBar.addMenu(fileMenu) @@ -321,7 +339,7 @@ class MainWindow(QMainWindow): green.setData(QColor(Qt.green)) editMenu.addMenu(markColorMenu) editMenu.addAction("Copy", self.copy, QKeySequence.Copy) - editMenu.addAction("Copy as Component", self.copyAsComponent, "Ctrl+Alt+c") + editMenu.addAction("Copy as component", self.copyAsComponent, "Ctrl+Alt+C") editMenu.addAction("Paste", self.paste, QKeySequence.Paste) editMenu.addSeparator() editMenu.addAction("Settings…", self.settings) @@ -330,20 +348,20 @@ class MainWindow(QMainWindow): fontMenu = QMenu("&Font", self) # TODO: work out sensible shortcuts and make sure they're cross-platform # ready - consider extracting them into separate file? - fontMenu.addAction("&Add glyph", self.addGlyph, "Ctrl+U") - fontMenu.addAction("Font &info", self.fontInfo, "Ctrl+M") - fontMenu.addAction("Font &features", self.fontFeatures, "Ctrl+F") + fontMenu.addAction("&Add glyph", self.addGlyph, "Ctrl+Alt+G") + fontMenu.addAction("Font &info", self.fontInfo, "Ctrl+Alt+I") + fontMenu.addAction("Font &features", self.fontFeatures, "Ctrl+Alt+F") fontMenu.addSeparator() fontMenu.addAction("Sort…", self.sortCharacters) menuBar.addMenu(fontMenu) pythonMenu = QMenu("&Python", self) - pythonMenu.addAction("Scripting &window", self.scripting) + pythonMenu.addAction("Scripting &window", self.scripting, "Ctrl+Alt+R") menuBar.addMenu(pythonMenu) windowMenu = QMenu("&Windows", self) - windowMenu.addAction("&Space center", self.spaceCenter, "Ctrl+Y") - windowMenu.addAction("&Groups window", self.fontGroups, "Ctrl+G") + windowMenu.addAction("&Space center", self.spaceCenter, "Ctrl+Alt+S") + windowMenu.addAction("&Groups window", self.fontGroups, "Ctrl+Alt+G") menuBar.addMenu(windowMenu) helpMenu = QMenu("&Help", self) @@ -394,6 +412,7 @@ class MainWindow(QMainWindow): try: font = TFont(path) except: + print(traceback.format_exc()) return window = MainWindow(font) window.show() @@ -642,6 +661,9 @@ class MainWindow(QMainWindow): if event.type() == QEvent.WindowActivate: app = QApplication.instance() app.currentMainWindow = self + lastSelectedGlyph = self.collectionWidget.lastSelectedGlyph() + if lastSelectedGlyph is not None: + app.setCurrentGlyph(lastSelectedGlyph) return super(MainWindow, self).event(event) def resizeEvent(self, event): @@ -700,11 +722,13 @@ class MainWindow(QMainWindow): def scripting(self): # TODO: see up here app = QApplication.instance() - if not (hasattr(app, 'scriptingWindow') and app.scriptingWindow.isVisible()): + if not hasattr(app, 'scriptingWindow'): app.scriptingWindow = MainScriptingWindow() app.scriptingWindow.show() - else: + elif app.scriptingWindow.isVisible(): app.scriptingWindow.raise_() + else: + app.scriptingWindow.show() def sortCharacters(self): sortDescriptor, ok = SortDialog.getDescriptor(self, self.sortDescriptor) diff --git a/Lib/defconQt/glyphCollectionView.py b/Lib/defconQt/glyphCollectionView.py index 2134134..4e25cbb 100644 --- a/Lib/defconQt/glyphCollectionView.py +++ b/Lib/defconQt/glyphCollectionView.py @@ -51,6 +51,7 @@ class GlyphCollectionWidget(QWidget): self.characterSelectedCallback = None self.doubleClickCallback = None + self.updateCurrentGlyph = False self._maybeDragPosition = None self.setFocusPolicy(Qt.ClickFocus) @@ -90,12 +91,20 @@ class GlyphCollectionWidget(QWidget): def _set_lastSelectedCell(self, index): self._lastSelectedCell = index + if self.updateCurrentGlyph: + glyph = self.lastSelectedGlyph() + app = QApplication.instance() + app.setCurrentGlyph(glyph) if index is not None: self.scrollToCell(index) lastSelectedCell = property(_get_lastSelectedCell, _set_lastSelectedCell, doc="The current lastSelectedCell in selection.") + def lastSelectedGlyph(self): + index = self._lastSelectedCell + return self._glyphs[index] if index is not None else None + def scrollArea(self): return self._scrollArea diff --git a/Lib/defconQt/glyphView.py b/Lib/defconQt/glyphView.py index fe340d1..b908d56 100644 --- a/Lib/defconQt/glyphView.py +++ b/Lib/defconQt/glyphView.py @@ -10,7 +10,6 @@ from PyQt5.QtGui import *#QBrush, QColor, QImage, QKeySequence, QPainter, QPaint from PyQt5.QtWidgets import *#(QAction, QActionGroup, QApplication, QFileDialog, #QGraphicsItem, QGraphicsEllipseItem, QGraphicsLineItem, QGraphicsPathItem, QGraphicsRectItem, QGraphicsScene, QGraphicsView, #QMainWindow, QMenu, QMessageBox, QStyle, QStyleOptionGraphicsItem, QWidget) -from PyQt5.QtOpenGL import QGL, QGLFormat, QGLWidget class AddAnchorDialog(QDialog): def __init__(self, pos=None, parent=None): @@ -139,45 +138,11 @@ class MainGfxWindow(QMainWindow): fileMenu = QMenu("&File", self) fileMenu.addAction("E&xit", self.close, QKeySequence.Quit) - menuBar.addMenu(fileMenu) - viewMenu = QMenu("&View", self) - self.backgroundAction = viewMenu.addAction("&Background") - self.backgroundAction.setEnabled(False) - self.backgroundAction.setCheckable(True) - self.backgroundAction.setChecked(False) - self.backgroundAction.toggled.connect(self.view.setViewBackground) - - self.outlineAction = viewMenu.addAction("&Outline") - self.outlineAction.setEnabled(False) - self.outlineAction.setCheckable(True) - self.outlineAction.setChecked(True) - self.outlineAction.toggled.connect(self.view.setViewOutline) - - menuBar.addMenu(viewMenu) - - rendererMenu = QMenu("&Renderer", self) - self.nativeAction = rendererMenu.addAction("&Native") - self.nativeAction.setCheckable(True) - self.nativeAction.setChecked(True) - - if QGLFormat.hasOpenGL(): - self.glAction = rendererMenu.addAction("&OpenGL") - self.glAction.setCheckable(True) - - self.imageAction = rendererMenu.addAction("&Image") - self.imageAction.setCheckable(True) - - rendererGroup = QActionGroup(self) - rendererGroup.addAction(self.nativeAction) - - if QGLFormat.hasOpenGL(): - rendererGroup.addAction(self.glAction) - - rendererGroup.addAction(self.imageAction) - - menuBar.addMenu(rendererMenu) + glyphMenu = QMenu("&Glyph", self) + glyphMenu.addAction("&Jump", self.changeGlyph) + menuBar.addMenu(glyphMenu) toolBar = QToolBar(self) toolBar.setMovable(False) @@ -201,8 +166,6 @@ class MainGfxWindow(QMainWindow): toolsGroup.addAction(knifeToolButton) self.addToolBar(toolBar) - rendererGroup.triggered.connect(self.setRenderer) - self.setCentralWidget(self.view) self.setWindowTitle(glyph.name, glyph.getParent()) self.adjustSize() @@ -212,6 +175,18 @@ class MainGfxWindow(QMainWindow): createAnchorAction.triggered.connect(self.view.createAnchor) self.addAction(createAnchorAction) + def changeGlyph(self): + glyph = self.view._glyph + newGlyph, ok = GotoDialog.getNewGlyph(self, glyph.getParent()) + if ok and newGlyph is not None: + self.view.setGlyph(newGlyph) + + def event(self, event): + if event.type() == QEvent.WindowActivate: + app = QApplication.instance() + app.setCurrentGlyph(self.view._glyph) + return super(MainGfxWindow, self).event(event) + def closeEvent(self, event): self.view._glyph.removeObserver(self, "Glyph.Changed") event.accept() @@ -219,15 +194,6 @@ class MainGfxWindow(QMainWindow): def _glyphChanged(self, notification): self.view._glyphChanged(notification) - def setRenderer(self, action): - if action == self.nativeAction: - self.view.setRenderer(GlyphView.Native) - elif action == self.glAction: - if QGLFormat.hasOpenGL(): - self.view.setRenderer(GlyphView.OpenGL) - elif action == self.imageAction: - self.view.setRenderer(GlyphView.Image) - def setWindowTitle(self, title, font=None): if font is not None: title = "%s – %s %s" % (title, font.info.familyName, font.info.styleName) super(MainGfxWindow, self).setWindowTitle(title) @@ -831,13 +797,6 @@ class GlyphScene(QGraphicsScene): self._glyphObject.dirty = True event.accept() return - elif key == Qt.Key_J: - view = self.views()[0] - glyph = view._glyph - newGlyph, ok = GotoDialog.getNewGlyph(self.parent(), glyph.getParent()) - if ok and newGlyph is not None: - view.setGlyph(newGlyph) - return elif event.matches(QKeySequence.Undo): if len(self._dataForUndo) > 0: undo = self._dataForUndo.pop() @@ -1301,12 +1260,8 @@ class GlyphScene(QGraphicsScene): event.accept() class GlyphView(QGraphicsView): - Native, OpenGL, Image = range(3) - def __init__(self, glyph, parent=None): super(GlyphView, self).__init__(parent) - - self.renderer = GlyphView.Native self._glyph = glyph self._glyph.addObserver(self, "_glyphChanged", "Glyph.Changed") self._impliedPointSize = 1000 @@ -1574,27 +1529,12 @@ class GlyphView(QGraphicsView): self._glyph = glyph # XXX: DRY ALERT! self.scene()._glyphObject = glyph + app = QApplication.instance() + app.setCurrentGlyph(glyph) self._glyph.addObserver(self, "_glyphChanged", "Glyph.Changed") self.parent().setWindowTitle(self._glyph.name, self._glyph.getParent()) self.redrawGlyph() - def setRenderer(self, renderer): - self.renderer = renderer - - if self.renderer == GlyphView.OpenGL: - if QGLFormat.hasOpenGL(): - self.setViewport(QGLWidget(QGLFormat(QGL.SampleBuffers))) - else: - self.setViewport(QWidget()) - - def setViewBackground(self, enable): - if self.backgroundItem: - self.backgroundItem.setVisible(enable) - - def setViewOutline(self, enable): - if self.outlineItem: - self.outlineItem.setVisible(enable) - def mousePressEvent(self, event): if (event.button() == Qt.MidButton): dragMode = self.dragMode() diff --git a/Lib/defconQt/scriptingWindow.py b/Lib/defconQt/scriptingWindow.py index eef90e3..f48fd0a 100644 --- a/Lib/defconQt/scriptingWindow.py +++ b/Lib/defconQt/scriptingWindow.py @@ -8,7 +8,6 @@ from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QPlainTextEdit class MainScriptingWindow(QMainWindow): def __init__(self): super(MainScriptingWindow, self).__init__() - self.setAttribute(Qt.WA_DeleteOnClose) self.editor = PythonEditor(parent=self) self.resize(600, 500) @@ -31,6 +30,7 @@ class MainScriptingWindow(QMainWindow): "__builtins__": __builtins__, "AllFonts": app.allFonts, "CurrentFont": app.currentFont, + "CurrentGlyph": app.currentGlyph, } try: code = compile(script, "<string>", "exec") @@ -57,9 +57,11 @@ class PythonEditor(CodeEditor): if key in self.autocomplete.keys(): super(PythonEditor, self).keyPressEvent(event) cursor = self.textCursor() - cursor.insertText(self.autocomplete[key][-1]) - cursor.movePosition(QTextCursor.PreviousCharacter) - self.setTextCursor(cursor) + ok = cursor.movePosition(QTextCursor.NextCharacter) + if not ok: + cursor.insertText(self.autocomplete[key][-1]) + cursor.movePosition(QTextCursor.PreviousCharacter) + self.setTextCursor(cursor) event.accept() return elif key == Qt.Key_Backspace: |
