summaryrefslogtreecommitdiffstats
path: root/searchdialog.cpp
blob: 8c255c810ad9b88fa9c1156c0d21d4513a64091e (plain)
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
#include <QLabel>
#include <QComboBox>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTreeView>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSettings>
#include <QKeyEvent>
#include <QApplication>

#include "searchdialog.h"
#include "searchview.h"
#include "helper.h"

SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
    //search bar
    QLabel *typeL = new QLabel(tr("Search by:"));
    mTypeSel = new QComboBox;
    mTypeSel->addItem(tr("Title"), Title);
    mTypeSel->addItem(tr("Filename"), Filename);
    mTypeSel->addItem(tr("Actor"), Actors);
    connect(mTypeSel, QOverload<int>::of(&QComboBox::activated), this, &SearchDialog::doSearch);
    mSearch = new QLineEdit;
    QPushButton *goB = new QPushButton(tr("Go!"));
    connect(goB, &QPushButton::clicked, this, &SearchDialog::doSearch);
    QHBoxLayout *topHBL = new QHBoxLayout;
    topHBL->addWidget(typeL);
    topHBL->addWidget(mTypeSel);
    topHBL->addWidget(mSearch);
    topHBL->addWidget(goB);

    //result view
    QGroupBox *resGB = new QGroupBox(tr("Search result"));
    mResM = new QStandardItemModel;
    QSortFilterProxyModel *resMProxy = new QSortFilterProxyModel;
    resMProxy->setSourceModel(mResM);
    mResV = new SearchView;
    mResV->setModel(resMProxy);
    connect(mResV->selectionModel(), &QItemSelectionModel::currentChanged, this, &SearchDialog::doResult);
    QHBoxLayout *resGBL = new QHBoxLayout;
    resGBL->addWidget(mResV);
    resGB->setLayout(resGBL);
    QGroupBox *dataGB = new QGroupBox(tr("Data"));
    mDataM = new QStandardItemModel;
    QSortFilterProxyModel *dataMProxy = new QSortFilterProxyModel;
    dataMProxy->setSourceModel(mDataM);
    mDataV = new SearchView;
    mDataV->setModel(dataMProxy);
    QHBoxLayout *dataGBL = new QHBoxLayout;
    dataGBL->addWidget(mDataV);
    dataGB->setLayout(dataGBL);
    QHBoxLayout *resL = new QHBoxLayout;
    resL->addWidget(resGB);
    resL->addWidget(dataGB);

    //hide button
    QPushButton *hideB = new QPushButton(tr("Close"));
    connect(hideB, &QPushButton::clicked, this, &SearchDialog::hide);
    QHBoxLayout *buttonL = new QHBoxLayout;
    buttonL->addStretch();
    buttonL->addWidget(hideB);
    buttonL->addStretch();

    //main layout
    QVBoxLayout *mainL = new QVBoxLayout;
    mainL->addLayout(topHBL);
    mainL->addLayout(resL);
    mainL->addLayout(buttonL);
    setLayout(mainL);
    setMinimumSize(QSize(1024, 468));
    readSettings();
    doSearch();
}

SearchDialog::~SearchDialog(){
    writeSettings();
}

void SearchDialog::doSearch(){
    int type = mTypeSel->currentData().toInt();
    qApp->setOverrideCursor(Qt::BusyCursor);
    if(type == Title){
        doSearchTitle();
    }else if(type == Filename){
        doSearchFilename();
    }else if(type == Actors){
        doSearchActor();
    }
    qApp->restoreOverrideCursor();
}

void SearchDialog::doResult(const QModelIndex &cur, const QModelIndex &prev){
    Q_UNUSED(prev)
    int type = mTypeSel->currentData().toInt();
    qApp->setOverrideCursor(Qt::BusyCursor);
    if(type == Title || type == Filename){
        doResultName(cur, type);
    }else if(type == Actors){
        doResultActor(cur);
    }
    qApp->restoreOverrideCursor();
}

void SearchDialog::doSearchTitle(){
    if(mSearch->text().isEmpty()){
        return;
    }
    mResV->setSortingEnabled(false);
    mResM->clear();
    mResM->setColumnCount(1);
    mResM->setHeaderData(0, Qt::Horizontal, tr("Title"));
    QIcon icon = Helper::icon(QColor(255,85,255), QChar(0x26A7));
    QStandardItem *root = mResM->invisibleRootItem();
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery tQ(db);
    tQ.prepare("SELECT DISTINCT(iseries_id), tseries_name FROM series WHERE tseries_name ~ :title ORDER BY tseries_name");
    tQ.bindValue(":title", mSearch->text());
    tQ.exec();
    while(tQ.next()){
        QStandardItem *cur = new QStandardItem(tQ.value(1).toString());
        cur->setIcon(icon);
        cur->setData(tQ.value(0), IdRole);
        cur->setEditable(false);
        root->appendRow(cur);
    }
    mResV->setSortingEnabled(true);
}

