Switching the account dialog to a window
[gc-dialer] / src / dialogs.py
index 71002ce..3258931 100644 (file)
@@ -7,8 +7,9 @@ import functools
 import copy
 import logging
 
-from PyQt4 import QtGui
-from PyQt4 import QtCore
+import util.qt_compat as qt_compat
+QtCore = qt_compat.QtCore
+QtGui = qt_compat.import_module("QtGui")
 
 import constants
 from util import qwrappers
@@ -80,8 +81,8 @@ class CredentialsDialog(object):
                except RuntimeError:
                        _moduleLogger.exception("Oh well")
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_close_window(self, checked = True):
                with qui_utils.notify_error(self._app.errorLog):
@@ -141,15 +142,15 @@ class AboutDialog(object):
                except RuntimeError:
                        _moduleLogger.exception("Oh well")
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_close_window(self, checked = True):
                with qui_utils.notify_error(self._app.errorLog):
                        self._dialog.reject()
 
 
-class AccountDialog(object):
+class AccountDialog(QtCore.QObject, qwrappers.WindowWrapper):
 
        # @bug Can't enter custom callback numbers
 
@@ -177,7 +178,11 @@ class AccountDialog(object):
        VOICEMAIL_CHECK_DISABLED = "Disabled"
        VOICEMAIL_CHECK_ENABLED = "Enabled"
 
-       def __init__(self, app):
+       settingsApproved = qt_compat.Signal()
+
+       def __init__(self, parent, app, errorLog):
+               QtCore.QObject.__init__(self)
+               qwrappers.WindowWrapper.__init__(self, parent, app)
                self._app = app
                self._doClear = False
 
@@ -198,6 +203,14 @@ class AccountDialog(object):
                self._callbackSelector = QtGui.QComboBox()
                #self._callbackSelector.setEditable(True)
                self._callbackSelector.setInsertPolicy(QtGui.QComboBox.InsertAtTop)
+               self._orientationSelector = QtGui.QComboBox()
+               for orientationMode in [
+                       self._app.DEFAULT_ORIENTATION,
+                       self._app.AUTO_ORIENTATION,
+                       self._app.LANDSCAPE_ORIENTATION,
+                       self._app.PORTRAIT_ORIENTATION,
+               ]:
+                       self._orientationSelector.addItem(orientationMode)
 
                self._update_notification_state()
 
@@ -216,9 +229,12 @@ class AccountDialog(object):
                self._credLayout.addWidget(self._smsNotificationButton, 5, 1)
                self._credLayout.addWidget(QtGui.QLabel("Other"), 6, 0)
                self._credLayout.addWidget(self._voicemailOnMissedButton, 6, 1)
+               self._credLayout.addWidget(QtGui.QLabel("Orientation"), 7, 0)
+               self._credLayout.addWidget(self._orientationSelector, 7, 1)
+               self._credLayout.addWidget(QtGui.QLabel(""), 8, 0)
+               self._credLayout.addWidget(QtGui.QLabel(""), 9, 0)
+               self._credLayout.addWidget(self._clearButton, 9, 1)
 
-               self._credLayout.addWidget(QtGui.QLabel(""), 7, 0)
-               self._credLayout.addWidget(self._clearButton, 7, 1)
                self._credWidget = QtGui.QWidget()
                self._credWidget.setLayout(self._credLayout)
                self._credWidget.setContentsMargins(0, 0, 0, 0)
@@ -228,28 +244,20 @@ class AccountDialog(object):
                self._scrollSettings.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
                self._scrollSettings.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
 
-               self._loginButton = QtGui.QPushButton("&Apply")
-               self._buttonLayout = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel)
-               self._buttonLayout.addButton(self._loginButton, QtGui.QDialogButtonBox.AcceptRole)
+               self._applyButton = QtGui.QPushButton("&Apply")
+               self._applyButton.clicked.connect(self._on_settings_apply)
+               self._cancelButton = QtGui.QPushButton("&Cancel")
+               self._cancelButton.clicked.connect(self._on_settings_cancel)
+               self._buttonLayout = QtGui.QDialogButtonBox()
+               self._buttonLayout.addButton(self._applyButton, QtGui.QDialogButtonBox.AcceptRole)
+               self._buttonLayout.addButton(self._cancelButton, QtGui.QDialogButtonBox.RejectRole)
 
-               self._layout = QtGui.QVBoxLayout()
                self._layout.addWidget(self._scrollSettings)
                self._layout.addWidget(self._buttonLayout)
+               self._layout.setDirection(QtGui.QBoxLayout.TopToBottom)
 
