From fb0626d4c624bdb640628b9c2dad93d62d90885c Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 24 Jan 2015 21:35:12 +0100 Subject: New: search dialog --- searchdialog.cpp | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 searchdialog.cpp (limited to 'searchdialog.cpp') diff --git a/searchdialog.cpp b/searchdialog.cpp new file mode 100644 index 0000000..a55a36a --- /dev/null +++ b/searchdialog.cpp @@ -0,0 +1,159 @@ +/* + 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "smtreeview.h" +#include "smtreeitem.h" +#include "smtreemodel.h" +#include "searchdialog.h" + +SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags flags) : QDialog(parent, flags){ + // Define GUI items + setWindowTitle(tr("Search for")); + mExFilenames = new QCheckBox(tr("Filenames")); + mExFilenames->setChecked(true); + mExMeta = new QCheckBox(tr("Metadata")); + mExMeta->setChecked(true); + mSearch = new QLineEdit; + mResult = new SmTreeView; + mDoSearch = new QPushButton(tr("Search")); + mClose = new QPushButton(tr("Close")); + + // init Model and View + const QStringList headers = QStringList() << tr("Match") << tr("Series") << tr("SeriesPartId"); + mModel = new SmTreeModel(headers, this); + mProxy = new QSortFilterProxyModel(this); + mProxy->setSourceModel(mModel); + mResult->setModel(mProxy); + mResult->setColumnHidden(2, true); + mResult->setSortingEnabled(true); + mResult->setEditTriggers(QAbstractItemView::NoEditTriggers); + connect(mResult, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(searchDoubleclicked())); + + // Layout + QVBoxLayout *mainLayout = new QVBoxLayout; + QGroupBox *searchGb = new QGroupBox(tr("Search for:")); + QVBoxLayout *searchGbL = new QVBoxLayout; + searchGbL->addWidget(mSearch); + searchGb->setLayout(searchGbL); + mainLayout->addWidget(searchGb); + QGroupBox *excludeGb = new QGroupBox(tr("Include:")); + QHBoxLayout *excludeGbL = new QHBoxLayout; + excludeGbL->addWidget(mExFilenames); + excludeGbL->addStretch(); + excludeGbL->addWidget(mExMeta); + excludeGb->setLayout(excludeGbL); + mainLayout->addWidget(excludeGb); + QGroupBox *resultGb = new QGroupBox(tr("Search result:")); + QHBoxLayout *resultGbL = new QHBoxLayout; + resultGbL->addWidget(mResult); + resultGb->setLayout(resultGbL); + mainLayout->addWidget(resultGb); + QHBoxLayout *buttonLayout = new QHBoxLayout; + mDoSearch = new QPushButton(tr("Search")); + mClose = new QPushButton(tr("Close")); + buttonLayout->addStretch(); + buttonLayout->addWidget(mDoSearch); + buttonLayout->addWidget(mClose); + mainLayout->addLayout(buttonLayout); + setLayout(mainLayout); + setMinimumWidth(800); + + // connect the dots... + connect(mClose, SIGNAL(clicked()), this, SLOT(hide())); + connect(mDoSearch, SIGNAL(clicked()), this, SLOT(search())); + connect(mExFilenames, SIGNAL(clicked()), this, SLOT(disableSearch())); + connect(mExMeta, SIGNAL(clicked()), this, SLOT(disableSearch())); + +} + +void SearchDialog::search(){ + if(mSearch->text().isEmpty()){ + return; + } + QSqlDatabase db = QSqlDatabase::database("treedb"); + SmTreeItem *root = new SmTreeItem(3, 0); + if(mExMeta->isChecked()){ + 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); + } + } + + if(mExFilenames->isChecked()){ + 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); + int 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::disableSearch(){ + bool exFnChecked = mExFilenames->isChecked(); + bool exMetaChecked = mExMeta->isChecked(); + if(!exFnChecked && !exMetaChecked){ + mDoSearch->setEnabled(false); + }else{ + mDoSearch->setEnabled(true); + } +} + +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); +} + +void SearchDialog::searchDoubleclicked(){ + QPersistentModelIndex pIdx = mResult->currentIndex(); + QModelIndex idx = mProxy->mapToSource(pIdx); + SmTreeItem *curItem = static_cast(idx.internalPointer()); + emit searchResultClicked(curItem->data(2).toInt()); +} -- cgit v1.2.3-70-g09d2