void SearchDialog::doSearchFilename(){
    if(mSearch->text().isEmpty()){
        return;
    }
    mResV->setSortingEnabled(false);
    mResM->clear();
    mResM->setColumnCount(1);
    mResM->setHeaderData(0, Qt::Horizontal, tr("Filename"));
    QIcon fnIcon = Helper::icon(QColor(255,85,255), QChar(0x26A6));
    QIcon oIcon = Helper::icon(QColor(255,85,255), QChar(0x26A5));
    QStandardItem *root = mResM->invisibleRootItem();
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery fnQ(db);
    fnQ.prepare("SELECT tfilename, iseriespart_id FROM files WHERE tfilename ~ :fn ORDER BY tfilename");
    fnQ.bindValue(":fn", mSearch->text());
    fnQ.exec();
    while(fnQ.next()){
        QStandardItem *cur = new QStandardItem(fnQ.value(0).toString());
        cur->setIcon(fnIcon);
        cur->setData(fnQ.value(1), IdRole);
        cur->setEditable(false);
        root->appendRow(cur);
    }
    QSqlQuery oQ(db);
    oQ.prepare("SELECT tname, iseriespart_id FROM files, files_origin WHERE tname ~ :fn AND files_origin.ifiles_id = files.ifiles_id ORDER BY tname");
    oQ.bindValue(":fn", mSearch->text());
    oQ.exec();
    while(oQ.next()){
        QStandardItem *cur = new QStandardItem(oQ.value(0).toString());
        cur->setIcon(oIcon);
        cur->setData(oQ.value(1), IdRole);
        cur->setEditable(false);
        root->appendRow(cur);
    }
    mResV->setSortingEnabled(true);
    mResV->sortByColumn(0);
}

void SearchDialog::doSearchActor(){
    if(mSearch->text().isEmpty()){
        return;
    }
    mResV->setSortingEnabled(false);
    mResM->clear();
    mResM->setColumnCount(1);
    mResM->setHeaderData(0, Qt::Horizontal, tr("Title"));
    QIcon icon = Helper::icon(QColor(255,85,255), QChar(0x26A8));
    QStandardItem *root = mResM->invisibleRootItem();
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery aQ(db);
    aQ.prepare("SELECT tactorname, iactors_id FROM actors WHERE tactorname ~ :name ORDER BY tactorname");
    aQ.bindValue(":name", mSearch->text());
    aQ.exec();
    while(aQ.next()){
        QStandardItem *cur = new QStandardItem(aQ.value(0).toString());
        cur->setIcon(icon);
        cur->setData(aQ.value(1), IdRole);
        cur->setEditable(false);
        doActorGenres(cur);
        root->appendRow(cur);
    }
    mResV->setSortingEnabled(true);
    mResV->sortByColumn(0);
}

void SearchDialog::doResultName(const QModelIndex &sel, int resType){
    int seriesId = sel.data(IdRole).toInt();
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery rQ(db);
    mDataM->clear();
    mDataM->setColumnCount(1);
    QIcon icon = Helper::icon(QColor(255,85,255), QChar(0x26A5));
    QStandardItem *root = mDataM->invisibleRootItem();
    if(resType == Title){
        rQ.prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.iseriesparts_id, seriesparts.tsubtitle FROM series, seriesparts WHERE series.iseries_id = :id AND seriesparts.iseries_id = series.iseries_id");
        mDataM->setHeaderData(0, Qt::Horizontal, tr("Parts"));
    }else if(resType == Filename){
        rQ.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");
        mDataM->setHeaderData(0, Qt::Horizontal, tr("Series"));
    }
    rQ.bindValue(":id", seriesId);
    rQ.exec();
    while(rQ.next()){
        int sPart = rQ.value(1).toInt();
        QString curDisp;
        if(sPart > 0){
            curDisp = QString("%1 %2").arg(rQ.value(0).toString()).arg(sPart, 3, 10, QChar('0'));
        }else{
            QString sub = rQ.value(3).toString();
            if(sub.isEmpty()){
                curDisp = QString("%1 - <no sub/part>").arg(rQ.value(0).toString());
            }else{
                curDisp = QString("%1 - %2").arg(rQ.value(0).toString()).arg(sub);
            }
        }
        QStandardItem *cur = new QStandardItem(curDisp);
        cur->setIcon(icon);
        cur->setData(rQ.value(2), IdRole);
        cur->setEditable(false);
        doChild(cur, Actor);
        doChild(cur, Genre);
        root->appendRow(cur);
    }
    mDataV->setSortingEnabled(true);
    mDataV->sortByColumn(0);
}

