1 | /* $Id: UIInformationConfiguration.cpp 98768 2023-02-28 09:24:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIInformationConfiguration class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-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 | /* COM includes: */
|
---|
29 |
|
---|
30 | /* Qt includes: */
|
---|
31 | #include <QAction>
|
---|
32 | #include <QApplication>
|
---|
33 | #include <QClipboard>
|
---|
34 | #include <QHeaderView>
|
---|
35 | #include <QMenu>
|
---|
36 | #include <QTableWidget>
|
---|
37 | #include <QTextDocument>
|
---|
38 | #include <QVBoxLayout>
|
---|
39 |
|
---|
40 | /* GUI includes: */
|
---|
41 | #include "UIDetailsGenerator.h"
|
---|
42 | #include "UICommon.h"
|
---|
43 | #include "UIExtraDataManager.h"
|
---|
44 | #include "UIIconPool.h"
|
---|
45 | #include "UIInformationConfiguration.h"
|
---|
46 | #include "UIMachine.h"
|
---|
47 | #include "UIVirtualBoxEventHandler.h"
|
---|
48 |
|
---|
49 | UIInformationConfiguration::UIInformationConfiguration(QWidget *pParent)
|
---|
50 | : QIWithRetranslateUI<QWidget>(pParent)
|
---|
51 | , m_pMainLayout(0)
|
---|
52 | , m_pTableWidget(0)
|
---|
53 | , m_pCopyWholeTableAction(0)
|
---|
54 | , m_iColumCount(3)
|
---|
55 | , m_iRowLeftMargin(0.2 * qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin))
|
---|
56 | , m_iRowTopMargin(0.2 * qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin))
|
---|
57 | , m_iRowRightMargin(0.2 * qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin))
|
---|
58 | , m_iRowBottomMargin(0.2 * qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin))
|
---|
59 | {
|
---|
60 | prepareObjects();
|
---|
61 | retranslateUi();
|
---|
62 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigMachineDataChange,
|
---|
63 | this, &UIInformationConfiguration::sltMachineDataChanged);
|
---|
64 | connect(&uiCommon(), &UICommon::sigMediumEnumerationFinished,
|
---|
65 | this, &UIInformationConfiguration::sltMachineDataChanged);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void UIInformationConfiguration::sltMachineDataChanged()
|
---|
69 | {
|
---|
70 | createTableItems();
|
---|
71 | }
|
---|
72 |
|
---|
73 | void UIInformationConfiguration::sltHandleTableContextMenuRequest(const QPoint &position)
|
---|
74 | {
|
---|
75 | if (!m_pCopyWholeTableAction)
|
---|
76 | return;
|
---|
77 |
|
---|
78 | QMenu menu(this);
|
---|
79 | menu.addAction(m_pCopyWholeTableAction);
|
---|
80 | menu.exec(mapToGlobal(position));
|
---|
81 | }
|
---|
82 |
|
---|
83 | void UIInformationConfiguration::sltCopyTableToClipboard()
|
---|
84 | {
|
---|
85 | QClipboard *pClipboard = QApplication::clipboard();
|
---|
86 | if (!pClipboard)
|
---|
87 | return;
|
---|
88 | pClipboard->setText(tableData(), QClipboard::Clipboard);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void UIInformationConfiguration::retranslateUi()
|
---|
92 | {
|
---|
93 | m_strGeneralTitle = QApplication::translate("UIVMInformationDialog", "General");
|
---|
94 | m_strSystemTitle = QApplication::translate("UIVMInformationDialog", "System");
|
---|
95 | m_strDisplayTitle = QApplication::translate("UIVMInformationDialog", "Display");
|
---|
96 | m_strStorageTitle = QApplication::translate("UIVMInformationDialog", "Storage");
|
---|
97 | m_strAudioTitle = QApplication::translate("UIVMInformationDialog", "Audio");
|
---|
98 | m_strNetworkTitle = QApplication::translate("UIVMInformationDialog", "Network");
|
---|
99 | m_strSerialPortsTitle = QApplication::translate("UIVMInformationDialog", "Serial Ports");
|
---|
100 | m_strUSBTitle = QApplication::translate("UIVMInformationDialog", "USB");
|
---|
101 | m_strSharedFoldersTitle = QApplication::translate("UIVMInformationDialog", "Shared Folders");
|
---|
102 | if (m_pCopyWholeTableAction)
|
---|
103 | m_pCopyWholeTableAction->setText(QApplication::translate("UIVMInformationDialog", "Copy All"));
|
---|
104 | createTableItems();
|
---|
105 | }
|
---|
106 |
|
---|
107 | void UIInformationConfiguration::createTableItems()
|
---|
108 | {
|
---|
109 | if (!m_pTableWidget || !gpMachine)
|
---|
110 | return;
|
---|
111 |
|
---|
112 | resetTable();
|
---|
113 | UITextTable infoRows;
|
---|
114 |
|
---|
115 | QFontMetrics fontMetrics(m_pTableWidget->font());
|
---|
116 | int iMaxColumn1Length = 0;
|
---|
117 |
|
---|
118 | /* General section: */
|
---|
119 | insertTitleRow(m_strGeneralTitle, UIIconPool::iconSet(":/machine_16px.png"), fontMetrics);
|
---|
120 | gpMachine->generateMachineInformationGeneral(UIExtraDataMetaDefs::DetailsElementOptionTypeGeneral_Default, infoRows);
|
---|
121 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
122 |
|
---|
123 | /* System section: */
|
---|
124 | insertTitleRow(m_strSystemTitle, UIIconPool::iconSet(":/chipset_16px.png"), fontMetrics);
|
---|
125 | infoRows.clear();
|
---|
126 | gpMachine->generateMachineInformationSystem(UIExtraDataMetaDefs::DetailsElementOptionTypeSystem_Default, infoRows);
|
---|
127 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
128 |
|
---|
129 | /* Display section: */
|
---|
130 | insertTitleRow(m_strDisplayTitle, UIIconPool::iconSet(":/vrdp_16px.png"), fontMetrics);
|
---|
131 | infoRows.clear();
|
---|
132 | gpMachine->generateMachineInformationDisplay(UIExtraDataMetaDefs::DetailsElementOptionTypeDisplay_Default, infoRows);
|
---|
133 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
134 |
|
---|
135 | /* Storage section: */
|
---|
136 | insertTitleRow(m_strStorageTitle, UIIconPool::iconSet(":/hd_16px.png"), fontMetrics);
|
---|
137 | infoRows.clear();
|
---|
138 | gpMachine->generateMachineInformationStorage(UIExtraDataMetaDefs::DetailsElementOptionTypeStorage_Default, infoRows);
|
---|
139 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
140 |
|
---|
141 | /* Audio section: */
|
---|
142 | insertTitleRow(m_strAudioTitle, UIIconPool::iconSet(":/sound_16px.png"), fontMetrics);
|
---|
143 | infoRows.clear();
|
---|
144 | gpMachine->generateMachineInformationAudio(UIExtraDataMetaDefs::DetailsElementOptionTypeAudio_Default, infoRows);
|
---|
145 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
146 |
|
---|
147 | /* Network section: */
|
---|
148 | insertTitleRow(m_strNetworkTitle, UIIconPool::iconSet(":/nw_16px.png"), fontMetrics);
|
---|
149 | infoRows.clear();
|
---|
150 | gpMachine->generateMachineInformationNetwork(UIExtraDataMetaDefs::DetailsElementOptionTypeNetwork_Default, infoRows);
|
---|
151 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
152 |
|
---|
153 | /* Serial port section: */
|
---|
154 | insertTitleRow(m_strSerialPortsTitle, UIIconPool::iconSet(":/serial_port_16px.png"), fontMetrics);
|
---|
155 | infoRows.clear();
|
---|
156 | gpMachine->generateMachineInformationSerial(UIExtraDataMetaDefs::DetailsElementOptionTypeSerial_Default, infoRows);
|
---|
157 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
158 |
|
---|
159 | /* USB section: */
|
---|
160 | insertTitleRow(m_strUSBTitle, UIIconPool::iconSet(":/usb_16px.png"), fontMetrics);
|
---|
161 | infoRows.clear();
|
---|
162 | gpMachine->generateMachineInformationUSB(UIExtraDataMetaDefs::DetailsElementOptionTypeUsb_Default, infoRows);
|
---|
163 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
164 |
|
---|
165 | /* Shared folders section: */
|
---|
166 | insertTitleRow(m_strSharedFoldersTitle, UIIconPool::iconSet(":/sf_16px.png"), fontMetrics);
|
---|
167 | infoRows.clear();
|
---|
168 | gpMachine->generateMachineInformationSharedFolders(UIExtraDataMetaDefs::DetailsElementOptionTypeSharedFolders_Default, infoRows);
|
---|
169 | insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
|
---|
170 |
|
---|
171 | m_pTableWidget->resizeColumnToContents(0);
|
---|
172 | /* Resize the column 1 a bit larger than the max string if contains: */
|
---|
173 | m_pTableWidget->setColumnWidth(1, 1.5 * iMaxColumn1Length);
|
---|
174 | m_pTableWidget->resizeColumnToContents(2);
|
---|
175 | m_pTableWidget->horizontalHeader()->setStretchLastSection(true);
|
---|
176 | }
|
---|
177 |
|
---|
178 | void UIInformationConfiguration::prepareObjects()
|
---|
179 | {
|
---|
180 | /* Create layout: */
|
---|
181 | m_pMainLayout = new QVBoxLayout(this);
|
---|
182 | if (!m_pMainLayout)
|
---|
183 | return;
|
---|
184 | m_pMainLayout->setSpacing(0);
|
---|
185 |
|
---|
186 | m_pTableWidget = new QTableWidget;
|
---|
187 | if (m_pTableWidget)
|
---|
188 | {
|
---|
189 | /* Configure the table by hiding the headers etc.: */
|
---|
190 | m_pTableWidget->setColumnCount(m_iColumCount);
|
---|
191 | m_pTableWidget->setAlternatingRowColors(true);
|
---|
192 | m_pTableWidget->verticalHeader()->hide();
|
---|
193 | m_pTableWidget->horizontalHeader()->hide();
|
---|
194 | m_pTableWidget->setShowGrid(false);
|
---|
195 | m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
---|
196 | m_pTableWidget->setFocusPolicy(Qt::NoFocus);
|
---|
197 | m_pTableWidget->setSelectionMode(QAbstractItemView::NoSelection);
|
---|
198 | m_pTableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
---|
199 | connect(m_pTableWidget, &QTableWidget::customContextMenuRequested,
|
---|
200 | this, &UIInformationConfiguration::sltHandleTableContextMenuRequest);
|
---|
201 | m_pMainLayout->addWidget(m_pTableWidget);
|
---|
202 | }
|
---|
203 | m_pCopyWholeTableAction = new QAction(this);
|
---|
204 | connect(m_pCopyWholeTableAction, &QAction::triggered,
|
---|
205 | this, &UIInformationConfiguration::sltCopyTableToClipboard);
|
---|
206 | }
|
---|
207 |
|
---|
208 | void UIInformationConfiguration::insertInfoRows(const UITextTable &table, const QFontMetrics &fontMetrics, int &iMaxColumn1Length)
|
---|
209 | {
|
---|
210 | foreach (const UITextTableLine &line, table)
|
---|
211 | {
|
---|
212 | insertInfoRow(removeHtmlFromString(line.string1()),
|
---|
213 | removeHtmlFromString(line.string2()),
|
---|
214 | fontMetrics, iMaxColumn1Length);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | void UIInformationConfiguration::insertTitleRow(const QString &strTitle, const QIcon &icon, const QFontMetrics &fontMetrics)
|
---|
219 | {
|
---|
220 | int iRow = m_pTableWidget->rowCount();
|
---|
221 | m_pTableWidget->insertRow(iRow);
|
---|
222 | QSize iconSize;
|
---|
223 | icon.actualSize(iconSize);
|
---|
224 | m_pTableWidget->setRowHeight(iRow,
|
---|
225 | qMax(fontMetrics.height() + m_iRowTopMargin + m_iRowBottomMargin, iconSize.height()));
|
---|
226 | m_pTableWidget->setItem(iRow, 0, new QTableWidgetItem(icon, ""));
|
---|
227 | QTableWidgetItem *pTitleItem = new QTableWidgetItem(strTitle);
|
---|
228 | QFont font = pTitleItem->font();
|
---|
229 | font.setBold(true);
|
---|
230 | pTitleItem->setFont(font);
|
---|
231 | m_pTableWidget->setItem(iRow, 1, pTitleItem);
|
---|
232 | }
|
---|
233 |
|
---|
234 | void UIInformationConfiguration::insertInfoRow(const QString strText1, const QString &strText2,
|
---|
235 | const QFontMetrics &fontMetrics, int &iMaxColumn1Length)
|
---|
236 | {
|
---|
237 | int iRow = m_pTableWidget->rowCount();
|
---|
238 | m_pTableWidget->insertRow(iRow);
|
---|
239 | m_pTableWidget->setRowHeight(iRow, fontMetrics.height() + m_iRowTopMargin + m_iRowBottomMargin);
|
---|
240 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
---|
241 | iMaxColumn1Length = qMax(iMaxColumn1Length, fontMetrics.horizontalAdvance(strText1));
|
---|
242 | #else
|
---|
243 | iMaxColumn1Length = qMax(iMaxColumn1Length, fontMetrics.width(strText1));
|
---|
244 | #endif
|
---|
245 | m_pTableWidget->setItem(iRow, 1, new QTableWidgetItem(strText1));
|
---|
246 | m_pTableWidget->setItem(iRow, 2, new QTableWidgetItem(strText2));
|
---|
247 | }
|
---|
248 |
|
---|
249 | void UIInformationConfiguration::resetTable()
|
---|
250 | {
|
---|
251 | if (m_pTableWidget)
|
---|
252 | {
|
---|
253 | m_pTableWidget->clear();
|
---|
254 | m_pTableWidget->setRowCount(0);
|
---|
255 | m_pTableWidget->setColumnCount(m_iColumCount);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | QString UIInformationConfiguration::removeHtmlFromString(const QString &strOriginal)
|
---|
260 | {
|
---|
261 | QTextDocument textDocument;
|
---|
262 | textDocument.setHtml(strOriginal);
|
---|
263 | return textDocument.toPlainText();
|
---|
264 | }
|
---|
265 |
|
---|
266 | QString UIInformationConfiguration::tableData() const
|
---|
267 | {
|
---|
268 | AssertReturn(m_pTableWidget, QString());
|
---|
269 | AssertReturn(m_pTableWidget->columnCount() == 3, QString());
|
---|
270 | QStringList data;
|
---|
271 | for (int i = 0; i < m_pTableWidget->rowCount(); ++i)
|
---|
272 | {
|
---|
273 | /* Skip the first column as it contains only icon and no text: */
|
---|
274 | QTableWidgetItem *pItem = m_pTableWidget->item(i, 1);
|
---|
275 | QString strColumn1 = pItem ? pItem->text() : QString();
|
---|
276 | pItem = m_pTableWidget->item(i, 2);
|
---|
277 | QString strColumn2 = pItem ? pItem->text() : QString();
|
---|
278 | if (strColumn2.isEmpty())
|
---|
279 | data << strColumn1;
|
---|
280 | else
|
---|
281 | data << strColumn1 << ": " << strColumn2;
|
---|
282 | data << "\n";
|
---|
283 | }
|
---|
284 | return data.join(QString());
|
---|
285 | }
|
---|