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
|
/*
* SPDX-FileCopyrightText: 2006 Peter Penz <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "viewpropsprogressinfo.h"
#include "applyviewpropsjob.h"
#include "views/viewproperties.h"
#include <KConfigGroup>
#include <KLocalizedString>
#include <QDialogButtonBox>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
ViewPropsProgressInfo::ViewPropsProgressInfo(QWidget* parent,
const QUrl& dir,
const ViewProperties& viewProps) :
QDialog(parent),
m_dir(dir),
m_viewProps(nullptr),
m_label(nullptr),
m_progressBar(nullptr),
m_dirSizeJob(nullptr),
m_applyViewPropsJob(nullptr),
m_timer(nullptr)
{
const QSize minSize = minimumSize();
setMinimumSize(QSize(320, minSize.height()));
setWindowTitle(i18nc("@title:window", "Applying View Properties"));
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
m_viewProps = new ViewProperties(dir);
m_viewProps->setDirProperties(viewProps);
// the view properties are stored by the ApplyViewPropsJob, so prevent
// that the view properties are saved twice:
m_viewProps->setAutoSaveEnabled(false);
auto layout = new QVBoxLayout(this);
m_label = new QLabel(i18nc("@info:progress", "Counting folders: %1", 0), this);
layout->addWidget(m_label);
m_progressBar = new QProgressBar(this);
m_progressBar->setMinimum(0);
m_progressBar->setMaximum(0);
m_progressBar->setValue(0);
layout->addWidget(m_progressBar);
layout->addStretch();
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
connect(buttonBox, &QDialogButtonBox::accepted, this, &ViewPropsProgressInfo::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &ViewPropsProgressInfo::reject);
layout->addWidget(buttonBox);
// Use the directory size job to count the number of directories first. This
// allows to give a progress indication for the user when applying the view
// properties later.
m_dirSizeJob = KIO::directorySize(dir);
connect(m_dirSizeJob, &KIO::DirectorySizeJob::result,
this, &ViewPropsProgressInfo::applyViewProperties);
// The directory size job cannot emit any progress signal, as it is not aware
// about the total number of directories. Therefor a timer is triggered, which
// periodically updates the current directory count.
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout,
this, &ViewPropsProgressInfo::updateProgress);
m_timer->start(300);
}
ViewPropsProgressInfo::~ViewPropsProgressInfo()
{
delete m_viewProps;
m_viewProps = nullptr;
}
void ViewPropsProgressInfo::closeEvent(QCloseEvent* event)
{
m_timer->stop();
m_applyViewPropsJob = nullptr;
QDialog::closeEvent(event);
}
void ViewPropsProgressInfo::reject()
{
if (m_dirSizeJob) {
m_dirSizeJob->kill();
m_dirSizeJob = nullptr;
}
if (m_applyViewPropsJob) {
m_applyViewPropsJob->kill();
m_applyViewPropsJob = nullptr;
}
QDialog::reject();
}
void ViewPropsProgressInfo::updateProgress()
{
if (m_dirSizeJob) {
const int subdirs = m_dirSizeJob->totalSubdirs();
m_label->setText(i18nc("@info:progress", "Counting folders: %1", subdirs));
}
if (m_applyViewPropsJob) {
const int progress = m_applyViewPropsJob->progress();
m_progressBar->setValue(progress);
}
}
void ViewPropsProgressInfo::applyViewProperties()
{
if (m_dirSizeJob->error()) {
return;
}
const int subdirs = m_dirSizeJob->totalSubdirs();
m_label->setText(i18nc("@info:progress", "Folders: %1", subdirs));
m_progressBar->setMaximum(subdirs);
m_dirSizeJob = nullptr;
m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps);
connect(m_applyViewPropsJob, &ApplyViewPropsJob::result,
this, &ViewPropsProgressInfo::close);
}
|