diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 84a26cea18..a8f80ab35a 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -8,12 +8,12 @@ from urllib import unquote from PyQt4.Qt import QVariant, QFileInfo, QObject, SIGNAL, QBuffer, Qt, \ QByteArray, QTranslator, QCoreApplication, QThread, \ QEvent, QTimer, pyqtSignal, QDate, QDesktopServices, \ - QFileDialog, QMessageBox, QPixmap, QFileIconProvider, \ - QIcon, QApplication, QDialog, QPushButton, QUrl, QFont + QFileDialog, QFileIconProvider, \ + QIcon, QApplication, QDialog, QUrl, QFont ORG_NAME = 'KovidsBrain' APP_UID = 'libprs500' -from calibre.constants import islinux, iswindows, isosx, isfreebsd, isfrozen +from calibre.constants import islinux, iswindows, isfreebsd, isfrozen from calibre.utils.config import Config, ConfigProxy, dynamic, JSONConfig from calibre.utils.localization import set_qt_translator from calibre.ebooks.metadata.meta import get_metadata, metadata_from_formats @@ -178,104 +178,40 @@ def is_widescreen(): def extension(path): return os.path.splitext(path)[1][1:].lower() -class CopyButton(QPushButton): - - ACTION_KEYS = [Qt.Key_Enter, Qt.Key_Return, Qt.Key_Space] - - def copied(self): - self.emit(SIGNAL('copy()')) - self.setDisabled(True) - self.setText(_('Copied')) - - - def keyPressEvent(self, ev): - try: - if ev.key() in self.ACTION_KEYS: - self.copied() - return - except: - pass - QPushButton.keyPressEvent(self, ev) - - - def keyReleaseEvent(self, ev): - try: - if ev.key() in self.ACTION_KEYS: - return - except: - pass - QPushButton.keyReleaseEvent(self, ev) - - def mouseReleaseEvent(self, ev): - ev.accept() - self.copied() - -class MessageBox(QMessageBox): - - def __init__(self, type_, title, msg, buttons, parent, det_msg=''): - QMessageBox.__init__(self, type_, title, msg, buttons, parent) - self.title = title - self.msg = msg - self.det_msg = det_msg - self.setDetailedText(det_msg) - # Cannot set keyboard shortcut as the event is not easy to filter - self.cb = CopyButton(_('Copy') if isosx else _('Copy to Clipboard')) - self.connect(self.cb, SIGNAL('copy()'), self.copy_to_clipboard) - self.addButton(self.cb, QMessageBox.ActionRole) - default_button = self.button(self.Ok) - if default_button is None: - default_button = self.button(self.Yes) - if default_button is not None: - self.setDefaultButton(default_button) - - def copy_to_clipboard(self): - QApplication.clipboard().setText('%s: %s\n\n%s' % - (self.title, self.msg, self.det_msg)) - - def warning_dialog(parent, title, msg, det_msg='', show=False, show_copy_button=True): - d = MessageBox(QMessageBox.Warning, 'WARNING: '+title, msg, QMessageBox.Ok, - parent, det_msg) - d.setEscapeButton(QMessageBox.Ok) - d.setIconPixmap(QPixmap(I('dialog_warning.png'))) - if not show_copy_button: - d.cb.setVisible(False) + from calibre.gui2.dialogs.message_box import MessageBox + d = MessageBox(MessageBox.WARNING, 'WARNING: '+title, msg, det_msg, parent=parent, + show_copy_button=show_copy_button) if show: return d.exec_() return d def error_dialog(parent, title, msg, det_msg='', show=False, show_copy_button=True): - d = MessageBox(QMessageBox.Critical, 'ERROR: '+title, msg, QMessageBox.Ok, - parent, det_msg) - d.setIconPixmap(QPixmap(I('dialog_error.png'))) - d.setEscapeButton(QMessageBox.Ok) - if not show_copy_button: - d.cb.setVisible(False) + from calibre.gui2.dialogs.message_box import MessageBox + d = MessageBox(MessageBox.ERROR, 'ERROR: '+title, msg, det_msg, parent=parent, + show_copy_button=show_copy_button) if show: return d.exec_() return d -def question_dialog(parent, title, msg, det_msg='', show_copy_button=True, - buttons=QMessageBox.Yes|QMessageBox.No, yes_button=QMessageBox.Yes): - d = MessageBox(QMessageBox.Question, title, msg, buttons, - parent, det_msg) - d.setIconPixmap(QPixmap(I('dialog_question.png'))) - d.setEscapeButton(QMessageBox.No) - if not show_copy_button: - d.cb.setVisible(False) +def question_dialog(parent, title, msg, det_msg='', show_copy_button=False, + buttons=None, yes_button=None): + from calibre.gui2.dialogs.message_box import MessageBox + d = MessageBox(MessageBox.QUESTION, title, msg, det_msg, parent=parent, + show_copy_button=show_copy_button) + if buttons is not None: + d.bb.setStandardButtons(buttons) - return d.exec_() == yes_button + return d.exec_() == d.Accepted def info_dialog(parent, title, msg, det_msg='', show=False, show_copy_button=True): - d = MessageBox(QMessageBox.Information, title, msg, QMessageBox.Ok, - parent, det_msg) - d.setIconPixmap(QPixmap(I('dialog_information.png'))) - if not show_copy_button: - d.cb.setVisible(False) + from calibre.gui2.dialogs.message_box import MessageBox + d = MessageBox(MessageBox.INFO, title, msg, det_msg, parent=parent, + show_copy_button=show_copy_button) if show: return d.exec_() diff --git a/src/calibre/gui2/dialogs/message_box.py b/src/calibre/gui2/dialogs/message_box.py new file mode 100644 index 0000000000..123476b734 --- /dev/null +++ b/src/calibre/gui2/dialogs/message_box.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai + +__license__ = 'GPL v3' +__copyright__ = '2011, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + + +from PyQt4.Qt import QDialog, QIcon, QApplication, QSize, QKeySequence, \ + QAction, Qt + +from calibre.constants import __version__ +from calibre.gui2.dialogs.message_box_ui import Ui_Dialog + +class MessageBox(QDialog, Ui_Dialog): + + ERROR = 0 + WARNING = 1 + INFO = 2 + QUESTION = 3 + + def __init__(self, type_, title, msg, det_msg='', show_copy_button=True, + parent=None): + QDialog.__init__(self, parent) + icon = { + self.ERROR : 'error', + self.WARNING: 'warning', + self.INFO: 'information', + self.QUESTION: 'question', + }[type_] + icon = 'dialog_%s.png'%icon + self.icon = QIcon(I(icon)) + self.setupUi(self) + + self.setWindowTitle(title) + self.setWindowIcon(self.icon) + self.icon_label.setPixmap(self.icon.pixmap(128, 128)) + self.msg.setText(msg) + self.det_msg.setPlainText(det_msg) + self.det_msg.setVisible(False) + + if det_msg: + self.show_det_msg = _('Show &details') + self.hide_det_msg = _('Hide &details') + self.det_msg_toggle = self.bb.addButton(self.show_det_msg, self.bb.ActionRole) + self.det_msg_toggle.clicked.connect(self.toggle_det_msg) + self.det_msg_toggle.setToolTip( + _('Show detailed information about this error')) + + if show_copy_button: + self.ctc_button = self.bb.addButton(_('&Copy to clipboard'), + self.bb.ActionRole) + self.ctc_button.clicked.connect(self.copy_to_clipboard) + + + self.copy_action = QAction(self) + self.addAction(self.copy_action) + self.copy_action.setShortcuts(QKeySequence.Copy) + self.copy_action.triggered.connect(self.copy_to_clipboard) + + self.is_question = type_ == self.QUESTION + if self.is_question: + self.bb.setStandardButtons(self.bb.Yes|self.bb.No) + self.bb.button(self.bb.Yes).setDefault(True) + else: + self.bb.button(self.bb.Ok).setDefault(True) + + self.do_resize() + + def toggle_det_msg(self, *args): + vis = self.det_msg.isVisible() + self.det_msg_toggle.setText(self.show_det_msg if vis else + self.hide_det_msg) + self.det_msg.setVisible(not vis) + self.do_resize() + + def do_resize(self): + sz = self.sizeHint() + QSize(100, 0) + sz.setWidth(min(500, sz.width())) + sz.setHeight(min(500, sz.height())) + self.resize(sz) + + def copy_to_clipboard(self, *args): + QApplication.clipboard().setText( + 'calibre, version %s\n%s: %s\n\n%s' % + (__version__, unicode(self.windowTitle()), + unicode(self.msg.text()), + unicode(self.det_msg.toPlainText()))) + self.ctc_button.setText(_('Copied')) + + def showEvent(self, ev): + ret = QDialog.showEvent(self, ev) + if self.is_question: + self.bb.button(self.bb.Yes).setFocus(Qt.OtherFocusReason) + else: + self.bb.button(self.bb.Ok).setFocus(Qt.OtherFocusReason) + return ret + +if __name__ == '__main__': + app = QApplication([]) + from calibre.gui2 import question_dialog + print question_dialog(None, 'title', 'msg goog ', + det_msg='det '*1000, + show_copy_button=True) diff --git a/src/calibre/gui2/dialogs/message_box.ui b/src/calibre/gui2/dialogs/message_box.ui new file mode 100644 index 0000000000..136e6d250e --- /dev/null +++ b/src/calibre/gui2/dialogs/message_box.ui @@ -0,0 +1,105 @@ + + + Dialog + + + + 0 + 0 + 497 + 235 + + + + Dialog + + + + + + + 68 + 68 + + + + + + + :/images/dialog_warning.png + + + true + + + + + + + + + + true + + + true + + + + + + + true + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Ok + + + + + + + + + + + bb + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + bb + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +