X-Git-Url: http://git.maemo.org/git/?p=gonvert;a=blobdiff_plain;f=src%2Fgonvert_qt.py;h=ab5a75417c4653dfd434ba548b1a2cd81476f6a5;hp=374d72ea27f160758e9a6e2dfa1bb8e311d82122;hb=9fe42c0fc8724b8a7e42cbb05ab9553117635c60;hpb=29a6c420dfe27d1b86f8611d56ba79d0a3556b48 diff --git a/src/gonvert_qt.py b/src/gonvert_qt.py index 374d72e..ab5a754 100755 --- a/src/gonvert_qt.py +++ b/src/gonvert_qt.py @@ -1,6 +1,10 @@ #!/usr/bin/env python # -*- coding: UTF8 -*- +#@todo Research Fn +#@todo Research optimizations +#@todo Look into switching favorites from selection to checking? + from __future__ import with_statement import sys @@ -13,16 +17,15 @@ from PyQt4 import QtGui from PyQt4 import QtCore import constants +import maeqt from util import misc as misc_utils import unit_data -_moduleLogger = logging.getLogger("gonvert_glade") +_moduleLogger = logging.getLogger(__name__) -def change_menu_label(widgets, labelname, newtext): - item_label = widgets.get_widget(labelname).get_children()[0] - item_label.set_text(newtext) +IS_MAEMO = True def split_number(number): @@ -54,10 +57,10 @@ class Gonvert(object): _DATA_PATHS = [ os.path.dirname(__file__), + os.path.join(os.path.dirname(__file__), "../share"), os.path.join(os.path.dirname(__file__), "../data"), - os.path.join(os.path.dirname(__file__), "../lib"), '/usr/share/gonvert', - '/usr/lib/gonvert', + '/opt/gonvert/share', ] def __init__(self, app): @@ -78,7 +81,19 @@ class Gonvert(object): self._jumpWindow = None self._recentWindow = None + self._mainWindow = None self._catWindow = None + self._quickWindow = None + + self._on_jump_close = lambda obj = None: self._on_child_close("_jumpWindow", obj) + self._on_recent_close = lambda obj = None: self._on_child_close("_recentWindow", obj) + self._on_cat_close = lambda obj = None: self._on_child_close("_catWindow", obj) + self._on_quick_close = lambda obj = None: self._on_child_close("_quickWindow", obj) + + self._condensedAction = QtGui.QAction(None) + self._condensedAction.setText("Condensed View") + self._condensedAction.setCheckable(True) + self._condensedAction.triggered.connect(self._on_condensed_start) self._jumpAction = QtGui.QAction(None) self._jumpAction.setText("Quick Jump") @@ -104,6 +119,25 @@ class Gonvert(object): self._showFavoritesAction.setCheckable(True) self._showFavoritesAction.setText("Favorites Only") + 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._sortByNameAction.setCheckable(True) + 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._sortByValueAction.setCheckable(True) + 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._sortByUnitAction.setCheckable(True) + + self._sortByNameAction.setChecked(True) + self._logAction = QtGui.QAction(None) self._logAction.setText("Log") self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l")) @@ -115,31 +149,49 @@ class Gonvert(object): self._quitAction.triggered.connect(self._on_quit) self._app.lastWindowClosed.connect(self._on_app_quit) - self.request_category() self.load_settings() + self.request_category() + if self._recent: + self._mainWindow.select_category(self._recent[-1][0]) + 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 + + if self._condensedAction.isChecked(): + if self._catWindow is not None: + self._catWindow.hide() + + if self._quickWindow is None: + self._quickWindow = QuickConvert(None, self) + self._quickWindow.window.destroyed.connect(self._on_quick_close) + else: + self._quickWindow.show() + + self._mainWindow = self._quickWindow + else: + if self._quickWindow is not None: + self._quickWindow.hide() + + if self._catWindow is None: + self._catWindow = CategoryWindow(None, self) + self._catWindow.window.destroyed.connect(self._on_cat_close) + else: + self._catWindow.window.show() + + self._mainWindow = self._catWindow + + return self._mainWindow def search_units(self): - if self._jumpWindow is not None: - 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)) + jumpWindow = QuickJump(None, self) + jumpWindow.window.destroyed.connect(self._on_jump_close) + self._jumpWindow = jumpWindow 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)) + recentWindow = Recent(None, self) + recentWindow.window.destroyed.connect(self._on_recent_close) + self._recentWindow = recentWindow return self._recentWindow def add_recent(self, categoryName, unitName): @@ -151,12 +203,16 @@ class Gonvert(object): assert catUnit not in self._recent self._recent.append(catUnit) - def get_recent_unit(self, categoryName): + def get_recent_unit(self, categoryName, fromMostRecent = 0): + recentUnitName = "" for catName, unitName in reversed(self._recent): if catName == categoryName: - return unitName - else: - return "" + recentUnitName = unitName + if fromMostRecent <= 0: + break + else: + fromMostRecent -= 1 + return recentUnitName def get_recent(self): return reversed(self._recent) @@ -185,6 +241,25 @@ class Gonvert(object): self._fullscreenAction.setChecked(settings.get("isFullScreen", False)) + sortBy = settings.get("sortBy", "name") + if sortBy not in ["name", "value", "unit"]: + _moduleLogger.info("Setting sortBy is not a valid value: %s" % sortBy) + sortBy = "name" + if sortBy == "name": + self._sortByNameAction.setChecked(True) + self._sortByValueAction.setChecked(False) + self._sortByUnitAction.setChecked(False) + elif sortBy == "value": + self._sortByNameAction.setChecked(False) + self._sortByValueAction.setChecked(True) + self._sortByUnitAction.setChecked(False) + elif sortBy == "unit": + self._sortByNameAction.setChecked(False) + self._sortByValueAction.setChecked(False) + self._sortByUnitAction.setChecked(True) + else: + raise RuntimeError("How did this sortBy come about? %s" % sortBy) + recent = settings.get("recent", self._recent) for category, unit in recent: self.add_recent(category, unit) @@ -197,10 +272,17 @@ class Gonvert(object): self._showFavoritesAction.setChecked(settings.get("showFavorites", True)) - if self._recent: - self._catWindow.select_category(self._recent[-1][0]) + self._condensedAction.setChecked(settings.get("useQuick", self._condensedAction.isChecked())) def save_settings(self): + if self._sortByNameAction.isChecked(): + sortBy = "name" + elif self._sortByValueAction.isChecked(): + sortBy = "value" + elif self._sortByUnitAction.isChecked(): + sortBy = "unit" + else: + raise RuntimeError("Unknown sorting value") settings = { "isFullScreen": self._fullscreenAction.isChecked(), "recent": self._recent, @@ -210,6 +292,8 @@ class Gonvert(object): for (catName, units) in self._hiddenUnits.iteritems() ), "showFavorites": self._showFavoritesAction.isChecked(), + "useQuick": self._condensedAction.isChecked(), + "sortBy": sortBy, } with open(constants._user_settings_, "w") as settingsFile: simplejson.dump(settings, settingsFile) @@ -231,6 +315,22 @@ class Gonvert(object): return self._fullscreenAction @property + def condensedAction(self): + return self._condensedAction + + @property + def sortByNameAction(self): + return self._sortByNameAction + + @property + def sortByValueAction(self): + return self._sortByValueAction + + @property + def sortByUnitAction(self): + return self._sortByUnitAction + + @property def logAction(self): return self._logAction @@ -245,11 +345,31 @@ class Gonvert(object): def _walk_children(self): if self._catWindow is not None: yield self._catWindow + if self._quickWindow is not None: + yield self._quickWindow if self._jumpWindow is not None: yield self._jumpWindow if self._recentWindow is not None: yield self._recentWindow + def _close_windows(self): + if self._catWindow is not None: + self._catWindow.window.destroyed.disconnect(self._on_cat_close) + self._catWindow.close() + self._catWindow = None + if self._quickWindow is not None: + self._quickWindow.window.destroyed.disconnect(self._on_quick_close) + self._quickWindow.close() + self._quickWindow = None + if self._jumpWindow is not None: + self._jumpWindow.window.destroyed.disconnect(self._on_jump_close) + self._jumpWindow.close() + self._jumpWindow = None + if self._recentWindow is not None: + self._recentWindow.window.destroyed.disconnect(self._on_recent_close) + self._recentWindow.close() + self._recentWindow = None + @misc_utils.log_exception(_moduleLogger) def _on_app_quit(self, checked = False): self.save_settings() @@ -267,6 +387,12 @@ class Gonvert(object): window.set_fullscreen(checked) @misc_utils.log_exception(_moduleLogger) + def _on_condensed_start(self, checked = False): + self.request_category() + if self._recent: + self._mainWindow.select_category(self._recent[-1][0]) + + @misc_utils.log_exception(_moduleLogger) def _on_jump_start(self, checked = False): self.search_units() @@ -283,8 +409,7 @@ class Gonvert(object): @misc_utils.log_exception(_moduleLogger) def _on_quit(self, checked = False): - for window in self._walk_children(): - window.close() + self._close_windows() class QuickJump(object): @@ -305,7 +430,8 @@ class QuickJump(object): self._resultsBox = QtGui.QTreeWidget() self._resultsBox.setHeaderLabels(["Categories", "Units"]) self._resultsBox.setHeaderHidden(True) - self._resultsBox.setAlternatingRowColors(True) + if not IS_MAEMO: + self._resultsBox.setAlternatingRowColors(True) self._resultsBox.itemClicked.connect(self._on_result_clicked) self._layout = QtGui.QVBoxLayout() @@ -317,8 +443,8 @@ class QuickJump(object): self._window = QtGui.QMainWindow(parent) self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) - if parent is not None: - self._window.setWindowModality(QtCore.Qt.WindowModal) + maeqt.set_autorient(self._window, True) + maeqt.set_stackable(self._window, True) self._window.setWindowTitle("%s - Quick Jump" % constants.__pretty_app_name__) self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) self._window.setCentralWidget(centralWidget) @@ -328,12 +454,17 @@ class QuickJump(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - fileMenu = self._window.menuBar().addMenu("&Units") - fileMenu.addAction(self._closeWindowAction) - fileMenu.addAction(self._app.quitAction) + if IS_MAEMO: + self._window.addAction(self._closeWindowAction) + self._window.addAction(self._app.quitAction) + self._window.addAction(self._app.fullscreenAction) + else: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) - viewMenu = self._window.menuBar().addMenu("&View") - viewMenu.addAction(self._app.fullscreenAction) + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) self._window.addAction(self._app.logAction) @@ -344,6 +475,12 @@ class QuickJump(object): def window(self): return self._window + def show(self): + self._window.show() + + def hide(self): + self._window.hide() + def close(self): self._window.close() @@ -392,7 +529,8 @@ class Recent(object): self._resultsBox = QtGui.QTreeWidget() self._resultsBox.setHeaderLabels(["Categories", "Units"]) self._resultsBox.setHeaderHidden(True) - self._resultsBox.setAlternatingRowColors(True) + if not IS_MAEMO: + self._resultsBox.setAlternatingRowColors(True) self._resultsBox.itemClicked.connect(self._on_result_clicked) self._layout = QtGui.QVBoxLayout() @@ -403,8 +541,8 @@ class Recent(object): self._window = QtGui.QMainWindow(parent) self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) - if parent is not None: - self._window.setWindowModality(QtCore.Qt.WindowModal) + maeqt.set_autorient(self._window, True) + maeqt.set_stackable(self._window, True) self._window.setWindowTitle("%s - Recent" % constants.__pretty_app_name__) self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) self._window.setCentralWidget(centralWidget) @@ -419,12 +557,17 @@ class Recent(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - fileMenu = self._window.menuBar().addMenu("&Units") - fileMenu.addAction(self._closeWindowAction) - fileMenu.addAction(self._app.quitAction) + if IS_MAEMO: + self._window.addAction(self._closeWindowAction) + self._window.addAction(self._app.quitAction) + self._window.addAction(self._app.fullscreenAction) + else: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) - viewMenu = self._window.menuBar().addMenu("&View") - viewMenu.addAction(self._app.fullscreenAction) + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) self._window.addAction(self._app.logAction) @@ -435,6 +578,12 @@ class Recent(object): def window(self): return self._window + def show(self): + self._window.show() + + def hide(self): + self._window.hide() + def close(self): self._window.close() @@ -458,7 +607,426 @@ class Recent(object): self.close() -class FavoriteCategoriesWindow(object): +class QuickConvert(object): + + def __init__(self, parent, app): + self._app = app + self._categoryName = "" + self._inputUnitName = "" + self._outputUnitName = "" + self._unitNames = [] + self._favoritesWindow = None + + self._inputUnitValue = QtGui.QLineEdit() + maeqt.mark_numbers_preferred(self._inputUnitValue) + self._inputUnitValue.textEdited.connect(self._on_value_edited) + self._inputUnitSymbol = QtGui.QLabel() + + self._outputUnitValue = QtGui.QLineEdit() + maeqt.mark_numbers_preferred(self._outputUnitValue) + self._outputUnitValue.textEdited.connect(self._on_output_value_edited) + self._outputUnitSymbol = QtGui.QLabel() + + self._conversionLayout = QtGui.QHBoxLayout() + self._conversionLayout.addWidget(self._inputUnitValue) + self._conversionLayout.addWidget(self._inputUnitSymbol) + self._conversionLayout.addWidget(self._outputUnitValue) + self._conversionLayout.addWidget(self._outputUnitSymbol) + + self._categoryView = QtGui.QTreeWidget() + self._categoryView.setHeaderLabels(["Categories"]) + self._categoryView.setHeaderHidden(False) + if not IS_MAEMO: + self._categoryView.setAlternatingRowColors(True) + self._categoryView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) + self._categoryView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) + for catName in unit_data.UNIT_CATEGORIES: + twi = QtGui.QTreeWidgetItem(self._categoryView) + twi.setText(0, catName) + self._categorySelection = self._categoryView.selectionModel() + self._categorySelection.selectionChanged.connect(self._on_category_selection_changed) + + self._inputView = QtGui.QTreeWidget() + self._inputView.setHeaderLabels(["From", "Name"]) + self._inputView.setHeaderHidden(False) + self._inputView.header().hideSection(1) + if not IS_MAEMO: + self._inputView.setAlternatingRowColors(True) + self._inputView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) + self._inputView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) + self._inputSelection = self._inputView.selectionModel() + self._inputSelection.selectionChanged.connect(self._on_input_selection_changed) + + self._outputView = QtGui.QTreeWidget() + self._outputView.setHeaderLabels(["To", "Name"]) + self._outputView.setHeaderHidden(False) + self._outputView.header().hideSection(1) + if not IS_MAEMO: + self._outputView.setAlternatingRowColors(True) + self._outputView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) + self._outputView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) + self._outputWidgets = [] + self._outputSelection = self._outputView.selectionModel() + self._outputSelection.selectionChanged.connect(self._on_output_selection_changed) + + self._selectionLayout = QtGui.QHBoxLayout() + self._selectionLayout.addWidget(self._categoryView) + self._selectionLayout.addWidget(self._inputView) + self._selectionLayout.addWidget(self._outputView) + + self._layout = QtGui.QVBoxLayout() + self._layout.addLayout(self._conversionLayout) + self._layout.addLayout(self._selectionLayout) + + centralWidget = QtGui.QWidget() + centralWidget.setLayout(self._layout) + + self._window = QtGui.QMainWindow(parent) + self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) + maeqt.set_autorient(self._window, True) + maeqt.set_stackable(self._window, True) + self._window.setWindowTitle("%s - Quick Convert" % (constants.__pretty_app_name__, )) + self._window.setWindowIcon(QtGui.QIcon(app.appIconPath)) + self._window.setCentralWidget(centralWidget) + + self._chooseCatFavoritesAction = QtGui.QAction(None) + self._chooseCatFavoritesAction.setText("Select Categories") + self._chooseCatFavoritesAction.triggered.connect(self._on_choose_category_favorites) + + self._chooseUnitFavoritesAction = QtGui.QAction(None) + self._chooseUnitFavoritesAction.setText("Select Units") + self._chooseUnitFavoritesAction.triggered.connect(self._on_choose_unit_favorites) + self._chooseUnitFavoritesAction.setEnabled(False) + + self._app.showFavoritesAction.toggled.connect(self._on_show_favorites) + + 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) + + if IS_MAEMO: + self._window.addAction(self._closeWindowAction) + self._window.addAction(self._app.quitAction) + self._window.addAction(self._app.fullscreenAction) + + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._chooseCatFavoritesAction) + fileMenu.addAction(self._chooseUnitFavoritesAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.showFavoritesAction) + viewMenu.addAction(self._app.condensedAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + else: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._chooseCatFavoritesAction) + fileMenu.addAction(self._chooseUnitFavoritesAction) + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.showFavoritesAction) + viewMenu.addAction(self._app.condensedAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.fullscreenAction) + + self._window.addAction(self._app.logAction) + + self._update_favorites() + self.set_fullscreen(self._app.fullscreenAction.isChecked()) + self._window.show() + + @property + def window(self): + return self._window + + def show(self): + self._window.show() + + def hide(self): + self._window.hide() + + def close(self): + self._window.close() + + def set_fullscreen(self, isFullscreen): + if isFullscreen: + self._window.showFullScreen() + else: + self._window.showNormal() + + def select_category(self, categoryName): + self._select_category(categoryName) + + i = unit_data.UNIT_CATEGORIES.index(categoryName) + rootIndex = self._categoryView.rootIndex() + currentIndex = self._categoryView.model().index(i, 0, rootIndex) + self._categoryView.scrollTo(currentIndex) + self._categoryView.setItemSelected(self._categoryView.topLevelItem(i), True) + + return self + + def select_unit(self, name): + self.select_input(name) + return self + + def select_input(self, name): + self._select_input(name) + + i = self._unitNames.index(name) + rootIndex = self._inputView.rootIndex() + currentIndex = self._inputView.model().index(i, 0, rootIndex) + self._inputView.scrollTo(currentIndex) + self._inputView.setItemSelected(self._inputView.topLevelItem(i), True) + + def select_output(self, name): + self._select_output(name) + + i = self._unitNames.index(name) + rootIndex = self._outputView.rootIndex() + currentIndex = self._outputView.model().index(i, 0, rootIndex) + self._outputView.scrollTo(currentIndex) + self._outputView.setItemSelected(self._outputView.topLevelItem(i), True) + + def _select_category(self, categoryName): + self._inputUnitName = "" + self._outputUnitName = "" + self._inputUnitValue.setText("") + self._inputUnitSymbol.setText("") + self._inputView.clear() + self._outputUnitValue.setText("") + self._outputUnitSymbol.setText("") + self._outputView.clear() + self._categoryName = categoryName + self._chooseUnitFavoritesAction.setEnabled(True) + + unitData = unit_data.UNIT_DESCRIPTIONS[categoryName] + self._unitNames = list(unit_data.get_units(categoryName)) + self._unitNames.sort() + for key in self._unitNames: + conversion, unit, description = unitData[key] + unit = key + + twi = QtGui.QTreeWidgetItem(self._inputView) + twi.setText(0, unit) + twi.setText(1, key) + + twi = QtGui.QTreeWidgetItem(self._outputView) + twi.setText(0, unit) + twi.setText(1, key) + + defaultInputUnitName = self._app.get_recent_unit(categoryName) + if defaultInputUnitName: + self.select_input(defaultInputUnitName) + defaultOutputUnitName = self._app.get_recent_unit(categoryName, 1) + assert defaultOutputUnitName + self.select_output(defaultOutputUnitName) + + def _select_input(self, name): + self._app.add_recent(self._categoryName, name) + self._inputUnitName = name + + unitData = unit_data.UNIT_DESCRIPTIONS[self._categoryName] + conversion, unit, description = unitData[name] + + self._inputUnitSymbol.setText(unit if unit else name) + + if "" not in [self._categoryName, self._inputUnitName, self._outputUnitName]: + self._update_output() + + def _select_output(self, name): + # Add the output to recent but don't make things weird by making it the most recent + self._app.add_recent(self._categoryName, name) + self._app.add_recent(self._categoryName, self._inputUnitName) + self._outputUnitName = name + + unitData = unit_data.UNIT_DESCRIPTIONS[self._categoryName] + conversion, unit, description = unitData[name] + + self._outputUnitSymbol.setText(unit if unit else name) + + if "" not in [self._categoryName, self._inputUnitName, self._outputUnitName]: + self._update_output() + + def _sanitize_value(self, userEntry): + if self._categoryName == "Computer Numbers": + if userEntry == '': + value = '0' + else: + value = userEntry + else: + if userEntry == '': + value = 0.0 + else: + value = float(userEntry) + return value + + def _update_output(self): + assert self._categoryName + assert self._inputUnitName + assert self._outputUnitName + + userInput = str(self._inputUnitValue.text()) + value = self._sanitize_value(userInput) + + unitData = unit_data.UNIT_DESCRIPTIONS[self._categoryName] + inputConversion, _, _ = unitData[self._inputUnitName] + outputConversion, _, _ = unitData[self._outputUnitName] + + func, arg = inputConversion + base = func.to_base(value, arg) + + func, arg = outputConversion + newValue = func.from_base(base, arg) + self._outputUnitValue.setText(str(newValue)) + + def _update_input(self): + assert self._categoryName + assert self._inputUnitName + assert self._outputUnitName + + userOutput = str(self._outputUnitValue.text()) + value = self._sanitize_value(userOutput) + + unitData = unit_data.UNIT_DESCRIPTIONS[self._categoryName] + inputConversion, _, _ = unitData[self._inputUnitName] + outputConversion, _, _ = unitData[self._outputUnitName] + + func, arg = outputConversion + base = func.to_base(value, arg) + + func, arg = inputConversion + newValue = func.from_base(base, arg) + self._inputUnitValue.setText(str(newValue)) + + def _update_favorites(self): + if self._app.showFavoritesAction.isChecked(): + assert self._categoryView.topLevelItemCount() == len(unit_data.UNIT_CATEGORIES) + for i, catName in enumerate(unit_data.UNIT_CATEGORIES): + if catName in self._app.hiddenCategories: + self._categoryView.setRowHidden(i, self._categoryView.rootIndex(), True) + else: + self._categoryView.setRowHidden(i, self._categoryView.rootIndex(), False) + + for i, unitName in enumerate(self._unitNames): + if unitName in self._app.get_hidden_units(self._categoryName): + self._inputView.setRowHidden(i, self._inputView.rootIndex(), True) + self._outputView.setRowHidden(i, self._outputView.rootIndex(), True) + else: + self._inputView.setRowHidden(i, self._inputView.rootIndex(), False) + self._outputView.setRowHidden(i, self._outputView.rootIndex(), False) + else: + for i in xrange(self._categoryView.topLevelItemCount()): + self._categoryView.setRowHidden(i, self._categoryView.rootIndex(), False) + + for i in xrange(len(self._unitNames)): + self._inputView.setRowHidden(i, self._inputView.rootIndex(), False) + self._outputView.setRowHidden(i, self._outputView.rootIndex(), False) + + @misc_utils.log_exception(_moduleLogger) + def _on_close_window(self, checked = True): + self.close() + + @misc_utils.log_exception(_moduleLogger) + def _on_show_favorites(self, checked = True): + if checked: + assert self._categoryView.topLevelItemCount() == len(unit_data.UNIT_CATEGORIES) + for i, catName in enumerate(unit_data.UNIT_CATEGORIES): + if catName in self._app.hiddenCategories: + self._categoryView.setRowHidden(i, self._categoryView.rootIndex(), True) + + for i, unitName in enumerate(self._unitNames): + if unitName in self._app.get_hidden_units(self._categoryName): + self._inputView.setRowHidden(i, self._inputView.rootIndex(), True) + self._outputView.setRowHidden(i, self._outputView.rootIndex(), True) + else: + for i in xrange(self._categoryView.topLevelItemCount()): + self._categoryView.setRowHidden(i, self._categoryView.rootIndex(), False) + + for i in xrange(len(self._unitNames)): + self._inputView.setRowHidden(i, self._inputView.rootIndex(), False) + self._outputView.setRowHidden(i, self._outputView.rootIndex(), False) + + @misc_utils.log_exception(_moduleLogger) + def _on_choose_category_favorites(self, obj = None): + assert self._favoritesWindow is None + self._favoritesWindow = FavoritesWindow( + self._window, + self._app, + unit_data.UNIT_CATEGORIES, + self._app.hiddenCategories + ) + self._favoritesWindow.window.destroyed.connect(self._on_close_favorites) + return self._favoritesWindow + + @misc_utils.log_exception(_moduleLogger) + def _on_choose_unit_favorites(self, obj = None): + assert self._favoritesWindow is None + self._favoritesWindow = FavoritesWindow( + self._window, + self._app, + unit_data.get_units(self._categoryName), + self._app.get_hidden_units(self._categoryName) + ) + self._favoritesWindow.window.destroyed.connect(self._on_close_favorites) + return self._favoritesWindow + + @misc_utils.log_exception(_moduleLogger) + def _on_close_favorites(self, obj = None): + self._favoritesWindow = None + self._update_favorites() + + @misc_utils.log_exception(_moduleLogger) + def _on_value_edited(self, *args): + self._update_output() + + @misc_utils.log_exception(_moduleLogger) + def _on_output_value_edited(self, *args): + self._update_input() + + @misc_utils.log_exception(_moduleLogger) + def _on_category_selection_changed(self, selected, deselected): + selectedNames = [ + str(item.text(0)) + for item in self._categoryView.selectedItems() + ] + assert len(selectedNames) == 1 + self._select_category(selectedNames[0]) + + @misc_utils.log_exception(_moduleLogger) + def _on_input_selection_changed(self, selected, deselected): + selectedNames = [ + str(item.text(1)) + for item in self._inputView.selectedItems() + ] + if selectedNames: + assert len(selectedNames) == 1 + name = selectedNames[0] + self._select_input(name) + else: + pass + + @misc_utils.log_exception(_moduleLogger) + def _on_output_selection_changed(self, selected, deselected): + selectedNames = [ + str(item.text(1)) + for item in self._outputView.selectedItems() + ] + if selectedNames: + assert len(selectedNames) == 1 + name = selectedNames[0] + self._select_output(name) + else: + pass + + +class FavoritesWindow(object): def __init__(self, parent, app, source, hidden): self._app = app @@ -468,7 +1036,8 @@ class FavoriteCategoriesWindow(object): self._categories = QtGui.QTreeWidget() self._categories.setHeaderLabels(["Categories"]) self._categories.setHeaderHidden(True) - self._categories.setAlternatingRowColors(True) + if not IS_MAEMO: + self._categories.setAlternatingRowColors(True) self._categories.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self._categories.setSelectionMode(QtGui.QAbstractItemView.MultiSelection) self._childWidgets = [] @@ -502,8 +1071,8 @@ class FavoriteCategoriesWindow(object): self._window = QtGui.QMainWindow(parent) self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) - if parent is not None: - self._window.setWindowModality(QtCore.Qt.WindowModal) + maeqt.set_autorient(self._window, True) + maeqt.set_stackable(self._window, True) self._window.setWindowTitle("%s - Favorites" % constants.__pretty_app_name__) self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) self._window.setCentralWidget(centralWidget) @@ -513,12 +1082,17 @@ class FavoriteCategoriesWindow(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - fileMenu = self._window.menuBar().addMenu("&Units") - fileMenu.addAction(self._closeWindowAction) - fileMenu.addAction(self._app.quitAction) + if IS_MAEMO: + self._window.addAction(self._closeWindowAction) + self._window.addAction(self._app.quitAction) + self._window.addAction(self._app.fullscreenAction) + else: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) - viewMenu = self._window.menuBar().addMenu("&View") - viewMenu.addAction(self._app.fullscreenAction) + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.fullscreenAction) self._window.addAction(self._app.logAction) @@ -529,6 +1103,12 @@ class FavoriteCategoriesWindow(object): def window(self): return self._window + def show(self): + self._window.show() + + def hide(self): + self._window.hide() + def close(self): self._window.close() @@ -581,7 +1161,10 @@ class CategoryWindow(object): self._categories.setHeaderLabels(["Categories"]) self._categories.itemClicked.connect(self._on_category_clicked) self._categories.setHeaderHidden(True) - self._categories.setAlternatingRowColors(True) + self._categories.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) + self._categories.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) + if not IS_MAEMO: + self._categories.setAlternatingRowColors(True) for catName in unit_data.UNIT_CATEGORIES: twi = QtGui.QTreeWidgetItem(self._categories) twi.setText(0, catName) @@ -594,8 +1177,8 @@ class CategoryWindow(object): self._window = QtGui.QMainWindow(parent) self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) - if parent is not None: - self._window.setWindowModality(QtCore.Qt.WindowModal) + maeqt.set_autorient(self._window, True) + maeqt.set_stackable(self._window, True) self._window.setWindowTitle("%s - Categories" % constants.__pretty_app_name__) self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) self._window.setCentralWidget(centralWidget) @@ -612,18 +1195,34 @@ class CategoryWindow(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - fileMenu = self._window.menuBar().addMenu("&Units") - fileMenu.addAction(self._chooseFavoritesAction) - fileMenu.addAction(self._closeWindowAction) - fileMenu.addAction(self._app.quitAction) + if IS_MAEMO: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._chooseFavoritesAction) - viewMenu = self._window.menuBar().addMenu("&View") - viewMenu.addAction(self._app.showFavoritesAction) - viewMenu.addSeparator() - viewMenu.addAction(self._app.jumpAction) - viewMenu.addAction(self._app.recentAction) - viewMenu.addSeparator() - viewMenu.addAction(self._app.fullscreenAction) + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.showFavoritesAction) + viewMenu.addAction(self._app.condensedAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + + self._window.addAction(self._closeWindowAction) + self._window.addAction(self._app.quitAction) + self._window.addAction(self._app.fullscreenAction) + else: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._chooseFavoritesAction) + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.showFavoritesAction) + viewMenu.addAction(self._app.condensedAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.fullscreenAction) self._window.addAction(self._app.logAction) @@ -641,21 +1240,30 @@ class CategoryWindow(object): if self._favoritesWindow is not None: yield self._favoritesWindow + def show(self): + self._window.show() + for child in self.walk_children(): + child.show() + + def hide(self): + for child in self.walk_children(): + child.hide() + self._window.hide() + def close(self): for child in self.walk_children(): + child.window.destroyed.disconnect(self._on_child_close) child.close() self._window.close() 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) + self._select_category(categoryName) i = unit_data.UNIT_CATEGORIES.index(categoryName) rootIndex = self._categories.rootIndex() currentIndex = self._categories.model().index(i, 0, rootIndex) self._categories.scrollTo(currentIndex) + self._categories.setItemSelected(self._categories.topLevelItem(i), True) return self._unitWindow def set_fullscreen(self, isFullscreen): @@ -666,6 +1274,13 @@ class CategoryWindow(object): for child in self.walk_children(): child.set_fullscreen(isFullscreen) + def _select_category(self, categoryName): + for child in self.walk_children(): + child.window.destroyed.disconnect(self._on_child_close) + child.close() + self._unitWindow = UnitWindow(self._window, categoryName, self._app) + self._unitWindow.window.destroyed.connect(self._on_child_close) + def _update_favorites(self): if self._app.showFavoritesAction.isChecked(): assert self._categories.topLevelItemCount() == len(unit_data.UNIT_CATEGORIES) @@ -679,18 +1294,6 @@ class CategoryWindow(object): self._categories.setRowHidden(i, self._categories.rootIndex(), False) @misc_utils.log_exception(_moduleLogger) - def _on_choose_favorites(self, obj = None): - assert self._favoritesWindow is None - self._favoritesWindow = FavoriteCategoriesWindow( - self._window, - self._app, - unit_data.UNIT_CATEGORIES, - self._app.hiddenCategories - ) - self._favoritesWindow.window.destroyed.connect(self._on_close_favorites) - return self._favoritesWindow - - @misc_utils.log_exception(_moduleLogger) def _on_show_favorites(self, checked = True): if checked: assert self._categories.topLevelItemCount() == len(unit_data.UNIT_CATEGORIES) @@ -702,6 +1305,18 @@ class CategoryWindow(object): self._categories.setRowHidden(i, self._categories.rootIndex(), False) @misc_utils.log_exception(_moduleLogger) + def _on_choose_favorites(self, obj = None): + assert self._favoritesWindow is None + self._favoritesWindow = FavoritesWindow( + self._window, + self._app, + unit_data.UNIT_CATEGORIES, + self._app.hiddenCategories + ) + self._favoritesWindow.window.destroyed.connect(self._on_close_favorites) + return self._favoritesWindow + + @misc_utils.log_exception(_moduleLogger) def _on_close_favorites(self, obj = None): self._favoritesWindow = None self._update_favorites() @@ -884,19 +1499,27 @@ class UnitModel(QtCore.QAbstractItemModel): func, arg = self._children[fromIndex].conversion base = func.to_base(value, arg) for i, child in enumerate(self._children): - if i == fromIndex: - continue func, arg = child.conversion newValue = func.from_base(base, arg) child.update_value(newValue) - if self._sortSettings is not None: + if ( + self._sortSettings is not None and + self._sortSettings[0] in [UnitData.VALUE_COLUMN_0, UnitData.VALUE_COLUMN_1] + ): + # Sort takes care of marking everything as changed self.sort(*self._sortSettings) - self._all_changed() + else: + self._values_changed() def __len__(self): return len(self._children) + def _values_changed(self): + topLeft = self.createIndex(0, UnitData.VALUE_COLUMN_0, self._children[0]) + bottomRight = self.createIndex(len(self._children)-1, UnitData.VALUE_COLUMN_1, self._children[-1]) + self.dataChanged.emit(topLeft, bottomRight) + def _all_changed(self): topLeft = self.createIndex(0, 0, self._children[0]) bottomRight = self.createIndex(len(self._children)-1, len(UnitData.HEADERS)-1, self._children[-1]) @@ -927,6 +1550,7 @@ class UnitWindow(object): self._selectedUnitName = QtGui.QLabel() self._selectedUnitValue = QtGui.QLineEdit() self._selectedUnitValue.textEdited.connect(self._on_value_edited) + maeqt.mark_numbers_preferred(self._selectedUnitValue) self._selectedUnitSymbol = QtGui.QLabel() self._selectedUnitLayout = QtGui.QHBoxLayout() @@ -939,12 +1563,31 @@ class UnitWindow(object): self._unitsView.setModel(self._unitsModel) self._unitsView.clicked.connect(self._on_unit_clicked) self._unitsView.setUniformRowHeights(True) - self._unitsView.header().setSortIndicatorShown(True) - self._unitsView.header().setClickable(True) self._unitsView.setSortingEnabled(True) - self._unitsView.setAlternatingRowColors(True) - if True: - self._unitsView.setHeaderHidden(True) + self._unitsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) + self._unitsView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) + self._unitsView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) + if not IS_MAEMO: + self._unitsView.setAlternatingRowColors(True) + self._unitsView.setHeaderHidden(True) + + viewHeader = self._unitsView.header() + viewHeader.setSortIndicatorShown(True) + viewHeader.setClickable(True) + + viewHeader.setResizeMode(UnitData.NAME_COLUMN, QtGui.QHeaderView.ResizeToContents) + viewHeader.setResizeMode(UnitData.VALUE_COLUMN_0, QtGui.QHeaderView.ResizeToContents) + viewHeader.setResizeMode(UnitData.VALUE_COLUMN_1, QtGui.QHeaderView.ResizeToContents) + viewHeader.setResizeMode(UnitData.UNIT_COLUMN, QtGui.QHeaderView.ResizeToContents) + viewHeader.setStretchLastSection(False) + + # Trying to make things faster by locking in the initial size of the immutable columns + nameSize = min(viewHeader.sectionSize(UnitData.NAME_COLUMN), 150) + viewHeader.setResizeMode(UnitData.NAME_COLUMN, QtGui.QHeaderView.Fixed) + viewHeader.resizeSection(UnitData.NAME_COLUMN, nameSize) + unitSize = min(viewHeader.sectionSize(UnitData.UNIT_COLUMN), 150) + viewHeader.setResizeMode(UnitData.UNIT_COLUMN, QtGui.QHeaderView.Fixed) + viewHeader.resizeSection(UnitData.UNIT_COLUMN, unitSize) self._layout = QtGui.QVBoxLayout() self._layout.addLayout(self._selectedUnitLayout) @@ -955,8 +1598,8 @@ class UnitWindow(object): self._window = QtGui.QMainWindow(parent) self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) - if parent is not None: - self._window.setWindowModality(QtCore.Qt.WindowModal) + maeqt.set_autorient(self._window, True) + maeqt.set_stackable(self._window, True) self._window.setWindowTitle("%s - %s" % (constants.__pretty_app_name__, category)) self._window.setWindowIcon(QtGui.QIcon(app.appIconPath)) self._window.setCentralWidget(centralWidget) @@ -966,26 +1609,18 @@ class UnitWindow(object): self.select_unit(defaultUnitName) else: self._select_unit(0) - self._unitsModel.sort(UnitData.VALUE_COLUMN_0) - - 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._sortByNameAction.setCheckable(True) - 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._sortByValueAction.setCheckable(True) - 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._sortByUnitAction.setCheckable(True) - self._sortByValueAction.setChecked(True) + if self._app.sortByNameAction.isChecked(): + sortColumn = UnitData.NAME_COLUMN + elif self._app.sortByValueAction.isChecked(): + sortColumn = UnitData.VALUE_COLUMN_0 + elif self._app.sortByUnitAction.isChecked(): + sortColumn = UnitData.UNIT_COLUMN + else: + raise RuntimeError("No sort column selected") + if sortColumn != 0: + # By default it sorts by he first column (name) + self._unitsModel.sort(sortColumn) self._chooseFavoritesAction = QtGui.QAction(None) self._chooseFavoritesAction.setText("Select Favorites") @@ -1009,26 +1644,46 @@ class UnitWindow(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - fileMenu = self._window.menuBar().addMenu("&Units") - fileMenu.addAction(self._chooseFavoritesAction) - fileMenu.addAction(self._closeWindowAction) - fileMenu.addAction(self._app.quitAction) - - viewMenu = self._window.menuBar().addMenu("&View") - viewMenu.addAction(self._app.showFavoritesAction) - viewMenu.addSeparator() - viewMenu.addAction(self._sortByNameAction) - viewMenu.addAction(self._sortByValueAction) - viewMenu.addAction(self._sortByUnitAction) - viewMenu.addSeparator() - viewMenu.addAction(self._app.jumpAction) - viewMenu.addAction(self._app.recentAction) - viewMenu.addSeparator() - viewMenu.addAction(self._app.fullscreenAction) - - 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) + if IS_MAEMO: + self._window.addAction(self._closeWindowAction) + self._window.addAction(self._app.quitAction) + self._window.addAction(self._app.fullscreenAction) + + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._chooseFavoritesAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.showFavoritesAction) + viewMenu.addAction(self._app.condensedAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.sortByNameAction) + viewMenu.addAction(self._app.sortByValueAction) + viewMenu.addAction(self._app.sortByUnitAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + else: + fileMenu = self._window.menuBar().addMenu("&Units") + fileMenu.addAction(self._chooseFavoritesAction) + fileMenu.addAction(self._closeWindowAction) + fileMenu.addAction(self._app.quitAction) + + viewMenu = self._window.menuBar().addMenu("&View") + viewMenu.addAction(self._app.showFavoritesAction) + viewMenu.addAction(self._app.condensedAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.sortByNameAction) + viewMenu.addAction(self._app.sortByValueAction) + viewMenu.addAction(self._app.sortByUnitAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.jumpAction) + viewMenu.addAction(self._app.recentAction) + viewMenu.addSeparator() + viewMenu.addAction(self._app.fullscreenAction) + + self._app.sortByNameAction.triggered.connect(self._on_sort_by_name) + self._app.sortByValueAction.triggered.connect(self._on_sort_by_value) + self._app.sortByUnitAction.triggered.connect(self._on_sort_by_unit) self._window.addAction(self._app.logAction) self._window.addAction(self._nextUnitAction) @@ -1043,8 +1698,19 @@ class UnitWindow(object): def window(self): return self._window + def show(self): + for child in self.walk_children(): + child.hide() + self._window.show() + + def hide(self): + for child in self.walk_children(): + child.hide() + self._window.hide() + def close(self): for child in self.walk_children(): + child.window.destroyed.disconnect(self._on_child_close) child.close() self._window.close() @@ -1058,11 +1724,14 @@ class UnitWindow(object): index = self._unitsModel.index_unit(unitName) self._select_unit(index) + qindex = self._unitsModel.createIndex(index, 0, self._unitsModel.get_unit(index)) + self._unitsView.scrollTo(qindex) + def walk_children(self): if self._favoritesWindow is not None: yield self._favoritesWindow - def _update_favorites(self): + def _update_favorites(self, force = False): if self._app.showFavoritesAction.isChecked(): unitNames = list(self._unitsModel.get_unit_names()) for i, unitName in enumerate(unitNames): @@ -1071,13 +1740,25 @@ class UnitWindow(object): else: self._unitsView.setRowHidden(i, self._unitsView.rootIndex(), False) else: + if force: + for i in xrange(len(self._unitsModel)): + self._unitsView.setRowHidden(i, self._unitsView.rootIndex(), False) + + @misc_utils.log_exception(_moduleLogger) + def _on_show_favorites(self, checked = True): + if checked: + unitNames = list(self._unitsModel.get_unit_names()) + for i, unitName in enumerate(unitNames): + if unitName in self._app.get_hidden_units(self._categoryName): + self._unitsView.setRowHidden(i, self._unitsView.rootIndex(), True) + else: for i in xrange(len(self._unitsModel)): self._unitsView.setRowHidden(i, self._unitsView.rootIndex(), False) @misc_utils.log_exception(_moduleLogger) def _on_choose_favorites(self, obj = None): assert self._favoritesWindow is None - self._favoritesWindow = FavoriteCategoriesWindow( + self._favoritesWindow = FavoritesWindow( self._window, self._app, unit_data.get_units(self._categoryName), @@ -1087,20 +1768,9 @@ class UnitWindow(object): return self._favoritesWindow @misc_utils.log_exception(_moduleLogger) - def _on_show_favorites(self, checked = True): - if checked: - unitNames = list(self._unitsModel.get_unit_names()) - for i, unitName in enumerate(unitNames): - if unitName in self._app.get_hidden_units(self._categoryName): - self._unitsView.setRowHidden(i, self._unitsView.rootIndex(), True) - else: - for i in xrange(len(self._unitsModel)): - self._unitsView.setRowHidden(i, self._unitsView.rootIndex(), False) - - @misc_utils.log_exception(_moduleLogger) def _on_close_favorites(self, obj = None): self._favoritesWindow = None - self._update_favorites() + self._update_favorites(force=True) @misc_utils.log_exception(_moduleLogger) def _on_previous_unit(self, checked = True): @@ -1143,8 +1813,6 @@ class UnitWindow(object): self._selectedUnitSymbol.setText(unit.unit) 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)