class InteractiveLetterItem(QGraphicsRectItem): """This is a class""" def __init__(self, *arg, **karg): QGraphicsRectItem.__init__(self, *arg, **karg) self.codon = None self.label = None self.setAcceptsHoverEvents(True) def hoverEnterEvent(self, e): """ when mouse is over""" if not self.label: self.label = QGraphicsRectItem(parent=self) #self.label.setY(-18) self.label.setX(11) self.label.setBrush(QBrush(QColor("white"))) self.label.text = QGraphicsSimpleTextItem(parent=self.label) self.setZValue(1) self.label.text.setText(self.codon) self.label.setRect(self.label.text.boundingRect()) self.label.setVisible(True) def hoverLeaveEvent(self, e): """when mouse leaves area""" if self.label: self.label.setVisible(False) self.setZValue(0)
class InteractiveItem(QGraphicsRectItem): def __init__(self, *arg, **karg): QGraphicsRectItem.__init__(self, *arg, **karg) self.node = None self.label = None self.setCursor(QtCore.Qt.PointingHandCursor) self.setAcceptsHoverEvents(True) def hoverEnterEvent (self, e): # There are many ways of adding interactive elements. With the # following code, I show/hide a text item over my custom # DynamicItemFace if not self.label: self.label = QGraphicsRectItem() self.label.setParentItem(self) # This is to ensure that the label is rendered over the # rest of item children (default ZValue for items is 0) self.label.setZValue(1) self.label.setBrush(QBrush(QColor("white"))) self.label.text = QGraphicsSimpleTextItem() self.label.text.setParentItem(self.label) self.label.text.setText(self.node.name) self.label.setRect(self.label.text.boundingRect()) self.label.setVisible(True) def hoverLeaveEvent(self, e): if self.label: self.label.setVisible(False)
class InteractiveLetterItem(QGraphicsRectItem): """This is a class""" def __init__(self, *arg, **karg): QGraphicsRectItem.__init__(self, *arg, **karg) self.codon = None self.label = None self.setAcceptsHoverEvents(True) def hoverEnterEvent (self, e): """ when mouse is over""" if not self.label: self.label = QGraphicsRectItem(parent=self) #self.label.setY(-18) self.label.setX(11) self.label.setBrush(QBrush(QColor("white"))) self.label.text = QGraphicsSimpleTextItem(parent=self.label) self.setZValue(1) self.label.text.setText(self.codon) self.label.setRect(self.label.text.boundingRect()) self.label.setVisible(True) def hoverLeaveEvent(self, e): """when mouse leaves area""" if self.label: self.label.setVisible(False) self.setZValue(0)
class InteractiveItem(QGraphicsRectItem): def __init__(self, *arg, **karg): QGraphicsRectItem.__init__(self, *arg, **karg) self.node = None self.label = None self.setCursor(QtCore.Qt.PointingHandCursor) self.setAcceptsHoverEvents(True) def hoverEnterEvent(self, e): # There are many ways of adding interactive elements. With the # following code, I show/hide a text item over my custom # DynamicItemFace if not self.label: self.label = QGraphicsRectItem() self.label.setParentItem(self) # This is to ensure that the label is rendered over the # rest of item children (default ZValue for items is 0) self.label.setZValue(1) self.label.setBrush(QBrush(QColor("white"))) self.label.text = QGraphicsSimpleTextItem() self.label.text.setParentItem(self.label) self.label.text.setText(self.node.name) self.label.setRect(self.label.text.boundingRect()) self.label.setVisible(True) def hoverLeaveEvent(self, e): if self.label: self.label.setVisible(False)
class MJScene(QGraphicsScene): """our scene with a potential Qt bug fix""" def __init__(self): QGraphicsScene.__init__(self) self.__disableFocusRect = False self._focusBoard = None self.focusRect = QGraphicsRectItem() pen = QPen(QColor(Qt.blue)) pen.setWidth(6) self.focusRect.setPen(pen) self.addItem(self.focusRect) self.focusRect.setZValue(ZValues.marker) self.focusRect.hide() def __focusRectVisible(self): """should we show it?""" game = InternalParameters.field.game board = self._focusBoard return bool(not self.__disableFocusRect and board and board.hasFocus and board.focusTile and game and not game.autoPlay) @apply def disableFocusRect(): # pylint: disable=E0202 """suppress focusrect""" def fget(self): # pylint: disable=W0212 return self.__disableFocusRect def fset(self, value): # pylint: disable=W0212 # always place or hide, even if value does not change self.__disableFocusRect = value if value: self.focusRect.hide() else: self.placeFocusRect() return property(**locals()) @apply def focusBoard(): # pylint: disable=E0202 """get / set the board that has its focusRect shown""" def fget(self): # pylint: disable=W0212 return self._focusBoard def fset(self, board): # pylint: disable=W0212 self._focusBoard = board focusTile = board.focusTile if board else None if focusTile: focusTile.graphics.setFocus() self.placeFocusRect() self.focusRect.setVisible(self.__focusRectVisible()) return property(**locals()) def placeFocusRect(self): """show a blue rect around tile""" board = self._focusBoard if isAlive(board) and self.__focusRectVisible(): rect = board.tileFaceRect() rect.setWidth(rect.width()*board.focusRectWidth()) self.focusRect.setRect(rect) self.focusRect.setPos(board.focusTile.graphics.pos()) self.focusRect.setRotation(board.sceneRotation()) self.focusRect.setScale(board.scale()) self.focusRect.show() else: self.focusRect.hide() def graphicsTileItems(self): """returns all GraphicsTileItems in the scene""" return (x for x in self.items() if isinstance(x, GraphicsTileItem)) def nonTiles(self): """returns all other items in the scene""" return (x for x in self.items() if not isinstance(x, GraphicsTileItem)) def removeTiles(self): """remove all tiles from scene""" for item in self.graphicsTileItems(): self.removeItem(item) self.focusRect.hide()
class MJScene(QGraphicsScene): """our scene with a potential Qt bug fix""" def __init__(self): QGraphicsScene.__init__(self) self.__disableFocusRect = False self._focusBoard = None self.focusRect = QGraphicsRectItem() pen = QPen(QColor(Qt.blue)) pen.setWidth(6) self.focusRect.setPen(pen) self.addItem(self.focusRect) self.focusRect.setZValue(ZValues.marker) self.focusRect.hide() def focusInEvent(self, event): """work around a qt bug. See https://bugreports.qt-project.org/browse/QTBUG-32890 This can be reproduced as follows: ./kajongg.py --game=whatever --autoplay=SomeRuleset such that the human player is the first one to discard a tile. wait until the main screen has been built click with the mouse into the middle of that window press left arrow key this will violate the assertion in GraphicsTileItem.keyPressEvent """ prev = self.focusItem() QGraphicsScene.focusInEvent(self, event) if prev and bool(prev.flags() & QGraphicsItem.ItemIsFocusable) and prev != self.focusItem(): self.setFocusItem(prev) def __focusRectVisible(self): """should we show it?""" game = Internal.field.game board = self._focusBoard return bool(not self.__disableFocusRect and board and board.hasFocus and board.focusTile and game and not game.autoPlay) @property def disableFocusRect(self): """suppress focusrect""" return self.__disableFocusRect @disableFocusRect.setter def disableFocusRect(self, value): """always place or hide, even if value does not change""" self.__disableFocusRect = value if value: self.focusRect.hide() else: self.placeFocusRect() @property def focusBoard(self): """get / set the board that has its focusRect shown""" return self._focusBoard @focusBoard.setter def focusBoard(self, board): """get / set the board that has its focusRect shown""" self._focusBoard = board focusTile = board.focusTile if board else None if focusTile: focusTile.graphics.setFocus() self.placeFocusRect() self.focusRect.setVisible(self.__focusRectVisible()) def placeFocusRect(self): """show a blue rect around tile""" board = self._focusBoard if isAlive(board) and self.__focusRectVisible(): rect = board.tileFaceRect() rect.setWidth(rect.width()*board.focusRectWidth()) self.focusRect.setRect(rect) self.focusRect.setPos(board.focusTile.graphics.pos()) self.focusRect.setRotation(board.sceneRotation()) self.focusRect.setScale(board.scale()) self.focusRect.show() else: self.focusRect.hide() def graphicsTileItems(self): """returns all GraphicsTileItems in the scene""" return (x for x in self.items() if isinstance(x, GraphicsTileItem)) def nonTiles(self): """returns all other items in the scene""" return (x for x in self.items() if not isinstance(x, GraphicsTileItem)) def removeTiles(self): """remove all tiles from scene""" for item in self.graphicsTileItems(): self.removeItem(item) self.focusRect.hide()