1 | /* $Id: UITakeSnapshotDialog.cpp 73926 2018-08-28 10:02:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UITakeSnapshotDialog class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
19 | # include <precomp.h>
|
---|
20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
21 |
|
---|
22 | /* Qt includes: */
|
---|
23 | # include <QGridLayout>
|
---|
24 | # include <QLabel>
|
---|
25 | # include <QLineEdit>
|
---|
26 | # include <QPushButton>
|
---|
27 | # include <QStyle>
|
---|
28 |
|
---|
29 | /* GUI includes: */
|
---|
30 | # include "QIDialogButtonBox.h"
|
---|
31 | # include "QILabel.h"
|
---|
32 | # include "VBoxUtils.h"
|
---|
33 | # include "UIDesktopWidgetWatchdog.h"
|
---|
34 | # include "UIMessageCenter.h"
|
---|
35 | # include "UITakeSnapshotDialog.h"
|
---|
36 |
|
---|
37 | /* COM includes: */
|
---|
38 | # include "COMEnums.h"
|
---|
39 | # include "CMachine.h"
|
---|
40 | # include "CMedium.h"
|
---|
41 | # include "CMediumAttachment.h"
|
---|
42 |
|
---|
43 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
44 |
|
---|
45 |
|
---|
46 | UITakeSnapshotDialog::UITakeSnapshotDialog(QWidget *pParent, const CMachine &comMachine)
|
---|
47 | : QIWithRetranslateUI<QIDialog>(pParent)
|
---|
48 | , m_comMachine(comMachine)
|
---|
49 | , m_cImmutableMedia(0)
|
---|
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 | 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_pLabelDescription->setText(tr("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 |
|
---|
111 | void UITakeSnapshotDialog::sltHandleNameChanged(const QString &strName)
|
---|
112 | {
|
---|
113 | /* Update button state depending on snapshot name value: */
|
---|
114 | m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(!strName.trimmed().isEmpty());
|
---|
115 | }
|
---|
116 |
|
---|
117 | void UITakeSnapshotDialog::prepare()
|
---|
118 | {
|
---|
119 | /* Prepare contents: */
|
---|
120 | prepareContents();
|
---|
121 |
|
---|
122 | /* Apply language settings: */
|
---|
123 | retranslateUi();
|
---|
124 |
|
---|
125 | /* Invent minimum size: */
|
---|
126 | QSize minimumSize;
|
---|
127 | const int iHostScreen = gpDesktop->screenNumber(parentWidget());
|
---|
128 | if (iHostScreen >= 0 && iHostScreen < gpDesktop->screenCount())
|
---|
129 | {
|
---|
130 | /* On the basis of current host-screen geometry if possible: */
|
---|
131 | const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
|
---|
132 | if (screenGeometry.isValid())
|
---|
133 | minimumSize = screenGeometry.size() / 4;
|
---|
134 | }
|
---|
135 | /* Fallback to default size if we failed: */
|
---|
136 | if (minimumSize.isNull())
|
---|
137 | minimumSize = QSize(800, 600);
|
---|
138 | /* Resize to initial size: */
|
---|
139 | setMinimumSize(minimumSize);
|
---|
140 | }
|
---|
141 |
|
---|
142 | void UITakeSnapshotDialog::prepareContents()
|
---|
143 | {
|
---|
144 | /* Create layout: */
|
---|
145 | QGridLayout *pLayout = new QGridLayout(this);
|
---|
146 | if (pLayout)
|
---|
147 | {
|
---|
148 | /* Configure layout: */
|
---|
149 | #ifdef VBOX_WS_MAC
|
---|
150 | pLayout->setSpacing(20);
|
---|
151 | pLayout->setContentsMargins(40, 20, 40, 20);
|
---|
152 | #else
|
---|
153 | pLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) * 2);
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /* Create sub-layout: */
|
---|
157 | QVBoxLayout *pSubLayout1 = new QVBoxLayout;
|
---|
158 | if (pSubLayout1)
|
---|
159 | {
|
---|
160 | /* Create icon label: */
|
---|
161 | m_pLabelIcon = new QLabel;
|
---|
162 | if (m_pLabelIcon)
|
---|
163 | {
|
---|
164 | /* Configure label: */
|
---|
165 | m_pLabelIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
---|
166 |
|
---|
167 | /* Add into layout: */
|
---|
168 | pSubLayout1->addWidget(m_pLabelIcon);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Add stretch: */
|
---|
172 | pSubLayout1->addStretch();
|
---|
173 |
|
---|
174 | /* Add into layout: */
|
---|
175 | pLayout->addLayout(pSubLayout1, 0, 0, 2, 1);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Create sub-layout 2: */
|
---|
179 | QVBoxLayout *pSubLayout2 = new QVBoxLayout;
|
---|
180 | if (pSubLayout2)
|
---|
181 | {
|
---|
182 | /* Configure layout: */
|
---|
183 | #ifdef VBOX_WS_MAC
|
---|
184 | pSubLayout2->setSpacing(5);
|
---|
185 | #else
|
---|
186 | pSubLayout2->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | /* Create name label: */
|
---|
190 | m_pLabelName = new QLabel;
|
---|
191 | if (m_pLabelName)
|
---|
192 | {
|
---|
193 | /* Add into layout: */
|
---|
194 | pSubLayout2->addWidget(m_pLabelName);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* Create name editor: */
|
---|
198 | m_pEditorName = new QLineEdit;
|
---|
199 | if (m_pEditorName)
|
---|
200 | {
|
---|
201 | /* Configure editor: */
|
---|
202 | m_pLabelName->setBuddy(m_pEditorName);
|
---|
203 | connect(m_pEditorName, &QLineEdit::textChanged,
|
---|
204 | this, &UITakeSnapshotDialog::sltHandleNameChanged);
|
---|
205 |
|
---|
206 | /* Add into layout: */
|
---|
207 | pSubLayout2->addWidget(m_pEditorName);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Add into layout: */
|
---|
211 | pLayout->addLayout(pSubLayout2, 0, 1);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Create sub-layout 3: */
|
---|
215 | QVBoxLayout *pSubLayout3 = new QVBoxLayout;
|
---|
216 | if (pSubLayout3)
|
---|
217 | {
|
---|
218 | /* Configure layout: */
|
---|
219 | #ifdef VBOX_WS_MAC
|
---|
220 | pSubLayout3->setSpacing(5);
|
---|
221 | #else
|
---|
222 | pSubLayout3->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | /* Create description label: */
|
---|
226 | m_pLabelDescription = new QLabel;
|
---|
227 | if (m_pLabelDescription)
|
---|
228 | {
|
---|
229 | /* Add into layout: */
|
---|
230 | pSubLayout3->addWidget(m_pLabelDescription);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* Create description editor: */
|
---|
234 | m_pEditorDescription = new QTextEdit;
|
---|
235 | if (m_pEditorDescription)
|
---|
236 | {
|
---|
237 | /* Configure editor: */
|
---|
238 | m_pLabelDescription->setBuddy(m_pEditorDescription);
|
---|
239 |
|
---|
240 | /* Add into layout: */
|
---|
241 | pSubLayout3->addWidget(m_pEditorDescription);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* Add into layout: */
|
---|
245 | pLayout->addLayout(pSubLayout3, 1, 1);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /* Create information label: */
|
---|
249 | m_pLabelInfo = new QILabel;
|
---|
250 | if (m_pLabelInfo)
|
---|
251 | {
|
---|
252 | /* Configure label: */
|
---|
253 | m_pLabelInfo->setWordWrap(true);
|
---|
254 | m_pLabelInfo->useSizeHintForWidth(400);
|
---|
255 |
|
---|
256 | /* Calculate the amount of immutable attachments: */
|
---|
257 | if (m_comMachine.GetState() == KMachineState_Paused)
|
---|
258 | {
|
---|
259 | foreach (const CMediumAttachment &comAttachment, m_comMachine.GetMediumAttachments())
|
---|
260 | {
|
---|
261 | CMedium comMedium = comAttachment.GetMedium();
|
---|
262 | if ( !comMedium.isNull()
|
---|
263 | && !comMedium.GetParent().isNull()
|
---|
264 | && comMedium.GetBase().GetType() == KMediumType_Immutable)
|
---|
265 | ++m_cImmutableMedia;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | /* Hide if machine have no immutable attachments: */
|
---|
269 | if (!m_cImmutableMedia)
|
---|
270 | m_pLabelInfo->setHidden(true);
|
---|
271 |
|
---|
272 | /* Add into layout: */
|
---|
273 | pLayout->addWidget(m_pLabelInfo, 2, 0, 1, 2);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* Create button-box: */
|
---|
277 | m_pButtonBox = new QIDialogButtonBox;
|
---|
278 | if (m_pButtonBox)
|
---|
279 | {
|
---|
280 | /* Configure button-box: */
|
---|
281 | m_pButtonBox->setStandardButtons( QDialogButtonBox::Ok
|
---|
282 | | QDialogButtonBox::Cancel
|
---|
283 | | QDialogButtonBox::Help);
|
---|
284 | connect(m_pButtonBox, &QIDialogButtonBox::accepted,
|
---|
285 | this, &UITakeSnapshotDialog::accept);
|
---|
286 | connect(m_pButtonBox, &QIDialogButtonBox::rejected,
|
---|
287 | this, &UITakeSnapshotDialog::reject);
|
---|
288 | connect(m_pButtonBox, &QIDialogButtonBox::helpRequested,
|
---|
289 | &msgCenter(), &UIMessageCenter::sltShowHelpHelpDialog);
|
---|
290 |
|
---|
291 | /* Add into layout: */
|
---|
292 | pLayout->addWidget(m_pButtonBox, 3, 0, 1, 2);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | void UITakeSnapshotDialog::updatePixmap()
|
---|
298 | {
|
---|
299 | const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
|
---|
300 | m_pLabelIcon->setPixmap(m_icon.pixmap(windowHandle(), QSize(iIconMetric, iIconMetric)));
|
---|
301 | }
|
---|