VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/UITakeSnapshotDialog.cpp

Last change on this file was 106061, checked in by vboxsync, 3 weeks 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.0 KB
Line 
1/* $Id: UITakeSnapshotDialog.cpp 106061 2024-09-16 14:03:52Z 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 "UIHelpBrowserDialog.h"
44#include "UIShortcutPool.h"
45#include "UITakeSnapshotDialog.h"
46#include "UITranslationEventListener.h"
47
48UITakeSnapshotDialog::UITakeSnapshotDialog(QWidget *pParent, ulong cImmutableMedia)
49 : 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
60void UITakeSnapshotDialog::setIcon(const QIcon &icon)
61{
62 m_icon = icon;
63 updatePixmap();
64}
65
66void UITakeSnapshotDialog::setName(const QString &strName)
67{
68 m_pEditorName->setText(strName);
69}
70
71QString UITakeSnapshotDialog::name() const
72{
73 return m_pEditorName->text();
74}
75
76QString UITakeSnapshotDialog::description() const
77{
78 return m_pEditorDescription->toPlainText();
79}
80
81bool 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 QIDialog::event(pEvent);
99}
100
101void UITakeSnapshotDialog::sltRetranslateUI()
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(UIShortcutPool::standardSequence(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
141void 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
147void UITakeSnapshotDialog::prepare()
148{
149 /* Prepare contents: */
150 prepareContents();
151
152 /* Apply language settings: */
153 sltRetranslateUI();
154 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
155 this, &UITakeSnapshotDialog::sltRetranslateUI);
156
157 /* Invent minimum size: */
158 QSize minimumSize;
159 const int iHostScreen = UIDesktopWidgetWatchdog::screenNumber(parentWidget());
160 if (iHostScreen >= 0 && iHostScreen < UIDesktopWidgetWatchdog::screenCount())
161 {
162 /* On the basis of current host-screen geometry if possible: */
163 const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
164 if (screenGeometry.isValid())
165 minimumSize = screenGeometry.size() / 4;
166 }
167 /* Fallback to default size if we failed: */
168 if (minimumSize.isNull())
169 minimumSize = QSize(800, 600);
170 /* Resize to initial size: */
171 setMinimumSize(minimumSize);
172}
173
174void UITakeSnapshotDialog::prepareContents()
175{
176 /* Create layout: */
177 QGridLayout *pLayout = new QGridLayout(this);
178 if (pLayout)
179 {
180 /* Configure layout: */
181#ifdef VBOX_WS_MAC
182 pLayout->setSpacing(20);
183 pLayout->setContentsMargins(40, 20, 40, 20);
184#else
185 pLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) * 2);
186#endif
187
188 /* Create sub-layout: */
189 QVBoxLayout *pSubLayout1 = new QVBoxLayout;
190 if (pSubLayout1)
191 {
192 /* Create icon label: */
193 m_pLabelIcon = new QLabel;
194 if (m_pLabelIcon)
195 {
196 /* Configure label: */
197 m_pLabelIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
198
199 /* Add into layout: */
200 pSubLayout1->addWidget(m_pLabelIcon);
201 }
202
203 /* Add stretch: */
204 pSubLayout1->addStretch();
205
206 /* Add into layout: */
207 pLayout->addLayout(pSubLayout1, 0, 0, 2, 1);
208 }
209
210 /* Create sub-layout 2: */
211 QVBoxLayout *pSubLayout2 = new QVBoxLayout;
212 if (pSubLayout2)
213 {
214 /* Configure layout: */
215#ifdef VBOX_WS_MAC
216 pSubLayout2->setSpacing(5);
217#else
218 pSubLayout2->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
219#endif
220
221 /* Create name label: */
222 m_pLabelName = new QLabel;
223 if (m_pLabelName)
224 {
225 /* Add into layout: */
226 pSubLayout2->addWidget(m_pLabelName);
227 }
228
229 /* Create name editor: */
230 m_pEditorName = new QLineEdit;
231 if (m_pEditorName)
232 {
233 /* Configure editor: */
234 m_pLabelName->setBuddy(m_pEditorName);
235 connect(m_pEditorName, &QLineEdit::textChanged,
236 this, &UITakeSnapshotDialog::sltHandleNameChanged);
237
238 /* Add into layout: */
239 pSubLayout2->addWidget(m_pEditorName);
240 }
241
242 /* Add into layout: */
243 pLayout->addLayout(pSubLayout2, 0, 1);
244 }
245
246 /* Create sub-layout 3: */
247 QVBoxLayout *pSubLayout3 = new QVBoxLayout;
248 if (pSubLayout3)
249 {
250 /* Configure layout: */
251#ifdef VBOX_WS_MAC
252 pSubLayout3->setSpacing(5);
253#else
254 pSubLayout3->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
255#endif
256
257 /* Create description label: */
258 m_pLabelDescription = new QLabel;
259 if (m_pLabelDescription)
260 {
261 /* Add into layout: */
262 pSubLayout3->addWidget(m_pLabelDescription);
263 }
264
265 /* Create description editor: */
266 m_pEditorDescription = new QTextEdit;
267 if (m_pEditorDescription)
268 {
269 /* Configure editor: */
270 m_pLabelDescription->setBuddy(m_pEditorDescription);
271
272 /* Add into layout: */
273 pSubLayout3->addWidget(m_pEditorDescription);
274 }
275
276 /* Add into layout: */
277 pLayout->addLayout(pSubLayout3, 1, 1);
278 }
279
280 /* Create information label: */
281 m_pLabelInfo = new QILabel;
282 if (m_pLabelInfo)
283 {
284 /* Configure label: */
285 m_pLabelInfo->setWordWrap(true);
286 m_pLabelInfo->useSizeHintForWidth(400);
287
288 /* Hide if machine have no immutable attachments: */
289 if (!m_cImmutableMedia)
290 m_pLabelInfo->setHidden(true);
291
292 /* Add into layout: */
293 pLayout->addWidget(m_pLabelInfo, 2, 0, 1, 2);
294 }
295
296 /* Create button-box: */
297 m_pButtonBox = new QIDialogButtonBox;
298 if (m_pButtonBox)
299 {
300 /* Configure button-box: */
301 m_pButtonBox->setStandardButtons( QDialogButtonBox::Ok
302 | QDialogButtonBox::Cancel
303 | QDialogButtonBox::Help);
304 connect(m_pButtonBox, &QIDialogButtonBox::accepted,
305 this, &UITakeSnapshotDialog::accept);
306 connect(m_pButtonBox, &QIDialogButtonBox::rejected,
307 this, &UITakeSnapshotDialog::reject);
308 connect(m_pButtonBox->button(QIDialogButtonBox::Help), &QPushButton::pressed,
309 m_pButtonBox, &QIDialogButtonBox::sltHandleHelpRequest);
310 m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(UIShortcutPool::standardSequence(QKeySequence::HelpContents));
311 uiCommon().setHelpKeyword(m_pButtonBox->button(QIDialogButtonBox::Help), "snapshots");
312
313 /* Add into layout: */
314 pLayout->addWidget(m_pButtonBox, 3, 0, 1, 2);
315 }
316 }
317}
318
319void UITakeSnapshotDialog::updatePixmap()
320{
321 const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
322 const qreal fDevicePixelRatio = windowHandle() ? windowHandle()->devicePixelRatio() : 1;
323 m_pLabelIcon->setPixmap(m_icon.pixmap(QSize(iIconMetric, iIconMetric), fDevicePixelRatio));
324}
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