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 = 32
75 OUTER_RADIUS_DEFAULT = 128
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 = 32
197 def __init__(self, filing):
198 self._filing = filing
200 self._cachedOuterRadius = self._filing.outerRadius()
201 self._cachedInnerRadius = self._filing.innerRadius()
202 canvasSize = self._cachedOuterRadius * 2 + 1
203 self._canvas = QtGui.QPixmap(canvasSize, canvasSize)
208 diameter = self._filing.outerRadius() * 2 + 1
209 return QtCore.QSize(diameter, diameter)
211 def centerSize(self):
212 painter = QtGui.QPainter(self._canvas)
213 text = self._filing.center().action().text()
214 fontMetrics = painter.fontMetrics()
216 textBoundingRect = fontMetrics.boundingRect(text)
218 textBoundingRect = QtCore.QRect()
219 textWidth = textBoundingRect.width()
220 textHeight = textBoundingRect.height()
223 textWidth + self.ICON_SIZE_DEFAULT,
224 max(textHeight, self.ICON_SIZE_DEFAULT),
227 def show(self, palette):
228 self.palette = palette
231 self._cachedOuterRadius != self._filing.outerRadius() or
232 self._cachedInnerRadius != self._filing.innerRadius()
234 self._cachedOuterRadius = self._filing.outerRadius()
235 self._cachedInnerRadius = self._filing.innerRadius()
236 self._canvas = self._canvas.scaled(self.pieSize())
238 if self._mask is None:
239 self._mask = QtGui.QBitmap(self._canvas.size())
240 self._mask.fill(QtCore.Qt.color0)
241 self._generate_mask(self._mask)
242 self._canvas.setMask(self._mask)
248 def paint(self, selectionIndex):
249 painter = QtGui.QPainter(self._canvas)
250 painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
252 adjustmentRect = self._canvas.rect().adjusted(0, 0, -1, -1)
254 numChildren = len(self._filing)
256 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
257 painter.setBrush(self.palette.highlight())
259 painter.setBrush(self.palette.background())
260 painter.setPen(self.palette.mid().color())
262 painter.drawRect(self._canvas.rect())
263 self._paint_center_foreground(painter, selectionIndex)
265 elif numChildren == 1:
266 if selectionIndex == 0 and self._filing[0].isEnabled():
267 painter.setBrush(self.palette.highlight())
269 painter.setBrush(self.palette.background())
271 painter.fillRect(self._canvas.rect(), painter.brush())
273 for i in xrange(len(self._filing)):
274 self._paint_slice_background(painter, adjustmentRect, i, selectionIndex)
276 self._paint_center_background(painter, adjustmentRect, selectionIndex)
277 self._paint_center_foreground(painter, selectionIndex)
279 for i in xrange(len(self._filing)):
280 self._paint_slice_foreground(painter, i, selectionIndex)
284 def _generate_mask(self, mask):
286 Specifies on the mask the shape of the pie menu
288 painter = QtGui.QPainter(mask)
289 painter.setPen(QtCore.Qt.color1)
290 painter.setBrush(QtCore.Qt.color1)
291 painter.drawEllipse(mask.rect().adjusted(0, 0, -1, -1))
293 def _paint_slice_background(self, painter, adjustmentRect, i, selectionIndex):
294 if i == selectionIndex and self._filing[i].isEnabled():
295 painter.setBrush(self.palette.highlight())
297 painter.setBrush(self.palette.background())
298 painter.setPen(self.palette.mid().color())
300 a = self._filing._index_to_angle(i, True)
301 b = self._filing._index_to_angle(i + 1, True)
308 startAngleInDeg = (a * 360 * 16) / _TWOPI
309 sizeInDeg = (size * 360 * 16) / _TWOPI
310 painter.drawPie(adjustmentRect, int(startAngleInDeg), int(sizeInDeg))
312 def _paint_slice_foreground(self, painter, i, selectionIndex):
313 child = self._filing[i]
315 a = self._filing._index_to_angle(i, True)
316 b = self._filing._index_to_angle(i + 1, True)
319 middleAngle = (a + b) / 2
320 averageRadius = (self._cachedInnerRadius + self._cachedOuterRadius) / 2
322 sliceX = averageRadius * math.cos(middleAngle)
323 sliceY = - averageRadius * math.sin(middleAngle)
325 piePos = self._canvas.rect().center()
329 painter, child.action(), i == selectionIndex, pieX+sliceX, pieY+sliceY
332 def _paint_label(self, painter, action, isSelected, x, y):
334 fontMetrics = painter.fontMetrics()
336 textBoundingRect = fontMetrics.boundingRect(text)
338 textBoundingRect = QtCore.QRect()
339 textWidth = textBoundingRect.width()
340 textHeight = textBoundingRect.height()
342 icon = action.icon().pixmap(
343 QtCore.QSize(self.ICON_SIZE_DEFAULT, self.ICON_SIZE_DEFAULT),
347 iconWidth = icon.width()
348 iconHeight = icon.width()
349 averageWidth = (iconWidth + textWidth)/2
350 if not icon.isNull():
351 iconRect = QtCore.QRect(
358 painter.drawPixmap(iconRect, icon)
362 if action.isEnabled():
363 pen = self.palette.highlightedText()
364 brush = self.palette.highlight()
366 pen = self.palette.mid()
367 brush = self.palette.background()
369 if action.isEnabled():
370 pen = self.palette.text()
372 pen = self.palette.mid()
373 brush = self.palette.background()
375 leftX = x - averageWidth + iconWidth
376 topY = y + textHeight/2
377 painter.setPen(pen.color())
378 painter.setBrush(brush)
379 painter.drawText(leftX, topY, text)
381 def _paint_center_background(self, painter, adjustmentRect, selectionIndex):
382 dark = self.palette.dark().color()
383 light = self.palette.light().color()
384 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
385 background = self.palette.highlight().color()
387 background = self.palette.background().color()
389 innerRadius = self._cachedInnerRadius
390 adjustmentCenterPos = adjustmentRect.center()
391 innerRect = QtCore.QRect(
392 adjustmentCenterPos.x() - innerRadius,
393 adjustmentCenterPos.y() - innerRadius,
398 painter.setPen(QtCore.Qt.NoPen)
399 painter.setBrush(background)
400 painter.drawPie(innerRect, 0, 360 * 16)
402 painter.setPen(QtGui.QPen(dark, 1))
403 painter.setBrush(QtCore.Qt.NoBrush)
404 painter.drawEllipse(innerRect)
406 painter.setPen(QtGui.QPen(dark, 1))
407 painter.setBrush(QtCore.Qt.NoBrush)
408 painter.drawEllipse(adjustmentRect)
410 r = QtCore.QRect(innerRect)
411 innerCenter = r.center()
412 innerRect.setLeft(innerCenter.x() + ((r.left() - innerCenter.x()) / 3) * 1)
413 innerRect.setRight(innerCenter.x() + ((r.right() - innerCenter.x()) / 3) * 1)
414 innerRect.setTop(innerCenter.y() + ((r.top() - innerCenter.y()) / 3) * 1)
415 innerRect.setBottom(innerCenter.y() + ((r.bottom() - innerCenter.y()) / 3) * 1)
417 def _paint_center_foreground(self, painter, selectionIndex):
418 centerPos = self._canvas.rect().center()
427 self._filing.center().action(),
428 selectionIndex == PieFiling.SELECTION_CENTER,
433 class QPieDisplay(QtGui.QWidget):
435 def __init__(self, filing, parent = None, flags = QtCore.Qt.Window):
436 QtGui.QWidget.__init__(self, parent, flags)
437 self._filing = filing
438 self._artist = PieArtist(self._filing)
439 self._selectionIndex = PieFiling.SELECTION_NONE
441 def popup(self, pos):
442 self._update_selection(pos)
446 return self._artist.pieSize()
448 @misc_utils.log_exception(_moduleLogger)
449 def showEvent(self, showEvent):
450 mask = self._artist.show(self.palette())
453 QtGui.QWidget.showEvent(self, showEvent)
455 @misc_utils.log_exception(_moduleLogger)
456 def hideEvent(self, hideEvent):
458 self._selectionIndex = PieFiling.SELECTION_NONE
459 QtGui.QWidget.hideEvent(self, hideEvent)
461 @misc_utils.log_exception(_moduleLogger)
462 def paintEvent(self, paintEvent):
463 canvas = self._artist.paint(self._selectionIndex)
465 screen = QtGui.QPainter(self)
466 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
468 QtGui.QWidget.paintEvent(self, paintEvent)
470 def selectAt(self, index):
471 self._selectionIndex = index
475 class QPieButton(QtGui.QWidget):
477 activated = QtCore.pyqtSignal(int)
478 highlighted = QtCore.pyqtSignal(int)
479 canceled = QtCore.pyqtSignal()
480 aboutToShow = QtCore.pyqtSignal()
481 aboutToHide = QtCore.pyqtSignal()
485 def __init__(self, buttonSlice, parent = None):
486 QtGui.QWidget.__init__(self, parent)
487 self._cachedCenterPosition = self.rect().center()
489 self._filing = PieFiling()
490 self._display = QPieDisplay(self._filing, None, QtCore.Qt.SplashScreen)
491 self._selectionIndex = PieFiling.SELECTION_NONE
493 self._buttonFiling = PieFiling()
494 self._buttonFiling.set_center(buttonSlice)
495 # @todo Figure out how to make the button auto-fill to content
496 self._buttonFiling.setOuterRadius(self.BUTTON_RADIUS)
497 self._buttonArtist = PieArtist(self._buttonFiling)
498 self._poppedUp = False
500 self._mousePosition = None
501 self.setFocusPolicy(QtCore.Qt.StrongFocus)
503 def insertItem(self, item, index = -1):
504 self._filing.insertItem(item, index)
506 def removeItemAt(self, index):
507 self._filing.removeItemAt(index)
509 def set_center(self, item):
510 self._filing.set_center(item)
512 def set_button(self, item):
518 def itemAt(self, index):
519 return self._filing.itemAt(index)
521 def indexAt(self, point):
522 return self._filing.indexAt(self._cachedCenterPosition, point)
524 def innerRadius(self):
525 return self._filing.innerRadius()
527 def setInnerRadius(self, radius):
528 self._filing.setInnerRadius(radius)
530 def outerRadius(self):
531 return self._filing.outerRadius()
533 def setOuterRadius(self, radius):
534 self._filing.setOuterRadius(radius)
536 def buttonRadius(self):
537 return self._buttonFiling.outerRadius()
539 def setButtonRadius(self, radius):
540 self._buttonFiling.setOuterRadius(radius)
543 return self._buttonArtist.pieSize()
545 @misc_utils.log_exception(_moduleLogger)
546 def mousePressEvent(self, mouseEvent):
547 self._popup_child(mouseEvent.globalPos())
548 lastSelection = self._selectionIndex
550 lastMousePos = mouseEvent.pos()
551 self._mousePosition = lastMousePos
552 self._update_selection(self._cachedCenterPosition)
554 if lastSelection != self._selectionIndex:
555 self.highlighted.emit(self._selectionIndex)
556 self._display.selectAt(self._selectionIndex)
558 @misc_utils.log_exception(_moduleLogger)
559 def mouseMoveEvent(self, mouseEvent):
560 lastSelection = self._selectionIndex
562 lastMousePos = mouseEvent.pos()
563 if self._mousePosition is None:
565 self._update_selection(lastMousePos)
568 self._update_selection(self._cachedCenterPosition + (lastMousePos - self._mousePosition))
570 if lastSelection != self._selectionIndex:
571 self.highlighted.emit(self._selectionIndex)
572 self._display.selectAt(self._selectionIndex)
574 @misc_utils.log_exception(_moduleLogger)
575 def mouseReleaseEvent(self, mouseEvent):
576 lastSelection = self._selectionIndex
578 lastMousePos = mouseEvent.pos()
579 if self._mousePosition is None:
581 self._update_selection(lastMousePos)
584 self._update_selection(self._cachedCenterPosition + (lastMousePos - self._mousePosition))
585 self._mousePosition = None
587 self._activate_at(self._selectionIndex)
590 @misc_utils.log_exception(_moduleLogger)
591 def keyPressEvent(self, keyEvent):
592 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
593 self._popup_child(QtGui.QCursor.pos())
594 if self._selectionIndex != len(self._filing) - 1:
595 nextSelection = self._selectionIndex + 1
598 self._select_at(nextSelection)
599 self._display.selectAt(self._selectionIndex)
600 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
601 self._popup_child(QtGui.QCursor.pos())
602 if 0 < self._selectionIndex:
603 nextSelection = self._selectionIndex - 1
605 nextSelection = len(self._filing) - 1
606 self._select_at(nextSelection)
607 self._display.selectAt(self._selectionIndex)
608 elif keyEvent.key() in [QtCore.Qt.Key_Space]:
609 self._popup_child(QtGui.QCursor.pos())
610 self._select_at(PieFiling.SELECTION_CENTER)
611 self._display.selectAt(self._selectionIndex)
612 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
613 self._activate_at(self._selectionIndex)
615 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
616 self._activate_at(PieFiling.SELECTION_NONE)
619 QtGui.QWidget.keyPressEvent(self, keyEvent)
621 @misc_utils.log_exception(_moduleLogger)
622 def showEvent(self, showEvent):
623 self._buttonArtist.show(self.palette())
624 self._cachedCenterPosition = self.rect().center()
626 QtGui.QWidget.showEvent(self, showEvent)
628 @misc_utils.log_exception(_moduleLogger)
629 def hideEvent(self, hideEvent):
631 self._select_at(PieFiling.SELECTION_NONE)
632 QtGui.QWidget.hideEvent(self, hideEvent)
634 @misc_utils.log_exception(_moduleLogger)
635 def paintEvent(self, paintEvent):
637 canvas = self._buttonArtist.paint(PieFiling.SELECTION_CENTER)
639 canvas = self._buttonArtist.paint(PieFiling.SELECTION_NONE)
641 screen = QtGui.QPainter(self)
642 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
644 QtGui.QWidget.paintEvent(self, paintEvent)
647 return iter(self._filing)
650 return len(self._filing)
652 def _popup_child(self, position):
653 self._poppedUp = True
654 self.aboutToShow.emit()
656 position = position - QtCore.QPoint(self._filing.outerRadius(), self._filing.outerRadius())
657 self._display.move(position)
662 def _hide_child(self):
663 self._poppedUp = False
664 self.aboutToHide.emit()
668 def _select_at(self, index):
669 self._selectionIndex = index
671 def _update_selection(self, lastMousePos):
672 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
673 if radius < self._filing.innerRadius():
674 self._select_at(PieFiling.SELECTION_CENTER)
675 elif radius <= self._filing.outerRadius():
676 self._select_at(self.indexAt(lastMousePos))
678 self._select_at(PieFiling.SELECTION_NONE)
680 def _activate_at(self, index):
681 if index == PieFiling.SELECTION_NONE:
684 elif index == PieFiling.SELECTION_CENTER:
685 child = self._filing.center()
687 child = self.itemAt(index)
689 if child.action().isEnabled():
690 child.action().trigger()
691 self.activated.emit(index)
696 class QPieMenu(QtGui.QWidget):
698 activated = QtCore.pyqtSignal(int)
699 highlighted = QtCore.pyqtSignal(int)
700 canceled = QtCore.pyqtSignal()
701 aboutToShow = QtCore.pyqtSignal()
702 aboutToHide = QtCore.pyqtSignal()
704 def __init__(self, parent = None):
705 QtGui.QWidget.__init__(self, parent)
706 self._cachedCenterPosition = self.rect().center()
708 self._filing = PieFiling()
709 self._artist = PieArtist(self._filing)
710 self._selectionIndex = PieFiling.SELECTION_NONE
712 self._mousePosition = ()
713 self.setFocusPolicy(QtCore.Qt.StrongFocus)
715 def popup(self, pos):
716 self._update_selection(pos)
719 def insertItem(self, item, index = -1):
720 self._filing.insertItem(item, index)
723 def removeItemAt(self, index):
724 self._filing.removeItemAt(index)
727 def set_center(self, item):
728 self._filing.set_center(item)
735 def itemAt(self, index):
736 return self._filing.itemAt(index)
738 def indexAt(self, point):
739 return self._filing.indexAt(self._cachedCenterPosition, point)
741 def innerRadius(self):
742 return self._filing.innerRadius()
744 def setInnerRadius(self, radius):
745 self._filing.setInnerRadius(radius)
748 def outerRadius(self):
749 return self._filing.outerRadius()
751 def setOuterRadius(self, radius):
752 self._filing.setOuterRadius(radius)
756 return self._artist.pieSize()
758 @misc_utils.log_exception(_moduleLogger)
759 def mousePressEvent(self, mouseEvent):
760 lastSelection = self._selectionIndex
762 lastMousePos = mouseEvent.pos()
763 self._update_selection(lastMousePos)
764 self._mousePosition = lastMousePos
766 if lastSelection != self._selectionIndex:
767 self.highlighted.emit(self._selectionIndex)
770 @misc_utils.log_exception(_moduleLogger)
771 def mouseMoveEvent(self, mouseEvent):
772 lastSelection = self._selectionIndex
774 lastMousePos = mouseEvent.pos()
775 self._update_selection(lastMousePos)
777 if lastSelection != self._selectionIndex:
778 self.highlighted.emit(self._selectionIndex)
781 @misc_utils.log_exception(_moduleLogger)
782 def mouseReleaseEvent(self, mouseEvent):
783 lastSelection = self._selectionIndex
785 lastMousePos = mouseEvent.pos()
786 self._update_selection(lastMousePos)
787 self._mousePosition = ()
789 self._activate_at(self._selectionIndex)
792 @misc_utils.log_exception(_moduleLogger)
793 def keyPressEvent(self, keyEvent):
794 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
795 if self._selectionIndex != len(self._filing) - 1:
796 nextSelection = self._selectionIndex + 1
799 self._select_at(nextSelection)
801 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
802 if 0 < self._selectionIndex:
803 nextSelection = self._selectionIndex - 1
805 nextSelection = len(self._filing) - 1
806 self._select_at(nextSelection)
808 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
809 self._activate_at(self._selectionIndex)
810 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
811 self._activate_at(PieFiling.SELECTION_NONE)
813 QtGui.QWidget.keyPressEvent(self, keyEvent)
815 @misc_utils.log_exception(_moduleLogger)
816 def showEvent(self, showEvent):
817 self.aboutToShow.emit()
818 self._cachedCenterPosition = self.rect().center()
820 mask = self._artist.show(self.palette())
823 lastMousePos = self.mapFromGlobal(QtGui.QCursor.pos())
824 self._update_selection(lastMousePos)
826 QtGui.QWidget.showEvent(self, showEvent)
828 @misc_utils.log_exception(_moduleLogger)
829 def hideEvent(self, hideEvent):
831 self._selectionIndex = PieFiling.SELECTION_NONE
832 QtGui.QWidget.hideEvent(self, hideEvent)
834 @misc_utils.log_exception(_moduleLogger)
835 def paintEvent(self, paintEvent):
836 canvas = self._artist.paint(self._selectionIndex)
838 screen = QtGui.QPainter(self)
839 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
841 QtGui.QWidget.paintEvent(self, paintEvent)
844 return iter(self._filing)
847 return len(self._filing)
849 def _select_at(self, index):
850 self._selectionIndex = index
852 def _update_selection(self, lastMousePos):
853 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
854 if radius < self._filing.innerRadius():
855 self._selectionIndex = PieFiling.SELECTION_CENTER
856 elif radius <= self._filing.outerRadius():
857 self._select_at(self.indexAt(lastMousePos))
859 self._selectionIndex = PieFiling.SELECTION_NONE
861 def _activate_at(self, index):
862 if index == PieFiling.SELECTION_NONE:
864 self.aboutToHide.emit()
867 elif index == PieFiling.SELECTION_CENTER:
868 child = self._filing.center()
870 child = self.itemAt(index)
872 if child.isEnabled():
873 child.action().trigger()
874 self.activated.emit(index)
877 self.aboutToHide.emit()
882 PieFiling.NULL_CENTER.setEnabled(False)
889 def _on_about_to_hide(app):
893 if __name__ == "__main__":
894 app = QtGui.QApplication([])
902 singleAction = QtGui.QAction(None)
903 singleAction.setText("Boo")
904 singleItem = QActionPieItem(singleAction)
906 spie.insertItem(singleItem)
910 oneAction = QtGui.QAction(None)
911 oneAction.setText("Chew")
912 oneItem = QActionPieItem(oneAction)
913 twoAction = QtGui.QAction(None)
914 twoAction.setText("Foo")
915 twoItem = QActionPieItem(twoAction)
916 iconTextAction = QtGui.QAction(None)
917 iconTextAction.setText("Icon")
918 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
919 iconTextItem = QActionPieItem(iconTextAction)
921 mpie.insertItem(oneItem)
922 mpie.insertItem(twoItem)
923 mpie.insertItem(oneItem)
924 mpie.insertItem(iconTextItem)
928 oneAction = QtGui.QAction(None)
929 oneAction.setText("Chew")
930 oneAction.triggered.connect(lambda: _print("Chew"))
931 oneItem = QActionPieItem(oneAction)
932 twoAction = QtGui.QAction(None)
933 twoAction.setText("Foo")
934 twoAction.triggered.connect(lambda: _print("Foo"))
935 twoItem = QActionPieItem(twoAction)
936 iconAction = QtGui.QAction(None)
937 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
938 iconAction.triggered.connect(lambda: _print("Icon"))
939 iconItem = QActionPieItem(iconAction)
940 iconTextAction = QtGui.QAction(None)
941 iconTextAction.setText("Icon")
942 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
943 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
944 iconTextItem = QActionPieItem(iconTextAction)
946 mpie.set_center(iconItem)
947 mpie.insertItem(oneItem)
948 mpie.insertItem(twoItem)
949 mpie.insertItem(oneItem)
950 mpie.insertItem(iconTextItem)
952 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
953 mpie.canceled.connect(lambda: _print("Canceled"))
956 oneAction = QtGui.QAction(None)
957 oneAction.setText("Chew")
958 oneAction.triggered.connect(lambda: _print("Chew"))
959 oneItem = QActionPieItem(oneAction)
960 twoAction = QtGui.QAction(None)
961 twoAction.setText("Foo")
962 twoAction.triggered.connect(lambda: _print("Foo"))
963 twoItem = QActionPieItem(twoAction)
964 iconAction = QtGui.QAction(None)
965 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
966 iconAction.triggered.connect(lambda: _print("Icon"))
967 iconItem = QActionPieItem(iconAction)
968 iconTextAction = QtGui.QAction(None)
969 iconTextAction.setText("Icon")
970 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
971 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
972 iconTextItem = QActionPieItem(iconTextAction)
973 pieFiling = PieFiling()
974 pieFiling.set_center(iconItem)
975 pieFiling.insertItem(oneItem)
976 pieFiling.insertItem(twoItem)
977 pieFiling.insertItem(oneItem)
978 pieFiling.insertItem(iconTextItem)
979 mpie = QPieDisplay(pieFiling)
983 oneAction = QtGui.QAction(None)
984 oneAction.setText("Chew")
985 oneAction.triggered.connect(lambda: _print("Chew"))
986 oneItem = QActionPieItem(oneAction)
987 twoAction = QtGui.QAction(None)
988 twoAction.setText("Foo")
989 twoAction.triggered.connect(lambda: _print("Foo"))
990 twoItem = QActionPieItem(twoAction)
991 iconAction = QtGui.QAction(None)
992 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
993 iconAction.triggered.connect(lambda: _print("Icon"))
994 iconItem = QActionPieItem(iconAction)
995 iconTextAction = QtGui.QAction(None)
996 iconTextAction.setText("Icon")
997 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
998 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
999 iconTextItem = QActionPieItem(iconTextAction)
1000 mpie = QPieButton(iconItem)
1001 mpie.set_center(iconItem)
1002 mpie.insertItem(oneItem)
1003 mpie.insertItem(twoItem)
1004 mpie.insertItem(oneItem)
1005 mpie.insertItem(iconTextItem)
1007 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
1008 mpie.canceled.connect(lambda: _print("Canceled"))