diff options
Diffstat (limited to 'examples/charts/callout/callout.py')
-rw-r--r-- | examples/charts/callout/callout.py | 86 |
1 files changed, 24 insertions, 62 deletions
diff --git a/examples/charts/callout/callout.py b/examples/charts/callout/callout.py index 99982e690..622938d16 100644 --- a/examples/charts/callout/callout.py +++ b/examples/charts/callout/callout.py @@ -1,49 +1,11 @@ - -############################################################################# -## -## Copyright (C) 2018 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause """PySide6 port of the Callout example from Qt v5.x""" import sys -from PySide6.QtWidgets import (QApplication, QGraphicsScene, - QGraphicsView, QGraphicsSimpleTextItem, QGraphicsItem) +from PySide6.QtWidgets import (QApplication, QGraphicsScene, QGraphicsView, + QGraphicsSimpleTextItem, QGraphicsItem) from PySide6.QtCore import Qt, QPointF, QRectF, QRect from PySide6.QtCharts import QChart, QLineSeries, QSplineSeries from PySide6.QtGui import QPainter, QFont, QFontMetrics, QPainterPath, QColor @@ -80,37 +42,37 @@ class Callout(QGraphicsItem): # establish the position of the anchor point in relation to _rect above = anchor.y() <= self._rect.top() - above_center = (anchor.y() > self._rect.top() and - anchor.y() <= self._rect.center().y()) - below_center = (anchor.y() > self._rect.center().y() and - anchor.y() <= self._rect.bottom()) + above_center = (anchor.y() > self._rect.top() + and anchor.y() <= self._rect.center().y()) + below_center = (anchor.y() > self._rect.center().y() + and anchor.y() <= self._rect.bottom()) below = anchor.y() > self._rect.bottom() on_left = anchor.x() <= self._rect.left() - left_of_center = (anchor.x() > self._rect.left() and - anchor.x() <= self._rect.center().x()) - right_of_center = (anchor.x() > self._rect.center().x() and - anchor.x() <= self._rect.right()) + left_of_center = (anchor.x() > self._rect.left() + and anchor.x() <= self._rect.center().x()) + right_of_center = (anchor.x() > self._rect.center().x() + and anchor.x() <= self._rect.right()) on_right = anchor.x() > self._rect.right() # get the nearest _rect corner. x = (on_right + right_of_center) * self._rect.width() y = (below + below_center) * self._rect.height() - corner_case = ((above and on_left) or (above and on_right) or - (below and on_left) or (below and on_right)) + corner_case = ((above and on_left) or (above and on_right) + or (below and on_left) or (below and on_right)) vertical = abs(anchor.x() - x) > abs(anchor.y() - y) - x1 = (x + left_of_center * 10 - right_of_center * 20 + corner_case * - int(not vertical) * (on_left * 10 - on_right * 20)) - y1 = (y + above_center * 10 - below_center * 20 + corner_case * - vertical * (above * 10 - below * 20)) + x1 = (x + left_of_center * 10 - right_of_center * 20 + corner_case + * int(not vertical) * (on_left * 10 - on_right * 20)) + y1 = (y + above_center * 10 - below_center * 20 + corner_case + * vertical * (above * 10 - below * 20)) point1.setX(x1) point1.setY(y1) - x2 = (x + left_of_center * 20 - right_of_center * 10 + corner_case * - int(not vertical) * (on_left * 20 - on_right * 10)) - y2 = (y + above_center * 20 - below_center * 10 + corner_case * - vertical * (above * 20 - below * 10)) + x2 = (x + left_of_center * 20 - right_of_center * 10 + corner_case + * int(not vertical) * (on_left * 20 - on_right * 10)) + y2 = (y + above_center * 20 - below_center * 10 + corner_case + * vertical * (above * 20 - below * 10)) point2.setX(x2) point2.setY(y2) @@ -128,7 +90,7 @@ class Callout(QGraphicsItem): def mouseMoveEvent(self, event): if event.buttons() & Qt.LeftButton: - self.setPos(mapToParent( + self.setPos(self.mapToParent( event.pos() - event.buttonDownPos(Qt.LeftButton))) event.setAccepted(True) else: @@ -165,7 +127,7 @@ class View(QGraphicsView): self._chart = QChart() self._chart.setMinimumSize(640, 480) self._chart.setTitle("Hover the line to show callout. Click the line " - "to make it stay") + "to make it stay") self._chart.legend().hide() self.series = QLineSeries() self.series.append(1, 3) |