-               self._dialog = QtGui.QDialog()
-               self._dialog.setWindowTitle("Account")
-               self._dialog.setLayout(self._layout)
-               self._buttonLayout.accepted.connect(self._dialog.accept)
-               self._buttonLayout.rejected.connect(self._dialog.reject)
-
-               self._closeWindowAction = QtGui.QAction(None)
-               self._closeWindowAction.setText("Close")
-               self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
-               self._closeWindowAction.triggered.connect(self._on_close_window)
-
-               self._dialog.addAction(self._closeWindowAction)
-               self._dialog.addAction(app.quitAction)
-               self._dialog.addAction(app.fullscreenAction)
+               self._window.setWindowTitle("Account")
+               self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, False)
 
        @property
        def doClear(self):
@@ -274,6 +282,11 @@ class AccountDialog(object):
        def set_account_number(self, num):
                self._accountNumberLabel.setText(num)
 
+       orientation = property(
+               lambda self: str(self._orientationSelector.currentText()),
+               lambda self, mode: qui_utils.set_current_index(self._orientationSelector, mode),
+       )
+
        def _set_voicemail_on_missed(self, status):
                if status == self.VOICEMAIL_CHECK_NOT_SUPPORTED:
                        self._voicemailOnMissedButton.setChecked(False)
@@ -297,17 +310,9 @@ class AccountDialog(object):
 
        updateVMOnMissedCall = property(_get_voicemail_on_missed, _set_voicemail_on_missed)
 
-       def _set_notifications(self, enabled):
-               for i in xrange(self._notificationSelecter.count()):
-                       if self._notificationSelecter.itemText(i) == enabled:
-                               self._notificationSelecter.setCurrentIndex(i)
-                               break
-               else:
-                       self._notificationSelecter.setCurrentIndex(0)
-
        notifications = property(
                lambda self: str(self._notificationSelecter.currentText()),
-               _set_notifications,
+               lambda self, enabled: qui_utils.set_current_index(self._notificationSelecter, enabled),
        )
 
        notifyOnMissed = property(
@@ -343,7 +348,7 @@ class AccountDialog(object):
        @property
        def selectedCallback(self):
                index = self._callbackSelector.currentIndex()
-               data = str(self._callbackSelector.itemData(index).toPyObject())
+               data = str(self._callbackSelector.itemData(index))
                return data
 
        def set_callbacks(self, choices, default):
@@ -362,16 +367,13 @@ class AccountDialog(object):
                        if uglyNumber == uglyDefault:
                                self._callbackSelector.setCurrentIndex(self._callbackSelector.count() - 1)
 
-       def run(self, parent=None):
+       def run(self):
                self._doClear = False
-               self._dialog.setParent(parent, QtCore.Qt.Dialog)
-
-               response = self._dialog.exec_()
-               return response
+               self._window.show()
 
        def close(self):
                try:
-                       self._dialog.reject()
+                       self._window.hide()
                except RuntimeError:
                        _moduleLogger.exception("Oh well")
 
@@ -402,26 +404,35 @@ class AccountDialog(object):
                        self._voicemailNotificationButton.setChecked(False)
                        self._smsNotificationButton.setChecked(False)
 
-       @QtCore.pyqtSlot(int)
+       @qt_compat.Slot(int)
        @misc_utils.log_exception(_moduleLogger)
        def _on_notification_change(self, index):
                with qui_utils.notify_error(self._app.errorLog):
                        self._update_notification_state()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
-       def _on_clear(self, checked = False):
+       def _on_settings_cancel(self, checked = False):
                with qui_utils.notify_error(self._app.errorLog):
-                       self._doClear = True
-                       self._dialog.accept()
+                       self.hide()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
-       def _on_close_window(self, checked = True):
+       def _on_settings_apply(self, checked = False):
                with qui_utils.notify_error(self._app.errorLog):
-                       self._dialog.reject()
+                       self.settingsApproved.emit()
+                       self.hide()
+
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_clear(self, checked = False):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._doClear = True
+                       self.settingsApproved.emit()
+                       self.hide()
 
 
 class ContactList(object):
@@ -481,7 +492,7 @@ class ContactList(object):
                        )
                        callback.__name__ = "thanks partials for not having names and pyqt for requiring them"
                        numberSelector.activated.connect(
-                               QtCore.pyqtSlot(int)(callback)
+                               qt_compat.Slot(int)(callback)
                        )
 
                        if self._closeIcon is self._SENTINEL_ICON:
