7 QtCore = qt_compat.QtCore
8 QtGui = qt_compat.import_module("QtGui")
10 import misc as misc_utils
13 _moduleLogger = logging.getLogger(__name__)
19 def _radius_at(center, pos):
24 radius = math.sqrt(xDelta ** 2 + yDelta ** 2)
28 def _angle_at(center, pos):
33 radius = math.sqrt(xDelta ** 2 + yDelta ** 2)
34 angle = math.acos(xDelta / radius)
36 angle = _TWOPI - angle
41 class QActionPieItem(object):
43 def __init__(self, action, weight = 1):
50 def setWeight(self, weight):
56 def setEnabled(self, enabled = True):
57 self._action.setEnabled(enabled)
60 return self._action.isEnabled()
63 class PieFiling(object):
65 INNER_RADIUS_DEFAULT = 64
66 OUTER_RADIUS_DEFAULT = 192
71 NULL_CENTER = QActionPieItem(QtGui.QAction(None))
74 self._innerRadius = self.INNER_RADIUS_DEFAULT
75 self._outerRadius = self.OUTER_RADIUS_DEFAULT
77 self._center = self.NULL_CENTER
79 self._cacheIndexToAngle = {}
80 self._cacheTotalWeight = 0
82 def insertItem(self, item, index = -1):
83 self._children.insert(index, item)
84 self._invalidate_cache()
86 def removeItemAt(self, index):
87 item = self._children.pop(index)
88 self._invalidate_cache()
90 def set_center(self, item):
92 item = self.NULL_CENTER
100 self._center = self.NULL_CENTER
101 self._invalidate_cache()
103 def itemAt(self, index):
104 return self._children[index]
106 def indexAt(self, center, point):
107 return self._angle_to_index(_angle_at(center, point))
109 def innerRadius(self):
110 return self._innerRadius
112 def setInnerRadius(self, radius):
113 self._innerRadius = radius
115 def outerRadius(self):
116 return self._outerRadius
118 def setOuterRadius(self, radius):
119 self._outerRadius = radius
122 return iter(self._children)
125 return len(self._children)
127 def __getitem__(self, index):
128 return self._children[index]
130 def _invalidate_cache(self):
131 self._cacheIndexToAngle.clear()
132 self._cacheTotalWeight = sum(child.weight() for child in self._children)
133 if self._cacheTotalWeight == 0:
134 self._cacheTotalWeight = 1
136 def _index_to_angle(self, index, isShifted):
137 key = index, isShifted
138 if key in self._cacheIndexToAngle:
139 return self._cacheIndexToAngle[key]
140 index = index % len(self._children)
142 baseAngle = _TWOPI / self._cacheTotalWeight
147 angle -= (self._children[0].weight() * baseAngle) / 2
149 angle -= baseAngle / 2
153 for i, child in enumerate(self._children):
156 angle += child.weight() * baseAngle
157 while _TWOPI < angle:
160 self._cacheIndexToAngle[key] = angle
163 def _angle_to_index(self, angle):
164 numChildren = len(self._children)
166 return self.SELECTION_CENTER
168 baseAngle = _TWOPI / self._cacheTotalWeight
170 iterAngle = math.pi / 2 - (self.itemAt(0).weight() * baseAngle) / 2
174 oldIterAngle = iterAngle
175 for index, child in enumerate(self._children):
176 iterAngle += child.weight() * baseAngle
177 if oldIterAngle < angle and angle <= iterAngle:
178 return index - 1 if index != 0 else numChildren - 1
179 elif oldIterAngle < (angle + _TWOPI) and (angle + _TWOPI <= iterAngle):
180 return index - 1 if index != 0 else numChildren - 1
181 oldIterAngle = iterAngle
184 class PieArtist(object):
186 ICON_SIZE_DEFAULT = 48
188 SHAPE_CIRCLE = "circle"
189 SHAPE_SQUARE = "square"
190 DEFAULT_SHAPE = SHAPE_SQUARE
192 BACKGROUND_FILL = "fill"
193 BACKGROUND_NOFILL = "no fill"
195 def __init__(self, filing, background = BACKGROUND_FILL):
196 self._filing = filing
198 self._cachedOuterRadius = self._filing.outerRadius()
199 self._cachedInnerRadius = self._filing.innerRadius()
200 canvasSize = self._cachedOuterRadius * 2 + 1
201 self._canvas = QtGui.QPixmap(canvasSize, canvasSize)
203 self._backgroundState = background
207 diameter = self._filing.outerRadius() * 2 + 1
208 return QtCore.QSize(diameter, diameter)
210 def centerSize(self):
211 painter = QtGui.QPainter(self._canvas)
212 text = self._filing.center().action().text()
213 fontMetrics = painter.fontMetrics()
215 textBoundingRect = fontMetrics.boundingRect(text)
217 textBoundingRect = QtCore.QRect()
218 textWidth = textBoundingRect.width()
219 textHeight = textBoundingRect.height()
222 textWidth + self.ICON_SIZE_DEFAULT,
223 max(textHeight, self.ICON_SIZE_DEFAULT),
226 def show(self, palette):
227 self.palette = palette
230 self._cachedOuterRadius != self._filing.outerRadius() or
231 self._cachedInnerRadius != self._filing.innerRadius()
233 self._cachedOuterRadius = self._filing.outerRadius()
234 self._cachedInnerRadius = self._filing.innerRadius()
235 self._canvas = self._canvas.scaled(self.pieSize())
237 if self._mask is None:
238 self._mask = QtGui.QBitmap(self._canvas.size())
239 self._mask.fill(QtCore.Qt.color0)
240 self._generate_mask(self._mask)
241 self._canvas.setMask(self._mask)
247 def paint(self, selectionIndex):
248 painter = QtGui.QPainter(self._canvas)
249 painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
251 self.paintPainter(selectionIndex, painter)
255 def paintPainter(self, selectionIndex, painter):
256 adjustmentRect = painter.viewport().adjusted(0, 0, -1, -1)
258 numChildren = len(self._filing)
260 self._paint_center_background(painter, adjustmentRect, selectionIndex)
261 self._paint_center_foreground(painter, adjustmentRect, selectionIndex)
264 for i in xrange(len(self._filing)):
265 self._paint_slice_background(painter, adjustmentRect, i, selectionIndex)
267 self._paint_center_background(painter, adjustmentRect, selectionIndex)
268 self._paint_center_foreground(painter, adjustmentRect, selectionIndex)
270 for i in xrange(len(self._filing)):
271 self._paint_slice_foreground(painter, adjustmentRect, i, selectionIndex)
273 def _generate_mask(self, mask):
275 Specifies on the mask the shape of the pie menu
277 painter = QtGui.QPainter(mask)
278 painter.setPen(QtCore.Qt.color1)
279 painter.setBrush(QtCore.Qt.color1)
280 if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
281 painter.drawRect(mask.rect())
282 elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
283 painter.drawEllipse(mask.rect().adjusted(0, 0, -1, -1))
285 raise NotImplementedError(self.DEFAULT_SHAPE)
287 def _paint_slice_background(self, painter, adjustmentRect, i, selectionIndex):
288 if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
289 currentWidth = adjustmentRect.width()
290 newWidth = math.sqrt(2) * currentWidth
291 dx = (newWidth - currentWidth) / 2
292 adjustmentRect = adjustmentRect.adjusted(-dx, -dx, dx, dx)
293 elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
296 raise NotImplementedError(self.DEFAULT_SHAPE)
298 if self._backgroundState == self.BACKGROUND_NOFILL:
299 painter.setBrush(QtGui.QBrush(QtCore.Qt.transparent))
300 painter.setPen(self.palette.highlight().color())
302 if i == selectionIndex and self._filing[i].isEnabled():
303 painter.setBrush(self.palette.highlight())
304 painter.setPen(self.palette.highlight().color())
306 painter.setBrush(self.palette.window())
307 painter.setPen(self.palette.window().color())
309 a = self._filing._index_to_angle(i, True)
310 b = self._filing._index_to_angle(i + 1, True)
317 startAngleInDeg = (a * 360 * 16) / _TWOPI
318 sizeInDeg = (size * 360 * 16) / _TWOPI
319 painter.drawPie(adjustmentRect, int(startAngleInDeg), int(sizeInDeg))
321 def _paint_slice_foreground(self, painter, adjustmentRect, i, selectionIndex):
322 child = self._filing[i]
324 a = self._filing._index_to_angle(i, True)
325 b = self._filing._index_to_angle(i + 1, True)
328 middleAngle = (a + b) / 2
329 averageRadius = (self._cachedInnerRadius + self._cachedOuterRadius) / 2
331 sliceX = averageRadius * math.cos(middleAngle)
332 sliceY = - averageRadius * math.sin(middleAngle)
334 piePos = adjustmentRect.center()
338 painter, child.action(), i == selectionIndex, pieX+sliceX, pieY+sliceY
341 def _paint_label(self, painter, action, isSelected, x, y):
343 fontMetrics = painter.fontMetrics()
345 textBoundingRect = fontMetrics.boundingRect(text)
347 textBoundingRect = QtCore.QRect()
348 textWidth = textBoundingRect.width()
349 textHeight = textBoundingRect.height()
351 icon = action.icon().pixmap(
352 QtCore.QSize(self.ICON_SIZE_DEFAULT, self.ICON_SIZE_DEFAULT),
356 iconWidth = icon.width()
357 iconHeight = icon.width()
358 averageWidth = (iconWidth + textWidth)/2
359 if not icon.isNull():
360 iconRect = QtCore.QRect(
367 painter.drawPixmap(iconRect, icon)
371 if action.isEnabled():
372 pen = self.palette.highlightedText()
373 brush = self.palette.highlight()
375 pen = self.palette.mid()
376 brush = self.palette.window()
378 if action.isEnabled():
379 pen = self.palette.windowText()
381 pen = self.palette.mid()
382 brush = self.palette.window()
384 leftX = x - averageWidth + iconWidth
385 topY = y + textHeight/2
386 painter.setPen(pen.color())
387 painter.setBrush(brush)
388 painter.drawText(leftX, topY, text)
390 def _paint_center_background(self, painter, adjustmentRect, selectionIndex):
391 if self._backgroundState == self.BACKGROUND_NOFILL:
393 if len(self._filing) == 0:
394 if self._backgroundState == self.BACKGROUND_NOFILL:
395 painter.setBrush(QtGui.QBrush(QtCore.Qt.transparent))
397 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
398 painter.setBrush(self.palette.highlight())
400 painter.setBrush(self.palette.window())
401 painter.setPen(self.palette.mid().color())
403 painter.drawRect(adjustmentRect)
405 dark = self.palette.mid().color()
406 light = self.palette.light().color()
407 if self._backgroundState == self.BACKGROUND_NOFILL:
408 background = QtGui.QBrush(QtCore.Qt.transparent)
410 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
411 background = self.palette.highlight().color()
413 background = self.palette.window().color()
415 innerRadius = self._cachedInnerRadius
416 adjustmentCenterPos = adjustmentRect.center()
417 innerRect = QtCore.QRect(
418 adjustmentCenterPos.x() - innerRadius,
419 adjustmentCenterPos.y() - innerRadius,
424 painter.setPen(QtCore.Qt.NoPen)
425 painter.setBrush(background)
426 painter.drawPie(innerRect, 0, 360 * 16)
428 if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
430 elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
431 painter.setPen(QtGui.QPen(dark, 1))
432 painter.setBrush(QtCore.Qt.NoBrush)
433 painter.drawEllipse(adjustmentRect)
435 raise NotImplementedError(self.DEFAULT_SHAPE)
437 def _paint_center_foreground(self, painter, adjustmentRect, selectionIndex):
438 centerPos = adjustmentRect.center()
447 self._filing.center().action(),
448 selectionIndex == PieFiling.SELECTION_CENTER,
453 class QPieDisplay(QtGui.QWidget):
455 def __init__(self, filing, parent = None, flags = QtCore.Qt.Window):
456 QtGui.QWidget.__init__(self, parent, flags)
457 self._filing = filing
458 self._artist = PieArtist(self._filing)
459 self._selectionIndex = PieFiling.SELECTION_NONE
461 def popup(self, pos):
462 self._update_selection(pos)
466 return self._artist.pieSize()
468 @misc_utils.log_exception(_moduleLogger)
469 def showEvent(self, showEvent):
470 mask = self._artist.show(self.palette())
473 QtGui.QWidget.showEvent(self, showEvent)
475 @misc_utils.log_exception(_moduleLogger)
476 def hideEvent(self, hideEvent):
478 self._selectionIndex = PieFiling.SELECTION_NONE
479 QtGui.QWidget.hideEvent(self, hideEvent)
481 @misc_utils.log_exception(_moduleLogger)
482 def paintEvent(self, paintEvent):
483 canvas = self._artist.paint(self._selectionIndex)
484 offset = (self.size() - canvas.size()) / 2
486 screen = QtGui.QPainter(self)
487 screen.drawPixmap(QtCore.QPoint(offset.width(), offset.height()), canvas)
489 QtGui.QWidget.paintEvent(self, paintEvent)
491 def selectAt(self, index):
492 oldIndex = self._selectionIndex
493 self._selectionIndex = index
498 class QPieButton(QtGui.QWidget):
500 activated = qt_compat.Signal(int)
501 highlighted = qt_compat.Signal(int)
502 canceled = qt_compat.Signal()
503 aboutToShow = qt_compat.Signal()
504 aboutToHide = qt_compat.Signal()
509 def __init__(self, buttonSlice, parent = None, buttonSlices = None):
510 # @bug Artifacts on Maemo 5 due to window 3D effects, find way to disable them for just these?
511 # @bug The pie's are being pushed back on screen on Maemo, leading to coordinate issues
512 QtGui.QWidget.__init__(self, parent)
513 self._cachedCenterPosition = self.rect().center()
515 self._filing = PieFiling()
516 self._display = QPieDisplay(self._filing, None, QtCore.Qt.SplashScreen)
517 self._selectionIndex = PieFiling.SELECTION_NONE
519 self._buttonFiling = PieFiling()
520 self._buttonFiling.set_center(buttonSlice)
521 if buttonSlices is not None:
522 for slice in buttonSlices:
523 self._buttonFiling.insertItem(slice)
524 self._buttonFiling.setOuterRadius(self.BUTTON_RADIUS)
525 self._buttonArtist = PieArtist(self._buttonFiling, PieArtist.BACKGROUND_NOFILL)
526 self._poppedUp = False
527 self._pressed = False
529 self._delayPopupTimer = QtCore.QTimer()
530 self._delayPopupTimer.setInterval(self.DELAY)
531 self._delayPopupTimer.setSingleShot(True)
532 self._delayPopupTimer.timeout.connect(self._on_delayed_popup)
533 self._popupLocation = None
535 self._mousePosition = None
536 self.setFocusPolicy(QtCore.Qt.StrongFocus)
539 QtGui.QSizePolicy.MinimumExpanding,
540 QtGui.QSizePolicy.MinimumExpanding,
544 def insertItem(self, item, index = -1):
545 self._filing.insertItem(item, index)
547 def removeItemAt(self, index):
548 self._filing.removeItemAt(index)
550 def set_center(self, item):
551 self._filing.set_center(item)
553 def set_button(self, item):
559 def itemAt(self, index):
560 return self._filing.itemAt(index)
562 def indexAt(self, point):
563 return self._filing.indexAt(self._cachedCenterPosition, point)
565 def innerRadius(self):
566 return self._filing.innerRadius()
568 def setInnerRadius(self, radius):
569 self._filing.setInnerRadius(radius)
571 def outerRadius(self):
572 return self._filing.outerRadius()
574 def setOuterRadius(self, radius):
575 self._filing.setOuterRadius(radius)
577 def buttonRadius(self):
578 return self._buttonFiling.outerRadius()
580 def setButtonRadius(self, radius):
581 self._buttonFiling.setOuterRadius(radius)
582 self._buttonFiling.setInnerRadius(radius / 2)
583 self._buttonArtist.show(self.palette())
585 def minimumSizeHint(self):
586 return self._buttonArtist.centerSize()
588 @misc_utils.log_exception(_moduleLogger)
589 def mousePressEvent(self, mouseEvent):
590 lastSelection = self._selectionIndex
592 lastMousePos = mouseEvent.pos()
593 self._mousePosition = lastMousePos
594 self._update_selection(self._cachedCenterPosition)
596 self.highlighted.emit(self._selectionIndex)
598 self._display.selectAt(self._selectionIndex)
601 self._popupLocation = mouseEvent.globalPos()
602 self._delayPopupTimer.start()
604 @misc_utils.log_exception(_moduleLogger)
605 def _on_delayed_popup(self):
606 assert self._popupLocation is not None, "Widget location abuse"
607 self._popup_child(self._popupLocation)
609 @misc_utils.log_exception(_moduleLogger)
610 def mouseMoveEvent(self, mouseEvent):
611 lastSelection = self._selectionIndex
613 lastMousePos = mouseEvent.pos()
614 if self._mousePosition is None:
616 self._update_selection(lastMousePos)
619 self._update_selection(
620 self._cachedCenterPosition + (lastMousePos - self._mousePosition),
624 if lastSelection != self._selectionIndex:
625 self.highlighted.emit(self._selectionIndex)
626 self._display.selectAt(self._selectionIndex)
628 if self._selectionIndex != PieFiling.SELECTION_CENTER and self._delayPopupTimer.isActive():
629 self._on_delayed_popup()
631 @misc_utils.log_exception(_moduleLogger)
632 def mouseReleaseEvent(self, mouseEvent):
633 self._delayPopupTimer.stop()
634 self._popupLocation = None
636 lastSelection = self._selectionIndex
638 lastMousePos = mouseEvent.pos()
639 if self._mousePosition is None:
641 self._update_selection(lastMousePos)
644 self._update_selection(
645 self._cachedCenterPosition + (lastMousePos - self._mousePosition),
648 self._mousePosition = None
650 self._activate_at(self._selectionIndex)
651 self._pressed = False
655 @misc_utils.log_exception(_moduleLogger)
656 def keyPressEvent(self, keyEvent):
657 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
658 self._popup_child(QtGui.QCursor.pos())
659 if self._selectionIndex != len(self._filing) - 1:
660 nextSelection = self._selectionIndex + 1
663 self._select_at(nextSelection)
664 self._display.selectAt(self._selectionIndex)
665 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
666 self._popup_child(QtGui.QCursor.pos())
667 if 0 < self._selectionIndex:
668 nextSelection = self._selectionIndex - 1
670 nextSelection = len(self._filing) - 1
671 self._select_at(nextSelection)
672 self._display.selectAt(self._selectionIndex)
673 elif keyEvent.key() in [QtCore.Qt.Key_Space]:
674 self._popup_child(QtGui.QCursor.pos())
675 self._select_at(PieFiling.SELECTION_CENTER)
676 self._display.selectAt(self._selectionIndex)
677 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
678 self._delayPopupTimer.stop()
679 self._popupLocation = None
680 self._activate_at(self._selectionIndex)
682 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
683 self._delayPopupTimer.stop()
684 self._popupLocation = None
685 self._activate_at(PieFiling.SELECTION_NONE)
688 QtGui.QWidget.keyPressEvent(self, keyEvent)
690 @misc_utils.log_exception(_moduleLogger)
691 def resizeEvent(self, resizeEvent):
692 self.setButtonRadius(min(resizeEvent.size().width(), resizeEvent.size().height()) / 2 - 1)
693 QtGui.QWidget.resizeEvent(self, resizeEvent)
695 @misc_utils.log_exception(_moduleLogger)
696 def showEvent(self, showEvent):
697 self._buttonArtist.show(self.palette())
698 self._cachedCenterPosition = self.rect().center()
700 QtGui.QWidget.showEvent(self, showEvent)
702 @misc_utils.log_exception(_moduleLogger)
703 def hideEvent(self, hideEvent):
705 self._select_at(PieFiling.SELECTION_NONE)
706 QtGui.QWidget.hideEvent(self, hideEvent)
708 @misc_utils.log_exception(_moduleLogger)
709 def paintEvent(self, paintEvent):
710 self.setButtonRadius(min(self.rect().width(), self.rect().height()) / 2 - 1)
712 selectionIndex = PieFiling.SELECTION_CENTER
714 selectionIndex = PieFiling.SELECTION_NONE
716 screen = QtGui.QStylePainter(self)
717 screen.setRenderHint(QtGui.QPainter.Antialiasing, True)
718 option = QtGui.QStyleOptionButton()
719 option.initFrom(self)
720 option.state = QtGui.QStyle.State_Sunken if self._pressed else QtGui.QStyle.State_Raised
722 screen.drawControl(QtGui.QStyle.CE_PushButton, option)
723 self._buttonArtist.paintPainter(selectionIndex, screen)
725 QtGui.QWidget.paintEvent(self, paintEvent)
728 return iter(self._filing)
731 return len(self._filing)
733 def _popup_child(self, position):
734 self._poppedUp = True
735 self.aboutToShow.emit()
737 self._delayPopupTimer.stop()
738 self._popupLocation = None
740 position = position - QtCore.QPoint(self._filing.outerRadius(), self._filing.outerRadius())
741 self._display.move(position)
746 def _hide_child(self):
747 self._poppedUp = False
748 self.aboutToHide.emit()
752 def _select_at(self, index):
753 self._selectionIndex = index
755 def _update_selection(self, lastMousePos, ignoreOuter = False):
756 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
757 if radius < self._filing.innerRadius():
758 self._select_at(PieFiling.SELECTION_CENTER)
759 elif radius <= self._filing.outerRadius() or ignoreOuter:
760 self._select_at(self.indexAt(lastMousePos))
762 self._select_at(PieFiling.SELECTION_NONE)
764 def _activate_at(self, index):
765 if index == PieFiling.SELECTION_NONE:
768 elif index == PieFiling.SELECTION_CENTER:
769 child = self._filing.center()
771 child = self.itemAt(index)
773 if child.action().isEnabled():
774 child.action().trigger()
775 self.activated.emit(index)
780 class QPieMenu(QtGui.QWidget):
782 activated = qt_compat.Signal(int)
783 highlighted = qt_compat.Signal(int)
784 canceled = qt_compat.Signal()
785 aboutToShow = qt_compat.Signal()
786 aboutToHide = qt_compat.Signal()
788 def __init__(self, parent = None):
789 QtGui.QWidget.__init__(self, parent)
790 self._cachedCenterPosition = self.rect().center()
792 self._filing = PieFiling()
793 self._artist = PieArtist(self._filing)
794 self._selectionIndex = PieFiling.SELECTION_NONE
796 self._mousePosition = ()
797 self.setFocusPolicy(QtCore.Qt.StrongFocus)
799 def popup(self, pos):
800 self._update_selection(pos)
803 def insertItem(self, item, index = -1):
804 self._filing.insertItem(item, index)
807 def removeItemAt(self, index):
808 self._filing.removeItemAt(index)
811 def set_center(self, item):
812 self._filing.set_center(item)
819 def itemAt(self, index):
820 return self._filing.itemAt(index)
822 def indexAt(self, point):
823 return self._filing.indexAt(self._cachedCenterPosition, point)
825 def innerRadius(self):
826 return self._filing.innerRadius()
828 def setInnerRadius(self, radius):
829 self._filing.setInnerRadius(radius)
832 def outerRadius(self):
833 return self._filing.outerRadius()
835 def setOuterRadius(self, radius):
836 self._filing.setOuterRadius(radius)
840 return self._artist.pieSize()
842 @misc_utils.log_exception(_moduleLogger)
843 def mousePressEvent(self, mouseEvent):
844 lastSelection = self._selectionIndex
846 lastMousePos = mouseEvent.pos()
847 self._update_selection(lastMousePos)
848 self._mousePosition = lastMousePos
850 if lastSelection != self._selectionIndex:
851 self.highlighted.emit(self._selectionIndex)
854 @misc_utils.log_exception(_moduleLogger)
855 def mouseMoveEvent(self, mouseEvent):
856 lastSelection = self._selectionIndex
858 lastMousePos = mouseEvent.pos()
859 self._update_selection(lastMousePos)
861 if lastSelection != self._selectionIndex:
862 self.highlighted.emit(self._selectionIndex)
865 @misc_utils.log_exception(_moduleLogger)
866 def mouseReleaseEvent(self, mouseEvent):
867 lastSelection = self._selectionIndex
869 lastMousePos = mouseEvent.pos()
870 self._update_selection(lastMousePos)
871 self._mousePosition = ()
873 self._activate_at(self._selectionIndex)
876 @misc_utils.log_exception(_moduleLogger)
877 def keyPressEvent(self, keyEvent):
878 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
879 if self._selectionIndex != len(self._filing) - 1:
880 nextSelection = self._selectionIndex + 1
883 self._select_at(nextSelection)
885 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
886 if 0 < self._selectionIndex:
887 nextSelection = self._selectionIndex - 1
889 nextSelection = len(self._filing) - 1
890 self._select_at(nextSelection)
892 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
893 self._activate_at(self._selectionIndex)
894 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
895 self._activate_at(PieFiling.SELECTION_NONE)
897 QtGui.QWidget.keyPressEvent(self, keyEvent)
899 @misc_utils.log_exception(_moduleLogger)
900 def showEvent(self, showEvent):
901 self.aboutToShow.emit()
902 self._cachedCenterPosition = self.rect().center()
904 mask = self._artist.show(self.palette())
907 lastMousePos = self.mapFromGlobal(QtGui.QCursor.pos())
908 self._update_selection(lastMousePos)
910 QtGui.QWidget.showEvent(self, showEvent)
912 @misc_utils.log_exception(_moduleLogger)
913 def hideEvent(self, hideEvent):
915 self._selectionIndex = PieFiling.SELECTION_NONE
916 QtGui.QWidget.hideEvent(self, hideEvent)
918 @misc_utils.log_exception(_moduleLogger)
919 def paintEvent(self, paintEvent):
920 canvas = self._artist.paint(self._selectionIndex)
922 screen = QtGui.QPainter(self)
923 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
925 QtGui.QWidget.paintEvent(self, paintEvent)
928 return iter(self._filing)
931 return len(self._filing)
933 def _select_at(self, index):
934 self._selectionIndex = index
936 def _update_selection(self, lastMousePos):
937 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
938 if radius < self._filing.innerRadius():
939 self._selectionIndex = PieFiling.SELECTION_CENTER
940 elif radius <= self._filing.outerRadius():
941 self._select_at(self.indexAt(lastMousePos))
943 self._selectionIndex = PieFiling.SELECTION_NONE
945 def _activate_at(self, index):
946 if index == PieFiling.SELECTION_NONE:
948 self.aboutToHide.emit()
951 elif index == PieFiling.SELECTION_CENTER:
952 child = self._filing.center()
954 child = self.itemAt(index)
956 if child.isEnabled():
957 child.action().trigger()
958 self.activated.emit(index)
961 self.aboutToHide.emit()
966 PieFiling.NULL_CENTER.setEnabled(False)
973 def _on_about_to_hide(app):
977 if __name__ == "__main__":
978 app = QtGui.QApplication([])
986 singleAction = QtGui.QAction(None)
987 singleAction.setText("Boo")
988 singleItem = QActionPieItem(singleAction)
990 spie.insertItem(singleItem)
994 oneAction = QtGui.QAction(None)
995 oneAction.setText("Chew")
996 oneItem = QActionPieItem(oneAction)
997 twoAction = QtGui.QAction(None)
998 twoAction.setText("Foo")
999 twoItem = QActionPieItem(twoAction)
1000 iconTextAction = QtGui.QAction(None)
1001 iconTextAction.setText("Icon")
1002 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1003 iconTextItem = QActionPieItem(iconTextAction)
1005 mpie.insertItem(oneItem)
1006 mpie.insertItem(twoItem)
1007 mpie.insertItem(oneItem)
1008 mpie.insertItem(iconTextItem)
1012 oneAction = QtGui.QAction(None)
1013 oneAction.setText("Chew")
1014 oneAction.triggered.connect(lambda: _print("Chew"))
1015 oneItem = QActionPieItem(oneAction)
1016 twoAction = QtGui.QAction(None)
1017 twoAction.setText("Foo")
1018 twoAction.triggered.connect(lambda: _print("Foo"))
1019 twoItem = QActionPieItem(twoAction)
1020 iconAction = QtGui.QAction(None)
1021 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
1022 iconAction.triggered.connect(lambda: _print("Icon"))
1023 iconItem = QActionPieItem(iconAction)
1024 iconTextAction = QtGui.QAction(None)
1025 iconTextAction.setText("Icon")
1026 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1027 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
1028 iconTextItem = QActionPieItem(iconTextAction)
1030 mpie.set_center(iconItem)
1031 mpie.insertItem(oneItem)
1032 mpie.insertItem(twoItem)
1033 mpie.insertItem(oneItem)
1034 mpie.insertItem(iconTextItem)
1036 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
1037 mpie.canceled.connect(lambda: _print("Canceled"))
1040 oneAction = QtGui.QAction(None)
1041 oneAction.setText("Chew")
1042 oneAction.triggered.connect(lambda: _print("Chew"))
1043 oneItem = QActionPieItem(oneAction)
1044 twoAction = QtGui.QAction(None)
1045 twoAction.setText("Foo")
1046 twoAction.triggered.connect(lambda: _print("Foo"))
1047 twoItem = QActionPieItem(twoAction)
1048 iconAction = QtGui.QAction(None)
1049 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
1050 iconAction.triggered.connect(lambda: _print("Icon"))
1051 iconItem = QActionPieItem(iconAction)
1052 iconTextAction = QtGui.QAction(None)
1053 iconTextAction.setText("Icon")
1054 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1055 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
1056 iconTextItem = QActionPieItem(iconTextAction)
1057 pieFiling = PieFiling()
1058 pieFiling.set_center(iconItem)
1059 pieFiling.insertItem(oneItem)
1060 pieFiling.insertItem(twoItem)
1061 pieFiling.insertItem(oneItem)
1062 pieFiling.insertItem(iconTextItem)
1063 mpie = QPieDisplay(pieFiling)
1067 oneAction = QtGui.QAction(None)
1068 oneAction.setText("Chew")
1069 oneAction.triggered.connect(lambda: _print("Chew"))
1070 oneItem = QActionPieItem(oneAction)
1071 twoAction = QtGui.QAction(None)
1072 twoAction.setText("Foo")
1073 twoAction.triggered.connect(lambda: _print("Foo"))
1074 twoItem = QActionPieItem(twoAction)
1075 iconAction = QtGui.QAction(None)
1076 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
1077 iconAction.triggered.connect(lambda: _print("Icon"))
1078 iconItem = QActionPieItem(iconAction)
1079 iconTextAction = QtGui.QAction(None)
1080 iconTextAction.setText("Icon")
1081 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
1082 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
1083 iconTextItem = QActionPieItem(iconTextAction)
1084 mpie = QPieButton(iconItem)
1085 mpie.set_center(iconItem)
1086 mpie.insertItem(oneItem)
1087 mpie.insertItem(twoItem)
1088 mpie.insertItem(oneItem)
1089 mpie.insertItem(iconTextItem)
1091 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
1092 mpie.canceled.connect(lambda: _print("Canceled"))