import sys
import os
import math
+import simplejson
import logging
from PyQt4 import QtGui
class Gonvert(object):
- # @todo Persist settings
# @todo Favorites
# @bug Fix the resurrecting window problem
- # @todo Ctrl+l support
# @todo Unit conversion window: focus always on input, arrows switch units
_DATA_PATHS = [
'/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._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._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):
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
return self._fullscreenAction
@property
+ def logAction(self):
+ return self._logAction
+
+ @property
def quitAction(self):
return self._quitAction
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.setFullscreen(self._isFullscreen)
+ window.set_fullscreen(self._isFullscreen)
@misc_utils.log_exception(_moduleLogger)
def _on_jump_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()
viewMenu.addAction(self._app.jumpAction)
viewMenu.addAction(self._app.recentAction)
+ self._window.addAction(self._app.logAction)
+
self._window.show()
def walk_children(self):
child.close()
self._window.close()
- def selectCategory(self, categoryName):
+ 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 setFullscreen(self, isFullscreen):
+ def set_fullscreen(self, isFullscreen):
if isFullscreen:
self._window.showFullScreen()
else:
self._window.showNormal()
for child in self.walk_children():
- child.setFullscreen(isFullscreen)
+ child.set_fullscreen(isFullscreen)
@misc_utils.log_exception(_moduleLogger)
def _on_close_window(self, checked = True):
@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):
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):
+ def set_fullscreen(self, isFullscreen):
if isFullscreen:
self._window.showFullScreen()
else:
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()
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):
+ def set_fullscreen(self, isFullscreen):
if isFullscreen:
self._window.showFullScreen()
else:
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()
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 setFullscreen(self, isFullscreen):
+ def set_fullscreen(self, isFullscreen):
if isFullscreen:
self._window.showFullScreen()
else:
def run_gonvert():
app = QtGui.QApplication([])
- handle = Gonvert()
+ handle = Gonvert(app)
return app.exec_()