@@ -767,13 +778,13 @@ class VoicemailPlayer(object):
                        self._session.download_voicemail(messageId)
                        self._hide()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_recipients_changed(self):
                with qui_utils.notify_error(self._app.errorLog):
                        self._update_state()
 
-       @QtCore.pyqtSlot(str, str)
+       @qt_compat.Slot(str, str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_voicemail_downloaded(self, messageId, filepath):
                with qui_utils.notify_error(self._app.errorLog):
@@ -801,9 +812,8 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                self._session.draft.called.connect(self._on_op_finished)
                self._session.draft.cancelled.connect(self._on_op_finished)
                self._session.draft.error.connect(self._on_op_error)
-               self._errorLog = errorLog
 
-               self._errorDisplay = qui_utils.ErrorDisplay(self._errorLog)
+               self._errorLog = errorLog
 
                self._targetList = ContactList(self._app, self._session)
                self._history = QtGui.QLabel()
@@ -857,6 +867,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                self._window.setWindowTitle("Contact")
                self._window.closed.connect(self._on_close_window)
                self._window.hidden.connect(self._on_close_window)
+               self._window.resized.connect(self._on_window_resized)
 
                self._scrollTimer = QtCore.QTimer()
                self._scrollTimer.setInterval(100)
@@ -867,7 +878,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                self._update_letter_count()
                self._update_target_fields()
                self.set_fullscreen(self._app.fullscreenAction.isChecked())
-               self.set_orientation(self._app.orientationAction.isChecked())
+               self.update_orientation(self._app.orientation)
 
        def close(self):
                if self._window is None:
@@ -906,12 +917,12 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                except RuntimeError:
                        _moduleLogger.exception("Oh well")
 
-       def set_orientation(self, isPortrait):
-               qwrappers.WindowWrapper.set_orientation(self, isPortrait)
+       def update_orientation(self, orientation):
+               qwrappers.WindowWrapper.update_orientation(self, orientation)
                self._scroll_to_bottom()
 
        def _update_letter_count(self):
-               count = self._smsEntry.toPlainText().size()
+               count = len(self._smsEntry.toPlainText())
                numTexts, numCharInText = divmod(count, self.MAX_CHAR)
                numTexts += 1
                numCharsLeftInText = self.MAX_CHAR - numCharInText
@@ -923,7 +934,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        self._dialButton.setEnabled(False)
                        self._smsButton.setEnabled(False)
                elif self._session.draft.get_num_contacts() == 1:
-                       count = self._smsEntry.toPlainText().size()
+                       count = len(self._smsEntry.toPlainText())
                        if count == 0:
                                self._dialButton.setEnabled(True)
                                self._smsButton.setEnabled(False)
@@ -932,7 +943,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                                self._smsButton.setEnabled(True)
                else:
                        self._dialButton.setEnabled(False)
-                       count = self._smsEntry.toPlainText().size()
+                       count = len(self._smsEntry.toPlainText())
                        if count == 0:
                                self._smsButton.setEnabled(False)
                        else:
@@ -1031,7 +1042,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        self._session.draft.message = message
                        self._session.draft.call()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_cancel_clicked(self, message):
                with qui_utils.notify_error(self._app.errorLog):
@@ -1050,7 +1061,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        number = numbers[index][0]
                        self._session.draft.set_selected_number(cid, number)
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_refresh_history(self):
                with qui_utils.notify_error(self._app.errorLog):
@@ -1061,14 +1072,14 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        (cid, ) = self._session.draft.get_contacts()
                        self._update_history(cid)
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_recipients_changed(self):
                with qui_utils.notify_error(self._app.errorLog):
                        self._update_target_fields()
                        self._update_button_state()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_op_started(self):
                with qui_utils.notify_error(self._app.errorLog):
@@ -1077,13 +1088,13 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        self._dialButton.setVisible(False)
                        self.show()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_calling_started(self):
                with qui_utils.notify_error(self._app.errorLog):
                        self._cancelButton.setVisible(True)
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_op_finished(self):
                with qui_utils.notify_error(self._app.errorLog):
@@ -1095,7 +1106,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        self.close()
                        self.destroy()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_op_error(self, message):
                with qui_utils.notify_error(self._app.errorLog):
@@ -1106,15 +1117,21 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
 
                        self._errorLog.push_error(message)
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_letter_count_changed(self):
                with qui_utils.notify_error(self._app.errorLog):
                        self._update_letter_count()
                        self._update_button_state()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_window_resized(self):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._scroll_to_bottom()
+
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_close_window(self, checked = True):
                with qui_utils.notify_error(self._app.errorLog):