summaryrefslogtreecommitdiffstats
path: root/searchdialog.cpp
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2015-01-24 21:35:12 +0100
committerArno <am@disconnect.de>2015-01-24 21:35:12 +0100
commitfb0626d4c624bdb640628b9c2dad93d62d90885c (patch)
tree008e7aa0d6791e73503b2468a89c9bedc47610b2 /searchdialog.cpp
parenta152afc1005c72e16208f15d84a6c2b24d32f578 (diff)
downloadSheMov-fb0626d4c624bdb640628b9c2dad93d62d90885c.tar.gz
SheMov-fb0626d4c624bdb640628b9c2dad93d62d90885c.tar.bz2
SheMov-fb0626d4c624bdb640628b9c2dad93d62d90885c.zip
New: search dialog
Diffstat (limited to 'searchdialog.cpp')
-rw-r--r--searchdialog.cpp159
1 files changed, 159 insertions, 0 deletions
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 <QLabel>
+#include <QCheckBox>
+#include <QLineEdit>
+#include <QPushButton>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
+#include <QGroupBox>
+#include <QSortFilterProxyModel>
+#include <QSqlDatabase>
+#include <QSqlQuery>
+
+#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<SmTreeItem*>(idx.internalPointer());
+ emit searchResultClicked(curItem->data(2).toInt());
+}