From 2f93a9ed1a61d23dfe1fdc595eaba8d5f421c0f6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 16 Oct 2010 13:44:05 -0500 Subject: [PATCH] Delay load less common windows for better startup time --- src/constants.py | 2 +- src/gonvert_qt.py | 365 +++-------------------------------------------------- src/windows.py | 343 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 365 insertions(+), 345 deletions(-) create mode 100644 src/windows.py diff --git a/src/constants.py b/src/constants.py index d7b0d99..41f3206 100644 --- a/src/constants.py +++ b/src/constants.py @@ -10,4 +10,4 @@ _user_settings_ = "%s/settings.json" % _data_path_ _user_logpath_ = "%s/gonvert.log" % _data_path_ PROFILE_STARTUP = False -FORCE_HILDON_LIKE = False +IS_MAEMO = True diff --git a/src/gonvert_qt.py b/src/gonvert_qt.py index 02b64c9..a937835 100755 --- a/src/gonvert_qt.py +++ b/src/gonvert_qt.py @@ -6,7 +6,6 @@ from __future__ import with_statement -import sys import os import math import simplejson @@ -24,9 +23,6 @@ import unit_data _moduleLogger = logging.getLogger(__name__) -IS_MAEMO = True - - def split_number(number): if number == 0.0: # Optimize the startup case @@ -186,13 +182,15 @@ class Gonvert(object): return self._mainWindow def search_units(self): - jumpWindow = QuickJump(None, self) + import windows + jumpWindow = windows.QuickJump(None, self) jumpWindow.window.destroyed.connect(self._on_jump_close) self._jumpWindow = jumpWindow return self._jumpWindow def show_recent(self): - recentWindow = Recent(None, self) + import windows + recentWindow = windows.Recent(None, self) recentWindow.window.destroyed.connect(self._on_recent_close) self._recentWindow = recentWindow return self._recentWindow @@ -415,201 +413,6 @@ class Gonvert(object): self._close_windows() -class QuickJump(object): - - MINIMAL_ENTRY = 3 - - def __init__(self, parent, app): - self._app = app - - self._searchLabel = QtGui.QLabel("Search:") - self._searchEntry = QtGui.QLineEdit("") - self._searchEntry.textEdited.connect(self._on_search_edited) - - self._entryLayout = QtGui.QHBoxLayout() - self._entryLayout.addWidget(self._searchLabel) - self._entryLayout.addWidget(self._searchEntry) - - self._resultsBox = QtGui.QTreeWidget() - self._resultsBox.setHeaderLabels(["Categories", "Units"]) - self._resultsBox.setHeaderHidden(True) - if not IS_MAEMO: - self._resultsBox.setAlternatingRowColors(True) - self._resultsBox.itemClicked.connect(self._on_result_clicked) - - self._layout = QtGui.QVBoxLayout() - self._layout.addLayout(self._entryLayout) - 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) - 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) - - self._closeWindowAction = QtGui.QAction(None) - self._closeWindowAction.setText("Close") - 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) - 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) - - self._window.addAction(self._app.logAction) - - 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() - - @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() - - @misc_utils.log_exception(_moduleLogger) - def _on_search_edited(self, *args): - userInput = self._searchEntry.text() - 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) - for unitIndex, unit in enumerate(units): - loweredUnit = unit.lower() - if lowerInput in loweredUnit: - twi = QtGui.QTreeWidgetItem(self._resultsBox) - twi.setText(0, category) - 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) - if not IS_MAEMO: - 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) - 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) - - 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("Close") - 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) - 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) - - self._window.addAction(self._app.logAction) - - 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() - - @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 QuickConvert(object): def __init__(self, parent, app): @@ -639,7 +442,7 @@ class QuickConvert(object): self._categoryView = QtGui.QTreeWidget() self._categoryView.setHeaderLabels(["Categories"]) self._categoryView.setHeaderHidden(False) - if not IS_MAEMO: + if not constants.IS_MAEMO: self._categoryView.setAlternatingRowColors(True) self._categoryView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self._categoryView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) @@ -653,7 +456,7 @@ class QuickConvert(object): self._inputView.setHeaderLabels(["From", "Name"]) self._inputView.setHeaderHidden(False) self._inputView.header().hideSection(1) - if not IS_MAEMO: + if not constants.IS_MAEMO: self._inputView.setAlternatingRowColors(True) self._inputView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self._inputView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) @@ -664,7 +467,7 @@ class QuickConvert(object): self._outputView.setHeaderLabels(["To", "Name"]) self._outputView.setHeaderHidden(False) self._outputView.header().hideSection(1) - if not IS_MAEMO: + if not constants.IS_MAEMO: self._outputView.setAlternatingRowColors(True) self._outputView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self._outputView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) @@ -708,7 +511,7 @@ class QuickConvert(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - if IS_MAEMO: + if constants.IS_MAEMO: self._window.addAction(self._closeWindowAction) self._window.addAction(self._app.quitAction) self._window.addAction(self._app.fullscreenAction) @@ -959,7 +762,8 @@ class QuickConvert(object): @misc_utils.log_exception(_moduleLogger) def _on_choose_category_favorites(self, obj = None): assert self._favoritesWindow is None - self._favoritesWindow = FavoritesWindow( + import windows + self._favoritesWindow = windows.FavoritesWindow( self._window, self._app, unit_data.UNIT_CATEGORIES, @@ -971,7 +775,8 @@ class QuickConvert(object): @misc_utils.log_exception(_moduleLogger) def _on_choose_unit_favorites(self, obj = None): assert self._favoritesWindow is None - self._favoritesWindow = FavoritesWindow( + import windows + self._favoritesWindow = windows.FavoritesWindow( self._window, self._app, unit_data.get_units(self._categoryName), @@ -1029,137 +834,6 @@ class QuickConvert(object): pass -class FavoritesWindow(object): - - def __init__(self, parent, app, source, hidden): - self._app = app - self._source = list(source) - self._hidden = hidden - - self._categories = QtGui.QTreeWidget() - self._categories.setHeaderLabels(["Categories"]) - self._categories.setHeaderHidden(True) - self._categories.setSelectionMode(QtGui.QAbstractItemView.NoSelection) - if not IS_MAEMO: - self._categories.setAlternatingRowColors(True) - self._childWidgets = [] - for catName in self._source: - twi = QtGui.QTreeWidgetItem(self._categories) - twi.setText(0, catName) - self._childWidgets.append(twi) - if catName in self._hidden: - twi.setCheckState(0, QtCore.Qt.Unchecked) - else: - twi.setCheckState(0, QtCore.Qt.Checked) - self._categories.itemChanged.connect(self._on_item_changed) - - self._allButton = QtGui.QPushButton("All") - self._allButton.clicked.connect(self._on_select_all) - self._invertButton = QtGui.QPushButton("Invert") - self._invertButton.clicked.connect(self._on_invert_selection) - self._noneButton = QtGui.QPushButton("None") - self._noneButton.clicked.connect(self._on_select_none) - - self._buttonLayout = QtGui.QHBoxLayout() - self._buttonLayout.addWidget(self._allButton) - self._buttonLayout.addWidget(self._invertButton) - self._buttonLayout.addWidget(self._noneButton) - - self._layout = QtGui.QVBoxLayout() - self._layout.addWidget(self._categories) - self._layout.addLayout(self._buttonLayout) - - 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 - Favorites" % constants.__pretty_app_name__) - self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) - self._window.setCentralWidget(centralWidget) - - self._closeWindowAction = QtGui.QAction(None) - self._closeWindowAction.setText("Close") - 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) - 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) - - self._window.addAction(self._app.logAction) - - 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() - - @misc_utils.log_exception(_moduleLogger) - def _on_select_all(self, checked = False): - for child in self._childWidgets: - child.setCheckState(0, QtCore.Qt.Checked) - - @misc_utils.log_exception(_moduleLogger) - def _on_invert_selection(self, checked = False): - for child in self._childWidgets: - state = child.checkState(0) - if state == QtCore.Qt.Unchecked: - newState = QtCore.Qt.Checked - elif state == QtCore.Qt.Checked: - newState = QtCore.Qt.Unchecked - else: - raise RuntimeError("Bad check state %r" % state) - child.setCheckState(0, newState) - - @misc_utils.log_exception(_moduleLogger) - def _on_select_none(self, checked = False): - for child in self._childWidgets: - child.setCheckState(0, QtCore.Qt.Unchecked) - - @misc_utils.log_exception(_moduleLogger) - def _on_item_changed(self, item, column): - state = item.checkState(column) - if state == QtCore.Qt.Unchecked: - name = str(item.text(column)) - self._hidden.add(name) - elif state == QtCore.Qt.Checked: - name = str(item.text(column)) - self._hidden.remove(name) - else: - raise RuntimeError("Bad check state %r" % state) - - @misc_utils.log_exception(_moduleLogger) - def _on_close_window(self, checked = True): - self.close() - - class CategoryWindow(object): def __init__(self, parent, app): @@ -1173,7 +847,7 @@ class CategoryWindow(object): self._categories.setHeaderHidden(True) self._categories.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self._categories.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) - if not IS_MAEMO: + if not constants.IS_MAEMO: self._categories.setAlternatingRowColors(True) for catName in unit_data.UNIT_CATEGORIES: twi = QtGui.QTreeWidgetItem(self._categories) @@ -1205,7 +879,7 @@ class CategoryWindow(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - if IS_MAEMO: + if constants.IS_MAEMO: fileMenu = self._window.menuBar().addMenu("&Units") fileMenu.addAction(self._chooseFavoritesAction) @@ -1317,7 +991,8 @@ class CategoryWindow(object): @misc_utils.log_exception(_moduleLogger) def _on_choose_favorites(self, obj = None): assert self._favoritesWindow is None - self._favoritesWindow = FavoritesWindow( + import windows + self._favoritesWindow = windows.FavoritesWindow( self._window, self._app, unit_data.UNIT_CATEGORIES, @@ -1594,7 +1269,7 @@ class UnitWindow(object): self._unitsView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) self._unitsView.setHeaderHidden(True) self._unitsView.clicked.connect(self._on_unit_clicked) - if not IS_MAEMO: + if not constants.IS_MAEMO: self._unitsView.setAlternatingRowColors(True) viewHeader = self._unitsView.header() @@ -1670,7 +1345,7 @@ class UnitWindow(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - if IS_MAEMO: + if constants.IS_MAEMO: self._window.addAction(self._closeWindowAction) self._window.addAction(self._app.quitAction) self._window.addAction(self._app.fullscreenAction) @@ -1795,7 +1470,8 @@ class UnitWindow(object): @misc_utils.log_exception(_moduleLogger) def _on_choose_favorites(self, obj = None): assert self._favoritesWindow is None - self._favoritesWindow = FavoritesWindow( + import windows + self._favoritesWindow = windows.FavoritesWindow( self._window, self._app, unit_data.get_units(self._categoryName), @@ -1882,6 +1558,7 @@ def run_gonvert(): if __name__ == "__main__": + import sys logging.basicConfig(level = logging.DEBUG) try: os.makedirs(constants._data_path_) diff --git a/src/windows.py b/src/windows.py new file mode 100644 index 0000000..f5174b8 --- /dev/null +++ b/src/windows.py @@ -0,0 +1,343 @@ +#!/usr/bin/env python + +from __future__ import with_statement +from __future__ import division + +import logging + +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(__name__) + + +class FavoritesWindow(object): + + def __init__(self, parent, app, source, hidden): + self._app = app + self._source = list(source) + self._hidden = hidden + + self._categories = QtGui.QTreeWidget() + self._categories.setHeaderLabels(["Categories"]) + self._categories.setHeaderHidden(True) + self._categories.setSelectionMode(QtGui.QAbstractItemView.NoSelection) + if not constants.IS_MAEMO: + self._categories.setAlternatingRowColors(True) + self._childWidgets = [] + for catName in self._source: + twi = QtGui.QTreeWidgetItem(self._categories) + twi.setText(0, catName) + self._childWidgets.append(twi) + if catName in self._hidden: + twi.setCheckState(0, QtCore.Qt.Unchecked) + else: + twi.setCheckState(0, QtCore.Qt.Checked) + self._categories.itemChanged.connect(self._on_item_changed) + + self._allButton = QtGui.QPushButton("All") + self._allButton.clicked.connect(self._on_select_all) + self._invertButton = QtGui.QPushButton("Invert") + self._invertButton.clicked.connect(self._on_invert_selection) + self._noneButton = QtGui.QPushButton("None") + self._noneButton.clicked.connect(self._on_select_none) + + self._buttonLayout = QtGui.QHBoxLayout() + self._buttonLayout.addWidget(self._allButton) + self._buttonLayout.addWidget(self._invertButton) + self._buttonLayout.addWidget(self._noneButton) + + self._layout = QtGui.QVBoxLayout() + self._layout.addWidget(self._categories) + self._layout.addLayout(self._buttonLayout) + + 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 - Favorites" % constants.__pretty_app_name__) + self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath)) + self._window.setCentralWidget(centralWidget) + + self._closeWindowAction = QtGui.QAction(None) + self._closeWindowAction.setText("Close") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + if constants.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) + + self._window.addAction(self._app.logAction) + + 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() + + @misc_utils.log_exception(_moduleLogger) + def _on_select_all(self, checked = False): + for child in self._childWidgets: + child.setCheckState(0, QtCore.Qt.Checked) + + @misc_utils.log_exception(_moduleLogger) + def _on_invert_selection(self, checked = False): + for child in self._childWidgets: + state = child.checkState(0) + if state == QtCore.Qt.Unchecked: + newState = QtCore.Qt.Checked + elif state == QtCore.Qt.Checked: + newState = QtCore.Qt.Unchecked + else: + raise RuntimeError("Bad check state %r" % state) + child.setCheckState(0, newState) + + @misc_utils.log_exception(_moduleLogger) + def _on_select_none(self, checked = False): + for child in self._childWidgets: + child.setCheckState(0, QtCore.Qt.Unchecked) + + @misc_utils.log_exception(_moduleLogger) + def _on_item_changed(self, item, column): + state = item.checkState(column) + if state == QtCore.Qt.Unchecked: + name = str(item.text(column)) + self._hidden.add(name) + elif state == QtCore.Qt.Checked: + name = str(item.text(column)) + self._hidden.remove(name) + else: + raise RuntimeError("Bad check state %r" % state) + + @misc_utils.log_exception(_moduleLogger) + def _on_close_window(self, checked = True): + self.close() + + +class QuickJump(object): + + MINIMAL_ENTRY = 3 + + def __init__(self, parent, app): + self._app = app + + self._searchLabel = QtGui.QLabel("Search:") + self._searchEntry = QtGui.QLineEdit("") + self._searchEntry.textEdited.connect(self._on_search_edited) + + self._entryLayout = QtGui.QHBoxLayout() + self._entryLayout.addWidget(self._searchLabel) + self._entryLayout.addWidget(self._searchEntry) + + self._resultsBox = QtGui.QTreeWidget() + self._resultsBox.setHeaderLabels(["Categories", "Units"]) + self._resultsBox.setHeaderHidden(True) + if not constants.IS_MAEMO: + self._resultsBox.setAlternatingRowColors(True) + self._resultsBox.itemClicked.connect(self._on_result_clicked) + + self._layout = QtGui.QVBoxLayout() + self._layout.addLayout(self._entryLayout) + 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) + 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) + + self._closeWindowAction = QtGui.QAction(None) + self._closeWindowAction.setText("Close") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + if constants.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) + + self._window.addAction(self._app.logAction) + + 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() + + @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() + + @misc_utils.log_exception(_moduleLogger) + def _on_search_edited(self, *args): + userInput = self._searchEntry.text() + 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) + for unitIndex, unit in enumerate(units): + loweredUnit = unit.lower() + if lowerInput in loweredUnit: + twi = QtGui.QTreeWidgetItem(self._resultsBox) + twi.setText(0, category) + 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) + if not constants.IS_MAEMO: + 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) + 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) + + 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("Close") + self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) + self._closeWindowAction.triggered.connect(self._on_close_window) + + if constants.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) + + self._window.addAction(self._app.logAction) + + 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() + + @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() -- 1.7.9.5