| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <private/bar_p.h> |
| 5 | #include <QtGui/QPainter> |
| 6 | #include <QtWidgets/QGraphicsSceneEvent> |
| 7 | #include <QtWidgets/QStyleOptionGraphicsItem> |
| 8 | #include <QtWidgets/QStyle> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | Bar::Bar(QBarSet *barset, QGraphicsItem *parent) : QGraphicsRectItem(parent), |
| 13 | m_index(-255), |
| 14 | m_layoutIndex(-255), |
| 15 | m_barset(barset), |
| 16 | m_labelItem(nullptr), |
| 17 | m_hovering(false), |
| 18 | m_mousePressed(false), |
| 19 | m_visualsDirty(true), |
| 20 | m_labelDirty(true) |
| 21 | { |
| 22 | setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton); |
| 23 | setAcceptHoverEvents(true); |
| 24 | setFlag(flag: QGraphicsItem::ItemIsSelectable); |
| 25 | } |
| 26 | |
| 27 | Bar::~Bar() |
| 28 | { |
| 29 | // End hover event, if bar is deleted during it |
| 30 | if (m_hovering) |
| 31 | emit hovered(status: false, index: m_index, barset: m_barset); |
| 32 | delete m_labelItem; |
| 33 | } |
| 34 | |
| 35 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent *event) |
| 36 | { |
| 37 | emit pressed(index: m_index, barset: m_barset); |
| 38 | m_mousePressed = true; |
| 39 | QGraphicsItem::mousePressEvent(event); |
| 40 | } |
| 41 | |
| 42 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent *event) |
| 43 | { |
| 44 | Q_UNUSED(event); |
| 45 | m_hovering = true; |
| 46 | emit hovered(status: true, index: m_index, barset: m_barset); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) |
| 51 | { |
| 52 | Q_UNUSED(event); |
| 53 | m_hovering = false; |
| 54 | emit hovered(status: false, index: m_index, barset: m_barset); |
| 55 | } |
| 56 | |
| 57 | void Bar::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
| 58 | { |
| 59 | emit released(index: m_index, barset: m_barset); |
| 60 | if (m_mousePressed) |
| 61 | emit clicked(index: m_index, barset: m_barset); |
| 62 | m_mousePressed = false; |
| 63 | QGraphicsItem::mouseReleaseEvent(event); |
| 64 | } |
| 65 | |
| 66 | void Bar::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) |
| 67 | { |
| 68 | emit doubleClicked(index: m_index, barset: m_barset); |
| 69 | QGraphicsItem::mouseDoubleClickEvent(event); |
| 70 | } |
| 71 | |
| 72 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
| 73 | { |
| 74 | // Remove selection border around bar |
| 75 | QStyleOptionGraphicsItem barOption(*option); |
| 76 | barOption.state &= ~QStyle::State_Selected; |
| 77 | QGraphicsRectItem::paint(painter, option: &barOption, widget); |
| 78 | } |
| 79 | |
| 80 | QT_END_NAMESPACE |
| 81 | |
| 82 | #include "moc_bar_p.cpp" |
| 83 | |