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