VirtualBox

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

Last change on this file since 97364 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

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

© 2023 Oracle
ContactPrivacy policyTerms of Use