summaryrefslogtreecommitdiffstats
path: root/src/VBox/Frontends/VirtualBox/src/UITakeSnapshotDialog.cpp
blob: 77b7fbb7a9bec81c2f83fb56c3e893ac6226c6c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* $Id: UITakeSnapshotDialog.cpp $ */
/** @file
 * VBox Qt GUI - UITakeSnapshotDialog class implementation.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/* Qt includes: */
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QStyle>

/* GUI includes: */
#include "QIDialogButtonBox.h"
#include "QILabel.h"
#include "VBoxUtils.h"
#include "UICommon.h"
#include "UIDesktopWidgetWatchdog.h"
#include "UIMessageCenter.h"
#include "UITakeSnapshotDialog.h"

/* COM includes: */
#include "COMEnums.h"
#include "CMachine.h"
#include "CMedium.h"
#include "CMediumAttachment.h"


UITakeSnapshotDialog::UITakeSnapshotDialog(QWidget *pParent, const CMachine &comMachine)
    : QIWithRetranslateUI<QIDialog>(pParent)
    , m_comMachine(comMachine)
    , m_cImmutableMedia(0)
    , m_pLabelIcon(0)
    , m_pLabelName(0), m_pEditorName(0)
    , m_pLabelDescription(0), m_pEditorDescription(0)
    , m_pLabelInfo(0)
    , m_pButtonBox(0)
{
    /* Prepare: */
    prepare();
}

void UITakeSnapshotDialog::setIcon(const QIcon &icon)
{
    m_icon = icon;
    updatePixmap();
}

void UITakeSnapshotDialog::setName(const QString &strName)
{
    m_pEditorName->setText(strName);
}

QString UITakeSnapshotDialog::name() const
{
    return m_pEditorName->text();
}

QString UITakeSnapshotDialog::description() const
{
    return m_pEditorDescription->toPlainText();
}

bool UITakeSnapshotDialog::event(QEvent *pEvent)
{
    /* Handle know event types: */
    switch (pEvent->type())
    {
        case QEvent::Show:
        case QEvent::ScreenChangeInternal:
        {
            /* Update pixmap: */
            updatePixmap();
            break;
        }
        default:
            break;
    }

    /* Call to base-class: */
    return QIWithRetranslateUI<QIDialog>::event(pEvent);
}

void UITakeSnapshotDialog::retranslateUi()
{
    setWindowTitle(tr("Take Snapshot of Virtual Machine"));
    m_pLabelName->setText(tr("Snapshot &Name"));
    m_pEditorName->setToolTip(tr("Holds the snapshot name"));
    m_pLabelDescription->setText(tr("Snapshot &Description"));
    m_pEditorDescription->setToolTip(tr("Holds the snapshot description"));
    m_pLabelInfo->setText(tr("Warning: You are taking a snapshot of a running machine which has %n immutable image(s) "
                             "attached to it. As long as you are working from this snapshot the immutable image(s) "
                             "will not be reset to avoid loss of data.", "", m_cImmutableMedia));

    if (m_pButtonBox)
    {
        m_pButtonBox->button(QDialogButtonBox::Ok)->setText(tr("Ok"));
        m_pButtonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
        m_pButtonBox->button(QDialogButtonBox::Help)->setText(tr("Help"));

        m_pButtonBox->button(QDialogButtonBox::Ok)->setStatusTip(tr("Take Snapshot and close the dialog"));
        m_pButtonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close dialog without taking a snapshot"));
        m_pButtonBox->button(QDialogButtonBox::Help)->setStatusTip(tr("Show dialog help"));

        m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);

        if (m_pButtonBox->button(QDialogButtonBox::Ok)->shortcut().toString().isEmpty())
            m_pButtonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Accept"));
        else
            m_pButtonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Accept (%1)").arg(m_pButtonBox->button(QDialogButtonBox::Ok)->shortcut().toString()));

        if (m_pButtonBox->button(QDialogButtonBox::Cancel)->shortcut().toString().isEmpty())
            m_pButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(tr("Cancel"));
        else
            m_pButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(tr("Cancel (%1)").arg(m_pButtonBox->button(QDialogButtonBox::Cancel)->shortcut().toString()));

        if (m_pButtonBox->button(QDialogButtonBox::Help)->shortcut().toString().isEmpty())
            m_pButtonBox->button(QDialogButtonBox::Help)->setToolTip(tr("Show Help"));
        else
            m_pButtonBox->button(QDialogButtonBox::Help)->setToolTip(tr("Show Help (%1)").arg(m_pButtonBox->button(QDialogButtonBox::Help)->shortcut().toString()));
    }
}

void UITakeSnapshotDialog::sltHandleNameChanged(const QString &strName)
{
    /* Update button state depending on snapshot name value: */
    m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(!strName.trimmed().isEmpty());
}

void UITakeSnapshotDialog::prepare()
{
    /* Prepare contents: */
    prepareContents();

    /* Apply language settings: */
    retranslateUi();

    /* Invent minimum size: */
    QSize minimumSize;
    const int iHostScreen = UIDesktopWidgetWatchdog::screenNumber(parentWidget());
    if (iHostScreen >= 0 && iHostScreen < UIDesktopWidgetWatchdog::screenCount())
    {
        /* On the basis of current host-screen geometry if possible: */
        const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
        if (screenGeometry.isValid())
            minimumSize = screenGeometry.size() / 4;
    }
    /* Fallback to default size if we failed: */
    if (minimumSize.isNull())
        minimumSize = QSize(800, 600);
    /* Resize to initial size: */
    setMinimumSize(minimumSize);
}

