Motiv
Marvelous OTF2 Traces Interactive Visualizer
Loading...
Searching...
No Matches
GenericIndicator.cpp
1/*
2 * Marvelous OTF2 Traces Interactive Visualizer (MOTIV)
3 * Copyright (C) 2023 Florian Gallrein, Björn Gehrke
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "GenericIndicator.hpp"
19
20#include <QPen>
21#include <QGraphicsSceneMouseEvent>
22
23#include "src/models/communication/Communication.hpp"
24#include "src/models/Slot.hpp"
25#include "src/ui/Constants.hpp"
26#include "src/models/communication/CollectiveCommunicationEvent.hpp"
27
28template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
29GenericIndicator<T, G>::GenericIndicator(T *element, QGraphicsItem *)
30 : G(), element_(element) {
31 this->setAcceptHoverEvents(true);
32 this->setAcceptedMouseButtons(Qt::LeftButton);
33}
34
35template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
36void GenericIndicator<T, G>::setOnDoubleClick(const std::function<void(T *)>& fn) {
37 onDoubleClick_ = fn;
38}
39
40template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
41void GenericIndicator<T, G>::setOnSelected(const std::function<void(T *)>& fn) {
42 onSelected_ = fn;
44
45template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
46void GenericIndicator<T, G>::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
47 if (onDoubleClick_ && respondToEvent(event->pos())) {
48 onDoubleClick_(element_);
49 event->accept();
50 } else {
51 G::mouseDoubleClickEvent(event);
52 }
53}
54
55template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
56void GenericIndicator<T, G>::mousePressEvent(QGraphicsSceneMouseEvent *event) {
57 if (onSelected_ && respondToEvent(event->pos())) {
58 onSelected_(element_);
59 event->accept();
60 } else {
61 G::mousePressEvent(event);
62 }
63}
64
65template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
66void GenericIndicator<T, G>::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
67 QPen p(G::pen());
68 p.setWidth(p.width() * 2);
69 G::setPen(p);
70
71 G::setZValue(G::zValue() + layers::Z_LAYER_HIGHLIGHTED_OFFSET);
72
73 QGraphicsItem::hoverEnterEvent(event);
74}
75
76template <class T, class G> requires std::is_base_of_v<QAbstractGraphicsShapeItem, G>
77void GenericIndicator<T, G>::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
78 QPen p(G::pen());
79 p.setWidth(p.width() / 2);
80 G::setPen(p);
81
82 G::setZValue(G::zValue() - layers::Z_LAYER_HIGHLIGHTED_OFFSET);
83
84 QGraphicsItem::hoverLeaveEvent(event);
85}
86
A generic indicator bundling common interaction behaviours.
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
void setOnSelected(const std::function< void(T *)> &fn)
Registers a select handler.
void setOnDoubleClick(const std::function< void(T *)> &fn)
Registers a double click handler.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override