summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/extensions/QIMessageBox.cpp
blob: e1321fcd8885af8ba485e5daa02b0e31f6ffa571 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* $Id$ */
/** @file
 * VBox Qt GUI - Qt extensions: QIMessageBox class implementation.
 */

/*
 * Copyright (C) 2006-2022 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.
 */

/* Qt includes: */
#include <QCheckBox>
#include <QClipboard>
#include <QHBoxLayout>
#include <QLabel>
#include <QMimeData>
#include <QPushButton>
#include <QRegExp>
#include <QRegularExpression>
#include <QStyle>
#include <QVBoxLayout>

/* GUI includes: */
#include "QIArrowSplitter.h"
#include "QIDialogButtonBox.h"
#include "QIMessageBox.h"
#include "QIRichTextLabel.h"
#include "UICommon.h"
#include "UIIconPool.h"
#include "UIMessageCenter.h"

/* Other VBox includes: */
#include <iprt/assert.h>


QIMessageBox::QIMessageBox(const QString &strTitle, const QString &strMessage, AlertIconType iconType,
                           int iButton1 /* = 0*/, int iButton2 /* = 0*/, int iButton3 /* = 0*/, QWidget *pParent /* = 0*/,
                           const QString &strHelpKeyword /* = QString() */)
    : QIDialog(pParent)
    , m_strTitle(strTitle)
    , m_iconType(iconType)
    , m_pLabelIcon(0)
    , m_strMessage(strMessage)
    , m_pLabelText(0)
    , m_pFlagCheckBox(0)
    , m_pDetailsContainer(0)
    , m_iButton1(iButton1)
    , m_iButton2(iButton2)
    , m_iButton3(iButton3)
    , m_iButtonEsc(0)
    , m_pButton1(0)
    , m_pButton2(0)
    , m_pButton3(0)
    , m_pButtonHelp(0)
    , m_pButtonBox(0)
    , m_strHelpKeyword(strHelpKeyword)
    , m_fDone(false)
{
    /* Prepare: */
    prepare();
}

void QIMessageBox::setDetailsText(const QString &strText)
{
    /* Make sure details-text is NOT empty: */
    AssertReturnVoid(!strText.isEmpty());

    /* Split details into paragraphs: */
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    QStringList paragraphs(strText.split("<!--EOP-->", Qt::SkipEmptyParts));
#else
    QStringList paragraphs(strText.split("<!--EOP-->", QString::SkipEmptyParts));
#endif
    /* Make sure details-text has at least one paragraph: */
    AssertReturnVoid(!paragraphs.isEmpty());

    /* Enumerate all the paragraphs: */
    QStringPairList details;
    foreach (const QString &strParagraph, paragraphs)
    {
        /* Split each paragraph into pairs: */
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
        QStringList parts(strParagraph.split("<!--EOM-->", Qt::KeepEmptyParts));
#else
        QStringList parts(strParagraph.split("<!--EOM-->", QString::KeepEmptyParts));
#endif
        /* Make sure each paragraph consist of 2 parts: */
        AssertReturnVoid(parts.size() == 2);
        /* Append each pair into details-list: */
        details << QStringPair(parts[0], parts[1]);
    }

    /* Pass details-list to details-container: */
    m_pDetailsContainer->setDetails(details);
    /* Update details-container finally: */
    updateDetailsContainer();
}

bool QIMessageBox::flagChecked() const
{
    return m_pFlagCheckBox->isChecked();
}

void QIMessageBox::setFlagChecked(bool fChecked)
{
    m_pFlagCheckBox->setChecked(fChecked);
}

void QIMessageBox::setFlagText(const QString &strFlagText)
{
    /* Pass text to flag check-box: */
    m_pFlagCheckBox->setText(strFlagText);
    /* Update flag check-box finally: */
    updateCheckBox();
}

void QIMessageBox::setButtonText(int iButton, const QString &strText)
{
    switch (iButton)
    {
        case 0: if (m_pButton1) m_pButton1->setText(strText); break;
        case 1: if (m_pButton2) m_pButton2->setText(strText); break;
        case 2: if (m_pButton3) m_pButton3->setText(strText); break;
        default: break;
    }
}

void QIMessageBox::polishEvent(QShowEvent *pPolishEvent)
{
    /* Call to base-class: */
    QIDialog::polishEvent(pPolishEvent);

    /* Update size finally: */
    sltUpdateSize();
}

void QIMessageBox::closeEvent(QCloseEvent *pCloseEvent)
{
    if (m_fDone)
        pCloseEvent->accept();
    else
    {
        pCloseEvent->ignore();
        reject();
    }
}

void QIMessageBox::sltUpdateSize()
{
    /* Fix minimum possible size: */
    setFixedSize(minimumSizeHint());
}

void QIMessageBox::sltCopy() const
{
    /* Create the error string with all errors. First the html version. */
    QString strError = "<html><body><p>" + m_strMessage + "</p>";
    foreach (const QStringPair &pair, m_pDetailsContainer->details())
        strError += pair.first + pair.second + "<br>";
    strError += "</body></html>";
    strError.remove(QRegularExpression("</+qt>"));
    strError = strError.replace(QRegularExpression("&nbsp;"), " ");
    /* Create a new mime data object holding both the html and the plain text version. */
    QMimeData *pMimeData = new QMimeData();
    pMimeData->setHtml(strError);
    /* Replace all the html entities. */
    strError = strError.replace(QRegularExpression("<br>|</tr>"), "\n");
    strError = strError.replace(QRegularExpression("</p>"), "\n\n");
    strError = strError.remove(QRegularExpression("<[^>]*>"));
    pMimeData->setText(strError);
    /* Add the mime data to the global clipboard. */
    QClipboard *pClipboard = QApplication::clipboard();
    pClipboard->setMimeData(pMimeData);
}

void QIMessageBox::reject()
{
    if (m_iButtonEsc)
    {
        QDialog::reject();
        setResult(m_iButtonEsc & AlertButtonMask);
    }
}

void QIMessageBox::prepare()
{
    /* Set caption: */
    setWindowTitle(m_strTitle);

    /* Create main-layout: */
    QVBoxLayout *pMainLayout = new QVBoxLayout(this);
    AssertPtrReturnVoid(pMainLayout);
    {
        /* Configure main-layout: */
#ifdef VBOX_WS_MAC
        pMainLayout->setContentsMargins(40, 20, 40, 20);
        pMainLayout->setSpacing(15);
#else
        pMainLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) * 2);
#endif
        /* Create top-layout: */
        QHBoxLayout *pTopLayout = new QHBoxLayout;
        AssertPtrReturnVoid(pTopLayout);
        {
            /* Configure top-layout: */
            pTopLayout->setContentsMargins(0, 0, 0, 0);
            /* Create icon-label: */
            m_pLabelIcon = new QLabel;
            AssertPtrReturnVoid(m_pLabelIcon);
            {
                /* Configure icon-label: */
                m_pLabelIcon->setPixmap(standardPixmap(m_iconType, this));
                m_pLabelIcon->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
                m_pLabelIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
                /* Add icon-label into top-layout: */
                pTopLayout->addWidget(m_pLabelIcon);
            }
            /* Create text-label: */
            m_pLabelText = new QIRichTextLabel;
            AssertPtrReturnVoid(m_pLabelText);
            {
                /* Configure text-label: */
                m_pLabelText->setText(compressLongWords(m_strMessage));
                /* Add text-label into top-layout: */
                pTopLayout->addWidget(m_pLabelText);
            }
            /* Add top-layout into main-layout: */
            pMainLayout->addLayout(pTopLayout);
        }
        /* Create details-container: */
        m_pDetailsContainer = new QIArrowSplitter;
        AssertPtrReturnVoid(m_pDetailsContainer);
        {
            /* Configure container: */
            connect(m_pDetailsContainer, &QIArrowSplitter::sigSizeHintChange,
                    this, &QIMessageBox::sltUpdateSize);
            /* Add details-container into main-layout: */
            pMainLayout->addWidget(m_pDetailsContainer);
            /* Update details-container finally: */
            updateDetailsContainer();
        }
        /* Create flag check-box: */
        m_pFlagCheckBox = new QCheckBox;
        AssertPtrReturnVoid(m_pFlagCheckBox);
        {
            /* Configure flag check-box: */
            m_pFlagCheckBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
            /* Add flag check-box into main-layout: */
            pMainLayout->addWidget(m_pFlagCheckBox, 0, Qt::AlignHCenter | Qt::AlignVCenter);
            /* Update flag check-box finally: */
            updateCheckBox();
        }
        /* Create button-box: */
        m_pButtonBox = new QIDialogButtonBox;
        AssertPtrReturnVoid(m_pButtonBox);
        {
            /* Configure button-box: */
            m_pButtonBox->setCenterButtons(true);
            m_pButton1 = createButton(m_iButton1);
            if (m_pButton1)
                connect(m_pButton1, &QPushButton::clicked, this, &QIMessageBox::sltDone1);
            m_pButton2 = createButton(m_iButton2);
            if (m_pButton2)
                connect(m_pButton2, &QPushButton::clicked, this, &QIMessageBox::sltDone2);
            m_pButton3 = createButton(m_iButton3);
            if (m_pButton3)
                connect(m_pButton3, &QPushButton::clicked, this, &QIMessageBox::sltDone3);
            /* Create the help button and connect it to relevant slot in case a help word is supplied: */
            if (!m_strHelpKeyword.isEmpty())
            {
                m_pButtonHelp = createButton(AlertButton_Help);
                if (m_pButtonHelp)
                {
                    uiCommon().setHelpKeyword(m_pButtonHelp, m_strHelpKeyword);
                    connect(m_pButtonHelp, &QPushButton::clicked, &msgCenter(), &UIMessageCenter::sltHandleHelpRequest);
                }
            }

            /* Make sure Escape button always set: */
            Assert(m_iButtonEsc);
            /* If this is a critical message add a "Copy to clipboard" button: */
            if (m_iconType == AlertIconType_Critical)
            {
                QPushButton *pCopyButton = createButton(AlertButton_Copy);
                pCopyButton->setToolTip(tr("Copy all errors to the clipboard"));
                connect(pCopyButton, &QPushButton::clicked, this, &QIMessageBox::sltCopy);
            }
            /* Add button-box into main-layout: */
            pMainLayout->addWidget(m_pButtonBox);

            /* Prepare focus. It is important to prepare focus after adding button-box to the layout as
             * parenting the button-box to the QDialog changes default button focus by Qt: */
            prepareFocus();
        }
    }
}