void UITakeSnapshotDialog::prepareContents()
{
    /* Create layout: */
    QGridLayout *pLayout = new QGridLayout(this);
    if (pLayout)
    {
        /* Configure layout: */
#ifdef VBOX_WS_MAC
        pLayout->setSpacing(20);
        pLayout->setContentsMargins(40, 20, 40, 20);
#else
        pLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) * 2);
#endif

        /* Create sub-layout: */
        QVBoxLayout *pSubLayout1 = new QVBoxLayout;
        if (pSubLayout1)
        {
            /* Create icon label: */
            m_pLabelIcon = new QLabel;
            if (m_pLabelIcon)
            {
                /* Configure label: */
                m_pLabelIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

                /* Add into layout: */
                pSubLayout1->addWidget(m_pLabelIcon);
            }

            /* Add stretch: */
            pSubLayout1->addStretch();

            /* Add into layout: */
            pLayout->addLayout(pSubLayout1, 0, 0, 2, 1);
        }

        /* Create sub-layout 2: */
        QVBoxLayout *pSubLayout2 = new QVBoxLayout;
        if (pSubLayout2)
        {
            /* Configure layout: */
#ifdef VBOX_WS_MAC
            pSubLayout2->setSpacing(5);
#else
            pSubLayout2->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
#endif

            /* Create name label: */
            m_pLabelName = new QLabel;
            if (m_pLabelName)
            {
                /* Add into layout: */
                pSubLayout2->addWidget(m_pLabelName);
            }

            /* Create name editor: */
            m_pEditorName = new QLineEdit;
            if (m_pEditorName)
            {
                /* Configure editor: */
                m_pLabelName->setBuddy(m_pEditorName);
                connect(m_pEditorName, &QLineEdit::textChanged,
                        this, &UITakeSnapshotDialog::sltHandleNameChanged);

                /* Add into layout: */
                pSubLayout2->addWidget(m_pEditorName);
            }

            /* Add into layout: */
            pLayout->addLayout(pSubLayout2, 0, 1);
        }

        /* Create sub-layout 3: */
        QVBoxLayout *pSubLayout3 = new QVBoxLayout;
        if (pSubLayout3)
        {
            /* Configure layout: */
#ifdef VBOX_WS_MAC
            pSubLayout3->setSpacing(5);
#else
            pSubLayout3->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
#endif

            /* Create description label: */
            m_pLabelDescription = new QLabel;
            if (m_pLabelDescription)
            {
                /* Add into layout: */
                pSubLayout3->addWidget(m_pLabelDescription);
            }

            /* Create description editor: */
            m_pEditorDescription = new QTextEdit;
            if (m_pEditorDescription)
            {
                /* Configure editor: */
                m_pLabelDescription->setBuddy(m_pEditorDescription);

                /* Add into layout: */
                pSubLayout3->addWidget(m_pEditorDescription);
            }

            /* Add into layout: */
            pLayout->addLayout(pSubLayout3, 1, 1);
        }

        /* Create information label: */
        m_pLabelInfo = new QILabel;
        if (m_pLabelInfo)
        {
            /* Configure label: */
            m_pLabelInfo->setWordWrap(true);
            m_pLabelInfo->useSizeHintForWidth(400);

            /* Calculate the amount of immutable attachments: */
            if (m_comMachine.GetState() == KMachineState_Paused)
            {
                foreach (const CMediumAttachment &comAttachment, m_comMachine.GetMediumAttachments())
                {
                    CMedium comMedium = comAttachment.GetMedium();
                    if (   !comMedium.isNull()
                        && !comMedium.GetParent().isNull()
                        && comMedium.GetBase().GetType() == KMediumType_Immutable)
                        ++m_cImmutableMedia;
                }
            }
            /* Hide if machine have no immutable attachments: */
            if (!m_cImmutableMedia)
                m_pLabelInfo->setHidden(true);

            /* Add into layout: */
            pLayout->addWidget(m_pLabelInfo, 2, 0, 1, 2);
        }

        /* Create button-box: */
        m_pButtonBox = new QIDialogButtonBox;
        if (m_pButtonBox)
        {
            /* Configure button-box: */
            m_pButtonBox->setStandardButtons(  QDialogButtonBox::Ok
                                             | QDialogButtonBox::Cancel
                                             | QDialogButtonBox::Help);
            connect(m_pButtonBox, &QIDialogButtonBox::accepted,
                    this, &UITakeSnapshotDialog::accept);
            connect(m_pButtonBox, &QIDialogButtonBox::rejected,
                    this, &UITakeSnapshotDialog::reject);
            connect(m_pButtonBox->button(QIDialogButtonBox::Help), &QPushButton::pressed,
                    &(msgCenter()), &UIMessageCenter::sltHandleHelpRequest);
            m_pButtonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);
            uiCommon().setHelpKeyword(m_pButtonBox->button(QIDialogButtonBox::Help), "snapshots");
            /* Add into layout: */
            pLayout->addWidget(m_pButtonBox, 3, 0, 1, 2);
        }
    }
}

void UITakeSnapshotDialog::updatePixmap()
{
    const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
    m_pLabelIcon->setPixmap(m_icon.pixmap(windowHandle(), QSize(iIconMetric, iIconMetric)));
}