1 | /* $Id: UIDetailsView.cpp 104251 2024-04-09 12:36:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIDetailsView class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2023 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 <QAccessibleWidget>
|
---|
30 | #include <QApplication>
|
---|
31 | #include <QScrollBar>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "UICommon.h"
|
---|
35 | #include "UIDetails.h"
|
---|
36 | #include "UIDetailsItem.h"
|
---|
37 | #include "UIDetailsModel.h"
|
---|
38 | #include "UIDetailsView.h"
|
---|
39 | #include "UITranslationEventListener.h"
|
---|
40 |
|
---|
41 | /* Other VBox includes: */
|
---|
42 | #include <iprt/assert.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /** QAccessibleWidget extension used as an accessibility interface for Details-view. */
|
---|
46 | class UIAccessibilityInterfaceForUIDetailsView : public QAccessibleWidget
|
---|
47 | {
|
---|
48 | public:
|
---|
49 |
|
---|
50 | /** Returns an accessibility interface for passed @a strClassname and @a pObject. */
|
---|
51 | static QAccessibleInterface *pFactory(const QString &strClassname, QObject *pObject)
|
---|
52 | {
|
---|
53 | /* Creating Details-view accessibility interface: */
|
---|
54 | if (pObject && strClassname == QLatin1String("UIDetailsView"))
|
---|
55 | return new UIAccessibilityInterfaceForUIDetailsView(qobject_cast<QWidget*>(pObject));
|
---|
56 |
|
---|
57 | /* Null by default: */
|
---|
58 | return 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Constructs an accessibility interface passing @a pWidget to the base-class. */
|
---|
62 | UIAccessibilityInterfaceForUIDetailsView(QWidget *pWidget)
|
---|
63 | : QAccessibleWidget(pWidget, QAccessible::List)
|
---|
64 | {}
|
---|
65 |
|
---|
66 | /** Returns the number of children. */
|
---|
67 | virtual int childCount() const RT_OVERRIDE
|
---|
68 | {
|
---|
69 | /* Make sure view still alive: */
|
---|
70 | AssertPtrReturn(view(), 0);
|
---|
71 |
|
---|
72 | /* What amount of children root has? */
|
---|
73 | const int cChildCount = view()->details()->model()->root()->items().size();
|
---|
74 |
|
---|
75 | /* Return amount of children root has (if there are many of children): */
|
---|
76 | if (cChildCount > 1)
|
---|
77 | return cChildCount;
|
---|
78 |
|
---|
79 | /* Return the number of children lone root child has (otherwise): */
|
---|
80 | return view()->details()->model()->root()->items().first()->items().size();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Returns the child with the passed @a iIndex. */
|
---|
84 | virtual QAccessibleInterface *child(int iIndex) const RT_OVERRIDE
|
---|
85 | {
|
---|
86 | /* Make sure view still alive: */
|
---|
87 | AssertPtrReturn(view(), 0);
|
---|
88 | /* Make sure index is valid: */
|
---|
89 | AssertReturn(iIndex >= 0 && iIndex < childCount(), 0);
|
---|
90 |
|
---|
91 | /* What amount of children root has? */
|
---|
92 | const int cChildCount = view()->details()->model()->root()->items().size();
|
---|
93 |
|
---|
94 | /* Return the root child with the passed iIndex (if there are many of children): */
|
---|
95 | if (cChildCount > 1)
|
---|
96 | return QAccessible::queryAccessibleInterface(view()->details()->model()->root()->items().at(iIndex));
|
---|
97 |
|
---|
98 | /* Return the lone root child's child with the passed iIndex (otherwise): */
|
---|
99 | return QAccessible::queryAccessibleInterface(view()->details()->model()->root()->items().first()->items().at(iIndex));
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Returns the index of passed @a pChild. */
|
---|
103 | virtual int indexOfChild(const QAccessibleInterface *pChild) const RT_OVERRIDE
|
---|
104 | {
|
---|
105 | /* Make sure view still alive: */
|
---|
106 | AssertPtrReturn(view(), -1);
|
---|
107 | /* Make sure child is valid: */
|
---|
108 | AssertReturn(pChild, -1);
|
---|
109 |
|
---|
110 | /* Acquire item itself: */
|
---|
111 | UIDetailsItem *pChildItem = qobject_cast<UIDetailsItem*>(pChild->object());
|
---|
112 |
|
---|
113 | /* Return the index of item in it's parent: */
|
---|
114 | return pChildItem && pChildItem->parentItem()
|
---|
115 | ? pChildItem->parentItem()->items().indexOf(pChildItem)
|
---|
116 | : -1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Returns a text for the passed @a enmTextRole. */
|
---|
120 | virtual QString text(QAccessible::Text enmTextRole) const RT_OVERRIDE
|
---|
121 | {
|
---|
122 | /* Make sure view still alive: */
|
---|
123 | AssertPtrReturn(view(), QString());
|
---|
124 |
|
---|
125 | /* Return view tool-tip: */
|
---|
126 | Q_UNUSED(enmTextRole);
|
---|
127 | return view()->whatsThis();
|
---|
128 | }
|
---|
129 |
|
---|
130 | private:
|
---|
131 |
|
---|
132 | /** Returns corresponding Details-view. */
|
---|
133 | UIDetailsView *view() const { return qobject_cast<UIDetailsView*>(widget()); }
|
---|
134 | };
|
---|
135 |
|
---|
136 |
|
---|
137 | UIDetailsView::UIDetailsView(UIDetails *pParent)
|
---|
138 | : QIGraphicsView(pParent)
|
---|
139 | , m_pDetails(pParent)
|
---|
140 | , m_iMinimumWidthHint(0)
|
---|
141 | {
|
---|
142 | prepare();
|
---|
143 | }
|
---|
144 |
|
---|
145 | void UIDetailsView::sltMinimumWidthHintChanged(int iHint)
|
---|
146 | {
|
---|
147 | /* Is there something changed? */
|
---|
148 | if (m_iMinimumWidthHint == iHint)
|
---|
149 | return;
|
---|
150 |
|
---|
151 | /* Remember new value: */
|
---|
152 | m_iMinimumWidthHint = iHint;
|
---|
153 | if (m_iMinimumWidthHint <= 0)
|
---|
154 | m_iMinimumWidthHint = 1;
|
---|
155 |
|
---|
156 | /* Set minimum view width according passed width-hint: */
|
---|
157 | setMinimumWidth(2 * frameWidth() + m_iMinimumWidthHint + verticalScrollBar()->sizeHint().width());
|
---|
158 |
|
---|
159 | /* Update scene-rect: */
|
---|
160 | updateSceneRect();
|
---|
161 | }
|
---|
162 |
|
---|
163 | void UIDetailsView::sltRetranslateUI()
|
---|
164 | {
|
---|
165 | /* Translate this: */
|
---|
166 | setWhatsThis(tr("Contains a list of Virtual Machine details."));
|
---|
167 | }
|
---|
168 |
|
---|
169 | void UIDetailsView::resizeEvent(QResizeEvent *pEvent)
|
---|
170 | {
|
---|
171 | /* Call to base-class: */
|
---|
172 | QIGraphicsView::resizeEvent(pEvent);
|
---|
173 | /* Notify listeners: */
|
---|
174 | emit sigResized();
|
---|
175 |
|
---|
176 | /* Update everything: */
|
---|
177 | updateSceneRect();
|
---|
178 | }
|
---|
179 |
|
---|
180 | void UIDetailsView::prepare()
|
---|
181 | {
|
---|
182 | /* Install Details-view accessibility interface factory: */
|
---|
183 | QAccessible::installFactory(UIAccessibilityInterfaceForUIDetailsView::pFactory);
|
---|
184 |
|
---|
185 | /* Prepares everything: */
|
---|
186 | prepareThis();
|
---|
187 |
|
---|
188 | /* Update everything: */
|
---|
189 | updateSceneRect();
|
---|
190 |
|
---|
191 | /* Translate finally: */
|
---|
192 | sltRetranslateUI();
|
---|
193 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
194 | this, &UIDetailsView::sltRetranslateUI);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void UIDetailsView::prepareThis()
|
---|
198 | {
|
---|
199 | /* Prepare palette: */
|
---|
200 | preparePalette();
|
---|
201 |
|
---|
202 | /* Prepare frame: */
|
---|
203 | setFrameShape(QFrame::NoFrame);
|
---|
204 | setFrameShadow(QFrame::Plain);
|
---|
205 | setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
---|
206 |
|
---|
207 | /* Prepare scroll-bars policy: */
|
---|
208 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
209 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
210 |
|
---|
211 | /* Prepare connections: */
|
---|
212 | connect(&uiCommon(), &UICommon::sigThemeChange,
|
---|
213 | this, &UIDetailsView::sltUpdatePalette);
|
---|
214 | }
|
---|
215 |
|
---|
216 | void UIDetailsView::preparePalette()
|
---|
217 | {
|
---|
218 | QPalette pal = QApplication::palette();
|
---|
219 | pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Active, QPalette::Window));
|
---|
220 | pal.setColor(QPalette::Inactive, QPalette::Base, pal.color(QPalette::Inactive, QPalette::Window));
|
---|
221 | setPalette(pal);
|
---|
222 | }
|
---|
223 |
|
---|
224 | void UIDetailsView::updateSceneRect()
|
---|
225 | {
|
---|
226 | setSceneRect(0, 0, m_iMinimumWidthHint, height());
|
---|
227 | }
|
---|