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._widget = QtGui.QWidget()
152 self._widget.setLayout(self._controlLayout)
159 def _show_error(self):
160 error = self._errorLog.peek_message()
161 self._message.setText(error.message)
162 self._severityLabel.setPixmap(self._icons[error.level])
166 @QtCore.pyqtSlot(bool)
167 @misc.log_exception(_moduleLogger)
168 def _on_close(self, checked = False):
172 @misc.log_exception(_moduleLogger)
173 def _on_message_pushed(self):
177 @misc.log_exception(_moduleLogger)
178 def _on_message_popped(self):
179 if len(self._errorLog) == 0:
180 self._message.setText("")
186 class QHtmlDelegate(QtGui.QStyledItemDelegate):
190 def __init__(self, *args, **kwd):
191 QtGui.QStyledItemDelegate.__init__(*((self, ) + args), **kwd)
192 self._width = self.UNDEFINED_SIZE
194 def paint(self, painter, option, index):
195 newOption = QtGui.QStyleOptionViewItemV4(option)
196 self.initStyleOption(newOption, index)
197 if newOption.widget is not None:
198 style = newOption.widget.style()
200 style = QtGui.QApplication.style()
202 doc = QtGui.QTextDocument()
203 doc.setHtml(newOption.text)
204 doc.setTextWidth(newOption.rect.width())
207 style.drawControl(QtGui.QStyle.CE_ItemViewItem, newOption, painter)
209 ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
210 if newOption.state & QtGui.QStyle.State_Selected:
211 ctx.palette.setColor(
213 newOption.palette.color(
214 QtGui.QPalette.Active,
215 QtGui.QPalette.HighlightedText
219 ctx.palette.setColor(
221 newOption.palette.color(
222 QtGui.QPalette.Active,
227 textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, newOption)
229 painter.translate(textRect.topLeft())
230 painter.setClipRect(textRect.translated(-textRect.topLeft()))
231 doc.documentLayout().draw(painter, ctx)
234 def setWidth(self, width):
235 # @bug we need to be emitting sizeHintChanged but it requires an index
238 def sizeHint(self, option, index):
239 newOption = QtGui.QStyleOptionViewItemV4(option)
240 self.initStyleOption(newOption, index)
242 doc = QtGui.QTextDocument()
243 doc.setHtml(newOption.text)
244 if self._width != self.UNDEFINED_SIZE:
247 width = newOption.rect.width()
248 doc.setTextWidth(width)
249 size = QtCore.QSize(doc.idealWidth(), doc.size().height())
253 def _null_set_stackable(window, isStackable):
257 def _maemo_set_stackable(window, isStackable):
258 window.setAttribute(QtCore.Qt.WA_Maemo5StackedWindow, isStackable)
262 QtCore.Qt.WA_Maemo5StackedWindow
263 set_stackable = _maemo_set_stackable
264 except AttributeError:
265 set_stackable = _null_set_stackable
268 def _null_set_autorient(window, isStackable):
272 def _maemo_set_autorient(window, isStackable):
273 window.setAttribute(QtCore.Qt.WA_Maemo5AutoOrientation, isStackable)
277 QtCore.Qt.WA_Maemo5AutoOrientation
278 set_autorient = _maemo_set_autorient
279 except AttributeError:
280 set_autorient = _null_set_autorient
283 def screen_orientation():
284 geom = QtGui.QApplication.desktop().screenGeometry()
285 if geom.width() <= geom.height():
286 return QtCore.Qt.Vertical
288 return QtCore.Qt.Horizontal
291 def _null_set_window_orientation(window, orientation):
295 def _maemo_set_window_orientation(window, orientation):
296 if orientation == QtCore.Qt.Vertical:
297 oldHint = QtCore.Qt.WA_Maemo5LandscapeOrientation
298 newHint = QtCore.Qt.WA_Maemo5PortraitOrientation
299 elif orientation == QtCore.Qt.Horizontal:
300 oldHint = QtCore.Qt.WA_Maemo5PortraitOrientation
301 newHint = QtCore.Qt.WA_Maemo5LandscapeOrientation
302 window.setAttribute(oldHint, False)
303 window.setAttribute(newHint, True)
307 QtCore.Qt.WA_Maemo5LandscapeOrientation
308 QtCore.Qt.WA_Maemo5PortraitOrientation
309 set_window_orientation = _maemo_set_window_orientation
310 except AttributeError:
311 set_window_orientation = _null_set_window_orientation
314 def _null_show_progress_indicator(window, isStackable):
318 def _maemo_show_progress_indicator(window, isStackable):
319 window.setAttribute(QtCore.Qt.WA_Maemo5ShowProgressIndicator, isStackable)
323 QtCore.Qt.WA_Maemo5ShowProgressIndicator
324 show_progress_indicator = _maemo_show_progress_indicator
325 except AttributeError:
326 show_progress_indicator = _null_show_progress_indicator
329 def _null_mark_numbers_preferred(widget):
333 def _newqt_mark_numbers_preferred(widget):
334 widget.setInputMethodHints(QtCore.Qt.ImhPreferNumbers)
338 QtCore.Qt.ImhPreferNumbers
339 mark_numbers_preferred = _newqt_mark_numbers_preferred
340 except AttributeError:
341 mark_numbers_preferred = _null_mark_numbers_preferred
344 def _null_get_theme_icon(iconNames, fallback = None):
345 icon = fallback if fallback is not None else QtGui.QIcon()
349 def _newqt_get_theme_icon(iconNames, fallback = None):
350 for iconName in iconNames:
351 if QtGui.QIcon.hasThemeIcon(iconName):
352 icon = QtGui.QIcon.fromTheme(iconName)
355 icon = fallback if fallback is not None else QtGui.QIcon()
360 QtGui.QIcon.fromTheme
361 get_theme_icon = _newqt_get_theme_icon
362 except AttributeError:
363 get_theme_icon = _null_get_theme_icon