6 from PyQt4 import QtGui
7 from PyQt4 import QtCore
10 from util import misc as misc_utils
12 class misc_utils(object):
15 def log_exception(logger):
22 _moduleLogger = logging.getLogger(__name__)
28 def _radius_at(center, pos):
33 radius = math.sqrt(xDelta ** 2 + yDelta ** 2)
37 def _angle_at(center, pos):
42 radius = math.sqrt(xDelta ** 2 + yDelta ** 2)
43 angle = math.acos(xDelta / radius)
45 angle = _TWOPI - angle
50 class QActionPieItem(object):
52 def __init__(self, action, weight = 1):
59 def setWeight(self, weight):
65 def setEnabled(self, enabled = True):
66 self._action.setEnabled(enabled)
69 return self._action.isEnabled()
72 class PieFiling(object):
74 INNER_RADIUS_DEFAULT = 64
75 OUTER_RADIUS_DEFAULT = 192
80 NULL_CENTER = QActionPieItem(QtGui.QAction(None))
83 self._innerRadius = self.INNER_RADIUS_DEFAULT
84 self._outerRadius = self.OUTER_RADIUS_DEFAULT
86 self._center = self.NULL_CENTER
88 self._cacheIndexToAngle = {}
89 self._cacheTotalWeight = 0
91 def insertItem(self, item, index = -1):
92 self._children.insert(index, item)
93 self._invalidate_cache()
95 def removeItemAt(self, index):
96 item = self._children.pop(index)
97 self._invalidate_cache()
99 def set_center(self, item):
101 item = self.NULL_CENTER
108 del self._children[:]
109 self._center = self.NULL_CENTER
110 self._invalidate_cache()
112 def itemAt(self, index):
113 return self._children[index]
115 def indexAt(self, center, point):
116 return self._angle_to_index(_angle_at(center, point))
118 def innerRadius(self):
119 return self._innerRadius
121 def setInnerRadius(self, radius):
122 self._innerRadius = radius
124 def outerRadius(self):
125 return self._outerRadius
127 def setOuterRadius(self, radius):
128 self._outerRadius = radius
131 return iter(self._children)
134 return len(self._children)
136 def __getitem__(self, index):
137 return self._children[index]
139 def _invalidate_cache(self):
140 self._cacheIndexToAngle.clear()
141 self._cacheTotalWeight = sum(child.weight() for child in self._children)
142 if self._cacheTotalWeight == 0:
143 self._cacheTotalWeight = 1
145 def _index_to_angle(self, index, isShifted):
146 key = index, isShifted
147 if key in self._cacheIndexToAngle:
148 return self._cacheIndexToAngle[key]
149 index = index % len(self._children)
151 baseAngle = _TWOPI / self._cacheTotalWeight
156 angle -= (self._children[0].weight() * baseAngle) / 2
158 angle -= baseAngle / 2
162 for i, child in enumerate(self._children):
165 angle += child.weight() * baseAngle
166 while _TWOPI < angle:
169 self._cacheIndexToAngle[key] = angle
172 def _angle_to_index(self, angle):
173 numChildren = len(self._children)
175 return self.SELECTION_CENTER
177 baseAngle = _TWOPI / self._cacheTotalWeight
179 iterAngle = math.pi / 2 - (self.itemAt(0).weight() * baseAngle) / 2
183 oldIterAngle = iterAngle
184 for index, child in enumerate(self._children):
185 iterAngle += child.weight() * baseAngle
186 if oldIterAngle < angle and angle <= iterAngle:
187 return index - 1 if index != 0 else numChildren - 1
188 elif oldIterAngle < (angle + _TWOPI) and (angle + _TWOPI <= iterAngle):
189 return index - 1 if index != 0 else numChildren - 1
190 oldIterAngle = iterAngle
193 class PieArtist(object):
195 ICON_SIZE_DEFAULT = 48
197 SHAPE_CIRCLE = "circle"
198 SHAPE_SQUARE = "square"
199 DEFAULT_SHAPE = SHAPE_SQUARE
201 def __init__(self, filing):
202 self._filing = filing
204 self._cachedOuterRadius = self._filing.outerRadius()
205 self._cachedInnerRadius = self._filing.innerRadius()
206 canvasSize = self._cachedOuterRadius * 2 + 1
207 self._canvas = QtGui.QPixmap(canvasSize, canvasSize)
212 diameter = self._filing.outerRadius() * 2 + 1
213 return QtCore.QSize(diameter, diameter)
215 def centerSize(self):
216 painter = QtGui.QPainter(self._canvas)
217 text = self._filing.center().action().text()
218 fontMetrics = painter.fontMetrics()
220 textBoundingRect = fontMetrics.boundingRect(text)
222 textBoundingRect = QtCore.QRect()
223 textWidth = textBoundingRect.width()
224 textHeight = textBoundingRect.height()
227 textWidth + self.ICON_SIZE_DEFAULT,
228 max(textHeight, self.ICON_SIZE_DEFAULT),
231 def show(self, palette):
232 self.palette = palette
235 self._cachedOuterRadius != self._filing.outerRadius() or
236 self._cachedInnerRadius != self._filing.innerRadius()
238 self._cachedOuterRadius = self._filing.outerRadius()
239 self._cachedInnerRadius = self._filing.innerRadius()
240 self._canvas = self._canvas.scaled(self.pieSize())
242 if self._mask is None:
243 self._mask = QtGui.QBitmap(self._canvas.size())
244 self._mask.fill(QtCore.Qt.color0)
245 self._generate_mask(self._mask)
246 self._canvas.setMask(self._mask)
252 def paint(self, selectionIndex):
253 painter = QtGui.QPainter(self._canvas)
254 painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
256 adjustmentRect = self._canvas.rect().adjusted(0, 0, -1, -1)
258 numChildren = len(self._filing)
260 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
261 painter.setBrush(self.palette.highlight())
263 painter.setBrush(self.palette.window())
264 painter.setPen(self.palette.mid().color())
266 painter.drawRect(self._canvas.rect())
267 self._paint_center_foreground(painter, selectionIndex)
269 elif numChildren == 1:
270 if selectionIndex == 0 and self._filing[0].isEnabled():
271 painter.setBrush(self.palette.highlight())
273 painter.setBrush(self.palette.window())
275 painter.fillRect(self._canvas.rect(), painter.brush())
277 for i in xrange(len(self._filing)):
278 self._paint_slice_background(painter, adjustmentRect, i, selectionIndex)
280 self._paint_center_background(painter, adjustmentRect, selectionIndex)
281 self._paint_center_foreground(painter, selectionIndex)
283 for i in xrange(len(self._filing)):
284 self._paint_slice_foreground(painter, i, selectionIndex)
288 def _generate_mask(self, mask):
290 Specifies on the mask the shape of the pie menu
292 painter = QtGui.QPainter(mask)
293 painter.setPen(QtCore.Qt.color1)
294 painter.setBrush(QtCore.Qt.color1)
295 if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
296 painter.drawRect(mask.rect())
297 elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
298 painter.drawEllipse(mask.rect().adjusted(0, 0, -1, -1))
300 raise NotImplementedError(self.DEFAULT_SHAPE)
302 def _paint_slice_background(self, painter, adjustmentRect, i, selectionIndex):
303 if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
304 currentWidth = adjustmentRect.width()
305 newWidth = math.sqrt(2) * currentWidth
306 dx = (newWidth - currentWidth) / 2
307 adjustmentRect = adjustmentRect.adjusted(-dx, -dx, dx, dx)
308 elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
311 raise NotImplementedError(self.DEFAULT_SHAPE)
313 if i == selectionIndex and self._filing[i].isEnabled():
314 painter.setBrush(self.palette.highlight())
316 painter.setBrush(self.palette.window())
317 painter.setPen(self.palette.mid().color())
319 a = self._filing._index_to_angle(i, True)
320 b = self._filing._index_to_angle(i + 1, True)
327 startAngleInDeg = (a * 360 * 16) / _TWOPI
328 sizeInDeg = (size * 360 * 16) / _TWOPI
329 painter.drawPie(adjustmentRect, int(startAngleInDeg), int(sizeInDeg))
331 def _paint_slice_foreground(self, painter, i, selectionIndex):
332 child = self._filing[i]
334 a = self._filing._index_to_angle(i, True)
335 b = self._filing._index_to_angle(i + 1, True)
338 middleAngle = (a + b) / 2
339 averageRadius = (self._cachedInnerRadius + self._cachedOuterRadius) / 2
341 sliceX = averageRadius * math.cos(middleAngle)
342 sliceY = - averageRadius * math.sin(middleAngle)
344 piePos = self._canvas.rect().center()
348 painter, child.action(), i == selectionIndex, pieX+sliceX, pieY+sliceY
351 def _paint_label(self, painter, action, isSelected, x, y):
353 fontMetrics = painter.fontMetrics()
355 textBoundingRect = fontMetrics.boundingRect(text)
357 textBoundingRect = QtCore.QRect()
358 textWidth = textBoundingRect.width()
359 textHeight = textBoundingRect.height()
361 icon = action.icon().pixmap(
362 QtCore.QSize(self.ICON_SIZE_DEFAULT, self.ICON_SIZE_DEFAULT),
366 iconWidth = icon.width()
367 iconHeight = icon.width()
368 averageWidth = (iconWidth + textWidth)/2
369 if not icon.isNull():
370 iconRect = QtCore.QRect(
377 painter.drawPixmap(iconRect, icon)
381 if action.isEnabled():
382 pen = self.palette.highlightedText()
383 brush = self.palette.highlight()
385 pen = self.palette.mid()
386 brush = self.palette.window()
388 if action.isEnabled():
389 pen = self.palette.windowText()
391 pen = self.palette.mid()
392 brush = self.palette.window()
394 leftX = x - averageWidth + iconWidth
395 topY = y + textHeight/2
396 painter.setPen(pen.color())
397 painter.setBrush(brush)
398 painter.drawText(leftX, topY, text)
400 def _paint_center_background(self, painter, adjustmentRect, selectionIndex):
401 dark = self.palette.mid().color()
402 light = self.palette.light().color()
403 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
404 background = self.palette.highlight().color()
406 background = self.palette.window().color()
408 innerRadius = self._cachedInnerRadius
409 adjustmentCenterPos = adjustmentRect.center()
410 innerRect = QtCore.QRect(
411 adjustmentCenterPos.x() - innerRadius,
412 adjustmentCenterPos.y() - innerRadius,
417 painter.setPen(QtCore.Qt.NoPen)
418 painter.setBrush(background)
419 painter.drawPie(innerRect, 0, 360 * 16)
421 painter.setPen(QtGui.QPen(dark, 1))
422 painter.setBrush(QtCore.Qt.NoBrush)
423 painter.drawEllipse(innerRect)
425 if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
427 elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
428 painter.setPen(QtGui.QPen(dark, 1))
429 painter.setBrush(QtCore.Qt.NoBrush)
430 painter.drawEllipse(adjustmentRect)
432 raise NotImplementedError(self.DEFAULT_SHAPE)
434 def _paint_center_foreground(self, painter, selectionIndex):
435 centerPos = self._canvas.rect().center()
444 self._filing.center().action(),
445 selectionIndex == PieFiling.SELECTION_CENTER,
450 class QPieDisplay(QtGui.QWidget):
452 def __init__(self, filing, parent = None, flags = QtCore.Qt.Window):
453 QtGui.QWidget.__init__(self, parent, flags)
454 self._filing = filing
455 self._artist = PieArtist(self._filing)
456 self._selectionIndex = PieFiling.SELECTION_NONE
458 def popup(self, pos):
459 self._update_selection(pos)
463 return self._artist.pieSize()
465 @misc_utils.log_exception(_moduleLogger)
466 def showEvent(self, showEvent):
467 mask = self._artist.show(self.palette())
470 QtGui.QWidget.showEvent(self, showEvent)
472 @misc_utils.log_exception(_moduleLogger)
473 def hideEvent(self, hideEvent):
475 self._selectionIndex = PieFiling.SELECTION_NONE
476 QtGui.QWidget.hideEvent(self, hideEvent)
478 @misc_utils.log_exception(_moduleLogger)
479 def paintEvent(self, paintEvent):
480 canvas = self._artist.paint(self._selectionIndex)
482 screen = QtGui.QPainter(self)
483 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
485 QtGui.QWidget.paintEvent(self, paintEvent)
487 def selectAt(self, index):
488 oldIndex = self._selectionIndex
489 self._selectionIndex = index
494 class QPieButton(QtGui.QWidget):
496 activated = QtCore.pyqtSignal(int)
497 highlighted = QtCore.pyqtSignal(int)
498 canceled = QtCore.pyqtSignal()
499 aboutToShow = QtCore.pyqtSignal()
500 aboutToHide = QtCore.pyqtSignal()
505 def __init__(self, buttonSlice, parent = None, buttonSlices = None):
506 # @bug Artifacts on Maemo 5 due to window 3D effects, find way to disable them for just these?
507 # @bug The pie's are being pushed back on screen on Maemo, leading to coordinate issues
508 QtGui.QWidget.__init__(self, parent)
509 self._cachedCenterPosition = self.rect().center()
511 self._filing = PieFiling()
512 self._display = QPieDisplay(self._filing, None, QtCore.Qt.SplashScreen)
513 self._selectionIndex = PieFiling.SELECTION_NONE
515 self._buttonFiling = PieFiling()
516 self._buttonFiling.set_center(buttonSlice)
517 if buttonSlices is not None:
518 for slice in buttonSlices:
519 self._buttonFiling.insertItem(slice)
520 self._buttonFiling.setOuterRadius(self.BUTTON_RADIUS)
521 self._buttonArtist = PieArtist(self._buttonFiling)
522 self._poppedUp = False
524 self._delayPopupTimer = QtCore.QTimer()
525 self._delayPopupTimer.setInterval(self.DELAY)
526 self._delayPopupTimer.setSingleShot(True)
527 self._delayPopupTimer.timeout.connect(self._on_delayed_popup)
528 self._popupLocation = None
530 self._mousePosition = None
531 self.setFocusPolicy(QtCore.Qt.StrongFocus)
534 QtGui.QSizePolicy.MinimumExpanding,
535 QtGui.QSizePolicy.MinimumExpanding,
539 def insertItem(self, item, index = -1):
540 self._filing.insertItem(item, index)
542 def removeItemAt(self, index):
543 self._filing.removeItemAt(index)
545 def set_center(self, item):
546 self._filing.set_center(item)
548 def set_button(self, item):
554 def itemAt(self, index):
555 return self._filing.itemAt(index)
557 def indexAt(self, point):
558 return self._filing.indexAt(self._cachedCenterPosition, point)
560 def innerRadius(self):
561 return self._filing.innerRadius()
563 def setInnerRadius(self, radius):
564 self._filing.setInnerRadius(radius)
566 def outerRadius(self):
567 return self._filing.outerRadius()
569 def setOuterRadius(self, radius):
570 self._filing.setOuterRadius(radius)
572 def buttonRadius(self):
573 return self._buttonFiling.outerRadius()
575 def setButtonRadius(self, radius):
576 self._buttonFiling.setOuterRadius(radius)
577 self._buttonFiling.setInnerRadius(radius / 2)
578 self._buttonArtist.show(self.palette())
581 return self._buttonArtist.pieSize()
583 def minimumSizeHint(self):
584 return self._buttonArtist.centerSize()
586 @misc_utils.log_exception(_moduleLogger)
587 def mousePressEvent(self, mouseEvent):
588 lastSelection = self._selectionIndex
590 lastMousePos = mouseEvent.pos()
591 self._mousePosition = lastMousePos
592 self._update_selection(self._cachedCenterPosition)
594 self.highlighted.emit(self._selectionIndex)
596 self._display.selectAt(self._selectionIndex)
597 self._popupLocation = mouseEvent.globalPos()
598 self._delayPopupTimer.start()
600 @misc_utils.log_exception(_moduleLogger)
601 def _on_delayed_popup(self):
602 assert self._popupLocation is not None, "Widget location abuse"
603 self._popup_child(self._popupLocation)
605 @misc_utils.log_exception(_moduleLogger)
606 def mouseMoveEvent(self, mouseEvent):
607 lastSelection = self._selectionIndex
609 lastMousePos = mouseEvent.pos()
610 if self._mousePosition is None:
612 self._update_selection(lastMousePos)
615 self._update_selection(
616 self._cachedCenterPosition + (lastMousePos - self._mousePosition),
620 if lastSelection != self._selectionIndex:
621 self.highlighted.emit(self._selectionIndex)
622 self._display.selectAt(self._selectionIndex)
624 if self._selectionIndex != PieFiling.SELECTION_CENTER and self._delayPopupTimer.isActive():
625 self._on_delayed_popup()
627 @misc_utils.log_exception(_moduleLogger)
628 def mouseReleaseEvent(self, mouseEvent):
629 self._delayPopupTimer.stop()
630 self._popupLocation = None
632 lastSelection = self._selectionIndex
634 lastMousePos = mouseEvent.pos()
635 if self._mousePosition is None:
637 self._update_selection(lastMousePos)
640 self._update_selection(
641 self._cachedCenterPosition + (lastMousePos - self._mousePosition),
644 self._mousePosition = None
646 self._activate_at(self._selectionIndex)
649 @misc_utils.log_exception(_moduleLogger)
650 def keyPressEvent(self, keyEvent):
651 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
652 self._popup_child(QtGui.QCursor.pos())
653 if self._selectionIndex != len(self._filing) - 1:
654 nextSelection = self._selectionIndex + 1
657 self._select_at(nextSelection)
658 self._display.selectAt(self._selectionIndex)
659 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
660 self._popup_child(QtGui.QCursor.pos())
661 if 0 < self._selectionIndex:
662 nextSelection = self._selectionIndex - 1
664 nextSelection = len(self._filing) - 1
665 self._select_at(nextSelection)
666 self._display.selectAt(self._selectionIndex)
667 elif keyEvent.key() in [QtCore.Qt.Key_Space]:
668 self._popup_child(QtGui.QCursor.pos())
669 self._select_at(PieFiling.SELECTION_CENTER)
670 self._display.selectAt(self._selectionIndex)
671 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
672 self._delayPopupTimer.stop()
673 self._popupLocation = None
674 self._activate_at(self._selectionIndex)
676 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
677 self._delayPopupTimer.stop()
678 self._popupLocation = None
679 self._activate_at(PieFiling.SELECTION_NONE)
682 QtGui.QWidget.keyPressEvent(self, keyEvent)
684 @misc_utils.log_exception(_moduleLogger)
685 def resizeEvent(self, resizeEvent):
686 self.setButtonRadius(min(resizeEvent.size().width(), resizeEvent.size().height()) / 2 - 1)
687 QtGui.QWidget.resizeEvent(self, resizeEvent)
689 @misc_utils.log_exception(_moduleLogger)
690 def showEvent(self, showEvent):
691 self._buttonArtist.show(self.palette())
692 self._cachedCenterPosition = self.rect().center()
694 QtGui.QWidget.showEvent(self, showEvent)
696 @misc_utils.log_exception(_moduleLogger)
697 def hideEvent(self, hideEvent):
699 self._select_at(PieFiling.SELECTION_NONE)
700 QtGui.QWidget.hideEvent(self, hideEvent)
702 @misc_utils.log_exception(_moduleLogger)
703 def paintEvent(self, paintEvent):
704 self.setButtonRadius(min(self.rect().width(), self.rect().height()) / 2 - 1)
706 canvas = self._buttonArtist.paint(PieFiling.SELECTION_CENTER)
708 canvas = self._buttonArtist.paint(PieFiling.SELECTION_NONE)
710 screen = QtGui.QPainter(self)
711 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
713 QtGui.QWidget.paintEvent(self, paintEvent)
716 return iter(self._filing)
719 return len(self._filing)
721 def _popup_child(self, position):
722 self._poppedUp = True
723 self.aboutToShow.emit()
725 self._delayPopupTimer.stop()
726 self._popupLocation = None
728 position = position - QtCore.QPoint(self._filing.outerRadius(), self._filing.outerRadius())
729 self._display.move(position)
734 def _hide_child(self):
735 self._poppedUp = False
736 self.aboutToHide.emit()
740 def _select_at(self, index):
741 self._selectionIndex = index
743 def _update_selection(self, lastMousePos, ignoreOuter = False):
744 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
745 if radius < self._filing.innerRadius():
746 self._select_at(PieFiling.SELECTION_CENTER)
747 elif radius <= self._filing.outerRadius() or ignoreOuter:
748 self._select_at(self.indexAt(lastMousePos))
750 self._select_at(PieFiling.SELECTION_NONE)
752 def _activate_at(self, index):
753 if index == PieFiling.SELECTION_NONE:
756 elif index == PieFiling.SELECTION_CENTER:
757 child = self._filing.center()
759 child = self.itemAt(index)
761 if child.action().isEnabled():
762 child.action().trigger()
763 self.activated.emit(index)
768 class QPieMenu(QtGui.QWidget):
770 activated = QtCore.pyqtSignal(int)
771 highlighted = QtCore.pyqtSignal(int)
772 canceled = QtCore.pyqtSignal()
773 aboutToShow = QtCore.pyqtSignal()
774 aboutToHide = QtCore.pyqtSignal()
776 def __init__(self, parent = None):
777 QtGui.QWidget.__init__(self, parent)
778 self._cachedCenterPosition = self.rect().center()
780 self._filing = PieFiling()
781 self._artist = PieArtist(self._filing)
782 self._selectionIndex = PieFiling.SELECTION_NONE
784 self._mousePosition = ()
785 self.setFocusPolicy(QtCore.Qt.StrongFocus)
787 def popup(self, pos):
788 self._update_selection(pos)
791 def insertItem(self, item, index = -1):
792 self._filing.insertItem(item, index)
795 def removeItemAt(self, index):
796 self._filing.removeItemAt(index)
799 def set_center(self, item):
800 self._filing.set_center(item)
807 def itemAt(self, index):
808 return self._filing.itemAt(index)
810 def indexAt(self, point):
811 return self._filing.indexAt(self._cachedCenterPosition, point)
813 def innerRadius(self):
814 return self._filing.innerRadius()
816 def setInnerRadius(self, radius):
817 self._filing.setInnerRadius(radius)
820 def outerRadius(self):
821 return self._filing.outerRadius()
823 def setOuterRadius(self, radius):
824 self._filing.setOuterRadius(radius)
828 return self._artist.pieSize()
830 @misc_utils.log_exception(_moduleLogger)
831 def mousePressEvent(self, mouseEvent):
832 lastSelection = self._selectionIndex
834 lastMousePos = mouseEvent.pos()
835 self._update_selection(lastMousePos)
836 self._mousePosition = lastMousePos
838 if lastSelection != self._selectionIndex:
839 self.highlighted.emit(self._selectionIndex)
842 @misc_utils.log_exception(_moduleLogger)
843 def mouseMoveEvent(self, mouseEvent):
844 lastSelection = self._selectionIndex
846 lastMousePos = mouseEvent.pos()
847 self._update_selection(lastMousePos)
849 if lastSelection != self._selectionIndex:
850 self.highlighted.emit(self._selectionIndex)
853 @misc_utils.log_exception(_moduleLogger)
854 def mouseReleaseEvent(self, mouseEvent):
855 lastSelection = self._selectionIndex
857 lastMousePos = mouseEvent.pos()
858 self._update_selection(lastMousePos)
859 self._mousePosition = ()
861 self._activate_at(self._selectionIndex)
864 @misc_utils.log_exception(_moduleLogger)
865 def keyPressEvent(self, keyEvent):
866 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
867 if self._selectionIndex != len(self._filing) - 1:
868 nextSelection = self._selectionIndex + 1
871 self._select_at(nextSelection)
873 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
874 if 0 < self._selectionIndex:
875 nextSelection = self._selectionIndex - 1
877 nextSelection = len(self._filing) - 1
878 self._select_at(nextSelection)
880 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
881 self._activate_at(self._selectionIndex)
882 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
883 self._activate_at(PieFiling.SELECTION_NONE)
885 QtGui.QWidget.keyPressEvent(self, keyEvent)
887 @misc_utils.log_exception(_moduleLogger)
888 def showEvent(self, showEvent):
889 self.aboutToShow.emit()
890 self._cachedCenterPosition = self.rect().center()
892 mask = self._artist.show(self.palette())
895 lastMousePos = self.mapFromGlobal(QtGui.QCursor.pos())
896 self._update_selection(lastMousePos)
898 QtGui.QWidget.showEvent(self, showEvent)
900 @misc_utils.log_exception(_moduleLogger)
901 def hideEvent(self, hideEvent):
903 self._selectionIndex = PieFiling.SELECTION_NONE
904 QtGui.QWidget.hideEvent(self, hideEvent)
906 @misc_utils.log_exception(_moduleLogger)
907 def paintEvent(self, paintEvent):
908 canvas = self._artist.paint(self._selectionIndex)
910 screen = QtGui.QPainter(self)
911 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
913 QtGui.QWidget.paintEvent(self, paintEvent)
916 return iter(self._filing)
919 return len(self._filing)
921 def _select_at(self, index):
922 self._selectionIndex = index
924 def _update_selection(self, lastMousePos):
925 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
926 if radius < self._filing.innerRadius():
927 self._selectionIndex = PieFiling.SELECTION_CENTER
928 elif radius <= self._filing.outerRadius():
929 self._select_at(self.indexAt(lastMousePos))
931 self._selectionIndex = PieFiling.SELECTION_NONE
933 def _activate_at(self, index):
934 if index == PieFiling.SELECTION_NONE:
936 self.aboutToHide.emit()
939 elif index == PieFiling.SELECTION_CENTER:
940 child = self._filing.center()
942 child = self.itemAt(index)
944 if child.isEnabled():
945 child.action().trigger()
946 self.activated.emit(index)
949 self.aboutToHide.emit()
954 PieFiling.NULL_CENTER.setEnabled(False)
961 def _on_about_to_hide(app):
965 if __name__ == "__main__":
966 app = QtGui.QApplication([])
974 singleAction = QtGui.QAction(None)
975 singleAction.setText("Boo")
976 singleItem = QActionPieItem(singleAction)
978 spie.insertItem(singleItem)
982 oneAction = QtGui.QAction(None)
983 oneAction.setText("Chew")
984 oneItem = QActionPieItem(oneAction)
985 twoAction = QtGui.QAction(None)
986 twoAction.setText("Foo")
987 twoItem = QActionPieItem(twoAction)
988 iconTextAction = QtGui.QAction(None)
989 iconTextAction.setText("Icon")
990 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
991 iconTextItem = QActionPieItem(iconTextAction)
993 mpie.insertItem(oneItem)
994 mpie.insertItem(twoItem)
995 mpie.insertItem(oneItem)
996 mpie.insertItem(iconTextItem)
1000 oneAction = QtGui.QAction(None)
1001 oneAction.setText("Chew")
1002 oneAction.triggered.connect(lambda: _print("Chew"))
1003 oneItem = QActionPieItem(oneAction)
1004 twoAction = QtGui.QAction(None)
1005 twoAction.setText("Foo")
1006 twoAction.triggered.connect(lambda: _print("Foo"))
1007 twoItem = QActionPieItem(twoAction)
1008 iconAction = QtGui.QAction(None)
1009 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
1010 iconAction.triggered.connect(lambda: _print("Icon"))
1011 iconItem = QActionPieItem(iconAction)
1012 iconTextAction = QtGui.QAction(None)
1013 iconTextAction.setText("Icon")
1014 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1015 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
1016 iconTextItem = QActionPieItem(iconTextAction)
1018 mpie.set_center(iconItem)
1019 mpie.insertItem(oneItem)
1020 mpie.insertItem(twoItem)
1021 mpie.insertItem(oneItem)
1022 mpie.insertItem(iconTextItem)
1024 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
1025 mpie.canceled.connect(lambda: _print("Canceled"))
1028 oneAction = QtGui.QAction(None)
1029 oneAction.setText("Chew")
1030 oneAction.triggered.connect(lambda: _print("Chew"))
1031 oneItem = QActionPieItem(oneAction)
1032 twoAction = QtGui.QAction(None)
1033 twoAction.setText("Foo")
1034 twoAction.triggered.connect(lambda: _print("Foo"))
1035 twoItem = QActionPieItem(twoAction)
1036 iconAction = QtGui.QAction(None)
1037 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
1038 iconAction.triggered.connect(lambda: _print("Icon"))
1039 iconItem = QActionPieItem(iconAction)
1040 iconTextAction = QtGui.QAction(None)
1041 iconTextAction.setText("Icon")
1042 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1043 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
1044 iconTextItem = QActionPieItem(iconTextAction)
1045 pieFiling = PieFiling()
1046 pieFiling.set_center(iconItem)
1047 pieFiling.insertItem(oneItem)
1048 pieFiling.insertItem(twoItem)
1049 pieFiling.insertItem(oneItem)
1050 pieFiling.insertItem(iconTextItem)
1051 mpie = QPieDisplay(pieFiling)
1055 oneAction = QtGui.QAction(None)
1056 oneAction.setText("Chew")
1057 oneAction.triggered.connect(lambda: _print("Chew"))
1058 oneItem = QActionPieItem(oneAction)
1059 twoAction = QtGui.QAction(None)
1060 twoAction.setText("Foo")
1061 twoAction.triggered.connect(lambda: _print("Foo"))
1062 twoItem = QActionPieItem(twoAction)
1063 iconAction = QtGui.QAction(None)
1064 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
1065 iconAction.triggered.connect(lambda: _print("Icon"))
1066 iconItem = QActionPieItem(iconAction)
1067 iconTextAction = QtGui.QAction(None)
1068 iconTextAction.setText("Icon")
1069 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1070 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
1071 iconTextItem = QActionPieItem(iconTextAction)
1072 mpie = QPieButton(iconItem)
1073 mpie.set_center(iconItem)
1074 mpie.insertItem(oneItem)
1075 mpie.insertItem(twoItem)
1076 mpie.insertItem(oneItem)
1077 mpie.insertItem(iconTextItem)
1079 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
1080 mpie.canceled.connect(lambda: _print("Canceled"))