6 from PyQt4 import QtCore
7 from PyQt4 import QtGui
12 _moduleLogger = logging.getLogger(__name__)
15 @contextlib.contextmanager
16 def notify_error(log):
23 @contextlib.contextmanager
24 def notify_busy(log, message):
25 log.push_busy(message)
32 class ErrorMessage(object):
38 def __init__(self, message, level):
39 self._message = message
41 self._time = datetime.datetime.now()
52 return "%s.%s(%r, %r)" % (__name__, self.__class__.__name__, self._message, self._level)
55 class QErrorLog(QtCore.QObject):
57 messagePushed = QtCore.pyqtSignal()
58 messagePopped = QtCore.pyqtSignal()
61 QtCore.QObject.__init__(self)
64 def push_busy(self, message):
65 _moduleLogger.info("Entering state: %s" % message)
66 self._push_message(message, ErrorMessage.LEVEL_BUSY)
68 def push_message(self, message):
69 self._push_message(message, ErrorMessage.LEVEL_INFO)
71 def push_error(self, message):
72 self._push_message(message, ErrorMessage.LEVEL_ERROR)
74 def push_exception(self):
75 userMessage = str(sys.exc_info()[1])
76 _moduleLogger.exception(userMessage)
77 self.push_error(userMessage)
79 def pop(self, message = None):
83 _moduleLogger.info("Exiting state: %s" % message)
86 for (i, error) in enumerate(self._messages)
87 if error.message == message
89 # Might be removed out of order
91 del self._messages[messageIndex[0]]
92 self.messagePopped.emit()
94 def peek_message(self):
95 return self._messages[0]
97 def _push_message(self, message, level):
98 self._messages.append(ErrorMessage(message, level))
99 # Sort is defined as stable, so this should be fine
100 self._messages.sort(key=lambda x: x.level)
101 self.messagePushed.emit()
104 return len(self._messages)
107 class ErrorDisplay(object):
109 _SENTINEL_ICON = QtGui.QIcon()
111 def __init__(self, errorLog):
112 self._errorLog = errorLog
113 self._errorLog.messagePushed.connect(self._on_message_pushed)
114 self._errorLog.messagePopped.connect(self._on_message_popped)
117 ErrorMessage.LEVEL_BUSY:
119 #("process-working", "view-refresh", "general_refresh", "gtk-refresh")
120 ("view-refresh", "general_refresh", "gtk-refresh", )
122 ErrorMessage.LEVEL_INFO:
124 ("dialog-information", "general_notes", "gtk-info")
126 ErrorMessage.LEVEL_ERROR:
128 ("dialog-error", "app_install_error", "gtk-dialog-error")
131 self._severityLabel = QtGui.QLabel()
132 self._severityLabel.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
134 self._message = QtGui.QLabel()
135 self._message.setText("Boo")
136 self._message.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
137 self._message.setWordWrap(True)
139 closeIcon = get_theme_icon(("window-close", "general_close", "gtk-close"), self._SENTINEL_ICON)
140 if closeIcon is not self._SENTINEL_ICON:
141 self._closeLabel = QtGui.QPushButton(closeIcon, "")
143 self._closeLabel = QtGui.QPushButton("X")
144 self._closeLabel.clicked.connect(self._on_close)
146 self._controlLayout = QtGui.QHBoxLayout()
147 self._controlLayout.addWidget(self._severityLabel, 1, QtCore.Qt.AlignCenter)
148 self._controlLayout.addWidget(self._message, 1000)
149 self._controlLayout.addWidget(self._closeLabel, 1, QtCore.Qt.AlignCenter)
151 self._topLevelLayout = QtGui.QHBoxLayout()
152 self._topLevelLayout.addLayout(self._controlLayout)
153 self._widget = QtGui.QWidget()
154 self._widget.setLayout(self._topLevelLayout)
161 def _show_error(self):
162 error = self._errorLog.peek_message()
163 self._message.setText(error.message)
164 self._severityLabel.setPixmap(self._icons[error.level])
168 @QtCore.pyqtSlot(bool)
169 @misc.log_exception(_moduleLogger)
170 def _on_close(self, checked = False):
174 @misc.log_exception(_moduleLogger)
175 def _on_message_pushed(self):
179 @misc.log_exception(_moduleLogger)
180 def _on_message_popped(self):
181 if len(self._errorLog) == 0:
182 self._message.setText("")
188 class QHtmlDelegate(QtGui.QStyledItemDelegate):
192 def __init__(self, *args, **kwd):
193 QtGui.QStyledItemDelegate.__init__(*((self, ) + args), **kwd)
194 self._width = self.UNDEFINED_SIZE
196 def paint(self, painter, option, index):
197 newOption = QtGui.QStyleOptionViewItemV4(option)
198 self.initStyleOption(newOption, index)
199 if newOption.widget is not None:
200 style = newOption.widget.style()
202 style = QtGui.QApplication.style()
204 doc = QtGui.QTextDocument()
205 doc.setHtml(newOption.text)
206 doc.setTextWidth(newOption.rect.width())
209 style.drawControl(QtGui.QStyle.CE_ItemViewItem, newOption, painter)
211 ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
212 if newOption.state & QtGui.QStyle.State_Selected:
213 ctx.palette.setColor(
215 newOption.palette.color(
216 QtGui.QPalette.Active,
217 QtGui.QPalette.HighlightedText
221 ctx.palette.setColor(
223 newOption.palette.color(
224 QtGui.QPalette.Active,
229 textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, newOption)
231 painter.translate(textRect.topLeft())
232 painter.setClipRect(textRect.translated(-textRect.topLeft()))
233 doc.documentLayout().draw(painter, ctx)
236 def setWidth(self, width):
237 # @bug we need to be emitting sizeHintChanged but it requires an index
240 def sizeHint(self, option, index):
241 newOption = QtGui.QStyleOptionViewItemV4(option)
242 self.initStyleOption(newOption, index)
244 doc = QtGui.QTextDocument()
245 doc.setHtml(newOption.text)
246 if self._width != self.UNDEFINED_SIZE:
249 width = newOption.rect.width()
250 doc.setTextWidth(width)
251 size = QtCore.QSize(doc.idealWidth(), doc.size().height())
255 def _null_set_stackable(window, isStackable):
259 def _maemo_set_stackable(window, isStackable):
260 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
264 QtCore.Qt.WA_Maemo5StackedWindow
265 set_stackable = _maemo_set_stackable
266 except AttributeError:
267 set_stackable = _null_set_stackable
270 def _null_set_autorient(window, isStackable):
274 def _maemo_set_autorient(window, isStackable):
275 window.setAttribute(QtCore.Qt.WA_Maemo5AutoOrientation, isStackable)
279 QtCore.Qt.WA_Maemo5AutoOrientation
280 set_autorient = _maemo_set_autorient
281 except AttributeError:
282 set_autorient = _null_set_autorient
285 def screen_orientation():
286 geom = QtGui.QApplication.desktop().screenGeometry()
287 if geom.width() <= geom.height():
288 return QtCore.Qt.Vertical
290 return QtCore.Qt.Horizontal
293 def _null_set_window_orientation(window, orientation):
297 def _maemo_set_window_orientation(window, orientation):
298 if orientation == QtCore.Qt.Vertical:
299 newHint = QtCore.Qt.WA_Maemo5LandscapeOrientation
300 oldHint = QtCore.Qt.WA_Maemo5PortraitOrientation
301 elif orientation == QtCore.Qt.Horizontal:
302 oldHint = QtCore.Qt.WA_Maemo5LandscapeOrientation
303 newHint = QtCore.Qt.WA_Maemo5PortraitOrientation
304 window.setAttribute(oldHint, False)
305 window.setAttribute(newHint, True)
309 QtCore.Qt.WA_Maemo5LandscapeOrientation
310 QtCore.Qt.WA_Maemo5PortraitOrientation
311 set_window_orientation = _maemo_set_window_orientation
312 except AttributeError:
313 set_window_orientation = _null_set_window_orientation
316 def _null_show_progress_indicator(window, isStackable):
320 def _maemo_show_progress_indicator(window, isStackable):
321 window.setAttribute(QtCore.Qt.WA_Maemo5ShowProgressIndicator, isStackable)
325 QtCore.Qt.WA_Maemo5ShowProgressIndicator
326 show_progress_indicator = _maemo_show_progress_indicator
327 except AttributeError:
328 show_progress_indicator = _null_show_progress_indicator
331 def _null_mark_numbers_preferred(widget):
335 def _newqt_mark_numbers_preferred(widget):
336 widget.setInputMethodHints(QtCore.Qt.ImhPreferNumbers)
340 QtCore.Qt.ImhPreferNumbers
341 mark_numbers_preferred = _newqt_mark_numbers_preferred
342 except AttributeError:
343 mark_numbers_preferred = _null_mark_numbers_preferred
346 def _null_get_theme_icon(iconNames, fallback = None):
347 icon = fallback if fallback is not None else QtGui.QIcon()
351 def _newqt_get_theme_icon(iconNames, fallback = None):
352 for iconName in iconNames:
353 if QtGui.QIcon.hasThemeIcon(iconName):
354 icon = QtGui.QIcon.fromTheme(iconName)
357 icon = fallback if fallback is not None else QtGui.QIcon()
362 QtGui.QIcon.fromTheme
363 get_theme_icon = _newqt_get_theme_icon
364 except AttributeError:
365 get_theme_icon = _null_get_theme_icon