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