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