diff options
author | Arno <arno@disconnect.de> | 2018-02-01 16:37:44 +0100 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2018-02-01 16:37:44 +0100 |
commit | 2a14c0ebca1d781b7279e1ed8d657364d70df46d (patch) | |
tree | dfcacee9e23be3e3c64b9ac516dedc7b79b80156 /searchdialog.cpp | |
parent | 4847284500c9b9792bd1ab8c2913d6f6c2307152 (diff) | |
download | ShemovCleaner-2a14c0ebca1d781b7279e1ed8d657364d70df46d.tar.gz ShemovCleaner-2a14c0ebca1d781b7279e1ed8d657364d70df46d.tar.bz2 ShemovCleaner-2a14c0ebca1d781b7279e1ed8d657364d70df46d.zip |
Implement a useful search
Display Title and Filename search as non-modal dialog, so one can
compare Files and Database. For now only Title search is implemented.
Filename search does nothing (yet).
Diffstat (limited to 'searchdialog.cpp')
-rw-r--r-- | searchdialog.cpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/searchdialog.cpp b/searchdialog.cpp new file mode 100644 index 0000000..dfefd79 --- /dev/null +++ b/searchdialog.cpp @@ -0,0 +1,144 @@ +#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 "searchdialog.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); + 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 QTreeView; + mResV->setModel(resMProxy); + connect(mResV->selectionModel(), &QItemSelectionModel::currentChanged, this, &SearchDialog::doResultTitle); + 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 QTreeView; + 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)); +} + +void SearchDialog::doSearch(){ + int type = mTypeSel->currentData().toInt(); + if(type == Title){ + doSearchTitle(); + } +} + +void SearchDialog::doResult(const QModelIndex &cur, const QModelIndex &prev){ + Q_UNUSED(prev) + int type = mTypeSel->currentData().toInt(); + if(type == Title){ + doResultTitle(cur); + } +} + +void SearchDialog::doSearchTitle(){ + if(mSearch->text().isEmpty()){ + return; + } + mResV->setSortingEnabled(false); + mResM->clear(); + mResM->setColumnCount(1); + mResM->setHeaderData(0, Qt::Horizontal, tr("Title")); + 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(QIcon(":/huge_bra.png")); + cur->setData(tQ.value(0), SeriesIdRole); + cur->setEditable(false); + root->appendRow(cur); + } + mResV->setSortingEnabled(true); +} + +void SearchDialog::doResultTitle(const QModelIndex &sel){ + int seriesId = sel.data(SeriesIdRole).toInt(); + QSqlDatabase db = QSqlDatabase::database("shemovdb"); + QSqlQuery rQ(db); + QStringList items; + rQ.prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.tsubtitle FROM series, seriesparts WHERE series.iseries_id = :sid AND seriesparts.iseries_id = series.iseries_id"); + rQ.bindValue(":sid", seriesId); + rQ.exec(); + while(rQ.next()){ + int sPart = rQ.value(1).toInt(); + QString sub = rQ.value(2).toString(); + QString cur; + if(sPart > 0){ + cur = QString("%1 - %2").arg(rQ.value(0).toString()).arg(sPart, 3, 10, QChar('0')); + }else{ + cur = QString("%1 - %2").arg(rQ.value(0).toString()).arg(sub); + } + items << cur; + } + std::sort(items.begin(), items.end()); + mDataV->setSortingEnabled(false); + mDataM->clear(); + mDataM->setColumnCount(1); + mDataM->setHeaderData(0, Qt::Horizontal, tr("Parts")); + QStandardItem *root = mDataM->invisibleRootItem(); + for(QString i : items){ + QStandardItem *cur = new QStandardItem(i); + cur->setIcon(QIcon(":/butt_plug.png")); + cur->setEditable(false); + root->appendRow(cur); + } + mDataV->setSortingEnabled(true); +} |