1 | /* $Id: UITakeSnapshotDialog.cpp 100075 2023-06-05 16:38:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UITakeSnapshotDialog class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 <QGridLayout>
|
---|
30 | #include <QLabel>
|
---|
31 | #include <QLineEdit>
|
---|
32 | #include <QPushButton>
|
---|
33 | #include <QStyle>
|
---|
34 | #ifdef VBOX_IS_QT6_OR_LATER
|
---|
35 | # include <QWindow>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | /* GUI includes: */
|
---|
39 | #include "QIDialogButtonBox.h"
|
---|
40 | #include "QILabel.h"
|
---|
41 | #include "VBoxUtils.h"
|
---|
42 | #include "UICommon.h"
|
---|
43 | #include "UIDesktopWidgetWatchdog.h"
|
---|
44 | #include "UIHelpBrowserDialog.h"
|
---|
45 | #include "UITakeSnapshotDialog.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | UITakeSnapshotDialog::UITakeSnapshotDialog(QWidget *pParent, ulong cImmutableMedia)
|
---|
49 | : QIWithRetranslateUI<QIDialog>(pParent)
|
---|
50 | , m_cImmutableMedia(cImmutableMedia)
|
---|
51 | , m_pLabelIcon(0)
|
---|
52 | , m_pLabelName(0), m_pEditorName(0)
|
---|
53 | , m_pLabelDescription(0), m_pEditorDescription(0)
|
---|
54 | , m_pLabelInfo(0)
|
---|
55 | , m_pButtonBox(0)
|
---|
56 | {
|
---|
57 | prepare();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void UITakeSnapshotDialog::setIcon(const QIcon &icon)
|
---|
61 | {
|
---|
62 | m_icon = icon;
|
---|
63 | updatePixmap();
|
---|
64 | }
|
---|
65 |
|
---|
66 | void UITakeSnapshotDialog::setName(const QString &strName)
|
---|
67 | {
|
---|
68 | m_pEditorName->setText(strName);
|
---|
69 | }
|
---|
70 |
|
---|
71 | QString UITakeSnapshotDialog::name() const
|
---|
72 | {
|
---|
73 | return m_pEditorName->text();
|
---|
74 | }
|
---|
75 |
|
---|
76 | QString UITakeSnapshotDialog::description() const
|
---|
77 | {
|
---|
78 | return m_pEditorDescription->toPlainText();
|
---|
79 | }
|
---|
80 |
|
---|
81 | bool UITakeSnapshotDialog::event(QEvent *pEvent)
|
---|
82 | {
|
---|
83 | /* Handle know event types: */
|
---|
84 | switch (pEvent->type())
|
---|
85 | {
|
---|
86 | case QEvent::Show:
|
---|
87 | case QEvent::ScreenChangeInternal:
|
---|
88 | {
|
---|
89 | /* Update pixmap: */
|
---|
90 | updatePixmap();
|
---|
91 | break;
|
---|
92 | }
|
---|
93 | default:
|
---|
94 | break;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* Call to base-class: */
|
---|
98 | return QIWithRetranslateUI<QIDialog>::event(pEvent);
|
---|
99 | }
|
---|
100 |
|
---|
101 | void UITakeSnapshotDialog::retranslateUi()
|
---|
102 | {
|
---|
103 | setWindowTitle(tr("Take Snapshot of Virtual Machine"));
|
---|
104 | m_pLabelName->setText(tr("Snapshot &Name"));
|
---|
105 | m_pEditorName->setToolTip(tr("Holds the snapshot name"));
|
---|
106 | m_pLabelDescription->setText(tr("Snapshot &Description"));
|
---|
107 | m_pEditorDescription->setToolTip(tr("Holds the snapshot description"));
|
---|
108 | m_pLabelInfo->setText(tr("Warning: You are taking a snapshot of a running machine which has %n immutable image(s) "
|
---|
109 | "attached to it. As long as you are working from this snapshot the immutable image(s) "
|
---|
110 | "will not be reset to avoid loss of data.", "", m_cImmutableMedia));
|
---|
111 |
|
---|
112 | if (m_pButtonBox)
|
---|
113 | {
|
---|
114 | m_pButtonBox->button(QDialogButtonBox::Ok)->setText(tr("Ok"));
|
---|
115 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
|
---|
116 | m_pButtonBox->button(QDialogButtonBox::Help)->setText(tr("Help"));
|
---|
117 |
|
---|
118 | m_pButtonBox->button(QDialogButtonBox::Ok)->setStatusTip(tr("Take Snapshot and close the dialog"));
|
---|
119 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close dialog without taking a snapshot"));
|
---|
120 | m_pButtonBox->button(QDialogButtonBox::Help)->setStatusTip(tr("Show dialog help"));
|
---|
121 |
|
---|
122 | m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);
|
---|
123 |
|
---|
124 | if (m_pButtonBox->button(QDialogButtonBox::Ok)->shortcut().toString().isEmpty())
|
---|
125 | m_pButtonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Accept"));
|
---|
126 | else
|
---|
127 | m_pButtonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Accept (%1)").arg(m_pButtonBox->button(QDialogButtonBox::Ok)->shortcut().toString()));
|
---|
128 |
|
---|
129 | if (m_pButtonBox->button(QDialogButtonBox::Cancel)->shortcut().toString().isEmpty())
|
---|
130 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(tr("Cancel"));
|
---|
131 | else
|
---|
132 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(tr("Cancel (%1)").arg(m_pButtonBox->button(QDialogButtonBox::Cancel)->shortcut().toString()));
|
---|
133 |
|
---|
134 | if (m_pButtonBox->button(QDialogButtonBox::Help)->shortcut().toString().isEmpty())
|
---|
135 | m_pButtonBox->button(QDialogButtonBox::Help)->setToolTip(tr("Show Help"));
|
---|
136 | else
|
---|
137 | m_pButtonBox->button(QDialogButtonBox::Help)->setToolTip(tr("Show Help (%1)").arg(m_pButtonBox->button(QDialogButtonBox::Help)->shortcut().toString()));
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void UITakeSnapshotDialog::sltHandleNameChanged(const QString &strName)
|
---|
142 | {
|
---|
143 | /* Update button state depending on snapshot name value: */
|
---|
144 | m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(!strName.trimmed().isEmpty());
|
---|
145 | }
|
---|
146 |
|
---|
147 | void UITakeSnapshotDialog::prepare()
|
---|
148 | {
|
---|
149 | /* Prepare contents: */
|
---|
150 | prepareContents();
|
---|
151 |
|
---|
152 | /* Apply language settings: */
|
---|
153 | retranslateUi();
|
---|
154 |
|
---|
155 | /* Invent minimum size: */
|
---|
156 | QSize minimumSize;
|
---|
157 | const int iHostScreen = UIDesktopWidgetWatchdog::screenNumber(parentWidget());
|
---|
158 | if (iHostScreen >= 0 && iHostScreen < UIDesktopWidgetWatchdog::screenCount())
|
---|
159 | {
|
---|
160 | /* On the basis of current host-screen geometry if possible: */
|
---|
161 | const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
|
---|
162 | if (screenGeometry.isValid())
|
---|
163 | minimumSize = screenGeometry.size() / 4;
|
---|
164 | }
|
---|
165 | /* Fallback to default size if we failed: */
|
---|
166 | if (minimumSize.isNull())
|
---|
167 | minimumSize = QSize(800, 600);
|
---|
168 | /* Resize to initial size: */
|
---|
169 | setMinimumSize(minimumSize);
|
---|
170 | }
|
---|
171 |
|
---|
172 | void UITakeSnapshotDialog::prepareContents()
|
---|
173 | {
|
---|
174 | /* Create layout: */
|
---|
175 | QGridLayout *pLayout = new QGridLayout(this);
|
---|
176 | if (pLayout)
|
---|
177 | {
|
---|
178 | /* Configure layout: */
|
---|
179 | #ifdef VBOX_WS_MAC
|
---|
180 | pLayout->setSpacing(20);
|
---|
181 | pLayout->setContentsMargins(40, 20, 40, 20);
|
---|
182 | #else
|
---|
183 | pLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) * 2);
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | /* Create sub-layout: */
|
---|
187 | QVBoxLayout *pSubLayout1 = new QVBoxLayout;
|
---|
188 | if (pSubLayout1)
|
---|
189 | {
|
---|
190 | /* Create icon label: */
|
---|
191 | m_pLabelIcon = new QLabel;
|
---|
192 | if (m_pLabelIcon)
|
---|
193 | {
|
---|
194 | /* Configure label: */
|
---|
195 | m_pLabelIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
---|
196 |
|
---|
197 | /* Add into layout: */
|
---|
198 | pSubLayout1->addWidget(m_pLabelIcon);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* Add stretch: */
|
---|
202 | pSubLayout1->addStretch();
|
---|
203 |
|
---|
204 | /* Add into layout: */
|
---|
205 | pLayout->addLayout(pSubLayout1, 0, 0, 2, 1);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* Create sub-layout 2: */
|
---|
209 | QVBoxLayout *pSubLayout2 = new QVBoxLayout;
|
---|
210 | if (pSubLayout2)
|
---|
211 | {
|
---|
212 | /* Configure layout: */
|
---|
213 | #ifdef VBOX_WS_MAC
|
---|
214 | pSubLayout2->setSpacing(5);
|
---|
215 | #else
|
---|
216 | pSubLayout2->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | /* Create name label: */
|
---|
220 | m_pLabelName = new QLabel;
|
---|
221 | if (m_pLabelName)
|
---|
222 | {
|
---|
223 | /* Add into layout: */
|
---|
224 | pSubLayout2->addWidget(m_pLabelName);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* Create name editor: */
|
---|
228 | m_pEditorName = new QLineEdit;
|
---|
229 | if (m_pEditorName)
|
---|
230 | {
|
---|
231 | /* Configure editor: */
|
---|
232 | m_pLabelName->setBuddy(m_pEditorName);
|
---|
233 | connect(m_pEditorName, &QLineEdit::textChanged,
|
---|
234 | this, &UITakeSnapshotDialog::sltHandleNameChanged);
|
---|
235 |
|
---|
236 | /* Add into layout: */
|
---|
237 | pSubLayout2->addWidget(m_pEditorName);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /* Add into layout: */
|
---|
241 | pLayout->addLayout(pSubLayout2, 0, 1);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* Create sub-layout 3: */
|
---|
245 | QVBoxLayout *pSubLayout3 = new QVBoxLayout;
|
---|
246 | if (pSubLayout3)
|
---|
247 | {
|
---|
248 | /* Configure layout: */
|
---|
249 | #ifdef VBOX_WS_MAC
|
---|
250 | pSubLayout3->setSpacing(5);
|
---|
251 | #else
|
---|
252 | pSubLayout3->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
|
---|
253 | #endif
|
---|
254 |
|
---|
255 | /* Create description label: */
|
---|
256 | m_pLabelDescription = new QLabel;
|
---|
257 | if (m_pLabelDescription)
|
---|
258 | {
|
---|
259 | /* Add into layout: */
|
---|
260 | pSubLayout3->addWidget(m_pLabelDescription);
|
---|
261 | }
|
---|
262 |
|
---|
263 | /* Create description editor: */
|
---|
264 | m_pEditorDescription = new QTextEdit;
|
---|
265 | if (m_pEditorDescription)
|
---|
266 | {
|
---|
267 | /* Configure editor: */
|
---|
268 | m_pLabelDescription->setBuddy(m_pEditorDescription);
|
---|
269 |
|
---|
270 | /* Add into layout: */
|
---|
271 | pSubLayout3->addWidget(m_pEditorDescription);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* Add into layout: */
|
---|
275 | pLayout->addLayout(pSubLayout3, 1, 1);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* Create information label: */
|
---|
279 | m_pLabelInfo = new QILabel;
|
---|
280 | if (m_pLabelInfo)
|
---|
281 | {
|
---|
282 | /* Configure label: */
|
---|
283 | m_pLabelInfo->setWordWrap(true);
|
---|
284 | m_pLabelInfo->useSizeHintForWidth(400);
|
---|
285 |
|
---|
286 | /* Hide if machine have no immutable attachments: */
|
---|
287 | if (!m_cImmutableMedia)
|
---|
288 | m_pLabelInfo->setHidden(true);
|
---|
289 |
|
---|
290 | /* Add into layout: */
|
---|
291 | pLayout->addWidget(m_pLabelInfo, 2, 0, 1, 2);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /* Create button-box: */
|
---|
295 | m_pButtonBox = new QIDialogButtonBox;
|
---|
296 | if (m_pButtonBox)
|
---|
297 | {
|
---|
298 | /* Configure button-box: */
|
---|
299 | m_pButtonBox->setStandardButtons( QDialogButtonBox::Ok
|
---|
300 | | QDialogButtonBox::Cancel
|
---|
301 | | QDialogButtonBox::Help);
|
---|
302 | connect(m_pButtonBox, &QIDialogButtonBox::accepted,
|
---|
303 | this, &UITakeSnapshotDialog::accept);
|
---|
304 | connect(m_pButtonBox, &QIDialogButtonBox::rejected,
|
---|
305 | this, &UITakeSnapshotDialog::reject);
|
---|
306 | connect(m_pButtonBox->button(QIDialogButtonBox::Help), &QPushButton::pressed,
|
---|
307 | m_pButtonBox, &QIDialogButtonBox::sltHandleHelpRequest);
|
---|
308 | m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);
|
---|
309 | uiCommon().setHelpKeyword(m_pButtonBox->button(QIDialogButtonBox::Help), "snapshots");
|
---|
310 |
|
---|
311 | /* Add into layout: */
|
---|
312 | pLayout->addWidget(m_pButtonBox, 3, 0, 1, 2);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | void UITakeSnapshotDialog::updatePixmap()
|
---|
318 | {
|
---|
319 | const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
|
---|
320 | #ifndef VBOX_IS_QT6_OR_LATER /* QIcon::pixmap taking QWindow is deprecated in Qt6 */
|
---|
321 | m_pLabelIcon->setPixmap(m_icon.pixmap(windowHandle(), QSize(iIconMetric, iIconMetric)));
|
---|
322 | #else
|
---|
323 | const qreal fDevicePixelRatio = windowHandle() ? windowHandle()->devicePixelRatio() : 1;
|
---|
324 | m_pLabelIcon->setPixmap(m_icon.pixmap(QSize(iIconMetric, iIconMetric), fDevicePixelRatio));
|
---|
325 | #endif
|
---|
326 | }
|
---|