void QIMessageBox::prepareFocus()
{
    /* Configure default button and focus: */
    if (m_pButton1 && (m_iButton1 & AlertButtonOption_Default))
    {
        m_pButton1->setDefault(true);
        m_pButton1->setFocus();
    }
    if (m_pButton2 && (m_iButton2 & AlertButtonOption_Default))
    {
        m_pButton2->setDefault(true);
        m_pButton2->setFocus();
    }
    if (m_pButton3 && (m_iButton3 & AlertButtonOption_Default))
    {
        m_pButton3->setDefault(true);
        m_pButton3->setFocus();
    }
}

QPushButton *QIMessageBox::createButton(int iButton)
{
    /* Not for AlertButton_NoButton: */
    if (iButton == 0)
        return 0;

    /* Prepare button text & role: */
    QString strText;
    QDialogButtonBox::ButtonRole role;
    switch (iButton & AlertButtonMask)
    {
        case AlertButton_Ok:      strText = tr("OK");     role = QDialogButtonBox::AcceptRole; break;
        case AlertButton_Cancel:  strText = tr("Cancel"); role = QDialogButtonBox::RejectRole; break;
        case AlertButton_Choice1: strText = tr("Yes");    role = QDialogButtonBox::YesRole; break;
        case AlertButton_Choice2: strText = tr("No");     role = QDialogButtonBox::NoRole; break;
        case AlertButton_Copy:    strText = tr("Copy");   role = QDialogButtonBox::ActionRole; break;
        case AlertButton_Help:    strText = tr("Help");   role = QDialogButtonBox::HelpRole; break;
        default:
            AssertMsgFailed(("Type %d is not supported!", iButton));
            return 0;
    }

    /* Create push-button: */
    QPushButton *pButton = m_pButtonBox->addButton(strText, role);

    /* Configure <escape> button: */
    if (iButton & AlertButtonOption_Escape)
        m_iButtonEsc = iButton & AlertButtonMask;

    /* Return button: */
    return pButton;
}

void QIMessageBox::updateDetailsContainer()
{
    /* Details-container with details is always visible: */
    m_pDetailsContainer->setVisible(!m_pDetailsContainer->details().isEmpty());
    /* Update size: */
    sltUpdateSize();
}

void QIMessageBox::updateCheckBox()
{
    /* Flag check-box with text is always visible: */
    m_pFlagCheckBox->setVisible(!m_pFlagCheckBox->text().isEmpty());
    /* Update size: */
    sltUpdateSize();
}

/* static */
QPixmap QIMessageBox::standardPixmap(AlertIconType iconType, QWidget *pWidget /* = 0*/)
{
    /* Prepare standard icon: */
    QIcon icon;
    switch (iconType)
    {
        case AlertIconType_Information:    icon = UIIconPool::defaultIcon(UIIconPool::UIDefaultIconType_MessageBoxInformation, pWidget); break;
        case AlertIconType_Warning:        icon = UIIconPool::defaultIcon(UIIconPool::UIDefaultIconType_MessageBoxWarning, pWidget); break;
        case AlertIconType_Critical:       icon = UIIconPool::defaultIcon(UIIconPool::UIDefaultIconType_MessageBoxCritical, pWidget); break;
        case AlertIconType_Question:       icon = UIIconPool::defaultIcon(UIIconPool::UIDefaultIconType_MessageBoxQuestion, pWidget); break;
        case AlertIconType_GuruMeditation: icon = UIIconPool::iconSet(":/meditation_32px.png"); break;
        default: break;
    }
    /* Return empty pixmap if nothing found: */
    if (icon.isNull())
        return QPixmap();
    /* Return pixmap of standard size if possible: */
    QStyle *pStyle = pWidget ? pWidget->style() : QApplication::style();
    int iSize = pStyle->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, pWidget);
    return icon.pixmap(iSize, iSize);
}

/* static */
QString QIMessageBox::compressLongWords(QString strText)
{
    // WORKAROUND:
    // The idea is to compress long words of more than 100 symbols in size consisting of alphanumeric
    // characters with ellipsiss using the following template:
    // "[50 first symbols]...[50 last symbols]"
    QRegExp re("[a-zA-Z0-9]{101,}");
    int iPosition = re.indexIn(strText);
    bool fChangeAllowed = iPosition != -1;
    while (fChangeAllowed)
    {
        QString strNewText = strText;
        const QString strFound = re.cap(0);
        strNewText.replace(iPosition, strFound.size(), strFound.left(50) + "..." + strFound.right(50));
        fChangeAllowed = fChangeAllowed && strText != strNewText;
        strText = strNewText;
        iPosition = re.indexIn(strText);
        fChangeAllowed = fChangeAllowed && iPosition != -1;
    }
    return strText;
}