VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/manager/chooser/UIChooserHandlerMouse.cpp@ 77366

Last change on this file since 77366 was 77366, checked in by vboxsync, 6 years ago

FE/Qt: bugref:9373: VirtualBox Manager: Chooser pane: Integrate pin button for making Global Tools item favorite.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: UIChooserHandlerMouse.cpp 77366 2019-02-19 16:00:39Z 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
29UIChooserHandlerMouse::UIChooserHandlerMouse(UIChooserModel *pParent)
30 : QObject(pParent)
31 , m_pModel(pParent)
32{
33}
34
35bool 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
48UIChooserModel* UIChooserHandlerMouse::model() const
49{
50 return m_pModel;
51}
52
53bool 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()->currentItem() == pGlobalItem
76 || pGlobalItem->isHovered()))
77 {
78 model()->handleToolButtonClick(pGlobalItem);
79 if (model()->currentItem() != pGlobalItem)
80 pClickedItem = pGlobalItem;
81 }
82 else
83 if ( pGlobalItem->isPinButtonArea(itemCursorPos)
84 && ( model()->currentItem() == 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()->currentItem() == pMachineItem
96 || pMachineItem->isHovered()))
97 {
98 model()->handleToolButtonClick(pMachineItem);
99 if (model()->currentItem() != 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()->currentItem();
113 int iFirstPosition = model()->navigationList().indexOf(pFirstItem);
114 int iClickedPosition = model()->navigationList().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()->navigationList().at(i);
120 else
121 for (int i = iFirstPosition; i >= iClickedPosition; --i)
122 items << model()->navigationList().at(i);
123 /* Set that list as current: */
124 model()->setCurrentItems(items);
125 /* Move focus to clicked item: */
126 model()->setFocusItem(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()->currentItems().contains(pClickedItem))
133 model()->removeFromCurrentItems(pClickedItem);
134 else
135 model()->addToCurrentItems(pClickedItem);
136 /* Move focus to clicked item: */
137 model()->setFocusItem(pClickedItem);
138 model()->makeSureSomeItemIsSelected();
139 }
140 /* Was no modifiers pressed? */
141 else if (pEvent->modifiers() == Qt::NoModifier)
142 {
143 /* Make clicked item the current one: */
144 model()->setCurrentItem(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()->currentItems().contains(pClickedItem))
168 model()->setCurrentItem(pClickedItem);
169 }
170 break;
171 }
172 default:
173 break;
174 }
175 }
176 /* Pass all other events: */
177 return false;
178}
179
180bool UIChooserHandlerMouse::handleMouseRelease(QGraphicsSceneMouseEvent*) const
181{
182 /* Pass all events: */
183 return false;
184}
185
186bool 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 /* Prepare variables: */
202 int iGroupItemWidth = pGroupItem->geometry().toRect().width();
203 int iMouseDoubleClickX = pEvent->scenePos().toPoint().x();
204 /* If it was a root: */
205 if (pGroupItem->isRoot())
206 {
207#if 0
208 /* Do not allow for unhovered root: */
209 if (!pGroupItem->isHovered())
210 return false;
211 /* Unindent root if possible: */
212 if (model()->root() != model()->mainRoot())
213 {
214 pGroupItem->setHovered(false);
215 model()->unindentRoot();
216 }
217#endif
218 }
219 /* If it was a simple group item: */
220 else
221 {
222 /* If click was at left part: */
223 if (iMouseDoubleClickX < iGroupItemWidth / 2)
224 {
225 /* Toggle it: */
226 if (pGroupItem->isClosed())
227 pGroupItem->open();
228 else if (pGroupItem->isOpened())
229 pGroupItem->close();
230 }
231#if 0
232 /* If click was at right part: */
233 else
234 {
235 /* Indent root with group item: */
236 pGroupItem->setHovered(false);
237 model()->indentRoot(pGroupItem);
238 }
239#endif
240 }
241 /* Filter that event out: */
242 return true;
243 }
244 /* Or a machine one? */
245 else if (pItemUnderMouse->type() == UIChooserItemType_Machine)
246 {
247 /* Activate machine-item: */
248 model()->activateMachineItem();
249 }
250 break;
251 }
252 default:
253 break;
254 }
255 }
256 /* Pass all other events: */
257 return false;
258}
259
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette