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
|
/*
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 <QSqlQuery>
#include <QTableWidget>
#include <QTreeView>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMutexLocker>
#include <QTableWidget>
#include <QModelIndex>
#include "dbanalyzer.h"
#include "smtreemodel.h"
#include "smtreeitem.h"
DbAnalyzerDialog::DbAnalyzerDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
//create tab widget
mTab = new QTabWidget;
//setup analyzer
mAnalyzer = new DbAnalyzer(this);
// no actors
QWidget *noActorsT = new QWidget;
QStringList noActorsHeaders = QStringList() << tr("Series") << tr("Part/Subtitle") << tr("Series Part") << tr("Seriespart Id") << tr("Series Id") << tr("Seriespart");
mNoActorsV = new QTreeView;
mNoActorsM = new SmTreeModel(noActorsHeaders, this);
mNoActorsV->setModel(mNoActorsM);
QVBoxLayout *noActorsL = new QVBoxLayout;
noActorsL->addWidget(mNoActorsV);
mNoActorsV->setColumnHidden(2, true);
mNoActorsV->setColumnHidden(3, true);
mNoActorsV->setColumnHidden(4, true);
mNoActorsV->setColumnHidden(5, true);
mNoActorsV->setEditTriggers(QTreeView::NoEditTriggers);
mNoActorsV->setSelectionBehavior(QAbstractItemView::SelectRows);
mNoActorsV->setSelectionMode(QAbstractItemView::SingleSelection);
mNoActorsV->setAlternatingRowColors(true);
connect(mNoActorsV, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(noActorsDoubleClicked(QModelIndex)));
noActorsT->setLayout(noActorsL);
//stray actors
QWidget *strayActorsT = new QWidget;
QStringList strayActorsHeaders = QStringList() << tr("Actor") << tr("Actor Id") << tr("Count");
mStrayActorsV = new QTreeView;
mStrayActorsM = new SmTreeModel(strayActorsHeaders, this);
mStrayActorsV->setModel(mStrayActorsM);
QVBoxLayout *strayActorsL = new QVBoxLayout;
strayActorsL->addWidget(mStrayActorsV);
mStrayActorsV->setColumnHidden(1, true);
mStrayActorsV->setEditTriggers(QTreeView::NoEditTriggers);
mStrayActorsV->setSelectionBehavior(QAbstractItemView::SelectRows);
mStrayActorsV->setSelectionMode(QAbstractItemView::ExtendedSelection);
mStrayActorsV->setAlternatingRowColors(true);
strayActorsT->setLayout(strayActorsL);
//buttons
mCancel = new QPushButton(tr("Cancel"));
connect(mCancel, SIGNAL(clicked()), this, SLOT(cancelAnalyzer()));
mClose = new QPushButton(tr("Close"));
connect(mClose, SIGNAL(clicked()), this, SLOT(accept()));
mDelete = new QPushButton(tr("Delete..."));
mDelete->setEnabled(false);
connect(mDelete, SIGNAL(clicked()), this, SLOT(deleteItems()));
//setup dialog
mTab->addTab(noActorsT, tr("No Actors"));
mTab->addTab(strayActorsT, tr("Stray actors"));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mTab);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(mCancel);
buttonLayout->addStretch();
buttonLayout->addWidget(mDelete);
buttonLayout->addWidget(mClose);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setMinimumWidth(500);
//get things going
connect(mTab, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
connect(mAnalyzer, SIGNAL(started()), this, SLOT(analyzerStarted()));
connect(mAnalyzer, SIGNAL(finished()), this, SLOT(analyzerFinished()));
mAnalyzer->start();
}
void DbAnalyzerDialog::cancelAnalyzer(){
mAnalyzer->setCancel(true);
}
void DbAnalyzerDialog::analyzerStarted(){
mCancel->setEnabled(true);
mClose->setEnabled(false);
}
void DbAnalyzerDialog::analyzerFinished(){
mCancel->setEnabled(false);
mClose->setEnabled(true);
populate(mNoActorsV, mNoActorsM, mAnalyzer->noActors());
populate(mStrayActorsV, mStrayActorsM, mAnalyzer->strayActors());
}
void DbAnalyzerDialog::noActorsDoubleClicked(const QModelIndex &idx){
if(!idx.isValid()){
return;
}
QModelIndex seriesPartIdx = mNoActorsM->index(idx.row(), 5, idx.parent());
QModelIndex seriesIdx = mNoActorsM->index(idx.row(), 4, idx.parent());
emit partClicked(seriesPartIdx.data().toInt(), seriesIdx.data().toInt());
}
void DbAnalyzerDialog::deleteItems(){
QTreeView *view = 0;
int deleteMode;
switch(mTab->currentIndex()){
case 0:
return;
break;
case 1:
view = mStrayActorsV;
deleteMode = Actors;
break;
default:
view = 0;
break;
}
Q_ASSERT(view);
QModelIndexList selected = view->selectionModel()->selectedRows(1);
QList<int> ids;
foreach(QModelIndex i, selected){
ids << i.data().toInt();
}
emit delItems(deleteMode, ids);
}
void DbAnalyzerDialog::tabChanged(int index){
mDelete->setEnabled(index != 0);
}
void DbAnalyzerDialog::populate(QTreeView *view, SmTreeModel *model, const QList<QList<QVariant> > &data){
const int columns = data.first().count();
if(columns == 0){
return;
}
SmTreeItem *root = new SmTreeItem(columns);
foreach(QList<QVariant> l, data){
SmTreeItem *child = new SmTreeItem(l, root);
root->appendChild(child);
}
model->setRoot(root);
view->resizeColumnToContents(0);
}
DbAnalyzer::DbAnalyzer(QObject *parent) : QThread(parent), mCanceled(false), mStatus(Fail) {
mDb = QSqlDatabase::cloneDatabase(QSqlDatabase::database("treedb"), "analyzerDb");
mDb.open();
mStatus = mDb.isOpen() ? Ok : Fail;
mNoActorQuery = new QSqlQuery(mDb);
mNoActorQuery->prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.tsubtitle, series.iseries_id, seriesparts.iseriesparts_id FROM series, seriesparts LEFT JOIN seriesparts_actormap ON seriesparts.iseriesparts_id = seriesparts_actormap.iseriesparts_id WHERE iactors_id IS NULL AND seriesparts.iseries_id = series.iseries_id ORDER BY tseries_name");
mStrayActorsQuery = new QSqlQuery(mDb);
mStrayActorsQuery->prepare("SELECT actors.tactorname, actors.iactors_id, COUNT(seriesparts_actormap.iactors_id) FROM actors LEFT JOIN seriesparts_actormap ON actors.iactors_id = seriesparts_actormap.iactors_id WHERE seriesparts_actormap.iactors_id IS NULL GROUP BY actors.iactors_id, actors.tactorname ORDER BY actors.tactorname;");
}
DbAnalyzer::~DbAnalyzer(){
delete mNoActorQuery;
delete mStrayActorsQuery;
mDb.close();
mDb = QSqlDatabase();
QSqlDatabase::removeDatabase("analyzerDb");
}
void DbAnalyzer::setCancel(bool canceled){
QMutexLocker m(&mCancelMutex);
mCanceled = canceled;
}
void DbAnalyzer::run(){
noActorsCheck();
strayActorsCheck();
}
void DbAnalyzer::noActorsCheck(){
if(!mNoActorQuery->exec()){
mStatus = Fail;
return;
}
emit message(tr("Fetching movies without actors"));
while(mNoActorQuery->next()){
if(mCanceled){
break;
}
QList<QVariant> res;
res << mNoActorQuery->value(0);
if(!mNoActorQuery->value(2).isNull()){
res << mNoActorQuery->value(2);
}else{
res << mNoActorQuery->value(1);
}
res << mNoActorQuery->value(1) << mNoActorQuery->value(2) << mNoActorQuery->value(3) << mNoActorQuery->value(4);
mNoActorR << res;
}
emit message(tr("Done fetching movies"));
}
void DbAnalyzer::strayActorsCheck(){
if(!mStrayActorsQuery->exec()){
QMutexLocker m(&mStatusMutex);
mStatus = Fail;
return;
}
emit message(tr("Fetching stray actors"));
while(mStrayActorsQuery->next()){
if(mCanceled){
break;
}
QList<QVariant> res;
res << mStrayActorsQuery->value(0) << mStrayActorsQuery->value(1) << mStrayActorsQuery->value(2);
mStrayActorR << res;
}
emit message(tr("Done fetching stray actors"));
}
|