import math
import logging
-from PyQt4 import QtGui
-from PyQt4 import QtCore
+import qt_compat
+QtCore = qt_compat.QtCore
+QtGui = qt_compat.import_module("QtGui")
-try:
- from util import misc as misc_utils
-except ImportError:
- class misc_utils(object):
-
- @staticmethod
- def log_exception(logger):
-
- def wrapper(func):
- return func
- return wrapper
+import misc as misc_utils
_moduleLogger = logging.getLogger(__name__)
SHAPE_SQUARE = "square"
DEFAULT_SHAPE = SHAPE_SQUARE
- def __init__(self, filing):
+ BACKGROUND_FILL = "fill"
+ BACKGROUND_NOFILL = "no fill"
+
+ def __init__(self, filing, background = BACKGROUND_FILL):
self._filing = filing
self._cachedOuterRadius = self._filing.outerRadius()
canvasSize = self._cachedOuterRadius * 2 + 1
self._canvas = QtGui.QPixmap(canvasSize, canvasSize)
self._mask = None
+ self._backgroundState = background
self.palette = None
def pieSize(self):
painter = QtGui.QPainter(self._canvas)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
- adjustmentRect = self._canvas.rect().adjusted(0, 0, -1, -1)
+ self.paintPainter(selectionIndex, painter)
+
+ return self._canvas
+
+ def paintPainter(self, selectionIndex, painter):
+ adjustmentRect = painter.viewport().adjusted(0, 0, -1, -1)
numChildren = len(self._filing)
if numChildren == 0:
- if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
- painter.setBrush(self.palette.highlight())
- else:
- painter.setBrush(self.palette.window())
- painter.setPen(self.palette.mid().color())
-
- painter.drawRect(self._canvas.rect())
- self._paint_center_foreground(painter, selectionIndex)
+ self._paint_center_background(painter, adjustmentRect, selectionIndex)
+ self._paint_center_foreground(painter, adjustmentRect, selectionIndex)
return self._canvas
- elif numChildren == 1:
- if selectionIndex == 0 and self._filing[0].isEnabled():
- painter.setBrush(self.palette.highlight())
- else:
- painter.setBrush(self.palette.window())
-
- painter.fillRect(self._canvas.rect(), painter.brush())
else:
for i in xrange(len(self._filing)):
self._paint_slice_background(painter, adjustmentRect, i, selectionIndex)
self._paint_center_background(painter, adjustmentRect, selectionIndex)
- self._paint_center_foreground(painter, selectionIndex)
+ self._paint_center_foreground(painter, adjustmentRect, selectionIndex)
for i in xrange(len(self._filing)):
- self._paint_slice_foreground(painter, i, selectionIndex)
-
- return self._canvas
+ self._paint_slice_foreground(painter, adjustmentRect, i, selectionIndex)
def _generate_mask(self, mask):
"""
else:
raise NotImplementedError(self.DEFAULT_SHAPE)
- if i == selectionIndex and self._filing[i].isEnabled():
- painter.setBrush(self.palette.highlight())
+ if self._backgroundState == self.BACKGROUND_NOFILL:
+ painter.setBrush(QtGui.QBrush(QtCore.Qt.transparent))
+ painter.setPen(self.palette.highlight().color())
else:
- painter.setBrush(self.palette.window())
- painter.setPen(self.palette.mid().color())
+ if i == selectionIndex and self._filing[i].isEnabled():
+ painter.setBrush(self.palette.highlight())
+ painter.setPen(self.palette.highlight().color())
+ else:
+ painter.setBrush(self.palette.window())
+ painter.setPen(self.palette.window().color())
a = self._filing._index_to_angle(i, True)
b = self._filing._index_to_angle(i + 1, True)
sizeInDeg = (size * 360 * 16) / _TWOPI
painter.drawPie(adjustmentRect, int(startAngleInDeg), int(sizeInDeg))
- def _paint_slice_foreground(self, painter, i, selectionIndex):
+ def _paint_slice_foreground(self, painter, adjustmentRect, i, selectionIndex):
child = self._filing[i]
a = self._filing._index_to_angle(i, True)
sliceX = averageRadius * math.cos(middleAngle)
sliceY = - averageRadius * math.sin(middleAngle)
- piePos = self._canvas.rect().center()
+ piePos = adjustmentRect.center()
pieX = piePos.x()
pieY = piePos.y()
self._paint_label(
painter.drawText(leftX, topY, text)
def _paint_center_background(self, painter, adjustmentRect, selectionIndex):
- dark = self.palette.mid().color()
- light = self.palette.light().color()
- if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
- background = self.palette.highlight().color()
- else:
- background = self.palette.window().color()
-
- innerRadius = self._cachedInnerRadius
- adjustmentCenterPos = adjustmentRect.center()
- innerRect = QtCore.QRect(
- adjustmentCenterPos.x() - innerRadius,
- adjustmentCenterPos.y() - innerRadius,
- innerRadius * 2 + 1,
- innerRadius * 2 + 1,
- )
+ if self._backgroundState == self.BACKGROUND_NOFILL:
+ return
+ if len(self._filing) == 0:
+ if self._backgroundState == self.BACKGROUND_NOFILL:
+ painter.setBrush(QtGui.QBrush(QtCore.Qt.transparent))
+ else:
+ if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
+ painter.setBrush(self.palette.highlight())
+ else:
+ painter.setBrush(self.palette.window())
+ painter.setPen(self.palette.mid().color())
- painter.setPen(QtCore.Qt.NoPen)
- painter.setBrush(background)
- painter.drawPie(innerRect, 0, 360 * 16)
+ painter.drawRect(adjustmentRect)
+ else:
+ dark = self.palette.mid().color()
+ light = self.palette.light().color()
+ if self._backgroundState == self.BACKGROUND_NOFILL:
+ background = QtGui.QBrush(QtCore.Qt.transparent)
+ else:
+ if selectionIndex == PieFiling.SELECTION_CENTER and self._filing.center().isEnabled():
+ background = self.palette.highlight().color()
+ else:
+ background = self.palette.window().color()
+
+ innerRadius = self._cachedInnerRadius
+ adjustmentCenterPos = adjustmentRect.center()
+ innerRect = QtCore.QRect(
+ adjustmentCenterPos.x() - innerRadius,
+ adjustmentCenterPos.y() - innerRadius,
+ innerRadius * 2 + 1,
+ innerRadius * 2 + 1,
+ )
- painter.setPen(QtGui.QPen(dark, 1))
- painter.setBrush(QtCore.Qt.NoBrush)
- painter.drawEllipse(innerRect)
+ painter.setPen(QtCore.Qt.NoPen)
+ painter.setBrush(background)
+ painter.drawPie(innerRect, 0, 360 * 16)
- if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
- pass
- elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
- painter.setPen(QtGui.QPen(dark, 1))
- painter.setBrush(QtCore.Qt.NoBrush)
- painter.drawEllipse(adjustmentRect)
- else:
- raise NotImplementedError(self.DEFAULT_SHAPE)
+ if self.DEFAULT_SHAPE == self.SHAPE_SQUARE:
+ pass
+ elif self.DEFAULT_SHAPE == self.SHAPE_CIRCLE:
+ painter.setPen(QtGui.QPen(dark, 1))
+ painter.setBrush(QtCore.Qt.NoBrush)
+ painter.drawEllipse(adjustmentRect)
+ else:
+ raise NotImplementedError(self.DEFAULT_SHAPE)
- def _paint_center_foreground(self, painter, selectionIndex):
- centerPos = self._canvas.rect().center()
+ def _paint_center_foreground(self, painter, adjustmentRect, selectionIndex):
+ centerPos = adjustmentRect.center()
pieX = centerPos.x()
pieY = centerPos.y()
@misc_utils.log_exception(_moduleLogger)
def paintEvent(self, paintEvent):
canvas = self._artist.paint(self._selectionIndex)
+ offset = (self.size() - canvas.size()) / 2
screen = QtGui.QPainter(self)
- screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
+ screen.drawPixmap(QtCore.QPoint(offset.width(), offset.height()), canvas)
QtGui.QWidget.paintEvent(self, paintEvent)
class QPieButton(QtGui.QWidget):
- activated = QtCore.pyqtSignal(int)
- highlighted = QtCore.pyqtSignal(int)
- canceled = QtCore.pyqtSignal()
- aboutToShow = QtCore.pyqtSignal()
- aboutToHide = QtCore.pyqtSignal()
+ activated = qt_compat.Signal(int)
+ highlighted = qt_compat.Signal(int)
+ canceled = qt_compat.Signal()
+ aboutToShow = qt_compat.Signal()
+ aboutToHide = qt_compat.Signal()
BUTTON_RADIUS = 24
DELAY = 250
- def __init__(self, buttonSlice, parent = None):
+ def __init__(self, buttonSlice, parent = None, buttonSlices = None):
# @bug Artifacts on Maemo 5 due to window 3D effects, find way to disable them for just these?
# @bug The pie's are being pushed back on screen on Maemo, leading to coordinate issues
QtGui.QWidget.__init__(self, parent)
self._buttonFiling = PieFiling()
self._buttonFiling.set_center(buttonSlice)
+ if buttonSlices is not None:
+ for slice in buttonSlices:
+ self._buttonFiling.insertItem(slice)
self._buttonFiling.setOuterRadius(self.BUTTON_RADIUS)
- self._buttonArtist = PieArtist(self._buttonFiling)
+ self._buttonArtist = PieArtist(self._buttonFiling, PieArtist.BACKGROUND_NOFILL)
self._poppedUp = False
+ self._pressed = False
self._delayPopupTimer = QtCore.QTimer()
self._delayPopupTimer.setInterval(self.DELAY)
self._mousePosition = None
self.setFocusPolicy(QtCore.Qt.StrongFocus)
+ self.setSizePolicy(
+ QtGui.QSizePolicy(
+ QtGui.QSizePolicy.MinimumExpanding,
+ QtGui.QSizePolicy.MinimumExpanding,
+ )
+ )
def insertItem(self, item, index = -1):
self._filing.insertItem(item, index)
def setButtonRadius(self, radius):
self._buttonFiling.setOuterRadius(radius)
+ self._buttonFiling.setInnerRadius(radius / 2)
self._buttonArtist.show(self.palette())
def minimumSizeHint(self):
self.highlighted.emit(self._selectionIndex)
self._display.selectAt(self._selectionIndex)
+ self._pressed = True
+ self.update()
self._popupLocation = mouseEvent.globalPos()
self._delayPopupTimer.start()
@misc_utils.log_exception(_moduleLogger)
def _on_delayed_popup(self):
- assert self._popupLocation is not None
+ assert self._popupLocation is not None, "Widget location abuse"
self._popup_child(self._popupLocation)
@misc_utils.log_exception(_moduleLogger)
self._mousePosition = None
self._activate_at(self._selectionIndex)
+ self._pressed = False
+ self.update()
self._hide_child()
@misc_utils.log_exception(_moduleLogger)
def paintEvent(self, paintEvent):
self.setButtonRadius(min(self.rect().width(), self.rect().height()) / 2 - 1)
if self._poppedUp:
- canvas = self._buttonArtist.paint(PieFiling.SELECTION_CENTER)
+ selectionIndex = PieFiling.SELECTION_CENTER
else:
- canvas = self._buttonArtist.paint(PieFiling.SELECTION_NONE)
+ selectionIndex = PieFiling.SELECTION_NONE
- screen = QtGui.QPainter(self)
- screen.drawPixmap(QtCore.QPoint(0, 0), canvas)
+ screen = QtGui.QStylePainter(self)
+ screen.setRenderHint(QtGui.QPainter.Antialiasing, True)
+ option = QtGui.QStyleOptionButton()
+ option.initFrom(self)
+ option.state = QtGui.QStyle.State_Sunken if self._pressed else QtGui.QStyle.State_Raised
+
+ screen.drawControl(QtGui.QStyle.CE_PushButton, option)
+ self._buttonArtist.paintPainter(selectionIndex, screen)
QtGui.QWidget.paintEvent(self, paintEvent)
class QPieMenu(QtGui.QWidget):
- activated = QtCore.pyqtSignal(int)
- highlighted = QtCore.pyqtSignal(int)
- canceled = QtCore.pyqtSignal()
- aboutToShow = QtCore.pyqtSignal()
- aboutToHide = QtCore.pyqtSignal()
+ activated = qt_compat.Signal(int)
+ highlighted = qt_compat.Signal(int)
+ canceled = qt_compat.Signal()
+ aboutToShow = qt_compat.Signal()
+ aboutToHide = qt_compat.Signal()
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)