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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
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 <QLabel>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QSortFilterProxyModel>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QToolBar>
#include <QApplication>
#include <QHeaderView>
#include <QSettings>
#include <QGroupBox>
#include <QComboBox>
#include <QStandardItemModel>
#include <QSplitter>
#include "moviepropertiesdialog.h"
#include "smtreeview.h"
#include "smtreeitem.h"
#include "smtreemodel.h"
#include "searchdialog.h"
#include "smglobals.h"
#include "helper.h"
FilenamesAndMetadata::FilenamesAndMetadata(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags){
// Define GUI items
setWindowTitle(tr("Search..."));
QLabel *l1 = new QLabel(tr("Search"));
mSearch = new QLineEdit;
connect(mSearch, &QLineEdit::returnPressed, this, &FilenamesAndMetadata::search);
QToolBar *searchTB = new QToolBar;
QAction *doSearchA = new QAction(Helper::icon(Qt::transparent, qApp->palette().color(QPalette::Text), QChar(0x2245), true, false), tr("Search"), this);
connect(doSearchA, &QAction::triggered, this, &FilenamesAndMetadata::search);
searchTB->addAction(doSearchA);
QAction *clearSearchA= new QAction(Helper::icon(Qt::transparent, qApp->palette().color(QPalette::Text), QChar(0x2694), true, false), tr("Clear"), this);
connect(clearSearchA, &QAction::triggered, [=] { mSearch->clear(); });
searchTB->addAction(clearSearchA);
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(l1);
topLayout->addWidget(mSearch);
topLayout->addWidget(searchTB);
// init Model and View
const QStringList headers = QStringList() << tr("Found") << tr("Series") << tr("SeriesPartId");
mModel = new SmTreeModel(headers, this);
mProxy = new QSortFilterProxyModel(this);
mProxy->setSourceModel(mModel);
mResult = new QTreeView;
mResult->setModel(mProxy);
mResult->setColumnHidden(2, true);
mResult->setSortingEnabled(true);
mResult->setEditTriggers(QAbstractItemView::NoEditTriggers);
QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addWidget(mResult);
// Layout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addLayout(bottomLayout);
setLayout(mainLayout);
}
void FilenamesAndMetadata::search(){
if(mSearch->text().isEmpty()){
return;
}
QSqlDatabase db = QSqlDatabase::database("treedb");
SmTreeItem *root = new SmTreeItem(3, nullptr);
//search metadata
QSqlQuery metadataQ(db);
metadataQ.prepare("SELECT iseriespart_id, tsubject, series.tseries_name, seriesparts.tsubtitle FROM metadata, seriesparts, series WHERE tsubject ~* :re AND metadata.iseriespart_id = seriesparts.iseriesparts_id AND seriesparts.iseries_id = series.iseries_id");
metadataQ.bindValue(":re", mSearch->text());
SmTreeItem *metadataItem = new SmTreeItem(QVariantList() << tr("Metadata") << QVariant() << QVariant(), root);
root->appendChild(metadataItem);
int ctr = 0;
metadataQ.exec();
while(metadataQ.next()){
++ctr;
appendChild(metadataQ.value(0), metadataQ.value(1), metadataQ.value(2), metadataQ.value(3), metadataItem);
}
if(ctr == 0){
appendEmpty(metadataItem);
}
//search filenames
QSqlQuery filenameQ(db);
filenameQ.prepare("SELECT tfilename, iseriespart_id, series.tseries_name, seriesparts.tsubtitle FROM files, seriesparts, series WHERE tfilename ~* :re AND files.iseriespart_id = seriesparts.iseriesparts_id AND seriesparts.iseries_id = series.iseries_id");
filenameQ.bindValue(":re", mSearch->text());
SmTreeItem *filenameItem = new SmTreeItem(QVariantList() << tr("Filenames") << QVariant() << QVariant(), root);
root->appendChild(filenameItem);
ctr = 0;
filenameQ.exec();
while(filenameQ.next()){
++ctr;
appendChild(filenameQ.value(1), filenameQ.value(0), filenameQ.value(2), filenameQ.value(3), filenameItem);
}
if(ctr == 0){
appendEmpty(filenameItem);
}
mModel->setRoot(root);
mResult->expandAll();
}
void FilenamesAndMetadata::appendChild(QVariant id, QVariant subject, QVariant name, QVariant sub, SmTreeItem *parent){
QString match;
if(!sub.toString().isEmpty()){
match = QString("%1 - %2").arg(name.toString()).arg(sub.toString());
}else{
match = name.toString();
}
QVariantList itemData = QVariantList() << subject << match << id;
SmTreeItem *retval = new SmTreeItem(itemData, parent);
parent->appendChild(retval);
}
void FilenamesAndMetadata::appendEmpty(SmTreeItem *parent){
SmTreeItem *emptyItem = new SmTreeItem(QVariantList() << tr("no match!") << QVariant() << QVariant(), parent);
parent->appendChild(emptyItem);
}
ActorsAndMore::ActorsAndMore(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) {
//search bar
QLabel *typeL = new QLabel(tr("Search by:"));
mTypeSel = new QComboBox;
mTypeSel->addItem(tr("Actor"), Actor);
mTypeSel->addItem(tr("Subtitle"), Title);
mSearch = new QLineEdit;
connect(mSearch, &QLineEdit::returnPressed, this, &ActorsAndMore::doSearch);
QToolBar *searchTB = new QToolBar;
QAction *doSearchA = new QAction(Helper::icon(Qt::transparent, qApp->palette().color(QPalette::Text), QChar(0x2245), true, false), tr("Search"), this);
connect(doSearchA, &QAction::triggered, this, &ActorsAndMore::doSearch);
searchTB->addAction(doSearchA);
QAction *clearSearchA= new QAction(Helper::icon(Qt::transparent, qApp->palette().color(QPalette::Text), QChar(0x2694), true, false), tr("Clear"), this);
connect(clearSearchA, &QAction::triggered, [=] { mSearch->clear(); });
searchTB->addAction(clearSearchA);
QHBoxLayout *topHBL = new QHBoxLayout;
topHBL->addWidget(typeL);
topHBL->addWidget(mSearch);
topHBL->addWidget(mTypeSel);
topHBL->addWidget(searchTB);
// result view
mResultModel = new QStandardItemModel;
QSortFilterProxyModel *resultProxy = new QSortFilterProxyModel;
resultProxy->setSourceModel(mResultModel);
mResultView = new QTreeView;
mResultView->setModel(resultProxy);
connect(mResultView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ActorsAndMore::doData);
mDataModel = new QStandardItemModel;
QSortFilterProxyModel *dataProxy = new QSortFilterProxyModel;
dataProxy->setSourceModel(mDataModel);
mDataView = new QTreeView;
mDataView->setExpandsOnDoubleClick(false);
connect(mDataView, &QTreeView::doubleClicked, this, &ActorsAndMore::dataDoubleClicked);
mDataView->setModel(dataProxy);
QHBoxLayout *resultHBL = new QHBoxLayout;
resultHBL->addWidget(mResultView);
resultHBL->addWidget(mDataView);
//main Layout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topHBL);
mainLayout->addLayout(resultHBL);
setLayout(mainLayout);
}
void ActorsAndMore::doSearch(){
QString input = mSearch->text();
if(input.isEmpty()){
return;
}
int searchType = mTypeSel->currentData().toInt();
if(searchType == Actor){
searchActor(input);
}else if(searchType == Title){
searchSubtitle(input);
}
}
void ActorsAndMore::searchActor(const QString &actor){
mResultView->setSortingEnabled(false);
mResultModel->clear();
mResultModel->setColumnCount(1);
mResultModel->setHeaderData(0, Qt::Horizontal, tr("Actor"));
QIcon icon = SmGlobals::instance()->iconFor("actor");
QStandardItem *root = mResultModel->invisibleRootItem();
QSqlDatabase db = QSqlDatabase::database("treedb");
QSqlQuery actorQ(db);
actorQ.prepare("SELECT tactorname, iactors_id FROM actors WHERE tactorname ~ :name");
actorQ.bindValue(":name", actor);
actorQ.exec();
while(actorQ.next()){
QStandardItem *cur = new QStandardItem(actorQ.value(0).toString());
cur->setIcon(icon);
cur->setData(actorQ.value(1), IdRole);
cur->setEditable(false);
getGenresForActor(cur);
root->appendRow(cur);
}
mResultView->setSortingEnabled(true);
mResultView->sortByColumn(0, Qt::AscendingOrder);
}
void ActorsAndMore::searchSubtitle(const QString &subtitle){
mResultView->setSortingEnabled(false);
mResultModel->clear();
mResultModel->setColumnCount(1);
mResultModel->setHeaderData(0, Qt::Horizontal, tr("Title"));
QIcon icon = SmGlobals::instance()->iconFor("series");
QStandardItem *root = mResultModel->invisibleRootItem();
QSqlDatabase db = QSqlDatabase::database("treedb");
QSqlQuery titleQ(db);
titleQ.prepare("SELECT DISTINCT(series.iseries_id), tseries_name FROM series, seriesparts WHERE seriesparts.tsubtitle ~ :title AND seriesparts.iseries_id = series.iseries_id ORDER BY tseries_name");
titleQ.bindValue(":title", subtitle);
titleQ.exec();
while(titleQ.next()){
QStandardItem *cur = new QStandardItem(titleQ.value(1).toString());
cur->setIcon(icon);
cur->setData(titleQ.value(0), IdRole);
cur->setEditable(false);
root->appendRow(cur);
}
mResultView->setSortingEnabled(true);
mResultView->sortByColumn(0, Qt::AscendingOrder);
}
void ActorsAndMore::getGenresForActor(QStandardItem *actorItem){
QStringList res;
QIcon icon = SmGlobals::instance()->iconFor("genre");
QSqlDatabase db = QSqlDatabase::database("treedb");
QSqlQuery genreQ(db);
genreQ.prepare("SELECT DISTINCT(genres.tgenrename) FROM genres, seriesparts, seriesparts_actormap, seriesparts_genremap WHERE seriesparts_actormap.iseriesparts_id = seriesparts.iseriesparts_id AND seriesparts.iseriesparts_id = seriesparts_genremap.iseriesparts_id AND seriesparts_genremap.igenres_id = genres.igenres_id AND seriesparts_actormap.iactors_id = :id ORDER BY genres.tgenrename");
genreQ.bindValue(":id", actorItem->data(IdRole));
genreQ.exec();
while(genreQ.next()){
res << genreQ.value(0).toString();
}
if(!res.isEmpty()){
for(QString g : res){
QStandardItem *cur = new QStandardItem(g);
cur->setIcon(icon);
cur->setEditable(false);
cur->setData(actorItem->data(IdRole), IdRole);
actorItem->appendRow(cur);
}
}
}
void ActorsAndMore::doData(const QModelIndex &cur, const QModelIndex &prev){
Q_UNUSED(prev)
int searchType = mTypeSel->currentData().toInt();
if(searchType == Actor){
getDataForActor(cur);
}else if(searchType == Title){
getDataForTitle(cur);
}
}
void ActorsAndMore::dataDoubleClicked(const QModelIndex &index){
QModelIndex cur = index;
while(cur.parent().isValid()){
cur = cur.parent();
}
int searchType = mTypeSel->currentData().toInt();
if(searchType == Actor || searchType == Title){
MoviePropertiesDialog propDlg(this);
propDlg.init(cur.data(IdRole).toInt());
propDlg.exec();
}
}
void ActorsAndMore::getDataForActor(QModelIndex cur){
mDataModel->clear();
mDataModel->setColumnCount(1);
mDataModel->setHeaderData(0, Qt::Horizontal, tr("Series"));
QIcon icon = SmGlobals::instance()->iconFor("series");
QVector<int> seriesParts;
QStandardItem *root = mDataModel->invisibleRootItem();
QSqlDatabase db = QSqlDatabase::database("treedb");
QSqlQuery seriesPartIdQ(db);
seriesPartIdQ.prepare("SELECT DISTINCT(seriesparts.iseriesparts_id) FROM seriesparts_actormap, seriesparts WHERE seriesparts_actormap.iactors_id = :id AND seriesparts_actormap.iseriesparts_id = seriesparts.iseriesparts_id");
seriesPartIdQ.bindValue(":id", cur.data(IdRole));
seriesPartIdQ.exec();
while(seriesPartIdQ.next()){
seriesParts << seriesPartIdQ.value(0).toInt();
}
QSqlQuery displayQ(db);
displayQ.prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.iseriesparts_id, seriesparts.tsubtitle FROM series, seriesparts WHERE seriesparts.iseriesparts_id = :id AND seriesparts.iseries_id = series.iseries_id");
for(int part : seriesParts){
displayQ.bindValue(":id", part);
displayQ.exec();
while(displayQ.next()){
int sPart = displayQ.value(1).toInt();
QString curDisp;
if(sPart > 0){
curDisp = QString("%1 %2").arg(displayQ.value(0).toString()).arg(sPart, 3, 10, QChar('0'));
}else{
QString sub = displayQ.value(3).toString();
if(sub.isEmpty()){
curDisp = QString("%1 - <no sub/part>").arg(displayQ.value(0).toString());
}else{
curDisp = QString("%1 - %2").arg(displayQ.value(0).toString()).arg(sub);
}
}
QStandardItem *cur = new QStandardItem(curDisp);
cur->setIcon(icon);
cur->setData(displayQ.value(2), IdRole);
cur->setEditable(false);
QSqlQuery filenameQ(db);
filenameQ.prepare("SELECT tfilename FROM files WHERE iseriespart_id = :fid");
filenameQ.bindValue(":fid", displayQ.value(2));
filenameQ.exec();
while(filenameQ.next()){
QStandardItem *curFile = new QStandardItem(filenameQ.value(0).toString());
curFile->setIcon(icon);
curFile->setEditable(false);
cur->appendRow(curFile);
}
root->appendRow(cur);
}
mDataView->setSortingEnabled(true);
mDataView->sortByColumn(0, Qt::AscendingOrder);
}
}
void ActorsAndMore::getDataForTitle(QModelIndex cur){
mDataModel->clear();
mDataModel->setColumnCount(1);
mDataModel->setHeaderData(0, Qt::Horizontal, tr("Subtitle"));
QIcon icon = SmGlobals::instance()->iconFor("series");
QStandardItem *root = mDataModel->invisibleRootItem();
QSqlDatabase db = QSqlDatabase::database("treedb");
QSqlQuery titleDataQ(db);
titleDataQ.prepare("SELECT tsubtitle, iseriesparts_id FROM seriesparts WHERE iseries_id = :id AND tsubtitle ~ :title ORDER BY tsubtitle");
titleDataQ.bindValue(":id", cur.data(IdRole));
titleDataQ.bindValue(":title", mSearch->text());
titleDataQ.exec();
while(titleDataQ.next()){
QStandardItem *curTitle = new QStandardItem(titleDataQ.value(0).toString());
curTitle->setIcon(icon);
curTitle->setData(titleDataQ.value(1), IdRole);
curTitle->setEditable(false);
root->appendRow(curTitle);
}
mDataView->setSortingEnabled(true);
mDataView->sortByColumn(0, Qt::AscendingOrder);
}
SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags flags) : QDialog(parent, flags) {
QHBoxLayout *gbLayout = new QHBoxLayout;
QGroupBox *metaFnGb = new QGroupBox(tr("Metadata and Filenames"));
FilenamesAndMetadata *metaFnW = new FilenamesAndMetadata;
gbLayout->addWidget(metaFnW);
metaFnGb->setLayout(gbLayout);
QHBoxLayout *gbLayout2 = new QHBoxLayout;
QGroupBox *actorsAndMoreGb = new QGroupBox(tr("Actors and more..."));
ActorsAndMore *actorsAndMoreW = new ActorsAndMore;
gbLayout2->addWidget(actorsAndMoreW);
actorsAndMoreGb->setLayout(gbLayout2);
QSplitter *splitter = new QSplitter(Qt::Vertical);
splitter->addWidget(metaFnGb);
splitter->addWidget(actorsAndMoreGb);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(splitter);
setLayout(mainLayout);
setWindowTitle(tr("Search Dialog"));
readSettings();
}
void SearchDialog::writeSettings(){
QSettings s;
s.setValue("searchdlgpos", pos());
s.setValue("searchdlgsize", size());
}
void SearchDialog::readSettings(){
QSettings s;
move(s.value("searchdlgpos").toPoint());
resize(s.value("searchdlgsize").toSize());
}
|