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
|
/*
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 <QDebug>
#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("Series part") << 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(1, true);
mNoActorsV->setColumnHidden(2, true);
mNoActorsV->setColumnHidden(3, true);
mNoActorsV->setEditTriggers(QTreeView::NoEditTriggers);
mNoActorsV->setSelectionBehavior(QAbstractItemView::SelectRows);
mNoActorsV->setSelectionMode(QAbstractItemView::SingleSelection);
connect(mNoActorsV, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(noActorsDoubleClicked(QModelIndex)));
noActorsT->setLayout(noActorsL);
//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()));
//setup dialog
mTab->addTab(noActorsT, tr("No Actors"));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mTab);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(mCancel);
buttonLayout->addWidget(mClose);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setMinimumWidth(500);
//get things going
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);
populateNoActors();
}
void DbAnalyzerDialog::noActorsDoubleClicked(const QModelIndex &idx){
qDebug() << idx;
if(!idx.isValid()){
return;
}
QModelIndex seriesPartIdx = mNoActorsM->index(idx.row(), 3, idx.parent());
QModelIndex seriesIdx = mNoActorsM->index(idx.row(), 2, idx.parent());
qDebug() << seriesPartIdx << seriesIdx;
//if(spIdx.isValid()){
emit partClicked(seriesPartIdx.data().toInt(), seriesIdx.data().toInt());
//}
}
void DbAnalyzerDialog::populateNoActors(){
const int columns = 4;
QList<QList<QVariant> > noActors = mAnalyzer->noActors();
SmTreeItem *root = new SmTreeItem(columns);
foreach(QList<QVariant> l, noActors){
SmTreeItem *child = new SmTreeItem(l, root);
root->appendChild(child);
}
mNoActorsM->setRoot(root);
mNoActorsV->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, 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");
}
DbAnalyzer::~DbAnalyzer(){
delete mNoActorQuery;
mDb.close();
}
void DbAnalyzer::setCancel(bool canceled){
QMutexLocker m(&mCancelMutex);
mCanceled = canceled;
}
void DbAnalyzer::run(){
noActorsCheck();
}
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;
for(int i = 0; i < 4; ++i){
if(i == 0){
if(mNoActorQuery->value(1).toInt() > 1){
QString title = QString("%1 %2").arg(mNoActorQuery->value(0).toString(), mNoActorQuery->value(1).toString());
res << title;
continue;
}else{
res << mNoActorQuery->value(0);
}
}
res << mNoActorQuery->value(i);
}
mNoActorR << res;
}
emit message(tr("Done!"));
}
|