From cd94b8fea992dc4dceecb474a3d73f4c92023611 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 8 Jun 2010 19:38:09 -0500 Subject: [PATCH] Start of QT calculator --- src/REPLACEME_qt.py | 235 --------------------------------------------------- src/ejpi_qt.py | 212 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+), 235 deletions(-) delete mode 100755 src/REPLACEME_qt.py create mode 100755 src/ejpi_qt.py diff --git a/src/REPLACEME_qt.py b/src/REPLACEME_qt.py deleted file mode 100755 index a66596e..0000000 --- a/src/REPLACEME_qt.py +++ /dev/null @@ -1,235 +0,0 @@ -#!/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) diff --git a/src/ejpi_qt.py b/src/ejpi_qt.py new file mode 100755 index 0000000..743b43d --- /dev/null +++ b/src/ejpi_qt.py @@ -0,0 +1,212 @@ +#!/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 + + +_moduleLogger = logging.getLogger(__name__) + + +IS_MAEMO = True + + +class Calculator(object): + + def __init__(self, app): + self._app = app + 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 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.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 = Calculator(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) -- 1.7.9.5