void SearchDialog::doResultActor(const QModelIndex &sel){
    mDataM->clear();
    mDataM->setColumnCount(1);
    mDataM->setHeaderData(0, Qt::Horizontal, tr("Series"));
    QIcon icon = Helper::icon(QColor(255,85,255), QChar(0x26A8));
    QVector<int> seriesParts;
    QStandardItem *root = mDataM->invisibleRootItem();
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery r1Q(db);
    r1Q.prepare("SELECT DISTINCT(seriesparts.iseriesparts_id) FROM seriesparts_actormap, seriesparts WHERE seriesparts_actormap.iactors_id = :id AND seriesparts_actormap.iseriesparts_id = seriesparts.iseriesparts_id");
    r1Q.bindValue(":id", sel.data(IdRole));
    r1Q.exec();
    while(r1Q.next()){
        seriesParts << r1Q.value(0).toInt();
    }
    QSqlQuery r2Q(db);
    r2Q.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 sp : seriesParts){
        r2Q.bindValue(":id", sp);
        r2Q.exec();
        while(r2Q.next()){
            int sPart = r2Q.value(1).toInt();
            QString curDisp;
            if(sPart > 0){
                curDisp = QString("%1 %2").arg(r2Q.value(0).toString()).arg(sPart, 3, 10, QChar('0'));
            }else{
                QString sub = r2Q.value(3).toString();
                if(sub.isEmpty()){
                    curDisp = QString("%1 - <no sub/part>").arg(r2Q.value(0).toString());
                }else{
                    curDisp = QString("%1 - %2").arg(r2Q.value(0).toString()).arg(sub);
                }
            }
            QStandardItem *cur = new QStandardItem(curDisp);
            cur->setIcon(icon);
            cur->setData(r2Q.value(2), IdRole);
            cur->setEditable(false);
            root->appendRow(cur);
        }
        mDataV->setSortingEnabled(true);
        mDataV->sortByColumn(0);
    }
}

void SearchDialog::writeSettings(){
    QSettings s;
    s.setValue("searchby", mTypeSel->currentText());
    s.setValue("searchvalue", mSearch->text());
}

void SearchDialog::readSettings(){
    QSettings s;
    QString val = s.value("searchvalue").toString();
    mSearch->setText(val);
    QString type = s.value("searchby").toString();
    int tIdx = mTypeSel->findText(type);
    if(tIdx > -1){
        mTypeSel->setCurrentIndex(tIdx);
    }
}

void SearchDialog::keyPressEvent(QKeyEvent *e){
    if(e->key() == Qt::Key_X && (e->modifiers() & Qt::ControlModifier)){
        hide();
        goto exit;
    }
    return QDialog::keyPressEvent(e);

    exit:
    e->accept();
}

void SearchDialog::doChild(QStandardItem *item, int childMode){
    QStringList res;
    QIcon aIcon;
    QString parentName;
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery rQ(db);
    if(childMode == Actor){
        rQ.prepare("SELECT actors.tactorname FROM seriesparts_actormap, actors WHERE seriesparts_actormap.iseriesparts_id = :id and seriesparts_actormap.iactors_id = actors.iactors_id ORDER by tactorname");
        parentName = tr("actors");
        aIcon = Helper::icon(QColor(255,85,255), 'A');
    }else if(childMode == Genre){
        rQ.prepare("SELECT genres.tgenrename FROM seriesparts_genremap, genres WHERE seriesparts_genremap.iseriesparts_id = :id and seriesparts_genremap.igenres_id = genres.igenres_id ORDER by tgenrename");
        parentName = tr("genres");
        aIcon = Helper::icon(QColor(255,85,255), 'G');
    }
    rQ.bindValue(":id", item->data(IdRole));
    rQ.exec();
    while(rQ.next()){
        res << rQ.value(0).toString();
    }
    if(!res.isEmpty()){
        QStandardItem *aItem = new QStandardItem(parentName);
        aItem->setEditable(false);
        aItem->setIcon(aIcon);
        for(QString a : res){
            QStandardItem *curItem = new QStandardItem(a);
            curItem->setIcon(aIcon);
            curItem->setEditable(false);
            aItem->appendRow(curItem);
        }
        item->appendRow(aItem);
    }
}

void SearchDialog::doActorGenres(QStandardItem *item){
    QStringList res;
    QIcon aIcon = Helper::icon(QColor(255,85,255), QChar(0x26A6));
    QSqlDatabase db = QSqlDatabase::database("shemovdb");
    QSqlQuery aQ(db);
    aQ.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");
    aQ.bindValue(":id", item->data(IdRole));
    aQ.exec();
    while(aQ.next()){
        res << aQ.value(0).toString();
    }
    if(!res.isEmpty()){
        std::sort(res.begin(), res.end());
        for(QString s : res){
            QStandardItem *cur = new QStandardItem(s);
            cur->setIcon(aIcon);
            cur->setEditable(false);
            cur->setData(item->data(IdRole), IdRole);
            item->appendRow(cur);
        }
    }
}