1 | /* $Id: UIChooserHandlerMouse.cpp 108687 2025-03-21 12:35:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIChooserHandlerMouse class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2025 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QGraphicsSceneMouseEvent>
|
---|
30 |
|
---|
31 | /* GUI incluedes: */
|
---|
32 | #include "UIChooserHandlerMouse.h"
|
---|
33 | #include "UIChooserModel.h"
|
---|
34 | #include "UIChooserItemGroup.h"
|
---|
35 | #include "UIChooserItemMachine.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | UIChooserHandlerMouse::UIChooserHandlerMouse(UIChooserModel *pParent)
|
---|
39 | : QObject(pParent)
|
---|
40 | , m_pModel(pParent)
|
---|
41 | {
|
---|
42 | }
|
---|
43 |
|
---|
44 | bool UIChooserHandlerMouse::handle(QGraphicsSceneMouseEvent *pEvent, UIMouseEventType type) const
|
---|
45 | {
|
---|
46 | /* Process passed event: */
|
---|
47 | switch (type)
|
---|
48 | {
|
---|
49 | case UIMouseEventType_Press: return handleMousePress(pEvent);
|
---|
50 | case UIMouseEventType_Release: return handleMouseRelease(pEvent);
|
---|
51 | case UIMouseEventType_DoubleClick: return handleMouseDoubleClick(pEvent);
|
---|
52 | }
|
---|
53 | /* Pass event if unknown: */
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | UIChooserModel* UIChooserHandlerMouse::model() const
|
---|
58 | {
|
---|
59 | return m_pModel;
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool UIChooserHandlerMouse::handleMousePress(QGraphicsSceneMouseEvent *pEvent) const
|
---|
63 | {
|
---|
64 | /* Get item under mouse cursor: */
|
---|
65 | QPointF scenePos = pEvent->scenePos();
|
---|
66 | if (QGraphicsItem *pItemUnderMouse = model()->itemAt(scenePos))
|
---|
67 | {
|
---|
68 | /* Which button it was? */
|
---|
69 | switch (pEvent->button())
|
---|
70 | {
|
---|
71 | /* Left one? */
|
---|
72 | case Qt::LeftButton:
|
---|
73 | {
|
---|
74 | /* Which item we just clicked? */
|
---|
75 | UIChooserItem *pClickedItem = 0;
|
---|
76 | /* Was that a group item? */
|
---|
77 | if (UIChooserItemGroup *pGroupItem = qgraphicsitem_cast<UIChooserItemGroup*>(pItemUnderMouse))
|
---|
78 | pClickedItem = pGroupItem;
|
---|
79 | /* Or a machine one? */
|
---|
80 | else if (UIChooserItemMachine *pMachineItem = qgraphicsitem_cast<UIChooserItemMachine*>(pItemUnderMouse))
|
---|
81 | pClickedItem = pMachineItem;
|
---|
82 | /* If we had clicked one of required item types: */
|
---|
83 | if (pClickedItem && !pClickedItem->isRoot())
|
---|
84 | {
|
---|
85 | /* Was 'shift' modifier pressed? */
|
---|
86 | if (pEvent->modifiers() == Qt::ShiftModifier)
|
---|
87 | {
|
---|
88 | /* Calculate positions: */
|
---|
89 | UIChooserItem *pFirstItem = model()->firstSelectedItem();
|
---|
90 | AssertPtrReturn(pFirstItem, false); // is failure possible?
|
---|
91 | const int iFirstPosition = model()->navigationItems().indexOf(pFirstItem);
|
---|
92 | const int iClickedPosition = model()->navigationItems().indexOf(pClickedItem);
|
---|
93 | /* Populate list of items from 'first' to 'clicked': */
|
---|
94 | QList<UIChooserItem*> items;
|
---|
95 | if (iFirstPosition <= iClickedPosition)
|
---|
96 | for (int i = iFirstPosition; i <= iClickedPosition; ++i)
|
---|
97 | items << model()->navigationItems().at(i);
|
---|
98 | else
|
---|
99 | for (int i = iFirstPosition; i >= iClickedPosition; --i)
|
---|
100 | items << model()->navigationItems().at(i);
|
---|
101 | /* Wipe out items of inconsistent types: */
|
---|
102 | QList<UIChooserItem*> filteredItems;
|
---|
103 | foreach (UIChooserItem *pIteratedItem, items)
|
---|
104 | filteredItems << pIteratedItem;
|
---|
105 | /* Make that list selected: */
|
---|
106 | model()->setSelectedItems(filteredItems);
|
---|
107 | /* Make item closest to clicked the current one: */
|
---|
108 | if (!filteredItems.isEmpty())
|
---|
109 | model()->setCurrentItem(filteredItems.last());
|
---|
110 | }
|
---|
111 | /* Was 'control' modifier pressed? */
|
---|
112 | else if (pEvent->modifiers() == Qt::ControlModifier)
|
---|
113 | {
|
---|
114 | /* Invert selection state for clicked item: */
|
---|
115 | if (model()->selectedItems().contains(pClickedItem))
|
---|
116 | model()->removeFromSelectedItems(pClickedItem);
|
---|
117 | else
|
---|
118 | {
|
---|
119 | UIChooserItem *pFirstItem = model()->firstSelectedItem();
|
---|
120 | AssertPtrReturn(pFirstItem, false); // is failure possible?
|
---|
121 | model()->addToSelectedItems(pClickedItem);
|
---|
122 | }
|
---|
123 | /* Make clicked item current one: */
|
---|
124 | model()->setCurrentItem(pClickedItem);
|
---|
125 | }
|
---|
126 | /* Was no modifiers pressed? */
|
---|
127 | else if (pEvent->modifiers() == Qt::NoModifier)
|
---|
128 | {
|
---|
129 | /* Make clicked item the only selected one: */
|
---|
130 | model()->setSelectedItem(pClickedItem);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | /* Right one? */
|
---|
136 | case Qt::RightButton:
|
---|
137 | {
|
---|
138 | /* Which item we just clicked? */
|
---|
139 | UIChooserItem *pClickedItem = 0;
|
---|
140 | /* Was that a group item? */
|
---|
141 | if (UIChooserItemGroup *pGroupItem = qgraphicsitem_cast<UIChooserItemGroup*>(pItemUnderMouse))
|
---|
142 | pClickedItem = pGroupItem;
|
---|
143 | /* Or a machine one? */
|
---|
144 | else if (UIChooserItemMachine *pMachineItem = qgraphicsitem_cast<UIChooserItemMachine*>(pItemUnderMouse))
|
---|
145 | pClickedItem = pMachineItem;
|
---|
146 | /* If we had clicked one of required item types: */
|
---|
147 | if (pClickedItem && !pClickedItem->isRoot())
|
---|
148 | {
|
---|
149 | /* Select clicked item if not selected yet: */
|
---|
150 | if (!model()->selectedItems().contains(pClickedItem))
|
---|
151 | model()->setSelectedItem(pClickedItem);
|
---|
152 | }
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | default:
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | /* Pass all other events: */
|
---|
160 | return false;
|
---|
161 | }
|
---|
162 |
|
---|
163 | bool UIChooserHandlerMouse::handleMouseRelease(QGraphicsSceneMouseEvent*) const
|
---|
164 | {
|
---|
165 | /* Pass all events: */
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 |
|
---|
169 | bool UIChooserHandlerMouse::handleMouseDoubleClick(QGraphicsSceneMouseEvent *pEvent) const
|
---|
170 | {
|
---|
171 | /* Get item under mouse cursor: */
|
---|
172 | QPointF scenePos = pEvent->scenePos();
|
---|
173 | if (QGraphicsItem *pItemUnderMouse = model()->itemAt(scenePos))
|
---|
174 | {
|
---|
175 | /* Which button it was? */
|
---|
176 | switch (pEvent->button())
|
---|
177 | {
|
---|
178 | /* Left one? */
|
---|
179 | case Qt::LeftButton:
|
---|
180 | {
|
---|
181 | /* Was that a group item? */
|
---|
182 | if (UIChooserItemGroup *pGroupItem = qgraphicsitem_cast<UIChooserItemGroup*>(pItemUnderMouse))
|
---|
183 | {
|
---|
184 | /* If it was not root: */
|
---|
185 | if (!pGroupItem->isRoot())
|
---|
186 | {
|
---|
187 | /* Toggle it: */
|
---|
188 | if (pGroupItem->isClosed())
|
---|
189 | pGroupItem->open();
|
---|
190 | else if (pGroupItem->isOpened())
|
---|
191 | pGroupItem->close();
|
---|
192 | }
|
---|
193 | /* Filter that event out: */
|
---|
194 | return true;
|
---|
195 | }
|
---|
196 | /* Or a machine one? */
|
---|
197 | else if (pItemUnderMouse->type() == UIChooserNodeType_Machine)
|
---|
198 | {
|
---|
199 | /* Start or show selected items: */
|
---|
200 | model()->startOrShowSelectedItems();
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | default:
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | /* Pass all other events: */
|
---|
209 | return false;
|
---|
210 | }
|
---|
211 |
|
---|