diff options
| author | Adrien Tétar | 2015-10-04 15:47:52 +0200 |
|---|---|---|
| committer | Adrien Tétar | 2015-10-04 15:54:23 +0200 |
| commit | b11dbffcf986b527cbb280e0dc6f655033e6aa2b (patch) | |
| tree | f629b1f2c18e50f03e59850e165cf935ac415141 | |
| parent | 8c420771a2427127bd849e7d8dd88762b8f34668 (diff) | |
| download | trufont-b11dbffcf986b527cbb280e0dc6f655033e6aa2b.tar.bz2 | |
fontView: add Recent Files support
| -rw-r--r-- | Lib/defconQt/__main__.py | 3 | ||||
| -rw-r--r-- | Lib/defconQt/fontView.py | 67 | ||||
| -rw-r--r-- | Lib/defconQt/scriptingWindow.py | 1 | ||||
| -rw-r--r-- | Lib/defconQt/spaceCenter.py | 1 |
4 files changed, 61 insertions, 11 deletions
diff --git a/Lib/defconQt/__main__.py b/Lib/defconQt/__main__.py index b656bef..b8feaf2 100644 --- a/Lib/defconQt/__main__.py +++ b/Lib/defconQt/__main__.py @@ -2,7 +2,6 @@ from defconQt.objects.defcon import TFont from defconQt.fontView import Application, MainWindow import sys import os - from PyQt5.QtGui import QIcon if len(sys.argv) < 2: @@ -21,6 +20,8 @@ representationFactories.registerAllFactories() #with PyCallGraph(output=GraphvizOutput()): app = Application(sys.argv) # TODO: http://stackoverflow.com/a/21330349/2037879 +app.setOrganizationName("A. Tétar & Co.") +app.setApplicationName("defconQt") app.setWindowIcon(QIcon("defconQt/resources/icon.png")) window = MainWindow(TFont(os.path.abspath(ufoFile))) window.show() diff --git a/Lib/defconQt/fontView.py b/Lib/defconQt/fontView.py index 96b6a81..b3c4757 100644 --- a/Lib/defconQt/fontView.py +++ b/Lib/defconQt/fontView.py @@ -57,6 +57,8 @@ class Application(QApplication): def currentFont(self): return self.currentMainWindow._font +MAX_RECENT_FILES = 6 + # TODO: implement Frederik's Glyph Construction Builder class AddGlyphDialog(QDialog): def __init__(self, currentGlyphs=None, parent=None): @@ -267,6 +269,8 @@ class SortDialog(QDialog): class MainWindow(QMainWindow): def __init__(self, font): super(MainWindow, self).__init__() + self.setAttribute(Qt.WA_DeleteOnClose) + squareSize = 56 self.collectionWidget = GlyphCollectionWidget(self) self._font = None @@ -277,12 +281,20 @@ class MainWindow(QMainWindow): self.collectionWidget.setFocus() menuBar = self.menuBar() + settings = QSettings() # TODO: make shortcuts platform-independent fileMenu = QMenu("&File", self) fileMenu.addAction("&New…", self.newFile, QKeySequence.New) fileMenu.addAction("&Open…", self.openFile, QKeySequence.Open) - # TODO: add functionality - #fileMenu.addMenu(QMenu("Open &Recent...", self)) + # recent files + self.recentFilesMenu = QMenu("Open &Recent...", self) + for i in range(MAX_RECENT_FILES): + action = QAction(self.recentFilesMenu) + action.setVisible(False) + action.triggered.connect(self.openRecentFont) + self.recentFilesMenu.addAction(action) + self.updateRecentFiles() + fileMenu.addMenu(self.recentFilesMenu) fileMenu.addSeparator() fileMenu.addAction("&Save", self.saveFile, QKeySequence.Save) fileMenu.addAction("Save &As…", self.saveFileAs, QKeySequence.SaveAs) @@ -311,6 +323,8 @@ class MainWindow(QMainWindow): editMenu.addAction("Copy", self.copy, QKeySequence.Copy) editMenu.addAction("Copy as Component", self.copyAsComponent, "Ctrl+Alt+c") editMenu.addAction("Paste", self.paste, QKeySequence.Paste) + editMenu.addSeparator() + editMenu.addAction("Settings…", self.settings) menuBar.addMenu(editMenu) fontMenu = QMenu("&Font", self) @@ -350,6 +364,7 @@ class MainWindow(QMainWindow): self.setCentralWidget(self.collectionWidget.scrollArea()) self.resize(605, 430) + self.setCurrentFile(font.path) self.setWindowTitle() def newFile(self): @@ -370,20 +385,23 @@ class MainWindow(QMainWindow): "UFO Fonts (metainfo.plist)") if not ok: return if path: - # TODO: error handling - path = os.path.dirname(path) - # TODO: I note that a change of self.font often goes with setWindowTitle(). - # Be more DRY. - #self.font = TFont(path) - #self.setWindowTitle() + if ".plist" in path: + path = os.path.dirname(path) for window in QApplication.topLevelWidgets(): if isinstance(window, MainWindow) and window._font.path == path: window.raise_() return - font = TFont(path) + try: + font = TFont(path) + except: + return window = MainWindow(font) window.show() + def openRecentFont(self): + fontPath = self.sender().toolTip() + self.openFile(fontPath) + def saveFile(self, path=None): if path is None and self.font.path is None: self.saveFileAs() @@ -398,6 +416,7 @@ class MainWindow(QMainWindow): self.font.dirty = False for glyph in self.font: glyph.dirty = False + self.setCurrentFile(path) self.setWindowModified(False) def saveFileAs(self): @@ -408,6 +427,36 @@ class MainWindow(QMainWindow): self.setWindowTitle() #return ok + def setCurrentFile(self, path): + settings = QSettings() + recentFiles = settings.value("core/recentFiles", [], type=str) + if path in recentFiles: + recentFiles.remove(path) + recentFiles.insert(0, path) + while len(recentFiles) > MAX_RECENT_FILES: + del recentFiles[-1] + settings.setValue("core/recentFiles", recentFiles) + for window in QApplication.topLevelWidgets(): + if isinstance(window, MainWindow): + window.updateRecentFiles() + + def updateRecentFiles(self): + settings = QSettings() + recentFiles = settings.value("core/recentFiles", [], type=str) + count = min(len(recentFiles), MAX_RECENT_FILES) + actions = self.recentFilesMenu.actions() + for index, recentFile in enumerate(recentFiles[:count]): + action = actions[index] + shortName = os.path.basename(recentFile.rstrip(os.sep)) + + action.setText(shortName) + action.setToolTip(recentFile) + action.setVisible(True) + for index in range(count, MAX_RECENT_FILES): + actions[index].setVisible(False) + + self.recentFilesMenu.setEnabled(len(recentFiles)) + def closeEvent(self, event): ok = self.maybeSaveBeforeExit() if ok: diff --git a/Lib/defconQt/scriptingWindow.py b/Lib/defconQt/scriptingWindow.py index 2448d48..eef90e3 100644 --- a/Lib/defconQt/scriptingWindow.py +++ b/Lib/defconQt/scriptingWindow.py @@ -8,6 +8,7 @@ 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) diff --git a/Lib/defconQt/spaceCenter.py b/Lib/defconQt/spaceCenter.py index 663398e..ac632ae 100644 --- a/Lib/defconQt/spaceCenter.py +++ b/Lib/defconQt/spaceCenter.py @@ -356,7 +356,6 @@ class GlyphsCanvas(QWidget): def wheelEvent(self, event): if event.modifiers() & Qt.ControlModifier: # TODO: should it snap to predefined pointSizes? is the scaling factor okay? - # see how rf behaves -> scaling factor grows with sz it seems # XXX: current alg. is not reversible... decay = event.angleDelta().y() / 120.0 scale = round(self.ptSize / 10) |
