1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <private/bar_p.h> |
31 | #include <QtGui/QPainter> |
32 | #include <QtWidgets/QGraphicsSceneEvent> |
33 | #include <QtWidgets/QStyleOptionGraphicsItem> |
34 | #include <QtWidgets/QStyle> |
35 | |
36 | QT_CHARTS_BEGIN_NAMESPACE |
37 | |
38 | Bar::Bar(QBarSet *barset, QGraphicsItem *parent) : QGraphicsRectItem(parent), |
39 | m_index(-255), |
40 | m_layoutIndex(-255), |
41 | m_barset(barset), |
42 | m_labelItem(nullptr), |
43 | m_hovering(false), |
44 | m_mousePressed(false), |
45 | m_visualsDirty(true), |
46 | m_labelDirty(true) |
47 | { |
48 | setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton); |
49 | setAcceptHoverEvents(true); |
50 | setFlag(flag: QGraphicsItem::ItemIsSelectable); |
51 | } |
52 | |
53 | Bar::~Bar() |
54 | { |
55 | // End hover event, if bar is deleted during it |
56 | if (m_hovering) |
57 | emit hovered(status: false, index: m_index, barset: m_barset); |
58 | delete m_labelItem; |
59 | } |
60 | |
61 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent *event) |
62 | { |
63 | emit pressed(index: m_index, barset: m_barset); |
64 | m_mousePressed = true; |
65 | QGraphicsItem::mousePressEvent(event); |
66 | } |
67 | |
68 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent *event) |
69 | { |
70 | Q_UNUSED(event) |
71 | m_hovering = true; |
72 | emit hovered(status: true, index: m_index, barset: m_barset); |
73 | |
74 | } |
75 | |
76 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) |
77 | { |
78 | Q_UNUSED(event) |
79 | m_hovering = false; |
80 | emit hovered(status: false, index: m_index, barset: m_barset); |
81 | } |
82 | |
83 | void Bar::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
84 | { |
85 | emit released(index: m_index, barset: m_barset); |
86 | if (m_mousePressed) |
87 | emit clicked(index: m_index, barset: m_barset); |
88 | m_mousePressed = false; |
89 | QGraphicsItem::mouseReleaseEvent(event); |
90 | } |
91 | |
92 | void Bar::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) |
93 | { |
94 | emit doubleClicked(index: m_index, barset: m_barset); |
95 | QGraphicsItem::mouseDoubleClickEvent(event); |
96 | } |
97 | |
98 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
99 | { |
100 | // Remove selection border around bar |
101 | QStyleOptionGraphicsItem barOption(*option); |
102 | barOption.state &= ~QStyle::State_Selected; |
103 | QGraphicsRectItem::paint(painter, option: &barOption, widget); |
104 | } |
105 | |
106 | QT_CHARTS_END_NAMESPACE |
107 | |
108 | #include "moc_bar_p.cpp" |
109 | |