1 | /* $Id: UIChooserView.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIChooserView class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 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 <QApplication>
|
---|
30 | #include <QAccessibleWidget>
|
---|
31 | #include <QScrollBar>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "UIChooserItem.h"
|
---|
35 | #include "UIChooserModel.h"
|
---|
36 | #include "UIChooserSearchWidget.h"
|
---|
37 | #include "UIChooserView.h"
|
---|
38 | #include "UICommon.h"
|
---|
39 | #include "UITranslationEventListener.h"
|
---|
40 |
|
---|
41 | /* Other VBox includes: */
|
---|
42 | #include <iprt/assert.h>
|
---|
43 |
|
---|
44 | /** QAccessibleWidget extension used as an accessibility interface for Chooser-view. */
|
---|
45 | class UIAccessibilityInterfaceForUIChooserView : public QAccessibleWidget
|
---|
46 | {
|
---|
47 | public:
|
---|
48 |
|
---|
49 | /** Returns an accessibility interface for passed @a strClassname and @a pObject. */
|
---|
50 | static QAccessibleInterface *pFactory(const QString &strClassname, QObject *pObject)
|
---|
51 | {
|
---|
52 | /* Creating Chooser-view accessibility interface: */
|
---|
53 | if (pObject && strClassname == QLatin1String("UIChooserView"))
|
---|
54 | return new UIAccessibilityInterfaceForUIChooserView(qobject_cast<QWidget*>(pObject));
|
---|
55 |
|
---|
56 | /* Null by default: */
|
---|
57 | return 0;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Constructs an accessibility interface passing @a pWidget to the base-class. */
|
---|
61 | UIAccessibilityInterfaceForUIChooserView(QWidget *pWidget)
|
---|
62 | : QAccessibleWidget(pWidget, QAccessible::List)
|
---|
63 | {}
|
---|
64 |
|
---|
65 | /** Returns the number of children. */
|
---|
66 | virtual int childCount() const RT_OVERRIDE
|
---|
67 | {
|
---|
68 | /* Make sure view still alive: */
|
---|
69 | AssertPtrReturn(view(), 0);
|
---|
70 |
|
---|
71 | /* Return the number of model children if model really assigned: */
|
---|
72 | return view()->model() ? view()->model()->root()->items().size() : 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Returns the child with the passed @a iIndex. */
|
---|
76 | virtual QAccessibleInterface *child(int iIndex) const RT_OVERRIDE
|
---|
77 | {
|
---|
78 | /* Make sure view still alive: */
|
---|
79 | AssertPtrReturn(view(), 0);
|
---|
80 | /* Make sure index is valid: */
|
---|
81 | AssertReturn(iIndex >= 0 && iIndex < childCount(), 0);
|
---|
82 |
|
---|
83 | /* Return the model child with the passed iIndex if model really assigned: */
|
---|
84 | return QAccessible::queryAccessibleInterface(view()->model() ? view()->model()->root()->items().at(iIndex) : 0);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Returns the index of passed @a pChild. */
|
---|
88 | virtual int indexOfChild(const QAccessibleInterface *pChild) const RT_OVERRIDE
|
---|
89 | {
|
---|
90 | /* Make sure view still alive: */
|
---|
91 | AssertPtrReturn(view(), -1);
|
---|
92 | /* Make sure child is valid: */
|
---|
93 | AssertReturn(pChild, -1);
|
---|
94 |
|
---|
95 | /* Acquire item itself: */
|
---|
96 | UIChooserItem *pChildItem = qobject_cast<UIChooserItem*>(pChild->object());
|
---|
97 |
|
---|
98 | /* Return the index of item in it's parent: */
|
---|
99 | return pChildItem && pChildItem->parentItem()
|
---|
100 | ? pChildItem->parentItem()->items().indexOf(pChildItem)
|
---|
101 | : -1;;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Returns a text for the passed @a enmTextRole. */
|
---|
105 | virtual QString text(QAccessible::Text enmTextRole) const RT_OVERRIDE
|
---|
106 | {
|
---|
107 | /* Make sure view still alive: */
|
---|
108 | AssertPtrReturn(view(), QString());
|
---|
109 |
|
---|
110 | /* Return view tool-tip: */
|
---|
111 | Q_UNUSED(enmTextRole);
|
---|
112 | return view()->whatsThis();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private:
|
---|
116 |
|
---|
117 | /** Returns corresponding Chooser-view. */
|
---|
118 | UIChooserView *view() const { return qobject_cast<UIChooserView*>(widget()); }
|
---|
119 | };
|
---|
120 |
|
---|
121 |
|
---|
122 | UIChooserView::UIChooserView(QWidget *pParent)
|
---|
123 | : QIGraphicsView(pParent)
|
---|
124 | , m_pChooserModel(0)
|
---|
125 | , m_pSearchWidget(0)
|
---|
126 | , m_iMinimumWidthHint(0)
|
---|
127 | {
|
---|
128 | prepare();
|
---|
129 | }
|
---|
130 |
|
---|
131 | void UIChooserView::setModel(UIChooserModel *pChooserModel)
|
---|
132 | {
|
---|
133 | m_pChooserModel = pChooserModel;
|
---|
134 | }
|
---|
135 |
|
---|
136 | UIChooserModel *UIChooserView::model() const
|
---|
137 | {
|
---|
138 | return m_pChooserModel;
|
---|
139 | }
|
---|
140 |
|
---|
141 | bool UIChooserView::isSearchWidgetVisible() const
|
---|
142 | {
|
---|
143 | /* Make sure search widget exists: */
|
---|
144 | AssertPtrReturn(m_pSearchWidget, false);
|
---|
145 |
|
---|
146 | /* Return widget visibility state: */
|
---|
147 | return m_pSearchWidget->isVisible();
|
---|
148 | }
|
---|
149 |
|
---|
150 | void UIChooserView::setSearchWidgetVisible(bool fVisible)
|
---|
151 | {
|
---|
152 | /* Make sure search widget exists: */
|
---|
153 | AssertPtrReturnVoid(m_pSearchWidget);
|
---|
154 |
|
---|
155 | /* Make sure keyboard focus is managed correctly: */
|
---|
156 | if (fVisible)
|
---|
157 | m_pSearchWidget->setFocus();
|
---|
158 | else
|
---|
159 | setFocus();
|
---|
160 |
|
---|
161 | /* Make sure visibility state is really changed: */
|
---|
162 | if (m_pSearchWidget->isVisible() == fVisible)
|
---|
163 | return;
|
---|
164 |
|
---|
165 | /* Set widget visibility state: */
|
---|
166 | m_pSearchWidget->setVisible(fVisible);
|
---|
167 |
|
---|
168 | /* Notify listeners: */
|
---|
169 | emit sigSearchWidgetVisibilityChanged(fVisible);
|
---|
170 |
|
---|
171 | /* Update geometry if widget is visible: */
|
---|
172 | if (m_pSearchWidget->isVisible())
|
---|
173 | updateSearchWidgetGeometry();
|
---|
174 |
|
---|
175 | /* Reset search each time widget visibility changed,
|
---|
176 | * Model can be undefined.. */
|
---|
177 | if (model())
|
---|
178 | model()->resetSearch();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void UIChooserView::setSearchResultsCount(int iTotalMatchCount, int iCurrentlyScrolledItemIndex)
|
---|
182 | {
|
---|
183 | /* Make sure search widget exists: */
|
---|
184 | AssertPtrReturnVoid(m_pSearchWidget);
|
---|
185 |
|
---|
186 | /* Update count of search results and scroll to certain result: */
|
---|
187 | m_pSearchWidget->setMatchCount(iTotalMatchCount);
|
---|
188 | m_pSearchWidget->setScroolToIndex(iCurrentlyScrolledItemIndex);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void UIChooserView::appendToSearchString(const QString &strSearchText)
|
---|
192 | {
|
---|
193 | /* Make sure search widget exists: */
|
---|
194 | AssertPtrReturnVoid(m_pSearchWidget);
|
---|
195 |
|
---|
196 | /* Update search string with passed text: */
|
---|
197 | m_pSearchWidget->appendToSearchString(strSearchText);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void UIChooserView::redoSearch()
|
---|
201 | {
|
---|
202 | /* Make sure search widget exists: */
|
---|
203 | AssertPtrReturnVoid(m_pSearchWidget);
|
---|
204 |
|
---|
205 | /* Pass request to search widget: */
|
---|
206 | m_pSearchWidget->redoSearch();
|
---|
207 | }
|
---|
208 |
|
---|
209 | void UIChooserView::sltMinimumWidthHintChanged(int iHint)
|
---|
210 | {
|
---|
211 | /* Is there something changed? */
|
---|
212 | if (m_iMinimumWidthHint == iHint)
|
---|
213 | return;
|
---|
214 |
|
---|
215 | /* Remember new value: */
|
---|
216 | m_iMinimumWidthHint = iHint;
|
---|
217 |
|
---|
218 | /* Set minimum view width according passed width-hint: */
|
---|
219 | setMinimumWidth(2 * frameWidth() + m_iMinimumWidthHint + verticalScrollBar()->sizeHint().width());
|
---|
220 |
|
---|
221 | /* Update scene rectangle: */
|
---|
222 | updateSceneRect();
|
---|
223 | }
|
---|
224 |
|
---|
225 | void UIChooserView::sltRedoSearch(const QString &strSearchTerm, int iSearchFlags)
|
---|
226 | {
|
---|
227 | /* Model can be undefined: */
|
---|
228 | if (!model())
|
---|
229 | return;
|
---|
230 |
|
---|
231 | /* Perform search: */
|
---|
232 | model()->performSearch(strSearchTerm, iSearchFlags);
|
---|
233 | }
|
---|
234 |
|
---|
235 | void UIChooserView::sltHandleScrollToSearchResult(bool fNext)
|
---|
236 | {
|
---|
237 | /* Model can be undefined: */
|
---|
238 | if (!model())
|
---|
239 | return;
|
---|
240 |
|
---|
241 | /* Move to requested search result: */
|
---|
242 | model()->selectSearchResult(fNext);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void UIChooserView::sltHandleSearchWidgetVisibilityToggle(bool fVisible)
|
---|
246 | {
|
---|
247 | setSearchWidgetVisible(fVisible);
|
---|
248 | }
|
---|
249 |
|
---|
250 | void UIChooserView::sltRetranslateUI()
|
---|
251 | {
|
---|
252 | /* Translate this: */
|
---|
253 | setWhatsThis(tr("Contains a tree of Virtual Machines and their groups"));
|
---|
254 | }
|
---|
255 |
|
---|
256 | void UIChooserView::resizeEvent(QResizeEvent *pEvent)
|
---|
257 | {
|
---|
258 | /* Call to base-class: */
|
---|
259 | QIGraphicsView::resizeEvent(pEvent);
|
---|
260 | /* Notify listeners: */
|
---|
261 | emit sigResized();
|
---|
262 |
|
---|
263 | /* Update everything: */
|
---|
264 | updateSceneRect();
|
---|
265 | updateSearchWidgetGeometry();
|
---|
266 | }
|
---|
267 |
|
---|
268 | void UIChooserView::prepare()
|
---|
269 | {
|
---|
270 | /* Install Chooser-view accessibility interface factory: */
|
---|
271 | QAccessible::installFactory(UIAccessibilityInterfaceForUIChooserView::pFactory);
|
---|
272 |
|
---|
273 | /* Prepare everything: */
|
---|
274 | prepareThis();
|
---|
275 | prepareWidget();
|
---|
276 |
|
---|
277 | /* Update everything: */
|
---|
278 | updateSceneRect();
|
---|
279 | updateSearchWidgetGeometry();
|
---|
280 |
|
---|
281 | /* Apply language settings: */
|
---|
282 | sltRetranslateUI();
|
---|
283 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
284 | this, &UIChooserView::sltRetranslateUI);
|
---|
285 | }
|
---|
286 |
|
---|
287 | void UIChooserView::prepareThis()
|
---|
288 | {
|
---|
289 | /* Prepare palette: */
|
---|
290 | preparePalette();
|
---|
291 |
|
---|
292 | /* Prepare frame: */
|
---|
293 | setFrameShape(QFrame::NoFrame);
|
---|
294 | setFrameShadow(QFrame::Plain);
|
---|
295 | setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
---|
296 |
|
---|
297 | /* Prepare scroll-bars policy: */
|
---|
298 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
299 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
300 |
|
---|
301 | /* Prepare connections: */
|
---|
302 | connect(&uiCommon(), &UICommon::sigThemeChange,
|
---|
303 | this, &UIChooserView::sltUpdatePalette);
|
---|
304 | }
|
---|
305 |
|
---|
306 | void UIChooserView::preparePalette()
|
---|
307 | {
|
---|
308 | QPalette pal = QApplication::palette();
|
---|
309 | pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Active, QPalette::Window));
|
---|
310 | pal.setColor(QPalette::Inactive, QPalette::Base, pal.color(QPalette::Inactive, QPalette::Window));
|
---|
311 | setPalette(pal);
|
---|
312 | }
|
---|
313 |
|
---|
314 | void UIChooserView::prepareWidget()
|
---|
315 | {
|
---|
316 | /* Create the search widget (initially hidden): */
|
---|
317 | m_pSearchWidget = new UIChooserSearchWidget(this);
|
---|
318 | if (m_pSearchWidget)
|
---|
319 | {
|
---|
320 | m_pSearchWidget->hide();
|
---|
321 | connect(m_pSearchWidget, &UIChooserSearchWidget::sigRedoSearch,
|
---|
322 | this, &UIChooserView::sltRedoSearch);
|
---|
323 | connect(m_pSearchWidget, &UIChooserSearchWidget::sigScrollToMatch,
|
---|
324 | this, &UIChooserView::sltHandleScrollToSearchResult);
|
---|
325 | connect(m_pSearchWidget, &UIChooserSearchWidget::sigToggleVisibility,
|
---|
326 | this, &UIChooserView::sltHandleSearchWidgetVisibilityToggle);
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | void UIChooserView::updateSceneRect()
|
---|
331 | {
|
---|
332 | setSceneRect(0, 0, m_iMinimumWidthHint, height());
|
---|
333 | }
|
---|
334 |
|
---|
335 | void UIChooserView::updateSearchWidgetGeometry()
|
---|
336 | {
|
---|
337 | /* Make sure search widget exists: */
|
---|
338 | AssertPtrReturnVoid(m_pSearchWidget);
|
---|
339 |
|
---|
340 | /* Update visible widget only: */
|
---|
341 | if (m_pSearchWidget->isVisible())
|
---|
342 | {
|
---|
343 | const int iHeight = m_pSearchWidget->height();
|
---|
344 | m_pSearchWidget->setGeometry(QRect(0, height() - iHeight, width(), iHeight));
|
---|
345 | }
|
---|
346 | }
|
---|