+#!/usr/bin/env python
+# -*- coding: UTF8 -*-
+
+from __future__ import with_statement
+
+import sys
+import os
+import simplejson
+import logging
+
+from PyQt4 import QtGui
+from PyQt4 import QtCore
+
+import constants
+import maeqt
+from util import misc as misc_utils
+import unit_data
+
+
+_moduleLogger = logging.getLogger(__name__)
+
+
+IS_MAEMO = True
+
+
+class REPLACEME(object):
+
+ _DATA_PATHS = [
+ os.path.dirname(__file__),
+ os.path.join(os.path.dirname(__file__), "../data"),
+ os.path.join(os.path.dirname(__file__), "../lib"),
+ '/usr/share/%s' % constants.__app_name__,
+ '/usr/lib/%s' % constants.__app_name__,
+ ]
+
+ def __init__(self, app):
+ self._dataPath = ""
+ for dataPath in self._DATA_PATHS:
+ appIconPath = os.path.join(dataPath, "pixmaps", "%s.png" % constants.__app_name__)
+ if os.path.isfile(appIconPath):
+ self._dataPath = dataPath
+ break
+ else:
+ raise RuntimeError("UI Descriptor not found!")
+ self._app = app
+ self._appIconPath = appIconPath
+ self._recent = []
+ self._hiddenCategories = set()
+ self._hiddenUnits = {}
+ self._clipboard = QtGui.QApplication.clipboard()
+
+ self._mainWindow = None
+
+ self._fullscreenAction = QtGui.QAction(None)
+ self._fullscreenAction.setText("Fullscreen")
+ self._fullscreenAction.setCheckable(True)
+ self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
+ self._fullscreenAction.toggled.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.load_settings()
+
+ def load_settings(self):
+ try:
+ with open(constants._user_settings_, "r") as settingsFile:
+ settings = simplejson.load(settingsFile)
+ except IOError, e:
+ _moduleLogger.info("No settings")
+ settings = {}
+ except ValueError:
+ _moduleLogger.info("Settings were corrupt")
+ settings = {}
+
+ self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
+
+ def save_settings(self):
+ settings = {
+ "isFullScreen": self._fullscreenAction.isChecked(),
+ }
+ with open(constants._user_settings_, "w") as settingsFile:
+ simplejson.dump(settings, settingsFile)
+
+ @property
+ def appIconPath(self):
+ return self._appIconPath
+
+ @property
+ def fullscreenAction(self):
+ return self._fullscreenAction
+
+ @property
+ def logAction(self):
+ return self._logAction
+
+ @property
+ def quitAction(self):
+ return self._quitAction
+
+ def _close_windows(self):
+ if self._mainWindow is not None:
+ self._mainWindow.window.destroyed.disconnect(self._on_child_close)
+ self._mainWindow.close()
+ self._mainWindow = None
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_app_quit(self, checked = False):
+ self.save_settings()
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_child_close(self, obj = None):
+ self._mainWindow = None
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_toggle_fullscreen(self, checked = False):
+ for window in self._walk_children():
+ window.set_fullscreen(checked)
+
+ @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):
+ self._close_windows()
+
+
+class MainWindow(object):
+
+ def __init__(self, parent, app):
+ self._app = app
+
+ self._layout = QtGui.QVBoxLayout()
+
+ centralWidget = QtGui.QWidget()
+ centralWidget.setLayout(self._layout)
+
+ self._window = QtGui.QMainWindow(parent)
+ self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
+ maeqt.set_autorient(self._window, True)
+ maeqt.set_stackable(self._window, True)
+ self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
+ self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
+ self._window.setCentralWidget(centralWidget)
+
+ self._closeWindowAction = QtGui.QAction(None)
+ self._closeWindowAction.setText("Close")
+ self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
+ self._closeWindowAction.triggered.connect(self._on_close_window)
+
+ if IS_MAEMO:
+ fileMenu = self._window.menuBar().addMenu("&File")
+
+ viewMenu = self._window.menuBar().addMenu("&View")
+
+ self._window.addAction(self._closeWindowAction)
+ self._window.addAction(self._app.quitAction)
+ self._window.addAction(self._app.fullscreenAction)
+ else:
+ fileMenu = self._window.menuBar().addMenu("&Units")
+ 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.set_fullscreen(self._app.fullscreenAction.isChecked())
+ self._window.show()
+
+ @property
+ def window(self):
+ return self._window
+
+ def walk_children(self):
+ return ()
+
+ def show(self):
+ self._window.show()
+ for child in self.walk_children():
+ child.show()
+
+ def hide(self):
+ for child in self.walk_children():
+ child.hide()
+ self._window.hide()
+
+ def close(self):
+ for child in self.walk_children():
+ child.window.destroyed.disconnect(self._on_child_close)
+ child.close()
+ self._window.close()
+
+ 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_close_window(self, checked = True):
+ self.close()
+
+
+def run():
+ app = QtGui.QApplication([])
+ handle = REPLACEME(app)
+ return app.exec_()
+
+
+if __name__ == "__main__":
+ logging.basicConfig(level = logging.DEBUG)
+ try:
+ os.makedirs(constants._data_path_)
+ except OSError, e:
+ if e.errno != 17:
+ raise
+
+ val = run()
+ sys.exit(val)