4 from PyQt4 import QtCore
5 from PyQt4 import QtGui
10 _moduleLogger = logging.getLogger(__name__)
13 class QErrorLog(QtCore.QObject):
15 messagePushed = QtCore.pyqtSignal()
16 messagePopped = QtCore.pyqtSignal()
19 QtCore.QObject.__init__(self)
22 def push_message(self, message):
23 self._messages.append(message)
24 self.messagePushed.emit()
26 def push_exception(self):
27 userMessage = str(sys.exc_info()[1])
28 _moduleLogger.exception(userMessage)
29 self.push_message(userMessage)
31 def pop_message(self):
33 self.messagePopped.emit()
35 def peek_message(self):
36 return self._messages[0]
39 return len(self._messages)
42 class ErrorDisplay(object):
44 def __init__(self, errorLog):
45 self._errorLog = errorLog
46 self._errorLog.messagePushed.connect(self._on_message_pushed)
47 self._errorLog.messagePopped.connect(self._on_message_popped)
49 errorIcon = get_theme_icon(("dialog-error", "app_install_error", "gtk-dialog-error"))
50 self._severityIcon = errorIcon.pixmap(32, 32)
51 self._severityLabel = QtGui.QLabel()
52 self._severityLabel.setPixmap(self._severityIcon)
54 self._message = QtGui.QLabel()
55 self._message.setText("Boo")
57 closeIcon = get_theme_icon(("window-close", "general_close", "gtk-close"))
58 self._closeLabel = QtGui.QPushButton(closeIcon, "")
59 self._closeLabel.clicked.connect(self._on_close)
61 self._controlLayout = QtGui.QHBoxLayout()
62 self._controlLayout.addWidget(self._severityLabel)
63 self._controlLayout.addWidget(self._message)
64 self._controlLayout.addWidget(self._closeLabel)
66 self._topLevelLayout = QtGui.QHBoxLayout()
67 self._topLevelLayout.addLayout(self._controlLayout)
68 self._widget = QtGui.QWidget()
69 self._widget.setLayout(self._topLevelLayout)
77 @QtCore.pyqtSlot(bool)
78 @misc.log_exception(_moduleLogger)
79 def _on_close(self, checked = False):
80 self._errorLog.pop_message()
83 @misc.log_exception(_moduleLogger)
84 def _on_message_pushed(self):
85 if 1 <= len(self._errorLog) and self._widget.isHidden():
86 self._message.setText(self._errorLog.peek_message())
90 @misc.log_exception(_moduleLogger)
91 def _on_message_popped(self):
92 if len(self._errorLog) == 0:
93 self._message.setText("")
96 self._message.setText(self._errorLog.peek_message())
99 class QHtmlDelegate(QtGui.QStyledItemDelegate):
101 def paint(self, painter, option, index):
102 newOption = QtGui.QStyleOptionViewItemV4(option)
103 self.initStyleOption(newOption, index)
105 doc = QtGui.QTextDocument()
106 doc.setHtml(newOption.text)
107 doc.setTextWidth(newOption.rect.width())
109 if newOption.widget is not None:
110 style = newOption.widget.style()
112 style = QtGui.QApplication.style()
115 style.drawControl(QtGui.QStyle.CE_ItemViewItem, newOption, painter)
117 ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
118 if newOption.state & QtGui.QStyle.State_Selected:
119 ctx.palette.setColor(
121 newOption.palette.color(
122 QtGui.QPalette.Active,
123 QtGui.QPalette.HighlightedText
127 textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, newOption)
129 painter.translate(textRect.topLeft())
130 painter.setClipRect(textRect.translated(-textRect.topLeft()))
131 doc.documentLayout().draw(painter, ctx)
134 def sizeHint(self, option, index):
135 newOption = QtGui.QStyleOptionViewItemV4(option)
136 self.initStyleOption(newOption, index)
138 doc = QtGui.QTextDocument()
139 doc.setHtml(newOption.text)
140 doc.setTextWidth(newOption.rect.width())
141 size = QtCore.QSize(doc.idealWidth(), doc.size().height())
145 def _null_set_stackable(window, isStackable):
149 def _maemo_set_stackable(window, isStackable):
150 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
154 QtCore.Qt.WA_Maemo5StackedWindow
155 set_stackable = _maemo_set_stackable
156 except AttributeError:
157 set_stackable = _null_set_stackable
160 def _null_set_autorient(window, isStackable):
164 def _maemo_set_autorient(window, isStackable):
165 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
169 QtCore.Qt.WA_Maemo5AutoOrientation
170 set_autorient = _maemo_set_autorient
171 except AttributeError:
172 set_autorient = _null_set_autorient
175 def _null_set_landscape(window, isStackable):
179 def _maemo_set_landscape(window, isStackable):
180 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
184 QtCore.Qt.WA_Maemo5LandscapeOrientation
185 set_landscape = _maemo_set_landscape
186 except AttributeError:
187 set_landscape = _null_set_landscape
190 def _null_set_portrait(window, isStackable):
194 def _maemo_set_portrait(window, isStackable):
195 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
199 QtCore.Qt.WA_Maemo5PortraitOrientation
200 set_portrait = _maemo_set_portrait
201 except AttributeError:
202 set_portrait = _null_set_portrait
205 def _null_show_progress_indicator(window, isStackable):
209 def _maemo_show_progress_indicator(window, isStackable):
210 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
214 QtCore.Qt.WA_Maemo5ShowProgressIndicator
215 show_progress_indicator = _maemo_show_progress_indicator
216 except AttributeError:
217 show_progress_indicator = _null_show_progress_indicator
220 def _null_mark_numbers_preferred(widget):
224 def _newqt_mark_numbers_preferred(widget):
225 widget.setInputMethodHints(QtCore.Qt.ImhPreferNumbers)
229 QtCore.Qt.ImhPreferNumbers
230 mark_numbers_preferred = _newqt_mark_numbers_preferred
231 except AttributeError:
232 mark_numbers_preferred = _null_mark_numbers_preferred
235 def screen_orientation():
236 geom = QtGui.QApplication.desktop().screenGeometry()
237 if geom.width() <= geom.height():
238 return QtCore.Qt.Vertical
240 return QtCore.Qt.Horizontal
243 def _null_get_theme_icon(iconNames, fallback = None):
244 icon = fallback if fallback is not None else QtGui.QIcon()
248 def _newqt_get_theme_icon(iconNames, fallback = None):
249 for iconName in iconNames:
250 if QtGui.QIcon.hasThemeIcon(iconName):
251 icon = QtGui.QIcon.fromTheme(iconName)
254 icon = fallback if fallback is not None else QtGui.QIcon()
259 QtGui.QIcon.fromTheme
260 get_theme_icon = _newqt_get_theme_icon
261 except AttributeError:
262 get_theme_icon = _null_get_theme_icon