VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/UIInformationConfiguration.cpp

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

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

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