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
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QComboBox>
#include <QDialog>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QSplitter>
#include <QVBoxLayout>
#include "archiveview.h"
#include "smglobals.h"
ArchiveView::ArchiveView(QWidget *parent) : QWidget(parent), mConstructing(true) {
QSettings s;
mArchiveModel = static_cast<ArchiveModel*>(SmGlobals::instance()->model("ArchiveModel"));
connect(mArchiveModel->collector(), SIGNAL(started()), this, SLOT(collectorStarted()));
connect(mArchiveModel->collector(), SIGNAL(finished()), this, SLOT(collectorFinished()));
connect(mArchiveModel, SIGNAL(needRefresh()), this, SLOT(refreshArchive()));
connect(mArchiveModel, SIGNAL(databaseError(QString)), this, SLOT(showDatabaseError(QString)));
mProgress = new ArchiveProgressDialog(this);
mProgress->setHidden(true);
connect(mArchiveModel->collector(), SIGNAL(message(QString)), mProgress, SLOT(setMessage(QString)));
mProxy = new ArchiveProxy;
mProxy->setSourceModel(mArchiveModel);
mTree = new ArchiveTree;
mTree->setModel(mProxy);
mTree->setColumnHidden(ArchiveModel::GenericId, true);
mTree->setColumnHidden(ArchiveModel::SeriesPartId, true);
mTree->setColumnHidden(ArchiveModel::SeriesPart, true);
mTree->setColumnHidden(ArchiveModel::Type, true);
mTree->setColumnHidden(ArchiveModel::Favorite, true);
mTree->setColumnHidden(ArchiveModel::Subtitle, true);
mTree->setColumnHidden(ArchiveModel::Count, true);
mTree->resizeColumnToContents(ArchiveModel::Name);
mTree->setEditTriggers(QAbstractItemView::NoEditTriggers);
connect(mTree, SIGNAL(expanded(QModelIndex)), this, SLOT(expandItem(QModelIndex)));
connect(mTree, SIGNAL(collapsed(QModelIndex)), this, SLOT(collapseItem(QModelIndex)));
QString sortOrderName = s.value("archivemodel/sortorder", "Series Name").toString();
mSortOrder = new QComboBox;
mSortOrder->addItems(mArchiveModel->availableOrders());
mSortOrder->setCurrentText(sortOrderName);
// this is _not_ redundant! when sortOrderName is the first
// item in the list, nothing will happen otherwise!
mArchiveModel->setOrder(sortOrderName);
connect(mSortOrder, SIGNAL(currentIndexChanged(QString)), mArchiveModel, SLOT(setOrder(QString)));
QLabel *l1 = new QLabel(tr("Filter"));
mFilter = new QLineEdit;
QString savedFilter = s.value("archivemodel/filter", QString()).toString();
connect(mFilter, SIGNAL(returnPressed()), this, SLOT(setFilter()));
QPushButton *filter = new QPushButton(tr("Filter"));
connect(filter, SIGNAL(clicked()), this, SLOT(setFilter()));
QPushButton *clear = new QPushButton(tr("Clear"));
mFilter->setText(savedFilter);
connect(clear, SIGNAL(clicked()), this, SLOT(clearFilter()));
mProxy->setFilter(savedFilter, currentSortOrder());
QHBoxLayout *filterLayout = new QHBoxLayout;
filterLayout->addWidget(l1);
filterLayout->addWidget(mFilter);
filterLayout->addWidget(filter);
filterLayout->addWidget(clear);
QVBoxLayout *treeLayout = new QVBoxLayout;
treeLayout->addWidget(mSortOrder);
treeLayout->addLayout(filterLayout);
treeLayout->addWidget(mTree);
QWidget *treeWidget = new QWidget;
treeWidget->setLayout(treeLayout);
mFiles = new ArchiveFiles;
QSplitter *treeSplitter = new QSplitter(Qt::Horizontal);
treeSplitter->addWidget(treeWidget);
treeSplitter->addWidget(mFiles);
treeSplitter->setStretchFactor(0, 1);
treeSplitter->setStretchFactor(1, 4);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(treeSplitter);
setLayout(mainLayout);
}
int ArchiveView::currentSortOrder() const {
return mArchiveModel->availableOrdersHash().value(mSortOrder->currentText());
}
void ArchiveView::writeSettings() {
QSettings s;
s.setValue("archivemodel/sortorder", mSortOrder->currentText());
s.setValue("archivemodel/filter", mFilter->text());
QVariantList expandedItems;
foreach(QPersistentModelIndex idx, mExpandedItems){
QStringList path = mArchiveModel->indexToPath(mProxy->mapToSource(idx));
expandedItems << path;
}
s.setValue("archivemodel/expandeditems", expandedItems);
}
QWidget *ArchiveView::progressDialog(){
return qobject_cast<QWidget *>(mProgress);
}
void ArchiveView::refreshArchive(){
writeSettings();
mArchiveModel->refresh();
}
void ArchiveView::setExpanded(){
QSettings s;
QVariantList expanded = s.value("archivemodel/expandeditems").toList();
foreach(QVariant pathList, expanded){
QStringList path = pathList.toStringList();
QModelIndexList expand = mArchiveModel->pathToIndex(path);
foreach(QModelIndex idx, expand){
mTree->expand(mProxy->mapFromSource(idx));
}
}
}
void ArchiveView::setFilter(){
QString filter = mFilter->text();
mProxy->setFilter(filter, currentSortOrder());
mTree->expandAll();
}
void ArchiveView::clearFilter(){
mFilter->clear();
mProxy->setFilter(QString(), currentSortOrder());
mTree->collapseAll();
}
void ArchiveView::collectorStarted(){
if(mConstructing){
return;
}
mProgress->show();
}
void ArchiveView::collectorFinished(){
mProgress->hide();
setExpanded();
}
void ArchiveView::showDatabaseError(const QString &errorMsg){
QMessageBox::critical(this, tr("Database Error"), errorMsg);
}
void ArchiveView::expandItem(const QModelIndex &idx){
mExpandedItems << QPersistentModelIndex(idx);
}
void ArchiveView::collapseItem(const QModelIndex &idx){
mExpandedItems.removeAll(QPersistentModelIndex(idx));
}
ArchiveTree::ArchiveTree(QWidget *parent) : SmTreeView(parent) {}
void ArchiveTree::setModel(ArchiveProxy *model){
mProxy = model;
mModel = qobject_cast<ArchiveModel*>(mProxy->sourceModel());
QTreeView::setModel(model);
}
void ArchiveTree::rename(){
QModelIndex idx = currentIndex();
int nodeType = idx.data(ArchiveModel::TypeRole).toInt();
if(nodeType == ArchiveModel::SeriesPartNode){
impossible();
return;
}
QString currentName = idx.data(ArchiveModel::NameRole).toString();
QString question = QString(tr("Rename %1 to:")).arg(currentName);
QString newName = QInputDialog::getText(this, tr("Rename"), question, QLineEdit::Normal, currentName);
if(!newName.isEmpty()){
QModelIndex realIdx = mProxy->mapToSource(idx);
mModel->setData(realIdx, newName, Qt::EditRole);
}
}
void ArchiveTree::remove(){
QModelIndex idx = currentIndex();
QModelIndex realIdx = mProxy->mapToSource(idx);
QString warningMsg = QString("Really remove %1?").arg(idx.data().toString());
int retval = QMessageBox::warning(this, tr("Question"), warningMsg, QMessageBox::Yes | QMessageBox::No);
if(retval == QMessageBox::Yes){
if(!mModel->removeNode(realIdx)){
impossible();
}
}
}
void ArchiveTree::impossible(const QString msg){
QMessageBox::critical(this, tr("Error"), msg);
}
ArchiveProgressDialog::ArchiveProgressDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
QHBoxLayout *mainLayout = new QHBoxLayout;
mMessage = new QLabel;
mainLayout->addWidget(mMessage);
setLayout(mainLayout);
}
void ArchiveProgressDialog::setMessage(const QString &msg){
QString message = QString(tr("<b>Gathering data... please wait!</b><br/><ul><li>%1</li></ul>")).arg(msg);
mMessage->setText(message);
}
ArchiveFiles::ArchiveFiles(QWidget *parent) : SmTreeView(parent){
}
ArchiveProxy::ArchiveProxy(QObject *parent) : QSortFilterProxyModel(parent) {}
void ArchiveProxy::setFilter(const QString &filter, int sortOrder){
mFilter = QRegExp(filter);
mSortOrder = sortOrder;
invalidateFilter();
}
bool ArchiveProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
if(mFilter.isEmpty()){
return true;
}
QModelIndex nameIdx = sourceModel()->index(sourceRow, ArchiveModel::Name, sourceParent);
ArchiveModel *model = qobject_cast<ArchiveModel*>(sourceModel());
return model->matchRecursive(nameIdx, mFilter);
}
|