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
|
/*
SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "infodialog.h"
#include "migration_debug.h"
#include <KLocalizedString>
#include <QApplication>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QListWidget>
#include <QProgressBar>
#include <QPushButton>
#include <QScrollBar>
#include <QVBoxLayout>
enum {
// The max value of the scrollbar. Don't change this without making the kmail
// migrator use this. It still uses hardcoded "100".
MAX_PROGRESS = 100
};
bool InfoDialog::mError = false;
InfoDialog::InfoDialog(bool closeWhenDone)
: mButtonBox(new QDialogButtonBox(QDialogButtonBox::Close, this))
, mList(new QListWidget(this))
, mStatusLabel(new QLabel(this))
, mProgressBar(new QProgressBar(this))
, mCloseWhenDone(closeWhenDone)
{
setAttribute(Qt::WA_DeleteOnClose);
auto mainLayout = new QVBoxLayout(this);
mList->setMinimumWidth(640);
mainLayout->addWidget(mList);
auto statusLayout = new QHBoxLayout;
mainLayout->addLayout(statusLayout);
mStatusLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
statusLayout->addWidget(mStatusLabel);
mProgressBar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
mProgressBar->setMinimumWidth(200);
mProgressBar->setFormat(i18nc("Percent value; %p is the value, % is the percent sign", "%p%"));
statusLayout->addWidget(mProgressBar);
connect(mButtonBox, &QDialogButtonBox::accepted, this, &InfoDialog::accept);
connect(mButtonBox, &QDialogButtonBox::rejected, this, &InfoDialog::reject);
mButtonBox->button(QDialogButtonBox::Close)->setEnabled(false);
mainLayout->addWidget(mButtonBox);
}
InfoDialog::~InfoDialog() = default;
static KMigratorBase::MessageType convertType(MigratorBase::MessageType type)
{
switch (type) {
case MigratorBase::Success:
return KMigratorBase::Success;
case MigratorBase::Error:
return KMigratorBase::Error;
case MigratorBase::Skip:
return KMigratorBase::Skip;
case MigratorBase::Warning:
return KMigratorBase::Warning;
case MigratorBase::Info:
return KMigratorBase::Info;
}
return KMigratorBase::Info;
}
void InfoDialog::message(MigratorBase::MessageType type, const QString &msg)
{
message(convertType(type), msg);
}
void InfoDialog::message(KMigratorBase::MessageType type, const QString &msg)
{
bool autoScroll = mAutoScrollList;
auto item = new QListWidgetItem(msg, mList);
switch (type) {
case KMigratorBase::Success:
item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok-apply")));
mChange = true;
qCDebug(MIGRATION_LOG) << msg;
break;
case KMigratorBase::Skip:
item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok")));
qCDebug(MIGRATION_LOG) << msg;
break;
case KMigratorBase::Info:
item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-information")));
qCDebug(MIGRATION_LOG) << msg;
break;
case KMigratorBase::Warning:
item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-warning")));
qCDebug(MIGRATION_LOG) << msg;
break;
case KMigratorBase::Error: {
item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-error")));
QFont currentFont = font();
currentFont.setBold(true);
item->setFont(currentFont);
mError = true;
qCCritical(MIGRATION_LOG) << msg;
break;
}
default:
qCCritical(MIGRATION_LOG) << "WTF?";
}
mAutoScrollList = autoScroll;
if (autoScroll) {
mList->scrollToItem(item);
}
}
void InfoDialog::migratorAdded()
{
++mMigratorCount;
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
}
void InfoDialog::migratorDone()
{
QApplication::restoreOverrideCursor();
--mMigratorCount;
if (mMigratorCount == 0) {
mButtonBox->button(QDialogButtonBox::Close)->setEnabled(true);
status(QString());
if (mCloseWhenDone && !hasError() && !hasChange()) {
accept();
}
}
}
void InfoDialog::status(const QString &msg)
{
mStatusLabel->setText(msg);
if (msg.isEmpty()) {
progress(0, MAX_PROGRESS, MAX_PROGRESS);
mProgressBar->setFormat(QString());
}
}
void InfoDialog::progress(int value)
{
mProgressBar->setFormat(QStringLiteral("%p%"));
mProgressBar->setValue(value);
}
void InfoDialog::progress(int min, int max, int value)
{
mProgressBar->setFormat(QStringLiteral("%p%"));
mProgressBar->setRange(min, max);
mProgressBar->setValue(value);
}
void InfoDialog::scrollBarMoved(int value)
{
mAutoScrollList = (value == mList->verticalScrollBar()->maximum());
}
#include "moc_infodialog.cpp"
|