import sys
import os
import math
+import simplejson
import logging
from PyQt4 import QtGui
class Gonvert(object):
+ # @todo Favorites
+ # @bug Fix the resurrecting window problem
+ # @todo Unit conversion window: focus always on input, arrows switch units
+
_DATA_PATHS = [
os.path.dirname(__file__),
os.path.join(os.path.dirname(__file__), "../data"),
'/usr/lib/gonvert',
]
- def __init__(self):
+ def __init__(self, app):
self._dataPath = ""
for dataPath in self._DATA_PATHS:
appIconPath = os.path.join(dataPath, "pixmaps", "gonvert.png")
break
else:
raise RuntimeError("UI Descriptor not found!")
+ self._app = app
self._appIconPath = appIconPath
+ self._recent = []
+ self._isFullscreen = False
+ self._clipboard = QtGui.QApplication.clipboard()
self._jumpWindow = None
+ self._recentWindow = None
self._catWindow = None
self._jumpAction = QtGui.QAction(None)
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._logAction = QtGui.QAction(None)
+ self._logAction.setText("Log")
+ self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
+ self._logAction.triggered.connect(self._on_log)
+
+ self._quitAction = QtGui.QAction(None)
+ self._quitAction.setText("Quit")
+ self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
+ self._quitAction.triggered.connect(self._on_quit)
+
+ self._app.lastWindowClosed.connect(self._on_app_quit)
self.request_category()
+ self.load_settings()
def request_category(self):
if self._catWindow is not 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
+ assert catUnit not in self._recent
+ 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)
+
+ def load_settings(self):
+ try:
+ with open(constants._user_settings_, "r") as settingsFile:
+ settings = simplejson.load(settingsFile)
+ except IOError, e:
+ settings = {}
+ self._isFullscreen = settings.get("isFullScreen", self._isFullscreen)
+ recent = settings.get("recent", self._recent)
+ for category, unit in recent:
+ self.add_recent(category, unit)
+
+ for window in self._walk_children():
+ window.set_fullscreen(self._isFullscreen)
+ if self._recent:
+ self._catWindow.select_category(self._recent[-1][0])
+
+ def save_settings(self):
+ settings = {
+ "isFullScreen": self._isFullscreen,
+ "recent": self._recent,
+ }
+ with open(constants._user_settings_, "w") as settingsFile:
+ simplejson.dump(settings, settingsFile)
+
@property
def appIconPath(self):
return self._appIconPath
def jumpAction(self):
return self._jumpAction
+ @property
+ def recentAction(self):
+ return self._recentAction
+
+ @property
+ def fullscreenAction(self):
+ return self._fullscreenAction
+
+ @property
+ def logAction(self):
+ return self._logAction
+
+ @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_app_quit(self, checked = False):
+ self.save_settings()
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_toggle_fullscreen(self, checked = False):
+ self._isFullscreen = not self._isFullscreen
+ for window in self._walk_children():
+ window.set_fullscreen(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_log(self, checked = False):
+ with open(constants._user_logpath_, "r") as f:
+ logLines = f.xreadlines()
+ log = "".join(logLines)
+ self._clipboard.setText(log)
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_quit(self, checked = False):
+ for window in self._walk_children():
+ window.close()
+
class CategoryWindow(object):
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._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._app.jumpAction.triggered.connect(self._on_jump_start)
+ self._window.addAction(self._app.logAction)
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):
- if self._unitWindow is not None:
- self._unitWindow.close()
- self._unitWindow = None
+ def select_category(self, categoryName):
+ for child in self.walk_children():
+ child.close()
self._unitWindow = UnitWindow(self._window, categoryName, self._app)
return self._unitWindow
+ def set_fullscreen(self, isFullscreen):
+ if isFullscreen:
+ self._window.showFullScreen()
+ else:
+ self._window.showNormal()
+ for child in self.walk_children():
+ child.set_fullscreen(isFullscreen)
+
@misc_utils.log_exception(_moduleLogger)
- def _on_jump_start(self, checked = False):
- self._app.search_units()
+ 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)
+ self.select_category(categoryName)
class QuickJump(object):
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._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 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.selectCategory(categoryName)
+ unitsWindow = catWindow.select_category(categoryName)
unitsWindow.select_unit(unitName)
+ self.close()
@misc_utils.log_exception(_moduleLogger)
def _on_search_edited(self, *args):
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)
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 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 UnitData(object):
HEADERS = ["Name", "Value", "", "Unit"]
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):
@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
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, 1, self._children[0])
- bottomRight = self.createIndex(len(self._children)-1, 2, self._children[-1])
+ 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):
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._window.setWindowIcon(QtGui.QIcon(app.appIconPath))
self._window.setCentralWidget(centralWidget)
- self._select_unit(0)
+ defaultUnitName = self._app.get_recent_unit(self._categoryName)
+ if defaultUnitName:
+ self.select_unit(defaultUnitName)
+ else:
+ self._select_unit(0)
+ self._unitsModel.sort(1)
self._sortActionGroup = QtGui.QActionGroup(None)
self._sortByNameAction = QtGui.QAction(self._sortActionGroup)
self._sortByUnitAction.setStatusTip("Sort the units by unit")
self._sortByUnitAction.setToolTip("Sort the units by unit")
+ self._sortByValueAction.setChecked(True)
+
+ 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)
+
+ 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)
viewMenu.addSeparator()
viewMenu.addAction(self._sortByNameAction)
viewMenu.addAction(self._sortByValueAction)
viewMenu.addAction(self._sortByUnitAction)
- self._app.jumpAction.triggered.connect(self._on_jump_start)
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)
+ self._window.addAction(self._app.logAction)
+
self._window.show()
def close(self):
self._window.close()
+ def set_fullscreen(self, isFullscreen):
+ if isFullscreen:
+ self._window.showFullScreen()
+ else:
+ self._window.showNormal()
+
def select_unit(self, unitName):
index = self._unitsModel.index_unit(unitName)
self._select_unit(index)
@misc_utils.log_exception(_moduleLogger)
+ def _on_close_window(self, checked = True):
+ self.close()
+
+ @misc_utils.log_exception(_moduleLogger)
def _on_sort_by_name(self, checked = False):
self._unitsModel.sort(0, QtCore.Qt.DescendingOrder)
self._unitsModel.sort(3, QtCore.Qt.DescendingOrder)
@misc_utils.log_exception(_moduleLogger)
- def _on_jump_start(self, checked = False):
- self._app.search_units()
-
- @misc_utils.log_exception(_moduleLogger)
def _on_unit_clicked(self, index):
self._select_unit(index.row())
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)
def run_gonvert():
app = QtGui.QApplication([])
- handle = Gonvert()
+ handle = Gonvert(app)
return app.exec_()