4 from __future__ import with_statement
11 from PyQt4 import QtGui
12 from PyQt4 import QtCore
15 from util import misc as misc_utils
19 _moduleLogger = logging.getLogger("gonvert_glade")
22 def change_menu_label(widgets, labelname, newtext):
23 item_label = widgets.get_widget(labelname).get_children()[0]
24 item_label.set_text(newtext)
27 def split_number(number):
29 fractional, integer = math.modf(number)
31 integerDisplay = number
32 fractionalDisplay = ""
34 integerDisplay = str(integer)
35 fractionalDisplay = str(fractional)
36 if "e+" in integerDisplay:
37 integerDisplay = number
38 fractionalDisplay = ""
39 elif "e-" in fractionalDisplay and 0.0 < integer:
40 integerDisplay = number
41 fractionalDisplay = ""
42 elif "e-" in fractionalDisplay:
44 fractionalDisplay = number
46 integerDisplay = integerDisplay.split(".", 1)[0] + "."
47 fractionalDisplay = fractionalDisplay.rsplit(".", 1)[-1]
49 return integerDisplay, fractionalDisplay
52 class Gonvert(object):
54 # @todo Remember last selection
56 # @todo Fullscreen support (showFullscreen, showNormal)
57 # @todo Close Window / Quit keyboard shortcuts
58 # @todo Ctrl+l support
59 # @todo Unit conversion window: focus always on input, arrows switch units
62 os.path.dirname(__file__),
63 os.path.join(os.path.dirname(__file__), "../data"),
64 os.path.join(os.path.dirname(__file__), "../lib"),
71 for dataPath in self._DATA_PATHS:
72 appIconPath = os.path.join(dataPath, "pixmaps", "gonvert.png")
73 if os.path.isfile(appIconPath):
74 self._dataPath = dataPath
77 raise RuntimeError("UI Descriptor not found!")
78 self._appIconPath = appIconPath
81 self._jumpWindow = None
82 self._recentWindow = None
83 self._catWindow = None
85 self._jumpAction = QtGui.QAction(None)
86 self._jumpAction.setText("Quick Jump")
87 self._jumpAction.setStatusTip("Search for a unit and jump straight to it")
88 self._jumpAction.setToolTip("Search for a unit and jump straight to it")
89 self._jumpAction.setShortcut(QtGui.QKeySequence("CTRL+j"))
90 self._jumpAction.triggered.connect(self._on_jump_start)
92 self._recentAction = QtGui.QAction(None)
93 self._recentAction.setText("Recent Units")
94 self._recentAction.setStatusTip("View the recent units")
95 self._recentAction.setToolTip("View the recent units")
96 self._recentAction.setShortcut(QtGui.QKeySequence("CTRL+r"))
97 self._recentAction.triggered.connect(self._on_recent_start)
99 self.request_category()
101 def request_category(self):
102 if self._catWindow is not None:
103 self._catWindow.close()
104 self._catWindow = None
105 self._catWindow = CategoryWindow(None, self)
106 return self._catWindow
108 def search_units(self):
109 if self._jumpWindow is not None:
110 self._jumpWindow.close()
111 self._jumpWindow = None
112 self._jumpWindow = QuickJump(None, self)
113 return self._jumpWindow
115 def show_recent(self):
116 if self._recentWindow is not None:
117 self._recentWindow.close()
118 self._recentWindow = None
119 self._recentWindow = Recent(None, self)
120 return self._recentWindow
122 def add_recent(self, categoryName, unitName):
123 catUnit = categoryName, unitName
125 self._recent.remove(catUnit)
127 pass # ignore if its not already in the recent history
128 self._recent.append(catUnit)
130 def get_recent_unit(self, categoryName):
131 for catName, unitName in reversed(self._recent):
132 if catName == categoryName:
137 def get_recent(self):
138 return reversed(self._recent)
141 def appIconPath(self):
142 return self._appIconPath
145 def jumpAction(self):
146 return self._jumpAction
149 def recentAction(self):
150 return self._recentAction
152 @misc_utils.log_exception(_moduleLogger)
153 def _on_jump_start(self, checked = False):
156 @misc_utils.log_exception(_moduleLogger)
157 def _on_recent_start(self, checked = False):
161 class CategoryWindow(object):
163 def __init__(self, parent, app):
165 self._unitWindow = None
167 self._categories = QtGui.QTreeWidget()
168 self._categories.setHeaderLabels(["Categories"])
169 self._categories.itemClicked.connect(self._on_category_clicked)
170 self._categories.setHeaderHidden(True)
171 self._categories.setAlternatingRowColors(True)
172 for catName in unit_data.UNIT_CATEGORIES:
173 twi = QtGui.QTreeWidgetItem(self._categories)
174 twi.setText(0, catName)
176 self._layout = QtGui.QVBoxLayout()
177 self._layout.addWidget(self._categories)
179 centralWidget = QtGui.QWidget()
180 centralWidget.setLayout(self._layout)
182 self._window = QtGui.QMainWindow(parent)
183 if parent is not None:
184 self._window.setWindowModality(QtCore.Qt.WindowModal)
185 self._window.setWindowTitle("%s - Categories" % constants.__pretty_app_name__)
186 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
187 self._window.setCentralWidget(centralWidget)
189 viewMenu = self._window.menuBar().addMenu("&View")
190 viewMenu.addAction(self._app.jumpAction)
191 viewMenu.addAction(self._app.recentAction)
196 if self._unitWindow is not None:
197 self._unitWindow.close()
198 self._unitWindow = None
201 def selectCategory(self, categoryName):
202 if self._unitWindow is not None:
203 self._unitWindow.close()
204 self._unitWindow = None
205 self._unitWindow = UnitWindow(self._window, categoryName, self._app)
206 return self._unitWindow
208 @misc_utils.log_exception(_moduleLogger)
209 def _on_category_clicked(self, item, columnIndex):
210 categoryName = unicode(item.text(0))
211 self.selectCategory(categoryName)
214 class QuickJump(object):
218 def __init__(self, parent, app):
221 self._searchLabel = QtGui.QLabel("Search:")
222 self._searchEntry = QtGui.QLineEdit("")
223 self._searchEntry.textEdited.connect(self._on_search_edited)
225 self._entryLayout = QtGui.QHBoxLayout()
226 self._entryLayout.addWidget(self._searchLabel)
227 self._entryLayout.addWidget(self._searchEntry)
229 self._resultsBox = QtGui.QTreeWidget()
230 self._resultsBox.setHeaderLabels(["Categories", "Units"])
231 self._resultsBox.setHeaderHidden(True)
232 self._resultsBox.setAlternatingRowColors(True)
233 self._resultsBox.itemClicked.connect(self._on_result_clicked)
235 self._layout = QtGui.QVBoxLayout()
236 self._layout.addLayout(self._entryLayout)
237 self._layout.addWidget(self._resultsBox)
239 centralWidget = QtGui.QWidget()
240 centralWidget.setLayout(self._layout)
242 self._window = QtGui.QMainWindow(parent)
243 if parent is not None:
244 self._window.setWindowModality(QtCore.Qt.WindowModal)
245 self._window.setWindowTitle("%s - Quick Jump" % constants.__pretty_app_name__)
246 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
247 self._window.setCentralWidget(centralWidget)
254 @misc_utils.log_exception(_moduleLogger)
255 def _on_result_clicked(self, item, columnIndex):
256 categoryName = unicode(item.text(0))
257 unitName = unicode(item.text(1))
258 catWindow = self._app.request_category()
259 unitsWindow = catWindow.selectCategory(categoryName)
260 unitsWindow.select_unit(unitName)
263 @misc_utils.log_exception(_moduleLogger)
264 def _on_search_edited(self, *args):
265 userInput = self._searchEntry.text()
266 if len(userInput) < self.MINIMAL_ENTRY:
269 self._resultsBox.clear()
270 lowerInput = str(userInput).lower()
271 for catIndex, category in enumerate(unit_data.UNIT_CATEGORIES):
272 units = unit_data.get_units(category)
273 for unitIndex, unit in enumerate(units):
274 loweredUnit = unit.lower()
275 if lowerInput in loweredUnit:
276 twi = QtGui.QTreeWidgetItem(self._resultsBox)
277 twi.setText(0, category)
281 class Recent(object):
283 def __init__(self, parent, app):
286 self._resultsBox = QtGui.QTreeWidget()
287 self._resultsBox.setHeaderLabels(["Categories", "Units"])
288 self._resultsBox.setHeaderHidden(True)
289 self._resultsBox.setAlternatingRowColors(True)
290 self._resultsBox.itemClicked.connect(self._on_result_clicked)
292 self._layout = QtGui.QVBoxLayout()
293 self._layout.addWidget(self._resultsBox)
295 centralWidget = QtGui.QWidget()
296 centralWidget.setLayout(self._layout)
298 self._window = QtGui.QMainWindow(parent)
299 if parent is not None:
300 self._window.setWindowModality(QtCore.Qt.WindowModal)
301 self._window.setWindowTitle("%s - Recent" % constants.__pretty_app_name__)
302 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
303 self._window.setCentralWidget(centralWidget)
305 for cat, unit in self._app.get_recent():
306 twi = QtGui.QTreeWidgetItem(self._resultsBox)
315 @misc_utils.log_exception(_moduleLogger)
316 def _on_result_clicked(self, item, columnIndex):
317 categoryName = unicode(item.text(0))
318 unitName = unicode(item.text(1))
319 catWindow = self._app.request_category()
320 unitsWindow = catWindow.selectCategory(categoryName)
321 unitsWindow.select_unit(unitName)
325 class UnitData(object):
327 HEADERS = ["Name", "Value", "", "Unit"]
328 ALIGNMENT = [QtCore.Qt.AlignLeft, QtCore.Qt.AlignRight, QtCore.Qt.AlignLeft, QtCore.Qt.AlignLeft]
330 def __init__(self, name, unit, description, conversion):
333 self._description = description
334 self._conversion = conversion
337 self._integerDisplay, self._fractionalDisplay = split_number(self._value)
347 def update_value(self, newValue):
348 self._value = newValue
349 self._integerDisplay, self._fractionalDisplay = split_number(newValue)
356 def conversion(self):
357 return self._conversion
359 def data(self, column):
361 return [self._name, self._integerDisplay, self._fractionalDisplay, self._unit][column]
366 class UnitModel(QtCore.QAbstractItemModel):
368 def __init__(self, categoryName, parent=None):
369 super(UnitModel, self).__init__(parent)
370 self._categoryName = categoryName
371 self._unitData = unit_data.UNIT_DESCRIPTIONS[self._categoryName]
374 for key in unit_data.get_units(self._categoryName):
375 conversion, unit, description = self._unitData[key]
376 self._children.append(UnitData(key, unit, description, conversion))
378 @misc_utils.log_exception(_moduleLogger)
379 def columnCount(self, parent):
383 return len(UnitData.HEADERS)
385 @misc_utils.log_exception(_moduleLogger)
386 def data(self, index, role):
387 if not index.isValid():
389 elif role == QtCore.Qt.TextAlignmentRole:
390 return UnitData.ALIGNMENT[index.column()]
391 elif role != QtCore.Qt.DisplayRole:
394 item = index.internalPointer()
395 if isinstance(item, UnitData):
396 return item.data(index.column())
397 elif item is UnitData.HEADERS:
398 return item[index.column()]
400 @misc_utils.log_exception(_moduleLogger)
401 def sort(self, column, order = QtCore.Qt.AscendingOrder):
402 isReverse = order == QtCore.Qt.AscendingOrder
404 key_func = lambda item: item.name
405 elif column in [1, 2]:
406 key_func = lambda item: item.value
408 key_func = lambda item: item.unit
409 self._children.sort(key=key_func, reverse = isReverse)
413 @misc_utils.log_exception(_moduleLogger)
414 def flags(self, index):
415 if not index.isValid():
416 return QtCore.Qt.NoItemFlags
418 return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
420 @misc_utils.log_exception(_moduleLogger)
421 def headerData(self, section, orientation, role):
422 if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
423 return UnitData.HEADERS[section]
427 @misc_utils.log_exception(_moduleLogger)
428 def index(self, row, column, parent):
429 if not self.hasIndex(row, column, parent):
430 return QtCore.QModelIndex()
433 return QtCore.QModelIndex()
435 parentItem = UnitData.HEADERS
436 childItem = self._children[row]
438 return self.createIndex(row, column, childItem)
440 return QtCore.QModelIndex()
442 @misc_utils.log_exception(_moduleLogger)
443 def parent(self, index):
444 if not index.isValid():
445 return QtCore.QModelIndex()
447 childItem = index.internalPointer()
448 if isinstance(childItem, UnitData):
449 return QtCore.QModelIndex()
450 elif childItem is UnitData.HEADERS:
453 @misc_utils.log_exception(_moduleLogger)
454 def rowCount(self, parent):
455 if 0 < parent.column():
458 if not parent.isValid():
459 return len(self._children)
461 return len(self._children)
463 def get_unit(self, index):
464 return self._children[index]
466 def index_unit(self, unitName):
467 for i, child in enumerate(self._children):
468 if child.name == unitName:
471 raise RuntimeError("Unit not found")
473 def update_values(self, fromIndex, userInput):
474 value = self._sanitize_value(userInput)
475 func, arg = self._children[fromIndex].conversion
476 base = func.to_base(value, arg)
477 for i, child in enumerate(self._children):
480 func, arg = child.conversion
481 newValue = func.from_base(base, arg)
482 child.update_value(newValue)
486 def _all_changed(self):
487 topLeft = self.createIndex(0, 1, self._children[0])
488 bottomRight = self.createIndex(len(self._children)-1, 2, self._children[-1])
489 self.dataChanged.emit(topLeft, bottomRight)
491 def _sanitize_value(self, userEntry):
492 if self._categoryName == "Computer Numbers":
501 value = float(userEntry)
505 class UnitWindow(object):
507 def __init__(self, parent, category, app):
509 self._categoryName = category
510 self._selectedIndex = 0
512 self._selectedUnitName = QtGui.QLabel()
513 self._selectedUnitValue = QtGui.QLineEdit()
514 self._selectedUnitValue.textEdited.connect(self._on_value_edited)
515 self._selectedUnitSymbol = QtGui.QLabel()
517 self._selectedUnitLayout = QtGui.QHBoxLayout()
518 self._selectedUnitLayout.addWidget(self._selectedUnitName)
519 self._selectedUnitLayout.addWidget(self._selectedUnitValue)
520 self._selectedUnitLayout.addWidget(self._selectedUnitSymbol)
522 self._unitsModel = UnitModel(self._categoryName)
523 self._unitsView = QtGui.QTreeView()
524 self._unitsView.setModel(self._unitsModel)
525 self._unitsView.clicked.connect(self._on_unit_clicked)
526 self._unitsView.setUniformRowHeights(True)
527 self._unitsView.header().setSortIndicatorShown(True)
528 self._unitsView.header().setClickable(True)
529 self._unitsView.setSortingEnabled(True)
530 self._unitsView.setAlternatingRowColors(True)
532 self._unitsView.setHeaderHidden(True)
534 self._layout = QtGui.QVBoxLayout()
535 self._layout.addLayout(self._selectedUnitLayout)
536 self._layout.addWidget(self._unitsView)
538 centralWidget = QtGui.QWidget()
539 centralWidget.setLayout(self._layout)
541 self._window = QtGui.QMainWindow(parent)
542 if parent is not None:
543 self._window.setWindowModality(QtCore.Qt.WindowModal)
544 self._window.setWindowTitle("%s - %s" % (constants.__pretty_app_name__, category))
545 self._window.setWindowIcon(QtGui.QIcon(app.appIconPath))
546 self._window.setCentralWidget(centralWidget)
548 defaultUnitName = self._app.get_recent_unit(self._categoryName)
550 self.select_unit(defaultUnitName)
553 self._unitsModel.sort(1)
555 self._sortActionGroup = QtGui.QActionGroup(None)
556 self._sortByNameAction = QtGui.QAction(self._sortActionGroup)
557 self._sortByNameAction.setText("Sort By Name")
558 self._sortByNameAction.setStatusTip("Sort the units by name")
559 self._sortByNameAction.setToolTip("Sort the units by name")
560 self._sortByValueAction = QtGui.QAction(self._sortActionGroup)
561 self._sortByValueAction.setText("Sort By Value")
562 self._sortByValueAction.setStatusTip("Sort the units by value")
563 self._sortByValueAction.setToolTip("Sort the units by value")
564 self._sortByUnitAction = QtGui.QAction(self._sortActionGroup)
565 self._sortByUnitAction.setText("Sort By Unit")
566 self._sortByUnitAction.setStatusTip("Sort the units by unit")
567 self._sortByUnitAction.setToolTip("Sort the units by unit")
569 self._sortByValueAction.setChecked(True)
571 viewMenu = self._window.menuBar().addMenu("&View")
572 viewMenu.addAction(self._app.jumpAction)
573 viewMenu.addAction(self._app.recentAction)
574 viewMenu.addSeparator()
575 viewMenu.addAction(self._sortByNameAction)
576 viewMenu.addAction(self._sortByValueAction)
577 viewMenu.addAction(self._sortByUnitAction)
579 self._sortByNameAction.triggered.connect(self._on_sort_by_name)
580 self._sortByValueAction.triggered.connect(self._on_sort_by_value)
581 self._sortByUnitAction.triggered.connect(self._on_sort_by_unit)
588 def select_unit(self, unitName):
589 index = self._unitsModel.index_unit(unitName)
590 self._select_unit(index)
592 @misc_utils.log_exception(_moduleLogger)
593 def _on_sort_by_name(self, checked = False):
594 self._unitsModel.sort(0, QtCore.Qt.DescendingOrder)
596 @misc_utils.log_exception(_moduleLogger)
597 def _on_sort_by_value(self, checked = False):
598 self._unitsModel.sort(1)
600 @misc_utils.log_exception(_moduleLogger)
601 def _on_sort_by_unit(self, checked = False):
602 self._unitsModel.sort(3, QtCore.Qt.DescendingOrder)
604 @misc_utils.log_exception(_moduleLogger)
605 def _on_unit_clicked(self, index):
606 self._select_unit(index.row())
608 @misc_utils.log_exception(_moduleLogger)
609 def _on_value_edited(self, *args):
610 userInput = self._selectedUnitValue.text()
611 self._unitsModel.update_values(self._selectedIndex, str(userInput))
613 def _select_unit(self, index):
614 unit = self._unitsModel.get_unit(index)
615 self._selectedUnitName.setText(unit.name)
616 self._selectedUnitValue.setText(str(unit.value))
617 self._selectedUnitSymbol.setText(unit.unit)
619 self._selectedIndex = index
620 qindex = self._unitsModel.createIndex(index, 0, self._unitsModel.get_unit(index))
621 self._unitsView.scrollTo(qindex)
622 self._app.add_recent(self._categoryName, self._unitsModel.get_unit(index).name)
626 app = QtGui.QApplication([])
631 if __name__ == "__main__":
632 logging.basicConfig(level = logging.DEBUG)
634 os.makedirs(constants._data_path_)