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())
261 painter.fillRect(self._canvas.rect(), painter.brush())
262 self._paint_center_foreground(painter, selectionIndex)
264 elif numChildren == 1:
265 if selectionIndex == 0 and self._filing[0].isEnabled():
266 painter.setBrush(self.palette.highlight())
268 painter.setBrush(self.palette.background())
270 painter.fillRect(self._canvas.rect(), painter.brush())
272 for i in xrange(len(self._filing)):
273 self._paint_slice_background(painter, adjustmentRect, i, selectionIndex)
275 self._paint_center_background(painter, adjustmentRect, selectionIndex)
276 self._paint_center_foreground(painter, selectionIndex)
278 for i in xrange(len(self._filing)):
279 self._paint_slice_foreground(painter, i, selectionIndex)
283 def _generate_mask(self, mask):
285 Specifies on the mask the shape of the pie menu
287 painter = QtGui.QPainter(mask)
288 painter.setPen(QtCore.Qt.color1)
289 painter.setBrush(QtCore.Qt.color1)
290 painter.drawEllipse(mask.rect().adjusted(0, 0, -1, -1))
292 def _paint_slice_background(self, painter, adjustmentRect, i, selectionIndex):
293 if i == selectionIndex and self._filing[i].isEnabled():
294 painter.setBrush(self.palette.highlight())
296 painter.setBrush(self.palette.background())
297 painter.setPen(self.palette.mid().color())
299 a = self._filing._index_to_angle(i, True)
300 b = self._filing._index_to_angle(i + 1, True)
307 startAngleInDeg = (a * 360 * 16) / _TWOPI
308 sizeInDeg = (size * 360 * 16) / _TWOPI
309 painter.drawPie(adjustmentRect, int(startAngleInDeg), int(sizeInDeg))
311 def _paint_slice_foreground(self, painter, i, selectionIndex):
312 child = self._filing[i]
314 a = self._filing._index_to_angle(i, True)
315 b = self._filing._index_to_angle(i + 1, True)
318 middleAngle = (a + b) / 2
319 averageRadius = (self._cachedInnerRadius + self._cachedOuterRadius) / 2
321 sliceX = averageRadius * math.cos(middleAngle)
322 sliceY = - averageRadius * math.sin(middleAngle)
324 piePos = self._canvas.rect().center()
328 painter, child.action(), i == selectionIndex, pieX+sliceX, pieY+sliceY
331 def _paint_label(self, painter, action, isSelected, x, y):
333 fontMetrics = painter.fontMetrics()
335 textBoundingRect = fontMetrics.boundingRect(text)
337 textBoundingRect = QtCore.QRect()
338 textWidth = textBoundingRect.width()
339 textHeight = textBoundingRect.height()
341 icon = action.icon().pixmap(
342 QtCore.QSize(self.ICON_SIZE_DEFAULT, self.ICON_SIZE_DEFAULT),
346 iconWidth = icon.width()
347 iconHeight = icon.width()
348 averageWidth = (iconWidth + textWidth)/2
349 if not icon.isNull():
350 iconRect = QtCore.QRect(
357 painter.drawPixmap(iconRect, icon)
361 if action.isEnabled():
362 pen = self.palette.highlightedText()
363 brush = self.palette.highlight()
365 pen = self.palette.mid()
366 brush = self.palette.background()
368 if action.isEnabled():
369 pen = self.palette.text()
371 pen = self.palette.mid()
372 brush = self.palette.background()
374 leftX = x - averageWidth + iconWidth
375 topY = y + textHeight/2
376 painter.setPen(pen.color())
377 painter.setBrush(brush)
378 painter.drawText(leftX, topY, text)
380 def _paint_center_background(self, painter, adjustmentRect, selectionIndex):
381 dark = self.palette.dark().color()
382 light = self.palette.light().color()
383 if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
384 background = self.palette.highlight().color()
386 background = self.palette.background().color()
388 innerRadius = self._cachedInnerRadius
389 adjustmentCenterPos = adjustmentRect.center()
390 innerRect = QtCore.QRect(
391 adjustmentCenterPos.x() - innerRadius,
392 adjustmentCenterPos.y() - innerRadius,
397 painter.setPen(QtCore.Qt.NoPen)
398 painter.setBrush(background)
399 painter.drawPie(innerRect, 0, 360 * 16)
401 painter.setPen(QtGui.QPen(dark, 1))
402 painter.setBrush(QtCore.Qt.NoBrush)
403 painter.drawEllipse(innerRect)
405 painter.setPen(QtGui.QPen(dark, 1))
406 painter.setBrush(QtCore.Qt.NoBrush)
407 painter.drawEllipse(adjustmentRect)
409 r = QtCore.QRect(innerRect)
410 innerCenter = r.center()
411 innerRect.setLeft(innerCenter.x() + ((r.left() - innerCenter.x()) / 3) * 1)
412 innerRect.setRight(innerCenter.x() + ((r.right() - innerCenter.x()) / 3) * 1)
413 innerRect.setTop(innerCenter.y() + ((r.top() - innerCenter.y()) / 3) * 1)
414 innerRect.setBottom(innerCenter.y() + ((r.bottom() - innerCenter.y()) / 3) * 1)
416 def _paint_center_foreground(self, painter, selectionIndex):
417 centerPos = self._canvas.rect().center()
426 self._filing.center().action(),
427 selectionIndex == PieFiling.SELECTION_CENTER,
432 class QPieDisplay(QtGui.QWidget):
434 def __init__(self, filing, parent = None, flags = QtCore.Qt.Window):
435 QtGui.QWidget.__init__(self, parent, flags)
436 self._filing = filing
437 self._artist = PieArtist(self._filing)
438 self._selectionIndex = PieFiling.SELECTION_NONE
440 def popup(self, pos):
441 self._update_selection(pos)
445 return self._artist.pieSize()
447 @misc_utils.log_exception(_moduleLogger)
448 def showEvent(self, showEvent):
449 mask = self._artist.show(self.palette())
452 QtGui.QWidget.showEvent(self, showEvent)
454 @misc_utils.log_exception(_moduleLogger)
455 def hideEvent(self, hideEvent):
457 self._selectionIndex = PieFiling.SELECTION_NONE
458 QtGui.QWidget.hideEvent(self, hideEvent)
460 @misc_utils.log_exception(_moduleLogger)
461 def paintEvent(self, paintEvent):
462 canvas = self._artist.paint(self._selectionIndex)
464 screen = QtGui.QPainter(self)
465 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
467 QtGui.QWidget.paintEvent(self, paintEvent)
469 def selectAt(self, index):
470 self._selectionIndex = index
474 class QPieButton(QtGui.QWidget):
476 activated = QtCore.pyqtSignal(int)
477 highlighted = QtCore.pyqtSignal(int)
478 canceled = QtCore.pyqtSignal()
479 aboutToShow = QtCore.pyqtSignal()
480 aboutToHide = QtCore.pyqtSignal()
482 def __init__(self, buttonSlice, parent = None):
483 QtGui.QWidget.__init__(self, parent)
484 self._cachedCenterPosition = self.rect().center()
486 self._filing = PieFiling()
487 self._display = QPieDisplay(self._filing, None, QtCore.Qt.SplashScreen)
488 self._selectionIndex = PieFiling.SELECTION_NONE
490 self._buttonFiling = PieFiling()
491 self._buttonFiling.set_center(buttonSlice)
492 self._buttonArtist = PieArtist(self._buttonFiling)
493 centerSize = self._buttonArtist.centerSize()
494 self._buttonFiling.setOuterRadius(max(centerSize.width(), centerSize.height()))
495 self._poppedUp = False
497 self._mousePosition = None
498 self.setFocusPolicy(QtCore.Qt.StrongFocus)
500 def insertItem(self, item, index = -1):
501 self._filing.insertItem(item, index)
503 def removeItemAt(self, index):
504 self._filing.removeItemAt(index)
506 def set_center(self, item):
507 self._filing.set_center(item)
509 def set_button(self, item):
515 def itemAt(self, index):
516 return self._filing.itemAt(index)
518 def indexAt(self, point):
519 return self._filing.indexAt(self._cachedCenterPosition, point)
521 def innerRadius(self):
522 return self._filing.innerRadius()
524 def setInnerRadius(self, radius):
525 self._filing.setInnerRadius(radius)
527 def outerRadius(self):
528 return self._filing.outerRadius()
530 def setOuterRadius(self, radius):
531 self._filing.setOuterRadius(radius)
534 return self._buttonArtist.pieSize()
536 @misc_utils.log_exception(_moduleLogger)
537 def mousePressEvent(self, mouseEvent):
538 self._popup_child(mouseEvent.globalPos())
539 lastSelection = self._selectionIndex
541 lastMousePos = mouseEvent.pos()
542 self._mousePosition = lastMousePos
543 self._update_selection(self._cachedCenterPosition)
545 if lastSelection != self._selectionIndex:
546 self.highlighted.emit(self._selectionIndex)
547 self._display.selectAt(self._selectionIndex)
549 @misc_utils.log_exception(_moduleLogger)
550 def mouseMoveEvent(self, mouseEvent):
551 lastSelection = self._selectionIndex
553 lastMousePos = mouseEvent.pos()
554 if self._mousePosition is None:
556 self._update_selection(lastMousePos)
559 self._update_selection(self._cachedCenterPosition + (lastMousePos - self._mousePosition))
561 if lastSelection != self._selectionIndex:
562 self.highlighted.emit(self._selectionIndex)
563 self._display.selectAt(self._selectionIndex)
565 @misc_utils.log_exception(_moduleLogger)
566 def mouseReleaseEvent(self, mouseEvent):
567 lastSelection = self._selectionIndex
569 lastMousePos = mouseEvent.pos()
570 if self._mousePosition is None:
572 self._update_selection(lastMousePos)
575 self._update_selection(self._cachedCenterPosition + (lastMousePos - self._mousePosition))
576 self._mousePosition = None
578 self._activate_at(self._selectionIndex)
581 @misc_utils.log_exception(_moduleLogger)
582 def keyPressEvent(self, keyEvent):
583 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
584 self._popup_child(QtGui.QCursor.pos())
585 if self._selectionIndex != len(self._filing) - 1:
586 nextSelection = self._selectionIndex + 1
589 self._select_at(nextSelection)
590 self._display.selectAt(self._selectionIndex)
591 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
592 self._popup_child(QtGui.QCursor.pos())
593 if 0 < self._selectionIndex:
594 nextSelection = self._selectionIndex - 1
596 nextSelection = len(self._filing) - 1
597 self._select_at(nextSelection)
598 self._display.selectAt(self._selectionIndex)
599 elif keyEvent.key() in [QtCore.Qt.Key_Space]:
600 self._popup_child(QtGui.QCursor.pos())
601 self._select_at(PieFiling.SELECTION_CENTER)
602 self._display.selectAt(self._selectionIndex)
603 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
604 self._activate_at(self._selectionIndex)
606 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
607 self._activate_at(PieFiling.SELECTION_NONE)
610 QtGui.QWidget.keyPressEvent(self, keyEvent)
612 @misc_utils.log_exception(_moduleLogger)
613 def showEvent(self, showEvent):
614 self._buttonArtist.show(self.palette())
615 self._cachedCenterPosition = self.rect().center()
617 QtGui.QWidget.showEvent(self, showEvent)
619 @misc_utils.log_exception(_moduleLogger)
620 def hideEvent(self, hideEvent):
622 self._select_at(PieFiling.SELECTION_NONE)
623 QtGui.QWidget.hideEvent(self, hideEvent)
625 @misc_utils.log_exception(_moduleLogger)
626 def paintEvent(self, paintEvent):
628 canvas = self._buttonArtist.paint(PieFiling.SELECTION_CENTER)
630 canvas = self._buttonArtist.paint(PieFiling.SELECTION_NONE)
632 screen = QtGui.QPainter(self)
633 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
635 QtGui.QWidget.paintEvent(self, paintEvent)
638 return iter(self._filing)
641 return len(self._filing)
643 def _popup_child(self, position):
644 self._poppedUp = True
645 self.aboutToShow.emit()
647 position = position - QtCore.QPoint(self._filing.outerRadius(), self._filing.outerRadius())
648 self._display.move(position)
653 def _hide_child(self):
654 self._poppedUp = False
655 self.aboutToHide.emit()
659 def _select_at(self, index):
660 self._selectionIndex = index
662 def _update_selection(self, lastMousePos):
663 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
664 if radius < self._filing.innerRadius():
665 self._select_at(PieFiling.SELECTION_CENTER)
666 elif radius <= self._filing.outerRadius():
667 self._select_at(self.indexAt(lastMousePos))
669 self._select_at(PieFiling.SELECTION_NONE)
671 def _activate_at(self, index):
672 if index == PieFiling.SELECTION_NONE:
675 elif index == PieFiling.SELECTION_CENTER:
676 child = self._filing.center()
678 child = self.itemAt(index)
680 if child.action().isEnabled():
681 child.action().trigger()
682 self.activated.emit(index)
687 class QPieMenu(QtGui.QWidget):
689 activated = QtCore.pyqtSignal(int)
690 highlighted = QtCore.pyqtSignal(int)
691 canceled = QtCore.pyqtSignal()
692 aboutToShow = QtCore.pyqtSignal()
693 aboutToHide = QtCore.pyqtSignal()
695 def __init__(self, parent = None):
696 QtGui.QWidget.__init__(self, parent)
697 self._cachedCenterPosition = self.rect().center()
699 self._filing = PieFiling()
700 self._artist = PieArtist(self._filing)
701 self._selectionIndex = PieFiling.SELECTION_NONE
703 self._mousePosition = ()
704 self.setFocusPolicy(QtCore.Qt.StrongFocus)
706 def popup(self, pos):
707 self._update_selection(pos)
710 def insertItem(self, item, index = -1):
711 self._filing.insertItem(item, index)
714 def removeItemAt(self, index):
715 self._filing.removeItemAt(index)
718 def set_center(self, item):
719 self._filing.set_center(item)
726 def itemAt(self, index):
727 return self._filing.itemAt(index)
729 def indexAt(self, point):
730 return self._filing.indexAt(self._cachedCenterPosition, point)
732 def innerRadius(self):
733 return self._filing.innerRadius()
735 def setInnerRadius(self, radius):
736 self._filing.setInnerRadius(radius)
739 def outerRadius(self):
740 return self._filing.outerRadius()
742 def setOuterRadius(self, radius):
743 self._filing.setOuterRadius(radius)
747 return self._artist.pieSize()
749 @misc_utils.log_exception(_moduleLogger)
750 def mousePressEvent(self, mouseEvent):
751 lastSelection = self._selectionIndex
753 lastMousePos = mouseEvent.pos()
754 self._update_selection(lastMousePos)
755 self._mousePosition = lastMousePos
757 if lastSelection != self._selectionIndex:
758 self.highlighted.emit(self._selectionIndex)
761 @misc_utils.log_exception(_moduleLogger)
762 def mouseMoveEvent(self, mouseEvent):
763 lastSelection = self._selectionIndex
765 lastMousePos = mouseEvent.pos()
766 self._update_selection(lastMousePos)
768 if lastSelection != self._selectionIndex:
769 self.highlighted.emit(self._selectionIndex)
772 @misc_utils.log_exception(_moduleLogger)
773 def mouseReleaseEvent(self, mouseEvent):
774 lastSelection = self._selectionIndex
776 lastMousePos = mouseEvent.pos()
777 self._update_selection(lastMousePos)
778 self._mousePosition = ()
780 self._activate_at(self._selectionIndex)
783 @misc_utils.log_exception(_moduleLogger)
784 def keyPressEvent(self, keyEvent):
785 if keyEvent.key() in [QtCore.Qt.Key_Right, QtCore.Qt.Key_Down, QtCore.Qt.Key_Tab]:
786 if self._selectionIndex != len(self._filing) - 1:
787 nextSelection = self._selectionIndex + 1
790 self._select_at(nextSelection)
792 elif keyEvent.key() in [QtCore.Qt.Key_Left, QtCore.Qt.Key_Up, QtCore.Qt.Key_Backtab]:
793 if 0 < self._selectionIndex:
794 nextSelection = self._selectionIndex - 1
796 nextSelection = len(self._filing) - 1
797 self._select_at(nextSelection)
799 elif keyEvent.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Space]:
800 self._activate_at(self._selectionIndex)
801 elif keyEvent.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Backspace]:
802 self._activate_at(PieFiling.SELECTION_NONE)
804 QtGui.QWidget.keyPressEvent(self, keyEvent)
806 @misc_utils.log_exception(_moduleLogger)
807 def showEvent(self, showEvent):
808 self.aboutToShow.emit()
809 self._cachedCenterPosition = self.rect().center()
811 mask = self._artist.show(self.palette())
814 lastMousePos = self.mapFromGlobal(QtGui.QCursor.pos())
815 self._update_selection(lastMousePos)
817 QtGui.QWidget.showEvent(self, showEvent)
819 @misc_utils.log_exception(_moduleLogger)
820 def hideEvent(self, hideEvent):
822 self._selectionIndex = PieFiling.SELECTION_NONE
823 QtGui.QWidget.hideEvent(self, hideEvent)
825 @misc_utils.log_exception(_moduleLogger)
826 def paintEvent(self, paintEvent):
827 canvas = self._artist.paint(self._selectionIndex)
829 screen = QtGui.QPainter(self)
830 screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
832 QtGui.QWidget.paintEvent(self, paintEvent)
835 return iter(self._filing)
838 return len(self._filing)
840 def _select_at(self, index):
841 self._selectionIndex = index
843 def _update_selection(self, lastMousePos):
844 radius = _radius_at(self._cachedCenterPosition, lastMousePos)
845 if radius < self._filing.innerRadius():
846 self._selectionIndex = PieFiling.SELECTION_CENTER
847 elif radius <= self._filing.outerRadius():
848 self._select_at(self.indexAt(lastMousePos))
850 self._selectionIndex = PieFiling.SELECTION_NONE
852 def _activate_at(self, index):
853 if index == PieFiling.SELECTION_NONE:
855 self.aboutToHide.emit()
858 elif index == PieFiling.SELECTION_CENTER:
859 child = self._filing.center()
861 child = self.itemAt(index)
863 if child.isEnabled():
864 child.action().trigger()
865 self.activated.emit(index)
868 self.aboutToHide.emit()
873 PieFiling.NULL_CENTER.setEnabled(False)
880 def _on_about_to_hide(app):
884 if __name__ == "__main__":
885 app = QtGui.QApplication([])
893 singleAction = QtGui.QAction(None)
894 singleAction.setText("Boo")
895 singleItem = QActionPieItem(singleAction)
897 spie.insertItem(singleItem)
901 oneAction = QtGui.QAction(None)
902 oneAction.setText("Chew")
903 oneItem = QActionPieItem(oneAction)
904 twoAction = QtGui.QAction(None)
905 twoAction.setText("Foo")
906 twoItem = QActionPieItem(twoAction)
907 iconTextAction = QtGui.QAction(None)
908 iconTextAction.setText("Icon")
909 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
910 iconTextItem = QActionPieItem(iconTextAction)
912 mpie.insertItem(oneItem)
913 mpie.insertItem(twoItem)
914 mpie.insertItem(oneItem)
915 mpie.insertItem(iconTextItem)
919 oneAction = QtGui.QAction(None)
920 oneAction.setText("Chew")
921 oneAction.triggered.connect(lambda: _print("Chew"))
922 oneItem = QActionPieItem(oneAction)
923 twoAction = QtGui.QAction(None)
924 twoAction.setText("Foo")
925 twoAction.triggered.connect(lambda: _print("Foo"))
926 twoItem = QActionPieItem(twoAction)
927 iconAction = QtGui.QAction(None)
928 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
929 iconAction.triggered.connect(lambda: _print("Icon"))
930 iconItem = QActionPieItem(iconAction)
931 iconTextAction = QtGui.QAction(None)
932 iconTextAction.setText("Icon")
933 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
934 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
935 iconTextItem = QActionPieItem(iconTextAction)
937 mpie.set_center(iconItem)
938 mpie.insertItem(oneItem)
939 mpie.insertItem(twoItem)
940 mpie.insertItem(oneItem)
941 mpie.insertItem(iconTextItem)
943 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
944 mpie.canceled.connect(lambda: _print("Canceled"))
947 oneAction = QtGui.QAction(None)
948 oneAction.setText("Chew")
949 oneAction.triggered.connect(lambda: _print("Chew"))
950 oneItem = QActionPieItem(oneAction)
951 twoAction = QtGui.QAction(None)
952 twoAction.setText("Foo")
953 twoAction.triggered.connect(lambda: _print("Foo"))
954 twoItem = QActionPieItem(twoAction)
955 iconAction = QtGui.QAction(None)
956 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
957 iconAction.triggered.connect(lambda: _print("Icon"))
958 iconItem = QActionPieItem(iconAction)
959 iconTextAction = QtGui.QAction(None)
960 iconTextAction.setText("Icon")
961 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
962 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
963 iconTextItem = QActionPieItem(iconTextAction)
964 pieFiling = PieFiling()
965 pieFiling.set_center(iconItem)
966 pieFiling.insertItem(oneItem)
967 pieFiling.insertItem(twoItem)
968 pieFiling.insertItem(oneItem)
969 pieFiling.insertItem(iconTextItem)
970 mpie = QPieDisplay(pieFiling)
974 oneAction = QtGui.QAction(None)
975 oneAction.setText("Chew")
976 oneAction.triggered.connect(lambda: _print("Chew"))
977 oneItem = QActionPieItem(oneAction)
978 twoAction = QtGui.QAction(None)
979 twoAction.setText("Foo")
980 twoAction.triggered.connect(lambda: _print("Foo"))
981 twoItem = QActionPieItem(twoAction)
982 iconAction = QtGui.QAction(None)
983 iconAction.setIcon(QtGui.QIcon.fromTheme("gtk-open"))
984 iconAction.triggered.connect(lambda: _print("Icon"))
985 iconItem = QActionPieItem(iconAction)
986 iconTextAction = QtGui.QAction(None)
987 iconTextAction.setText("Icon")
988 iconTextAction.setIcon(QtGui.QIcon.fromTheme("gtk-close"))
989 iconTextAction.triggered.connect(lambda: _print("Icon and text"))
990 iconTextItem = QActionPieItem(iconTextAction)
991 mpie = QPieButton(iconItem)
992 mpie.set_center(iconItem)
993 mpie.insertItem(oneItem)
994 mpie.insertItem(twoItem)
995 mpie.insertItem(oneItem)
996 mpie.insertItem(iconTextItem)
998 mpie.aboutToHide.connect(lambda: _on_about_to_hide(app))
999 mpie.canceled.connect(lambda: _print("Canceled"))