summaryrefslogtreecommitdiffstats
path: root/searchdialog.cpp
blob: 642f02f6d0985cf95dbf14cc0fba26fb3b22c53c (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
/*
  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 "smtreeview.h"
#include "smtreeitem.h"
#include "smtreemodel.h"
#include "searchdialog.h"
#include "helper.h"

SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags flags) : QDialog(parent, flags){
    // Define GUI items
    setWindowTitle(tr("Search..."));
    QLabel *l1 = new QLabel(tr("Search"));
    mSearch = new QLineEdit;
    connect(mSearch, &QLineEdit::returnPressed, this, &SearchDialog::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, &SearchDialog::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);
    setMinimumSize(QSize(1024, 600));
    connect(this, &SearchDialog::rejected, this, &SearchDialog::writeSettings);
    readSettings();
}

void SearchDialog::show(){
    mSearch->setFocus();
    mSearch->selectAll();
    QDialog::show();
}

void SearchDialog::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 SearchDialog::writeSettings(){
    QHeaderView *h = mResult->header();
    QSettings s;
    s.setValue("searchdlgheaders", h->saveState());
    s.setValue("searchdlgpos", pos());
}

void SearchDialog::readSettings(){
    QSettings s;
    QByteArray headerState = s.value("searchdlgheaders").toByteArray();
    mResult->header()->restoreState(headerState);
    move(s.value("searchdlgpos").toPoint());
}

void SearchDialog::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 SearchDialog::appendEmpty(SmTreeItem *parent){
    SmTreeItem *emptyItem = new SmTreeItem(QVariantList() << tr("no match!") << QVariant() << QVariant(), parent);
    parent->appendChild(emptyItem);
}