VR-Engage  2.3
Loading...
Searching...
No Matches
stationListView.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2026 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file stationListView.h
7//! \ingroup vreManager
8//! \brief Station list view with a custom drag pixmap
9
10#pragma once
11
12#include "vreManager/export.h"
14
15#include <QApplication>
16#include <QDrag>
17#include <QListView>
18#include <QMimeData>
19#include <QMouseEvent>
20#include <QPainter>
21
22//! \brief QListView subclass that renders a station name pill as the drag pixmap.
23//!
24//! Overrides startDrag() so that when the user drags a station card the cursor
25//! shows a compact rounded label with just the station name, making it easy to
26//! read which station is being dragged over the entity/role tree.
27class VREMANAGER_DLL DtStationListView : public QListView
28{
29public:
30 explicit DtStationListView(QWidget* parent = nullptr)
31 : QListView(parent)
34 {
35 }
36
37protected:
38 void mousePressEvent(QMouseEvent* event) override
39 {
40 myPressedIndex = QModelIndex();
41
42 if (event->button() == Qt::LeftButton)
43 {
44 QModelIndex index = indexAt(event->pos());
45 if (index.isValid())
46 {
47 myPressedIndex = index;
48 myDragStartPos = event->pos();
49
50 setCurrentIndex(index);
51 selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
52 }
53 else
54 {
55 clearSelection();
56 setCurrentIndex(QModelIndex());
57 }
58 }
59 QListView::mousePressEvent(event);
60 }
61
62 void mouseMoveEvent(QMouseEvent* event) override
63 {
64 if (myPressedIndex.isValid()
65 && (event->buttons() & Qt::LeftButton)
66 && (event->pos() - myDragStartPos).manhattanLength()
67 >= QApplication::startDragDistance())
68 {
69 // Ensure the pressed item is selected for startDrag().
70 if (!selectionModel()->isSelected(myPressedIndex))
71 {
72 setCurrentIndex(myPressedIndex);
73 selectionModel()->select(myPressedIndex, QItemSelectionModel::ClearAndSelect);
74 }
75 startDrag(model()->supportedDragActions());
76 myPressedIndex = QModelIndex();
77 return;
78 }
79 QListView::mouseMoveEvent(event);
80 }
81
82 void startDrag(Qt::DropActions supportedActions) override
83 {
84 QModelIndex index;
85 if (myPressedIndex.isValid())
86 {
87 index = QModelIndex(myPressedIndex);
88 }
89 else if (!selectedIndexes().isEmpty())
90 {
91 index = selectedIndexes().first();
92 }
93 if (!index.isValid())
94 {
95 return;
96 }
97
98 const QString name = index.data(makVre::StationNameRole).toString();
99 if (name.isEmpty())
100 {
101 QListView::startDrag(supportedActions);
102 return;
103 }
104
105 QModelIndexList indexes;
106 indexes << index;
107 QMimeData* mimeData = model()->mimeData(indexes);
108 if (!mimeData)
109 {
110 return;
111 }
112
113 QFont f = font();
114 const QFontMetrics fm(f);
115 const int hPad = 10;
116 const int vPad = 5;
117 const int w = fm.horizontalAdvance(name) + hPad * 2;
118 const int h = fm.height() + vPad * 2;
119
120 QPixmap pixmap(w, h);
121 pixmap.fill(Qt::transparent);
122 {
123 QPainter p(&pixmap);
124 p.setRenderHint(QPainter::Antialiasing);
125 QColor bg = palette().color(QPalette::Mid);
126 bg.setAlpha(210);
127 p.setBrush(bg);
128 p.setPen(Qt::NoPen);
129 p.drawRoundedRect(QRect(0, 0, w, h), 4, 4);
130 p.setFont(f);
131 p.setPen(palette().color(QPalette::Text));
132 p.drawText(QRect(0, 0, w, h), Qt::AlignCenter, name);
133 }
134
135 auto* drag = new QDrag(this);
136 drag->setMimeData(mimeData);
137 drag->setPixmap(pixmap);
138 drag->setHotSpot(QPoint(w / 2, h / 2));
139 drag->exec(supportedActions, Qt::MoveAction);
140
141 myPressedIndex = QModelIndex();
142 }
143
144private:
146 QPersistentModelIndex myPressedIndex;
147};
void mouseMoveEvent(QMouseEvent *event) override
Definition stationListView.h:62
QPersistentModelIndex myPressedIndex
Definition stationListView.h:146
DtStationListView(QWidget *parent=nullptr)
Definition stationListView.h:30
void mousePressEvent(QMouseEvent *event) override
Definition stationListView.h:38
void startDrag(Qt::DropActions supportedActions) override
Definition stationListView.h:82
QPoint myDragStartPos
Definition stationListView.h:145
Export macros for the VREngage Manager library.
#define VREMANAGER_DLL
Platform-specific export/import macro for VREngage Manager library.
Definition export.h:28
@ StationNameRole
QString: Station name.
Definition vreStationListModel.h:46
List model for VR-Engage station display.