X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fgonvert_qt.py;h=8df6dbd11551146221ca68a35cb1e9a3818f1e2d;hb=5df615b4bc2ad215bba3a2266a2b91878d2fab39;hp=11cfc275e5435c6848d9603f4cadc9c0f5e5b819;hpb=2b9fb6924693c062ba2476601160603676860658;p=gonvert diff --git a/src/gonvert_qt.py b/src/gonvert_qt.py index 11cfc27..8df6dbd 100755 --- a/src/gonvert_qt.py +++ b/src/gonvert_qt.py @@ -6,6 +6,7 @@ from __future__ import with_statement import sys import os import math +import simplejson import logging from PyQt4 import QtGui @@ -51,6 +52,8 @@ def split_number(number): class Gonvert(object): + # @todo Favorites + _DATA_PATHS = [ os.path.dirname(__file__), os.path.join(os.path.dirname(__file__), "../data"), @@ -59,7 +62,7 @@ class Gonvert(object): '/usr/lib/gonvert', ] - def __init__(self): + def __init__(self, app): self._dataPath = "" for dataPath in self._DATA_PATHS: appIconPath = os.path.join(dataPath, "pixmaps", "gonvert.png") @@ -68,9 +71,14 @@ class Gonvert(object): break else: raise RuntimeError("UI Descriptor not found!") + self._app = app self._appIconPath = appIconPath + self._recent = [] + self._isFullscreen = False + self._clipboard = QtGui.QApplication.clipboard() self._jumpWindow = None + self._recentWindow = None self._catWindow = None self._jumpAction = QtGui.QAction(None) @@ -78,14 +86,40 @@ class Gonvert(object): self._jumpAction.setStatusTip("Search for a unit and jump straight to it") self._jumpAction.setToolTip("Search for a unit and jump straight to it") self._jumpAction.setShortcut(QtGui.QKeySequence("CTRL+j")) - + self._jumpAction.triggered.connect(self._on_jump_start) + + self._recentAction = QtGui.QAction(None) + self._recentAction.setText("Recent Units") + self._recentAction.setStatusTip("View the recent units") + self._recentAction.setToolTip("View the recent units") + self._recentAction.setShortcut(QtGui.QKeySequence("CTRL+r")) + self._recentAction.triggered.connect(self._on_recent_start) + + self._fullscreenAction = QtGui.QAction(None) + self._fullscreenAction.setText("Toggle Fullscreen") + self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter")) + self._fullscreenAction.triggered.connect(self._on_toggle_fullscreen) + + self._logAction = QtGui.QAction(None) + self._logAction.setText("Log") + self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l")) + self._logAction.triggered.connect(self._on_log) + + self._quitAction = QtGui.QAction(None) + self._quitAction.setText("Quit") + self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q")) + self._quitAction.triggered.connect(self._on_quit) + + self._app.lastWindowClosed.connect(self._on_app_quit) self.request_category() + self.load_settings() def request_category(self): if self._catWindow is not None: self._catWindow.close() self._catWindow = None self._catWindow = CategoryWindow(None, self) + self._catWindow.window.destroyed.connect(lambda obj = None: self._on_child_close("_catWindow", obj)) return self._catWindow def search_units(self): @@ -93,8 +127,60 @@ class Gonvert(object): self._jumpWindow.close() self._jumpWindow = None self._jumpWindow = QuickJump(None, self) + self._jumpWindow.window.destroyed.connect(lambda obj = None: self._on_child_close("_jumpWindow", obj)) return self._jumpWindow + def show_recent(self): + if self._recentWindow is not None: + self._recentWindow.close() + self._recentWindow = None + self._recentWindow = Recent(None, self) + self._recentWindow.window.destroyed.connect(lambda obj = None: self._on_child_close("_recentWindow", obj)) + return self._recentWindow + + def add_recent(self, categoryName, unitName): + catUnit = categoryName, unitName + try: + self._recent.remove(catUnit) + except ValueError: + pass # ignore if its not already in the recent history + assert catUnit not in self._recent + self._recent.append(catUnit) + + def get_recent_unit(self, categoryName): + for catName, unitName in reversed(self._recent): + if catName == categoryName: + return unitName + else: + return "" + + def get_recent(self): + return reversed(self._recent) + + def load_settings(self): + try: + with open(constants._user_settings_, "r") as settingsFile: + settings = simplejson.load(settingsFile) + except IOError, e: + settings = {} + self._isFullscreen = settings.get("isFullScreen", self._isFullscreen) + recent = settings.get("recent", self._recent) + for category, unit in recent: + self.add_recent(category, unit) + + for window in self._walk_children(): + window.set_fullscreen(self._isFullscreen) + if self._recent: + self._catWindow.select_category(self._recent[-1][0]) + + def save_settings(self): + settings = { + "isFullScreen": self._isFullscreen, + "recent": self._recent, + } + with open(constants._user_settings_, "w") as settingsFile: + simplejson.dump(settings, settingsFile) + @property def appIconPath(self): return self._appIconPath @@ -103,6 +189,67 @@ class Gonvert(object): def jumpAction(self): return self._jumpAction + @property + def recentAction(self): + return self._recentAction + + @property + def fullscreenAction(self): + return self._fullscreenAction + + @property + def logAction(self): + return self._logAction + + @property + def quitAction(self): + return self._quitAction + + def _walk_children(self): + if self._catWindow is not None: + yield self._catWindow + if self._jumpWindow is not None: + yield self._jumpWindow + if self._recentWindow is not None: + yield self._recentWindow + + @misc_utils.log_exception(_moduleLogger) + def _on_app_quit(self, checked = False): + self.save_settings() + + @misc_utils.log_exception(_moduleLogger) + def _on_child_close(self, name, obj = None): + if not hasattr(self, name): + _moduleLogger.info("Something weird going on when we don't have a %s" % name) + return + setattr(self, name, None) + + @misc_utils.log_exception(_moduleLogger) + def _on_toggle_fullscreen(self, checked = False): + self._isFullscreen = not self._isFullscreen + for window in self._walk_children(): + window.set_fullscreen(self._isFullscreen) + + @misc_utils.log_exception(_moduleLogger) + def _on_jump_start(self, checked = False): + self.search_units() + + @misc_utils.log_exception(_moduleLogger) + def _on_recent_start(self, checked = False): + self.show_recent() + + @misc_utils.log_exception(_moduleLogger) + def _on_log(self, checked = False): + with open(constants._user_logpath_, "r") as f: + logLines = f.xreadlines() + log = "".join(logLines) + self._clipboard.setText(log) + + @misc_utils.log_exception(_moduleLogger) + def _on_quit(self, checked = False): + for window in self._walk_children(): + window.close() + class CategoryWindow(object): @@ -114,6 +261,7 @@ class CategoryWindow(object): self._categories.setHeaderLabels(["Categories"]) self._categories.itemClicked.connect(self._on_category_clicked) self._categories.setHeaderHidden(True) + self._categories.setAlternatingRowColors(True) for catName in unit_data.UNIT_CATEGORIES: twi = QtGui.QTreeWidgetItem(self._categories) twi.setText(0, catName) @@ -125,36 +273,72 @@ class CategoryWindow(object): centralWidget.setLayout(self._layout) self._window = QtGui.QMainWindow(parent) + self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) if parent is not None: self._window.setWindowModality(QtCore.Qt.WindowModal) self._window.setWindowTitle("%s - Categories" % constants.__pretty_app_name__) self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) self._window.setCentralWidget(centralWidget) - self._app.jumpAction.triggered.connect(self._on_jump_start) + self._closeWindowAction = QtGui.QAction(None) + self._closeWindowAction.setText("Window") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + fileMenu = self._window.menuBar().addMenu("&File") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) + viewMenu.addSeparator() viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + + self._window.addAction(self._app.logAction) self._window.show() + @property + def window(self): + return self._window + + def walk_children(self): + if self._unitWindow is not None: + yield self._unitWindow + def close(self): + for child in self.walk_children(): + child.close() self._window.close() - def selectCategory(self, categoryName): - if self._unitWindow is not None: - self._unitWindow.close() - self._unitWindow = None + def select_category(self, categoryName): + for child in self.walk_children(): + child.close() self._unitWindow = UnitWindow(self._window, categoryName, self._app) + self._unitWindow.window.destroyed.connect(self._on_child_close) return self._unitWindow + def set_fullscreen(self, isFullscreen): + if isFullscreen: + self._window.showFullScreen() + else: + self._window.showNormal() + for child in self.walk_children(): + child.set_fullscreen(isFullscreen) + @misc_utils.log_exception(_moduleLogger) - def _on_jump_start(self, checked = False): - self._app.search_units() + def _on_child_close(self, obj = None): + self._unitWindow = None + + @misc_utils.log_exception(_moduleLogger) + def _on_close_window(self, checked = True): + self.close() @misc_utils.log_exception(_moduleLogger) def _on_category_clicked(self, item, columnIndex): categoryName = unicode(item.text(0)) - self.selectCategory(categoryName) + self.select_category(categoryName) class QuickJump(object): @@ -175,6 +359,7 @@ class QuickJump(object): self._resultsBox = QtGui.QTreeWidget() self._resultsBox.setHeaderLabels(["Categories", "Units"]) self._resultsBox.setHeaderHidden(True) + self._resultsBox.setAlternatingRowColors(True) self._resultsBox.itemClicked.connect(self._on_result_clicked) self._layout = QtGui.QVBoxLayout() @@ -185,24 +370,54 @@ class QuickJump(object): centralWidget.setLayout(self._layout) self._window = QtGui.QMainWindow(parent) + self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) if parent is not None: self._window.setWindowModality(QtCore.Qt.WindowModal) self._window.setWindowTitle("%s - Quick Jump" % constants.__pretty_app_name__) self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) self._window.setCentralWidget(centralWidget) + self._closeWindowAction = QtGui.QAction(None) + self._closeWindowAction.setText("Window") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + fileMenu = self._window.menuBar().addMenu("&File") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) + + self._window.addAction(self._app.logAction) + self._window.show() + @property + def window(self): + return self._window + def close(self): self._window.close() + def set_fullscreen(self, isFullscreen): + if isFullscreen: + self._window.showFullScreen() + else: + self._window.showNormal() + + @misc_utils.log_exception(_moduleLogger) + def _on_close_window(self, checked = True): + self.close() + @misc_utils.log_exception(_moduleLogger) def _on_result_clicked(self, item, columnIndex): categoryName = unicode(item.text(0)) unitName = unicode(item.text(1)) catWindow = self._app.request_category() - unitsWindow = catWindow.selectCategory(categoryName) + unitsWindow = catWindow.select_category(categoryName) unitsWindow.select_unit(unitName) + self.close() @misc_utils.log_exception(_moduleLogger) def _on_search_edited(self, *args): @@ -210,6 +425,7 @@ class QuickJump(object): if len(userInput) < self.MINIMAL_ENTRY: return + self._resultsBox.clear() lowerInput = str(userInput).lower() for catIndex, category in enumerate(unit_data.UNIT_CATEGORIES): units = unit_data.get_units(category) @@ -221,6 +437,79 @@ class QuickJump(object): twi.setText(1, unit) +class Recent(object): + + def __init__(self, parent, app): + self._app = app + + self._resultsBox = QtGui.QTreeWidget() + self._resultsBox.setHeaderLabels(["Categories", "Units"]) + self._resultsBox.setHeaderHidden(True) + self._resultsBox.setAlternatingRowColors(True) + self._resultsBox.itemClicked.connect(self._on_result_clicked) + + self._layout = QtGui.QVBoxLayout() + self._layout.addWidget(self._resultsBox) + + centralWidget = QtGui.QWidget() + centralWidget.setLayout(self._layout) + + self._window = QtGui.QMainWindow(parent) + self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) + if parent is not None: + self._window.setWindowModality(QtCore.Qt.WindowModal) + self._window.setWindowTitle("%s - Recent" % constants.__pretty_app_name__) + self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) + self._window.setCentralWidget(centralWidget) + + for cat, unit in self._app.get_recent(): + twi = QtGui.QTreeWidgetItem(self._resultsBox) + twi.setText(0, cat) + twi.setText(1, unit) + + self._closeWindowAction = QtGui.QAction(None) + self._closeWindowAction.setText("Window") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + fileMenu = self._window.menuBar().addMenu("&File") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) + + self._window.addAction(self._app.logAction) + + self._window.show() + + @property + def window(self): + return self._window + + def close(self): + self._window.close() + + def set_fullscreen(self, isFullscreen): + if isFullscreen: + self._window.showFullScreen() + else: + self._window.showNormal() + + @misc_utils.log_exception(_moduleLogger) + def _on_close_window(self, checked = True): + self.close() + + @misc_utils.log_exception(_moduleLogger) + def _on_result_clicked(self, item, columnIndex): + categoryName = unicode(item.text(0)) + unitName = unicode(item.text(1)) + catWindow = self._app.request_category() + unitsWindow = catWindow.select_category(categoryName) + unitsWindow.select_unit(unitName) + self.close() + + class UnitData(object): HEADERS = ["Name", "Value", "", "Unit"] @@ -273,6 +562,7 @@ class UnitModel(QtCore.QAbstractItemModel): for key in unit_data.get_units(self._categoryName): conversion, unit, description = self._unitData[key] self._children.append(UnitData(key, unit, description, conversion)) + self._sortSettings = None @misc_utils.log_exception(_moduleLogger) def columnCount(self, parent): @@ -298,6 +588,7 @@ class UnitModel(QtCore.QAbstractItemModel): @misc_utils.log_exception(_moduleLogger) def sort(self, column, order = QtCore.Qt.AscendingOrder): + self._sortSettings = column, order isReverse = order == QtCore.Qt.AscendingOrder if column == 0: key_func = lambda item: item.name @@ -360,6 +651,7 @@ class UnitModel(QtCore.QAbstractItemModel): return len(self._children) def get_unit(self, index): + assert 0 <= index return self._children[index] def index_unit(self, unitName): @@ -380,11 +672,13 @@ class UnitModel(QtCore.QAbstractItemModel): newValue = func.from_base(base, arg) child.update_value(newValue) + if self._sortSettings is not None: + self.sort(*self._sortSettings) self._all_changed() def _all_changed(self): - topLeft = self.createIndex(0, 1, self._children[0]) - bottomRight = self.createIndex(len(self._children)-1, 2, self._children[-1]) + topLeft = self.createIndex(0, 0, self._children[0]) + bottomRight = self.createIndex(len(self._children)-1, len(UnitData.HEADERS)-1, self._children[-1]) self.dataChanged.emit(topLeft, bottomRight) def _sanitize_value(self, userEntry): @@ -426,6 +720,7 @@ class UnitWindow(object): self._unitsView.header().setSortIndicatorShown(True) self._unitsView.header().setClickable(True) self._unitsView.setSortingEnabled(True) + self._unitsView.setAlternatingRowColors(True) if True: self._unitsView.setHeaderHidden(True) @@ -437,30 +732,115 @@ class UnitWindow(object): centralWidget.setLayout(self._layout) self._window = QtGui.QMainWindow(parent) + self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) if parent is not None: self._window.setWindowModality(QtCore.Qt.WindowModal) self._window.setWindowTitle("%s - %s" % (constants.__pretty_app_name__, category)) self._window.setWindowIcon(QtGui.QIcon(app.appIconPath)) self._window.setCentralWidget(centralWidget) - self._select_unit(0) + defaultUnitName = self._app.get_recent_unit(self._categoryName) + if defaultUnitName: + self.select_unit(defaultUnitName) + else: + self._select_unit(0) + self._unitsModel.sort(1) + + self._sortActionGroup = QtGui.QActionGroup(None) + self._sortByNameAction = QtGui.QAction(self._sortActionGroup) + self._sortByNameAction.setText("Sort By Name") + self._sortByNameAction.setStatusTip("Sort the units by name") + self._sortByNameAction.setToolTip("Sort the units by name") + self._sortByValueAction = QtGui.QAction(self._sortActionGroup) + self._sortByValueAction.setText("Sort By Value") + self._sortByValueAction.setStatusTip("Sort the units by value") + self._sortByValueAction.setToolTip("Sort the units by value") + self._sortByUnitAction = QtGui.QAction(self._sortActionGroup) + self._sortByUnitAction.setText("Sort By Unit") + self._sortByUnitAction.setStatusTip("Sort the units by unit") + self._sortByUnitAction.setToolTip("Sort the units by unit") + + self._sortByValueAction.setChecked(True) + + self._previousUnitAction = QtGui.QAction(None) + self._previousUnitAction.setText("Previous Unit") + self._previousUnitAction.setShortcut(QtGui.QKeySequence("Up")) + self._previousUnitAction.triggered.connect(self._on_previous_unit) + + self._nextUnitAction = QtGui.QAction(None) + self._nextUnitAction.setText("Next Unit") + self._nextUnitAction.setShortcut(QtGui.QKeySequence("Down")) + self._nextUnitAction.triggered.connect(self._on_next_unit) + + self._closeWindowAction = QtGui.QAction(None) + self._closeWindowAction.setText("Close Window") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + fileMenu = self._window.menuBar().addMenu("&File") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) - self._app.jumpAction.triggered.connect(self._on_jump_start) viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) + viewMenu.addSeparator() viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + viewMenu.addSeparator() + viewMenu.addAction(self._sortByNameAction) + viewMenu.addAction(self._sortByValueAction) + viewMenu.addAction(self._sortByUnitAction) + + self._sortByNameAction.triggered.connect(self._on_sort_by_name) + self._sortByValueAction.triggered.connect(self._on_sort_by_value) + self._sortByUnitAction.triggered.connect(self._on_sort_by_unit) + + self._window.addAction(self._app.logAction) + self._window.addAction(self._nextUnitAction) + self._window.addAction(self._previousUnitAction) self._window.show() + @property + def window(self): + return self._window + def close(self): self._window.close() + def set_fullscreen(self, isFullscreen): + if isFullscreen: + self._window.showFullScreen() + else: + self._window.showNormal() + def select_unit(self, unitName): index = self._unitsModel.index_unit(unitName) self._select_unit(index) @misc_utils.log_exception(_moduleLogger) - def _on_jump_start(self, checked = False): - self._app.search_units() + def _on_previous_unit(self, checked = True): + self._select_unit(self._selectedIndex - 1) + + @misc_utils.log_exception(_moduleLogger) + def _on_next_unit(self, checked = True): + self._select_unit(self._selectedIndex + 1) + + @misc_utils.log_exception(_moduleLogger) + def _on_close_window(self, checked = True): + self.close() + + @misc_utils.log_exception(_moduleLogger) + def _on_sort_by_name(self, checked = False): + self._unitsModel.sort(0, QtCore.Qt.DescendingOrder) + + @misc_utils.log_exception(_moduleLogger) + def _on_sort_by_value(self, checked = False): + self._unitsModel.sort(1) + + @misc_utils.log_exception(_moduleLogger) + def _on_sort_by_unit(self, checked = False): + self._unitsModel.sort(3, QtCore.Qt.DescendingOrder) @misc_utils.log_exception(_moduleLogger) def _on_unit_clicked(self, index): @@ -480,11 +860,12 @@ class UnitWindow(object): self._selectedIndex = index qindex = self._unitsModel.createIndex(index, 0, self._unitsModel.get_unit(index)) self._unitsView.scrollTo(qindex) + self._app.add_recent(self._categoryName, self._unitsModel.get_unit(index).name) def run_gonvert(): app = QtGui.QApplication([]) - handle = Gonvert() + handle = Gonvert(app) return app.exec_()