From 5b558aaccb2e96d69c01721f12eaf6c96f30e02b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 10 Jan 2011 22:44:20 -0600 Subject: [PATCH] Porting to qwrappers --- src/ejpi_qt.py | 225 ++++++++------------------------------------------------ src/maeqt.py | 122 ------------------------------ 2 files changed, 32 insertions(+), 315 deletions(-) delete mode 100644 src/maeqt.py diff --git a/src/ejpi_qt.py b/src/ejpi_qt.py index 5ec4e06..4550dcf 100755 --- a/src/ejpi_qt.py +++ b/src/ejpi_qt.py @@ -10,12 +10,12 @@ import string import logging from PyQt4 import QtGui -from PyQt4 import QtCore import constants -import maeqt from util import misc as misc_utils +from util import qui_utils +from util import qwrappers from util import qtpie from util import qtpieboard import plugin_utils @@ -26,38 +26,13 @@ import qhistory _moduleLogger = logging.getLogger(__name__) -class Calculator(object): +class Calculator(qwrappers.ApplicationWrapper): 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() - - self._mainWindow = MainWindow(None, self) - self._mainWindow.window.destroyed.connect(self._on_child_close) + qwrappers.ApplicationWrapper.__init__(self, app, constants) def load_settings(self): try: @@ -80,114 +55,22 @@ class Calculator(object): simplejson.dump(settings, settingsFile) @property - def fullscreenAction(self): - return self._fullscreenAction - - @property - def logAction(self): - return self._logAction + def dataPath(self): + return self._dataPath - @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) + def _new_main_window(self): + return MainWindow(None, self) @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 QErrorDisplay(object): - - def __init__(self): - self._messages = [] - - errorIcon = maeqt.get_theme_icon(("dialog-error", "app_install_error", "gtk-dialog-error")) - self._severityIcon = errorIcon.pixmap(32, 32) - self._severityLabel = QtGui.QLabel() - self._severityLabel.setPixmap(self._severityIcon) - - self._message = QtGui.QLabel() - self._message.setText("Boo") - - closeIcon = maeqt.get_theme_icon(("window-close", "general_close", "gtk-close")) - self._closeLabel = QtGui.QPushButton(closeIcon, "") - self._closeLabel.clicked.connect(self._on_close) - - self._controlLayout = QtGui.QHBoxLayout() - self._controlLayout.addWidget(self._severityLabel) - self._controlLayout.addWidget(self._message) - self._controlLayout.addWidget(self._closeLabel) - - self._topLevelLayout = QtGui.QHBoxLayout() - self._topLevelLayout.addLayout(self._controlLayout) - self._widget = QtGui.QWidget() - self._widget.setLayout(self._topLevelLayout) - self._hide_message() - - @property - def toplevel(self): - return self._widget - - def push_message(self, message): - self._messages.append(message) - if 1 == len(self._messages): - self._show_message(message) - - def push_exception(self): - userMessage = str(sys.exc_info()[1]) - _moduleLogger.exception(userMessage) - self.push_message(userMessage) - - def pop_message(self): - del self._messages[0] - if 0 == len(self._messages): - self._hide_message() - else: - self._message.setText(self._messages[0]) - - def _on_close(self, *args): - self.pop_message() - - def _show_message(self, message): - self._message.setText(message) - self._widget.show() - - def _hide_message(self): - self._message.setText("") - self._widget.hide() + def _on_about(self, checked = True): + raise NotImplementedError("Booh") class QValueEntry(object): def __init__(self): self._widget = QtGui.QLineEdit("") - maeqt.mark_numbers_preferred(self._widget) + qui_utils.mark_numbers_preferred(self._widget) @property def toplevel(self): @@ -235,7 +118,7 @@ class QValueEntry(object): value = property(get_value, set_value, clear) -class MainWindow(object): +class MainWindow(qwrappers.WindowWrapper): _plugin_search_paths = [ os.path.join(os.path.dirname(__file__), "plugins/"), @@ -244,10 +127,9 @@ class MainWindow(object): _user_history = "%s/history.stack" % constants._data_path_ def __init__(self, parent, app): - self._app = app + qwrappers.WindowWrapper.__init__(self, parent, app) - self._errorDisplay = QErrorDisplay() - self._historyView = qhistory.QCalcHistory(self._errorDisplay) + self._historyView = qhistory.QCalcHistory(self._app.errorLog) self._userEntry = QValueEntry() self._userEntry.entry.returnPressed.connect(self._on_push) self._userEntryLayout = QtGui.QHBoxLayout() @@ -255,34 +137,14 @@ class MainWindow(object): self._controlLayout = QtGui.QVBoxLayout() self._controlLayout.setContentsMargins(0, 0, 0, 0) - self._controlLayout.addWidget(self._errorDisplay.toplevel, 0) self._controlLayout.addWidget(self._historyView.toplevel, 1000) self._controlLayout.addLayout(self._userEntryLayout, 0) self._keyboardTabs = QtGui.QTabWidget() - if maeqt.screen_orientation() == QtCore.Qt.Vertical: - defaultLayoutOrientation = QtGui.QBoxLayout.TopToBottom - self._keyboardTabs.setTabPosition(QtGui.QTabWidget.East) - else: - defaultLayoutOrientation = QtGui.QBoxLayout.LeftToRight - self._keyboardTabs.setTabPosition(QtGui.QTabWidget.North) - self._layout = QtGui.QBoxLayout(defaultLayoutOrientation) - self._layout.setContentsMargins(0, 0, 0, 0) self._layout.addLayout(self._controlLayout) self._layout.addWidget(self._keyboardTabs) - 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._window.destroyed.connect(self._on_window_closed) - self._copyItemAction = QtGui.QAction(None) self._copyItemAction.setText("Copy") self._copyItemAction.setShortcut(QtGui.QKeySequence("CTRL+c")) @@ -300,11 +162,6 @@ class MainWindow(object): self._window.addAction(self._copyItemAction) self._window.addAction(self._pasteItemAction) - self._window.addAction(self._closeWindowAction) - self._window.addAction(self._app.quitAction) - self._window.addAction(self._app.fullscreenAction) - - self._window.addAction(self._app.logAction) self._constantPlugins = plugin_utils.ConstantPluginManager() self._constantPlugins.add_path(*self._plugin_search_paths) @@ -356,39 +213,21 @@ class MainWindow(object): self.enable_plugin(self._keyboardPlugins.lookup_plugin("Alphabet")) self.set_fullscreen(self._app.fullscreenAction.isChecked()) - - self._window.show() - - @property - def window(self): - return self._window + self.set_orientation(self._app.orientationAction.isChecked()) 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() + def set_orientation(self, isPortrait): + qwrappers.WindowWrapper.set_orientation(self, isPortrait) + if isPortrait: + defaultLayoutOrientation = QtGui.QBoxLayout.TopToBottom + tabPosition = QtGui.QTabWidget.South else: - self._window.showNormal() - for child in self.walk_children(): - child.set_fullscreen(isFullscreen) + defaultLayoutOrientation = QtGui.QBoxLayout.LeftToRight + tabPosition = QtGui.QTabWidget.North + self._layout.setDirection(defaultLayoutOrientation) + self._keyboardTabs.setTabPosition(tabPosition) def enable_plugin(self, pluginId): self._keyboardPlugins.enable_plugin(pluginId) @@ -415,6 +254,10 @@ class MainWindow(object): else: self._keyboardTabs.addTab(pluginKeyboard.toplevel, icon, "") + def close(self): + qwrappers.WindowWrapper.close(self) + self._save_history() + def _load_history(self): serialized = [] try: @@ -436,6 +279,10 @@ class MainWindow(object): f.write("%s\n" % line) @misc_utils.log_exception(_moduleLogger) + def _on_child_close(self, something = None): + self._child = None + + @misc_utils.log_exception(_moduleLogger) def _on_copy(self, *args): eqNode = self._historyView.peek() resultNode = eqNode.simplify() @@ -472,14 +319,6 @@ class MainWindow(object): def _on_clear_all(self, *args): self._history.clear() - @misc_utils.log_exception(_moduleLogger) - def _on_window_closed(self, checked = True): - self._save_history() - - @misc_utils.log_exception(_moduleLogger) - def _on_close_window(self, checked = True): - self.close() - def run(): app = QtGui.QApplication([]) diff --git a/src/maeqt.py b/src/maeqt.py deleted file mode 100644 index d5eb18b..0000000 --- a/src/maeqt.py +++ /dev/null @@ -1,122 +0,0 @@ -from PyQt4 import QtCore -from PyQt4 import QtGui - - -def _null_set_stackable(window, isStackable): - pass - - -def _maemo_set_stackable(window, isStackable): - window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable) - - -try: - QtCore.Qt.WA_Maemo5StackedWindow - set_stackable = _maemo_set_stackable -except AttributeError: - set_stackable = _null_set_stackable - - -def _null_set_autorient(window, isStackable): - pass - - -def _maemo_set_autorient(window, isStackable): - window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable) - - -try: - QtCore.Qt.WA_Maemo5AutoOrientation - set_autorient = _maemo_set_autorient -except AttributeError: - set_autorient = _null_set_autorient - - -def _null_set_landscape(window, isStackable): - pass - - -def _maemo_set_landscape(window, isStackable): - window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable) - - -try: - QtCore.Qt.WA_Maemo5LandscapeOrientation - set_landscape = _maemo_set_landscape -except AttributeError: - set_landscape = _null_set_landscape - - -def _null_set_portrait(window, isStackable): - pass - - -def _maemo_set_portrait(window, isStackable): - window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable) - - -try: - QtCore.Qt.WA_Maemo5PortraitOrientation - set_portrait = _maemo_set_portrait -except AttributeError: - set_portrait = _null_set_portrait - - -def _null_show_progress_indicator(window, isStackable): - pass - - -def _maemo_show_progress_indicator(window, isStackable): - window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable) - - -try: - QtCore.Qt.WA_Maemo5ShowProgressIndicator - show_progress_indicator = _maemo_show_progress_indicator -except AttributeError: - show_progress_indicator = _null_show_progress_indicator - - -def _null_mark_numbers_preferred(widget): - pass - - -def _newqt_mark_numbers_preferred(widget): - widget.setInputMethodHints(QtCore.Qt.ImhPreferNumbers) - - -try: - QtCore.Qt.ImhPreferNumbers - mark_numbers_preferred = _newqt_mark_numbers_preferred -except AttributeError: - mark_numbers_preferred = _null_mark_numbers_preferred - - -def screen_orientation(): - geom = QtGui.QApplication.desktop().screenGeometry() - if geom.width() <= geom.height(): - return QtCore.Qt.Vertical - else: - return QtCore.Qt.Horizontal - - -def _null_get_theme_icon(iconNames, fallback = None): - icon = fallback if fallback is not None else QtGui.QIcon() - return icon - - -def _newqt_get_theme_icon(iconNames, fallback = None): - for iconName in iconNames: - if QtGui.QIcon.hasThemeIcon(iconName): - icon = QtGui.QIcon.fromTheme(iconName) - break - else: - icon = fallback if fallback is not None else QtGui.QIcon() - return icon - - -try: - QtGui.QIcon.fromTheme - get_theme_icon = _newqt_get_theme_icon -except AttributeError: - get_theme_icon = _null_get_theme_icon -- 1.7.9.5