/* 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 #include #include #include #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); }