[26719] | 1 | /* $Id: UITakeSnapshotDialog.cpp 104358 2024-04-18 05:33:40Z vboxsync $ */
|
---|
[8743] | 2 | /** @file
|
---|
[67535] | 3 | * VBox Qt GUI - UITakeSnapshotDialog class implementation.
|
---|
[8743] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[8743] | 8 | *
|
---|
[96407] | 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
|
---|
[8743] | 26 | */
|
---|
| 27 |
|
---|
[41587] | 28 | /* Qt includes: */
|
---|
[104358] | 29 | #include <QApplication>
|
---|
[76606] | 30 | #include <QGridLayout>
|
---|
| 31 | #include <QLabel>
|
---|
| 32 | #include <QLineEdit>
|
---|
| 33 | #include <QPushButton>
|
---|
| 34 | #include <QStyle>
|
---|
[101560] | 35 | #include <QWindow>
|
---|
[25116] | 36 |
|
---|
[41587] | 37 | /* GUI includes: */
|
---|
[76606] | 38 | #include "QIDialogButtonBox.h"
|
---|
| 39 | #include "QILabel.h"
|
---|
| 40 | #include "VBoxUtils.h"
|
---|
[86991] | 41 | #include "UICommon.h"
|
---|
[76606] | 42 | #include "UIDesktopWidgetWatchdog.h"
|
---|
[99946] | 43 | #include "UIHelpBrowserDialog.h"
|
---|
[103578] | 44 | #include "UIShortcutPool.h"
|
---|
[76606] | 45 | #include "UITakeSnapshotDialog.h"
|
---|
[104358] | 46 | #include "UITranslationEventListener.h"
|
---|
[28934] | 47 |
|
---|
[98779] | 48 | UITakeSnapshotDialog::UITakeSnapshotDialog(QWidget *pParent, ulong cImmutableMedia)
|
---|
[104358] | 49 | : QIDialog(pParent)
|
---|
[98779] | 50 | , m_cImmutableMedia(cImmutableMedia)
|
---|
[67535] | 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)
|
---|
[8743] | 56 | {
|
---|
[67535] | 57 | prepare();
|
---|
| 58 | }
|
---|
[8743] | 59 |
|
---|
[72813] | 60 | void UITakeSnapshotDialog::setIcon(const QIcon &icon)
|
---|
[67535] | 61 | {
|
---|
[72813] | 62 | m_icon = icon;
|
---|
| 63 | updatePixmap();
|
---|
[67535] | 64 | }
|
---|
[8743] | 65 |
|
---|
[67535] | 66 | void UITakeSnapshotDialog::setName(const QString &strName)
|
---|
| 67 | {
|
---|
| 68 | m_pEditorName->setText(strName);
|
---|
| 69 | }
|
---|
[9089] | 70 |
|
---|
[67535] | 71 | QString UITakeSnapshotDialog::name() const
|
---|
| 72 | {
|
---|
| 73 | return m_pEditorName->text();
|
---|
| 74 | }
|
---|
[25116] | 75 |
|
---|
[67535] | 76 | QString UITakeSnapshotDialog::description() const
|
---|
| 77 | {
|
---|
| 78 | return m_pEditorDescription->toPlainText();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[72813] | 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: */
|
---|
[104358] | 98 | return QIDialog::event(pEvent);
|
---|
[72813] | 99 | }
|
---|
| 100 |
|
---|
[104358] | 101 | void UITakeSnapshotDialog::sltRetranslateUI()
|
---|
[67535] | 102 | {
|
---|
| 103 | setWindowTitle(tr("Take Snapshot of Virtual Machine"));
|
---|
| 104 | m_pLabelName->setText(tr("Snapshot &Name"));
|
---|
[95490] | 105 | m_pEditorName->setToolTip(tr("Holds the snapshot name"));
|
---|
[67535] | 106 | m_pLabelDescription->setText(tr("Snapshot &Description"));
|
---|
[95490] | 107 | m_pEditorDescription->setToolTip(tr("Holds the snapshot description"));
|
---|
[67535] | 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) "
|
---|
[73926] | 110 | "will not be reset to avoid loss of data.", "", m_cImmutableMedia));
|
---|
[86991] | 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 |
|
---|
[103578] | 122 | m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(UIShortcutPool::standardSequence(QKeySequence::HelpContents));
|
---|
[86991] | 123 |
|
---|
[95490] | 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()));
|
---|
[86991] | 138 | }
|
---|
[67535] | 139 | }
|
---|
| 140 |
|
---|
| 141 | void 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 |
|
---|
| 147 | void UITakeSnapshotDialog::prepare()
|
---|
| 148 | {
|
---|
[72022] | 149 | /* Prepare contents: */
|
---|
| 150 | prepareContents();
|
---|
| 151 |
|
---|
| 152 | /* Apply language settings: */
|
---|
[104358] | 153 | sltRetranslateUI();
|
---|
| 154 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
| 155 | this, &UITakeSnapshotDialog::sltRetranslateUI);
|
---|
[72022] | 156 |
|
---|
| 157 | /* Invent minimum size: */
|
---|
| 158 | QSize minimumSize;
|
---|
[97681] | 159 | const int iHostScreen = UIDesktopWidgetWatchdog::screenNumber(parentWidget());
|
---|
| 160 | if (iHostScreen >= 0 && iHostScreen < UIDesktopWidgetWatchdog::screenCount())
|
---|
[72022] | 161 | {
|
---|
| 162 | /* On the basis of current host-screen geometry if possible: */
|
---|
[97682] | 163 | const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
|
---|
[72022] | 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 |
|
---|
| 174 | void UITakeSnapshotDialog::prepareContents()
|
---|
| 175 | {
|
---|
[67535] | 176 | /* Create layout: */
|
---|
| 177 | QGridLayout *pLayout = new QGridLayout(this);
|
---|
[72022] | 178 | if (pLayout)
|
---|
[25116] | 179 | {
|
---|
[69823] | 180 | /* Configure layout: */
|
---|
[70523] | 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
|
---|
[69823] | 187 |
|
---|
[67535] | 188 | /* Create sub-layout: */
|
---|
| 189 | QVBoxLayout *pSubLayout1 = new QVBoxLayout;
|
---|
[72022] | 190 | if (pSubLayout1)
|
---|
[25116] | 191 | {
|
---|
[67535] | 192 | /* Create icon label: */
|
---|
| 193 | m_pLabelIcon = new QLabel;
|
---|
[72022] | 194 | if (m_pLabelIcon)
|
---|
[67535] | 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);
|
---|
[25116] | 208 | }
|
---|
| 209 |
|
---|
[67535] | 210 | /* Create sub-layout 2: */
|
---|
| 211 | QVBoxLayout *pSubLayout2 = new QVBoxLayout;
|
---|
[72022] | 212 | if (pSubLayout2)
|
---|
[67535] | 213 | {
|
---|
| 214 | /* Configure layout: */
|
---|
[70523] | 215 | #ifdef VBOX_WS_MAC
|
---|
| 216 | pSubLayout2->setSpacing(5);
|
---|
| 217 | #else
|
---|
| 218 | pSubLayout2->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
|
---|
| 219 | #endif
|
---|
[25116] | 220 |
|
---|
[67535] | 221 | /* Create name label: */
|
---|
| 222 | m_pLabelName = new QLabel;
|
---|
[72022] | 223 | if (m_pLabelName)
|
---|
[67535] | 224 | {
|
---|
| 225 | /* Add into layout: */
|
---|
| 226 | pSubLayout2->addWidget(m_pLabelName);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /* Create name editor: */
|
---|
| 230 | m_pEditorName = new QLineEdit;
|
---|
[72022] | 231 | if (m_pEditorName)
|
---|
[67535] | 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;
|
---|
[72022] | 248 | if (pSubLayout3)
|
---|
[67535] | 249 | {
|
---|
| 250 | /* Configure layout: */
|
---|
[70523] | 251 | #ifdef VBOX_WS_MAC
|
---|
| 252 | pSubLayout3->setSpacing(5);
|
---|
| 253 | #else
|
---|
| 254 | pSubLayout3->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
|
---|
| 255 | #endif
|
---|
[67535] | 256 |
|
---|
| 257 | /* Create description label: */
|
---|
| 258 | m_pLabelDescription = new QLabel;
|
---|
[72022] | 259 | if (m_pLabelDescription)
|
---|
[67535] | 260 | {
|
---|
| 261 | /* Add into layout: */
|
---|
| 262 | pSubLayout3->addWidget(m_pLabelDescription);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /* Create description editor: */
|
---|
| 266 | m_pEditorDescription = new QTextEdit;
|
---|
[72022] | 267 | if (m_pEditorDescription)
|
---|
[67535] | 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;
|
---|
[72022] | 282 | if (m_pLabelInfo)
|
---|
[67535] | 283 | {
|
---|
| 284 | /* Configure label: */
|
---|
| 285 | m_pLabelInfo->setWordWrap(true);
|
---|
| 286 | m_pLabelInfo->useSizeHintForWidth(400);
|
---|
| 287 |
|
---|
| 288 | /* Hide if machine have no immutable attachments: */
|
---|
[73926] | 289 | if (!m_cImmutableMedia)
|
---|
[67535] | 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;
|
---|
[72022] | 298 | if (m_pButtonBox)
|
---|
[67535] | 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);
|
---|
[86939] | 308 | connect(m_pButtonBox->button(QIDialogButtonBox::Help), &QPushButton::pressed,
|
---|
[99946] | 309 | m_pButtonBox, &QIDialogButtonBox::sltHandleHelpRequest);
|
---|
[103578] | 310 | m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(UIShortcutPool::standardSequence(QKeySequence::HelpContents));
|
---|
[86991] | 311 | uiCommon().setHelpKeyword(m_pButtonBox->button(QIDialogButtonBox::Help), "snapshots");
|
---|
[99946] | 312 |
|
---|
[67535] | 313 | /* Add into layout: */
|
---|
| 314 | pLayout->addWidget(m_pButtonBox, 3, 0, 1, 2);
|
---|
| 315 | }
|
---|
[25116] | 316 | }
|
---|
[9089] | 317 | }
|
---|
[72813] | 318 |
|
---|
| 319 | void UITakeSnapshotDialog::updatePixmap()
|
---|
| 320 | {
|
---|
| 321 | const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
|
---|
[100075] | 322 | const qreal fDevicePixelRatio = windowHandle() ? windowHandle()->devicePixelRatio() : 1;
|
---|
| 323 | m_pLabelIcon->setPixmap(m_icon.pixmap(QSize(iIconMetric, iIconMetric), fDevicePixelRatio));
|
---|
[72813] | 324 | }
|
---|