summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/widgets/UIProgressDialog.cpp
blob: 03aad59dd19a257c2b74f0f89cdc7101a30818df (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
/* $Id: UIProgressDialog.cpp $ */
/** @file
 *
 * VBox frontends: Qt GUI ("VirtualBox"):
 * UIProgressDialog class implementation
 */

/*
 * Copyright (C) 2006-2010 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/* VBox includes */
#include "UIProgressDialog.h"
#include "COMDefs.h"
#include "QIDialogButtonBox.h"
#include "QILabel.h"
#include "UISpecialControls.h"
#include "VBoxGlobal.h"

#ifdef Q_WS_MAC
# include "VBoxUtils-darwin.h"
#endif /* Q_WS_MAC */

/* Qt includes */
#include <QCloseEvent>
#include <QEventLoop>
#include <QProgressBar>
#include <QTime>
#include <QTimer>
#include <QVBoxLayout>

const char *UIProgressDialog::m_spcszOpDescTpl = "%1 ... (%2/%3)";

UIProgressDialog::UIProgressDialog(CProgress &progress,
                                   const QString &strTitle,
                                   QPixmap *pImage /* = 0 */,
                                   bool fSheetOnDarwin /* = false */,
                                   int cMinDuration /* = 2000 */,
                                   QWidget *pParent /* = 0 */)
  : QIDialog(pParent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint)
  , m_progress(progress)
  , m_pImageLbl(0)
  , m_pCancelBtn(0)
  , m_fCancelEnabled(false)
  , m_cOperations(m_progress.GetOperationCount())
  , m_iCurrentOperation(m_progress.GetOperation() + 1)
  , m_fEnded(false)
{
    setModal(true);

    QHBoxLayout *pLayout0 = new QHBoxLayout(this);

#ifdef Q_WS_MAC
    /* No sheets in another mode than normal for now. Firstly it looks ugly and
     * secondly in some cases it is broken. */
    if (   fSheetOnDarwin
        && vboxGlobal().isSheetWindowsAllowed(pParent))
        setWindowFlags(Qt::Sheet);
    ::darwinSetHidesAllTitleButtons(this);
    ::darwinSetShowsResizeIndicator(this, false);
    if (pImage)
        pLayout0->setContentsMargins(30, 15, 30, 15);
    else
        pLayout0->setContentsMargins(6, 6, 6, 6);
#else
    NOREF(fSheetOnDarwin);
#endif /* Q_WS_MAC */

    if (pImage)
    {
        m_pImageLbl = new QILabel(this);
        m_pImageLbl->setPixmap(*pImage);
        pLayout0->addWidget(m_pImageLbl);
    }

    QVBoxLayout *pLayout1 = new QVBoxLayout();
    pLayout1->setMargin(0);
    pLayout0->addLayout(pLayout1);
    pLayout1->addStretch(1);
    m_pDescriptionLbl = new QILabel(this);
    pLayout1->addWidget(m_pDescriptionLbl, 0, Qt::AlignHCenter);

    QHBoxLayout *pLayout2 = new QHBoxLayout();
    pLayout2->setMargin(0);
    pLayout1->addLayout(pLayout2);

    m_progressBar = new QProgressBar(this);
    pLayout2->addWidget(m_progressBar, 0, Qt::AlignVCenter);

    if (m_cOperations > 1)
        m_pDescriptionLbl->setText(QString(m_spcszOpDescTpl)
                                   .arg(m_progress.GetOperationDescription())
                                   .arg(m_iCurrentOperation).arg(m_cOperations));
    else
        m_pDescriptionLbl->setText(QString("%1 ...")
                                   .arg(m_progress.GetOperationDescription()));
    m_progressBar->setMaximum(100);
    setWindowTitle(QString("%1: %2").arg(strTitle, m_progress.GetDescription()));
    m_progressBar->setValue(0);
    m_fCancelEnabled = m_progress.GetCancelable();
    if (m_fCancelEnabled)
    {
        m_pCancelBtn = new UIMiniCancelButton(this);
        m_pCancelBtn->setFocusPolicy(Qt::ClickFocus);
        pLayout2->addWidget(m_pCancelBtn, 0, Qt::AlignVCenter);
        connect(m_pCancelBtn, SIGNAL(clicked()), this, SLOT(cancelOperation()));
    }

    m_pEtaLbl = new QILabel(this);
    pLayout1->addWidget(m_pEtaLbl, 0, Qt::AlignLeft | Qt::AlignVCenter);

    pLayout1->addStretch(1);

    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    retranslateUi();

    /* The progress dialog will be shown automatically after
     * the duration is over if progress is not finished yet. */
    QTimer::singleShot(cMinDuration, this, SLOT(showDialog()));
}

void UIProgressDialog::retranslateUi()
{
    m_strCancel = tr("Canceling...");
    if (m_pCancelBtn)
    {
        m_pCancelBtn->setText(tr("&Cancel"));
        m_pCancelBtn->setToolTip(tr("Cancel the current operation"));
    }
}

int UIProgressDialog::run(int cRefreshInterval)
{
    if (m_progress.isOk())
    {
        /* Start refresh timer */
        int id = startTimer(cRefreshInterval);

        /* Set busy cursor.
         * We don't do this on the Mac, cause regarding the design rules of
         * Apple there is no busy window behavior. A window should always be
         * responsive and is it in our case (We show the progress dialog bar). */
#ifndef Q_WS_MAC
        if (m_fCancelEnabled)
            QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
        else
            QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
#endif /* Q_WS_MAC */

        /* Enter the modal loop, but don't show the window immediately */
        exec(false);

        /* Kill refresh timer */
        killTimer(id);

#ifndef Q_WS_MAC
        /* Reset the busy cursor */
        QApplication::restoreOverrideCursor();
#endif /* Q_WS_MAC */

        return result();
    }
    return Rejected;
}

void UIProgressDialog::reject()
{
    if (m_fCancelEnabled)
        cancelOperation();
}

void UIProgressDialog::timerEvent(QTimerEvent * /* pEvent */)
{
    /* We should hide progress-dialog
     * if it was already finalized but not yet closed.
     * This could happens in case of some other
     * modal dialog prevents our event-loop from
     * being exit overlapping 'this'. */
    if (m_fEnded && !isHidden())
    {
        hide();
        return;
    }
    else if (m_fEnded)
        return;

    if (!m_fEnded && (!m_progress.isOk() || m_progress.GetCompleted()))
    {
        /* Progress finished */
        if (m_progress.isOk())
        {
            m_progressBar->setValue(100);
            done(Accepted);
        }
        /* Progress is not valid */
        else
            done(Rejected);

        /* Request to exit loop */
        m_fEnded = true;
        return;
    }

    if (!m_progress.GetCanceled())
    {
        /* Update the progress dialog */
        /* First ETA */
        long newTime = m_progress.GetTimeRemaining();
        long seconds;
        long minutes;
        long hours;
        long days;

        seconds  = newTime < 0 ? 0 : newTime;
        minutes  = seconds / 60;
        seconds -= minutes * 60;
        hours    = minutes / 60;
        minutes -= hours   * 60;
        days     = hours   / 24;
        hours   -= days    * 24;

        QString strDays = VBoxGlobal::daysToString(days);
        QString strHours = VBoxGlobal::hoursToString(hours);
        QString strMinutes = VBoxGlobal::minutesToString(minutes);
        QString strSeconds = VBoxGlobal::secondsToString(seconds);

        QString strTwoComp = tr("%1, %2 remaining", "You may wish to translate this more like \"Time remaining: %1, %2\"");
        QString strOneComp = tr("%1 remaining", "You may wish to translate this more like \"Time remaining: %1\"");

        if      (days > 1 && hours > 0)
            m_pEtaLbl->setText(strTwoComp.arg(strDays).arg(strHours));
        else if (days > 1)
            m_pEtaLbl->setText(strOneComp.arg(strDays));
        else if (days > 0 && hours > 0)
            m_pEtaLbl->setText(strTwoComp.arg(strDays).arg(strHours));
        else if (days > 0 && minutes > 5)
            m_pEtaLbl->setText(strTwoComp.arg(strDays).arg(strMinutes));
        else if (days > 0)
            m_pEtaLbl->setText(strOneComp.arg(strDays));
        else if (hours > 2)
            m_pEtaLbl->setText(strOneComp.arg(strHours));
        else if (hours > 0 && minutes > 0)
            m_pEtaLbl->setText(strTwoComp.arg(strHours).arg(strMinutes));
        else if (hours > 0)
            m_pEtaLbl->setText(strOneComp.arg(strHours));
        else if (minutes > 2)
            m_pEtaLbl->setText(strOneComp.arg(strMinutes));
        else if (minutes > 0 && seconds > 5)
            m_pEtaLbl->setText(strTwoComp.arg(strMinutes).arg(strSeconds));
        else if (minutes > 0)
            m_pEtaLbl->setText(strOneComp.arg(strMinutes));
        else if (seconds > 5)
            m_pEtaLbl->setText(strOneComp.arg(strSeconds));
        else if (seconds > 0)
            m_pEtaLbl->setText(tr("A few seconds remaining"));
        else
            m_pEtaLbl->clear();

        /* Then operation text if changed */
        ulong newOp = m_progress.GetOperation() + 1;
        if (newOp != m_iCurrentOperation)
        {
            m_iCurrentOperation = newOp;
            m_pDescriptionLbl->setText(QString(m_spcszOpDescTpl)
                                       .arg(m_progress.GetOperationDescription())
                                       .arg(m_iCurrentOperation).arg(m_cOperations));
        }
        m_progressBar->setValue(m_progress.GetPercent());
    }else
        m_pEtaLbl->setText(m_strCancel);
}

void UIProgressDialog::closeEvent(QCloseEvent *pEvent)
{
    if (m_fCancelEnabled)
        cancelOperation();
    else
        pEvent->ignore();
}

void UIProgressDialog::showDialog()
{
    /* We should not show progress-dialog
     * if it was already finalized but not yet closed.
     * This could happens in case of some other
     * modal dialog prevents our event-loop from
     * being exit overlapping 'this'. */
    if (!m_fEnded)
        show();
}

void UIProgressDialog::cancelOperation()
{
    if (m_pCancelBtn)
        m_pCancelBtn->setEnabled(false);
    m_progress.Cancel();
}