+ 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.addAction(self._app.logAction)
+
+ 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.addAction(self._app.logAction)
+
+ 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):
+
+ def __init__(self, categoryName, parent=None):
+ super(UnitModel, self).__init__(parent)
+ self._categoryName = categoryName
+ self._unitData = unit_data.UNIT_DESCRIPTIONS[self._categoryName]
+
+ self._children = []
+ 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):
+ if parent.isValid():
+ return 0
+ else:
+ return len(UnitData.HEADERS)
+
+ @misc_utils.log_exception(_moduleLogger)
+ def data(self, index, role):
+ if not index.isValid():
+ return None
+ elif role == QtCore.Qt.TextAlignmentRole:
+ return UnitData.ALIGNMENT[index.column()]
+ elif role != QtCore.Qt.DisplayRole:
+ return None
+
+ item = index.internalPointer()
+ if isinstance(item, UnitData):
+ return item.data(index.column())
+ elif item is UnitData.HEADERS:
+ return item[index.column()]
+
+ @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
+ elif column in [1, 2]:
+ key_func = lambda item: item.value
+ elif column == 3:
+ key_func = lambda item: item.unit
+ self._children.sort(key=key_func, reverse = isReverse)
+
+ self._all_changed()
+
+ @misc_utils.log_exception(_moduleLogger)
+ def flags(self, index):
+ if not index.isValid():
+ return QtCore.Qt.NoItemFlags
+
+ return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
+
+ @misc_utils.log_exception(_moduleLogger)
+ def headerData(self, section, orientation, role):
+ if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
+ return UnitData.HEADERS[section]
+
+ return None
+
+ @misc_utils.log_exception(_moduleLogger)
+ def index(self, row, column, parent):
+ if not self.hasIndex(row, column, parent):
+ return QtCore.QModelIndex()
+
+ if parent.isValid():
+ return QtCore.QModelIndex()
+
+ parentItem = UnitData.HEADERS
+ childItem = self._children[row]
+ if childItem:
+ return self.createIndex(row, column, childItem)
+ else:
+ return QtCore.QModelIndex()
+
+ @misc_utils.log_exception(_moduleLogger)
+ def parent(self, index):
+ if not index.isValid():
+ return QtCore.QModelIndex()
+
+ childItem = index.internalPointer()
+ if isinstance(childItem, UnitData):
+ return QtCore.QModelIndex()
+ elif childItem is UnitData.HEADERS:
+ return None
+
+ @misc_utils.log_exception(_moduleLogger)
+ def rowCount(self, parent):
+ if 0 < parent.column():
+ return 0
+
+ if not parent.isValid():
+ return len(self._children)
+ else:
+ return len(self._children)
+
+ def get_unit(self, index):
+ return self._children[index]
+
+ def index_unit(self, unitName):
+ for i, child in enumerate(self._children):
+ if child.name == unitName:
+ return i
+ else:
+ raise RuntimeError("Unit not found")
+
+ def update_values(self, fromIndex, userInput):
+ value = self._sanitize_value(userInput)
+ 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:
+ self.sort(*self._sortSettings)
+ self._all_changed()
+
+ 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])
+ self.dataChanged.emit(topLeft, bottomRight)
+
+ 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
+