File: kprogressdialog.cpp

package info (click to toggle)
kde4libs 4%3A4.8.4-4%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,588 kB
  • sloc: cpp: 746,914; xml: 8,370; ansic: 6,295; java: 4,060; perl: 2,930; yacc: 2,462; sh: 1,225; python: 1,081; ruby: 337; lex: 278; makefile: 29
file content (256 lines) | stat: -rw-r--r-- 5,733 bytes parent folder | download | duplicates (3)
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
/* This file is part of the KDE libraries
   Copyright (C) 1996 Martynas Kunigelis // krazy:exclude=copyright (email unknown)
   Copyright (C) 2006-2007 Urs Wolfer <uwolfer at kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kprogressdialog.h"

#include <QLabel>
#include <QLayout>
#include <QProgressBar>
#include <QTimer>

#include <kguiitem.h>
#include <kpushbutton.h>

class KProgressDialog::KProgressDialogPrivate
{
public:
    KProgressDialogPrivate(KProgressDialog *q)
        : q(q),
          cancelButtonShown(true),
          mAutoClose(true),
          mAutoReset(false),
          mCancelled(false),
          mAllowCancel(true),
          mShown(false),
          mMinDuration(2000)
    {
    }

    void slotAutoShow();
    void slotAutoActions(int percentage);

    KProgressDialog *q;
    bool          cancelButtonShown : 1;
    bool          mAutoClose : 1;
    bool          mAutoReset : 1;
    bool          mCancelled : 1;
    bool          mAllowCancel : 1;
    bool          mShown : 1;
    QString       mCancelText;
    QLabel*       mLabel;
    QProgressBar* mProgressBar;
    QTimer*       mShowTimer;
    int           mMinDuration;
};

KProgressDialog::KProgressDialog(QWidget* parent, const QString& caption,
                                 const QString& text, Qt::WFlags flags)
  : KDialog(parent, flags),
    d(new KProgressDialogPrivate(this))
{
    setCaption(caption);
    setButtons(KDialog::Cancel);

    d->mShowTimer = new QTimer(this);

    d->mCancelText = KDialog::buttonText(KDialog::Cancel);

    QWidget *mainWidget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout(mainWidget);
    layout->setMargin(10);

    d->mLabel = new QLabel(text, mainWidget);
    layout->addWidget(d->mLabel);

    d->mProgressBar = new QProgressBar(mainWidget);
    layout->addWidget(d->mProgressBar);

    setMainWidget(mainWidget);

    connect(d->mProgressBar, SIGNAL(valueChanged(int)),
            this, SLOT(slotAutoActions(int)));
    connect(d->mShowTimer, SIGNAL(timeout()), this, SLOT(slotAutoShow()));
    d->mShowTimer->setSingleShot(true);
    d->mShowTimer->start(d->mMinDuration);
}

KProgressDialog::~KProgressDialog()
{
    delete d;
}

void KProgressDialog::KProgressDialogPrivate::slotAutoShow()
{
    if (mShown || mCancelled)
    {
        return;
    }

    q->show();
}

void KProgressDialog::showEvent(QShowEvent *event)
{
    d->mShown = true;
    KDialog::showEvent(event);
}

void KProgressDialog::reject()
{
    d->mCancelled = true;

    if (d->mAllowCancel)
    {
        KDialog::reject();
    }
}

bool KProgressDialog::wasCancelled() const
{
    return d->mCancelled;
}

void KProgressDialog::ignoreCancel()
{
    d->mCancelled = false;
}

void KProgressDialog::setMinimumDuration(int ms)
{
    d->mMinDuration = ms;
    if (!d->mShown)
    {
        d->mShowTimer->stop();
        d->mShowTimer->setSingleShot(true);
        d->mShowTimer->start(d->mMinDuration);
    }
}

int KProgressDialog::minimumDuration() const
{
    return d->mMinDuration;
}

void KProgressDialog::setAllowCancel(bool allowCancel)
{
    d->mAllowCancel = allowCancel;
    showCancelButton(allowCancel);
}

bool KProgressDialog::allowCancel() const
{
    return d->mAllowCancel;
}

QProgressBar* KProgressDialog::progressBar()
{
    return d->mProgressBar;
}

const QProgressBar* KProgressDialog::progressBar() const
{
    return d->mProgressBar;
}

void KProgressDialog::setLabelText(const QString& text)
{
    d->mLabel->setText(text);
}

QString KProgressDialog::labelText() const
{
    return d->mLabel->text();
}

void KProgressDialog::showCancelButton(bool show)
{
    showButton(Cancel, show);
}

bool KProgressDialog::autoClose() const
{
    return d->mAutoClose;
}

void KProgressDialog::setAutoClose(bool autoClose)
{
    d->mAutoClose = autoClose;
}

bool KProgressDialog::autoReset() const
{
    return d->mAutoReset;
}

void KProgressDialog::setAutoReset(bool autoReset)
{
    d->mAutoReset = autoReset;
}

void KProgressDialog::setButtonText(const QString& text)
{
    d->mCancelText = text;
    setButtonGuiItem(Cancel, KGuiItem(text));
}

QString KProgressDialog::buttonText() const
{
    return d->mCancelText;
}

void KProgressDialog::KProgressDialogPrivate::slotAutoActions(int percentage)
{
    if (percentage < mProgressBar->maximum() ||
        (mProgressBar->minimum() == mProgressBar->maximum())) // progress dialogs with busy indicators (see #178648)
    {
        if (!cancelButtonShown)
        {
            q->setButtonGuiItem(KDialog::Cancel, KGuiItem(mCancelText));
            cancelButtonShown = true;
        }
        return;
    }

    mShowTimer->stop();

    if (mAutoReset)
    {
        mProgressBar->setValue(0);
    }
    else
    {
        q->setAllowCancel(true);
        q->setButtonGuiItem(Cancel, KStandardGuiItem::close());
        cancelButtonShown = false;
    }

    if (mAutoClose)
    {
        if (mShown)
        {
            q->hide();
        }
        else
        {
            emit q->finished();
        }
    }
}

#include "kprogressdialog.moc"