VirtualBox

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

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

FE/Qt. bugref:10622. Using new UITranslationEventListener in the runtime UI related classes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: UIInformationConfiguration.cpp 104290 2024-04-11 09:37:29Z 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 "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(&uiCommon(), &UICommon::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 createTableItems();
108}
109
110void UIInformationConfiguration::createTableItems()
111{
112 if (!m_pTableWidget || !gpMachine)
113 return;
114
115 resetTable();
116 UITextTable infoRows;
117
118 QFontMetrics fontMetrics(m_pTableWidget->font());
119 int iMaxColumn1Length = 0;
120
121 /* General section: */
122 insertTitleRow(m_strGeneralTitle, UIIconPool::iconSet(":/machine_16px.png"), fontMetrics);
123 gpMachine->generateMachineInformationGeneral(UIExtraDataMetaDefs::DetailsElementOptionTypeGeneral_Default, infoRows);
124 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
125
126 /* System section: */
127 insertTitleRow(m_strSystemTitle, UIIconPool::iconSet(":/chipset_16px.png"), fontMetrics);
128 infoRows.clear();
129 gpMachine->generateMachineInformationSystem(UIExtraDataMetaDefs::DetailsElementOptionTypeSystem_Default, infoRows);
130 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
131
132 /* Display section: */
133 insertTitleRow(m_strDisplayTitle, UIIconPool::iconSet(":/vrdp_16px.png"), fontMetrics);
134 infoRows.clear();
135 gpMachine->generateMachineInformationDisplay(UIExtraDataMetaDefs::DetailsElementOptionTypeDisplay_Default, infoRows);
136 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
137
138 /* Storage section: */
139 insertTitleRow(m_strStorageTitle, UIIconPool::iconSet(":/hd_16px.png"), fontMetrics);
140 infoRows.clear();
141 gpMachine->generateMachineInformationStorage(UIExtraDataMetaDefs::DetailsElementOptionTypeStorage_Default, infoRows);
142 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
143
144 /* Audio section: */
145 insertTitleRow(m_strAudioTitle, UIIconPool::iconSet(":/sound_16px.png"), fontMetrics);
146 infoRows.clear();
147 gpMachine->generateMachineInformationAudio(UIExtraDataMetaDefs::DetailsElementOptionTypeAudio_Default, infoRows);
148 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
149
150 /* Network section: */
151 insertTitleRow(m_strNetworkTitle, UIIconPool::iconSet(":/nw_16px.png"), fontMetrics);
152 infoRows.clear();
153 gpMachine->generateMachineInformationNetwork(UIExtraDataMetaDefs::DetailsElementOptionTypeNetwork_Default, infoRows);
154 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
155
156 /* Serial port section: */
157 insertTitleRow(m_strSerialPortsTitle, UIIconPool::iconSet(":/serial_port_16px.png"), fontMetrics);
158 infoRows.clear();
159 gpMachine->generateMachineInformationSerial(UIExtraDataMetaDefs::DetailsElementOptionTypeSerial_Default, infoRows);
160 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
161
162 /* USB section: */
163 insertTitleRow(m_strUSBTitle, UIIconPool::iconSet(":/usb_16px.png"), fontMetrics);
164 infoRows.clear();
165 gpMachine->generateMachineInformationUSB(UIExtraDataMetaDefs::DetailsElementOptionTypeUsb_Default, infoRows);
166 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
167
168 /* Shared folders section: */
169 insertTitleRow(m_strSharedFoldersTitle, UIIconPool::iconSet(":/sf_16px.png"), fontMetrics);
170 infoRows.clear();
171 gpMachine->generateMachineInformationSharedFolders(UIExtraDataMetaDefs::DetailsElementOptionTypeSharedFolders_Default, infoRows);
172 insertInfoRows(infoRows, fontMetrics, iMaxColumn1Length);
173
174 m_pTableWidget->resizeColumnToContents(0);
175 /* Resize the column 1 a bit larger than the max string if contains: */
176 m_pTableWidget->setColumnWidth(1, 1.5 * iMaxColumn1Length);
177 m_pTableWidget->resizeColumnToContents(2);
178 m_pTableWidget->horizontalHeader()->setStretchLastSection(true);
179}
180
181void UIInformationConfiguration::prepareObjects()
182{
183 /* Create layout: */
184 m_pMainLayout = new QVBoxLayout(this);
185 if (!m_pMainLayout)
186 return;
187 m_pMainLayout->setSpacing(0);
188
189 m_pTableWidget = new QTableWidget;
190 if (m_pTableWidget)
191 {
192 /* Configure the table by hiding the headers etc.: */
193 m_pTableWidget->setColumnCount(m_iColumCount);
194 m_pTableWidget->setAlternatingRowColors(true);
195 m_pTableWidget->verticalHeader()->hide();
196 m_pTableWidget->horizontalHeader()->hide();
197 m_pTableWidget->setShowGrid(false);
198 m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
199 m_pTableWidget->setFocusPolicy(Qt::NoFocus);
200 m_pTableWidget->setSelectionMode(QAbstractItemView::NoSelection);
201 m_pTableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
202 connect(m_pTableWidget, &QTableWidget::customContextMenuRequested,
203 this, &UIInformationConfiguration::sltHandleTableContextMenuRequest);
204 m_pMainLayout->addWidget(m_pTableWidget);
205 }
206 m_pCopyWholeTableAction = new QAction(this);
207 connect(m_pCopyWholeTableAction, &QAction::triggered,
208 this, &UIInformationConfiguration::sltCopyTableToClipboard);
209}
210
211void UIInformationConfiguration::insertInfoRows(const UITextTable &table, const QFontMetrics &fontMetrics, int &iMaxColumn1Length)
212{
213 foreach (const UITextTableLine &line, table)
214 {
215 insertInfoRow(removeHtmlFromString(line.string1()),
216 removeHtmlFromString(line.string2()),
217 fontMetrics, iMaxColumn1Length);
218 }
219}
220
221void UIInformationConfiguration::insertTitleRow(const QString &strTitle, const QIcon &icon, const QFontMetrics &fontMetrics)
222{
223 int iRow = m_pTableWidget->rowCount();
224 m_pTableWidget->insertRow(iRow);
225 QSize iconSize;
226 icon.actualSize(iconSize);
227 m_pTableWidget->setRowHeight(iRow,
228 qMax(fontMetrics.height() + m_iRowTopMargin + m_iRowBottomMargin, iconSize.height()));
229 m_pTableWidget->setItem(iRow, 0, new QTableWidgetItem(icon, ""));
230 QTableWidgetItem *pTitleItem = new QTableWidgetItem(strTitle);
231 QFont font = pTitleItem->font();
232 font.setBold(true);
233 pTitleItem->setFont(font);
234 m_pTableWidget->setItem(iRow, 1, pTitleItem);
235}
236
237void UIInformationConfiguration::insertInfoRow(const QString strText1, const QString &strText2,
238 const QFontMetrics &fontMetrics, int &iMaxColumn1Length)
239{
240 int iRow = m_pTableWidget->rowCount();
241 m_pTableWidget->insertRow(iRow);
242 m_pTableWidget->setRowHeight(iRow, fontMetrics.height() + m_iRowTopMargin + m_iRowBottomMargin);
243#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
244 iMaxColumn1Length = qMax(iMaxColumn1Length, fontMetrics.horizontalAdvance(strText1));
245#else
246 iMaxColumn1Length = qMax(iMaxColumn1Length, fontMetrics.width(strText1));
247#endif
248 m_pTableWidget->setItem(iRow, 1, new QTableWidgetItem(strText1));
249 m_pTableWidget->setItem(iRow, 2, new QTableWidgetItem(strText2));
250}
251
252void UIInformationConfiguration::resetTable()
253{
254 if (m_pTableWidget)
255 {
256 m_pTableWidget->clear();
257 m_pTableWidget->setRowCount(0);
258 m_pTableWidget->setColumnCount(m_iColumCount);
259 }
260}
261
262QString UIInformationConfiguration::removeHtmlFromString(const QString &strOriginal)
263{
264 QTextDocument textDocument;
265 textDocument.setHtml(strOriginal);
266 return textDocument.toPlainText();
267}
268
269QString UIInformationConfiguration::tableData() const
270{
271 AssertReturn(m_pTableWidget, QString());
272 AssertReturn(m_pTableWidget->columnCount() == 3, QString());
273 QStringList data;
274 for (int i = 0; i < m_pTableWidget->rowCount(); ++i)
275 {
276 /* Skip the first column as it contains only icon and no text: */
277 QTableWidgetItem *pItem = m_pTableWidget->item(i, 1);
278 QString strColumn1 = pItem ? pItem->text() : QString();
279 pItem = m_pTableWidget->item(i, 2);
280 QString strColumn2 = pItem ? pItem->text() : QString();
281 if (strColumn2.isEmpty())
282 data << strColumn1;
283 else
284 data << strColumn1 << ": " << strColumn2;
285 data << "\n";
286 }
287 return data.join(QString());
288}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use