+ self._appIconPath = appIconPath
+ self._recent = []
+ self._isFullscreen = False
+
+ self._jumpWindow = None
+ self._recentWindow = None
+ self._catWindow = None
+
+ self._jumpAction = QtGui.QAction(None)
+ self._jumpAction.setText("Quick Jump")
+ 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._quitAction = QtGui.QAction(None)
+ self._quitAction.setText("Quit")
+ self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
+ self._quitAction.triggered.connect(self._on_quit)
+
+ self.request_category()
+
+ def request_category(self):
+ if self._catWindow is not None:
+ self._catWindow.close()
+ self._catWindow = None
+ self._catWindow = CategoryWindow(None, self)
+ return self._catWindow
+
+ def search_units(self):
+ if self._jumpWindow is not None:
+ self._jumpWindow.close()
+ self._jumpWindow = None
+ self._jumpWindow = QuickJump(None, self)
+ return self._jumpWindow
+
+ def show_recent(self):
+ if self._recentWindow is not None:
+ self._recentWindow.close()
+ self._recentWindow = None
+ self._recentWindow = Recent(None, self)
+ 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
+ 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)
+
+ @property
+ def appIconPath(self):
+ return self._appIconPath
+
+ @property
+ def jumpAction(self):
+ return self._jumpAction
+
+ @property
+ def recentAction(self):
+ return self._recentAction
+
+ @property
+ def fullscreenAction(self):
+ return self._fullscreenAction
+
+ @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_toggle_fullscreen(self, checked = False):
+ self._isFullscreen = not self._isFullscreen
+ for window in self._walk_children():
+ window.setFullscreen(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_quit(self, checked = False):
+ for window in self._walk_children():
+ window.close()
+
+
+class CategoryWindow(object):
+
+ def __init__(self, parent, app):
+ self._app = app
+ self._unitWindow = None
+
+ self._categories = QtGui.QTreeWidget()
+ 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)
+
+ self._layout = QtGui.QVBoxLayout()
+ self._layout.addWidget(self._categories)
+
+ centralWidget = QtGui.QWidget()
+ centralWidget.setLayout(self._layout)
+
+ self._window = QtGui.QMainWindow(parent)
+ 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._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.show()
+
+ 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):
+ for child in self.walk_children():
+ child.close()
+ self._unitWindow = UnitWindow(self._window, categoryName, self._app)
+ return self._unitWindow
+
+ def setFullscreen(self, isFullscreen):
+ if isFullscreen:
+ self._window.showFullScreen()
+ else:
+ self._window.showNormal()
+ for child in self.walk_children():
+ child.setFullscreen(isFullscreen)
+
+ @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)
+
+
+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)
+ 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)
+ 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.show()
+
+ def close(self):
+ self._window.close()
+
+ def setFullscreen(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.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)
+ 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)
+ 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.show()
+
+ def close(self):
+ self._window.close()
+
+ def setFullscreen(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.select_unit(unitName)
+ self.close()
+
+
+class UnitData(object):
+
+ HEADERS = ["Name", "Value", "", "Unit"]
+ ALIGNMENT = [QtCore.Qt.AlignLeft, QtCore.Qt.AlignRight, QtCore.Qt.AlignLeft, QtCore.Qt.AlignLeft]
+
+ def __init__(self, name, unit, description, conversion):
+ self._name = name
+ self._unit = unit
+ self._description = description
+ self._conversion = conversion
+
+ self._value = 0.0
+ self._integerDisplay, self._fractionalDisplay = split_number(self._value)
+
+ @property
+ def name(self):
+ return self._name
+
+ @property
+ def value(self):
+ return self._value
+
+ def update_value(self, newValue):
+ self._value = newValue
+ self._integerDisplay, self._fractionalDisplay = split_number(newValue)
+
+ @property
+ def unit(self):
+ return self._unit
+
+ @property
+ def conversion(self):
+ return self._conversion
+
+ def data(self, column):
+ try:
+ return [self._name, self._integerDisplay, self._fractionalDisplay, self._unit][column]
+ except IndexError:
+ return None
+
+
+class UnitModel(QtCore.QAbstractItemModel):