#include #include #include #include #include #include #include #include #include #include #include #include #include "searchdialog.h" #include "helper.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::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::doResult); 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(); }else if(type == Filename){ doSearchFilename(); } } void SearchDialog::doResult(const QModelIndex &cur, const QModelIndex &prev){ Q_UNUSED(prev) int type = mTypeSel->currentData().toInt(); if(type == Title || type == Filename){ doResultName(cur, type); } } 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), IdRole); cur->setEditable(false); root->appendRow(cur); } mResV->setSortingEnabled(true); } void SearchDialog::doSearchFilename(){ if(mSearch->text().isEmpty()){ return; } mResV->setSortingEnabled(false); mResM->clear(); mResM->setColumnCount(1); mResM->setHeaderData(0, Qt::Horizontal, tr("Filename")); QStandardItem *root = mResM->invisibleRootItem(); QSqlDatabase db = QSqlDatabase::database("shemovdb"); QSqlQuery fnQ(db); fnQ.prepare("SELECT tfilename, iseriespart_id FROM files WHERE tfilename ~ :fn ORDER BY tfilename"); fnQ.bindValue(":fn", mSearch->text()); fnQ.exec(); while(fnQ.next()){ QStandardItem *cur = new QStandardItem(fnQ.value(0).toString()); cur->setIcon(QIcon(":/gaping_ass.png")); cur->setData(fnQ.value(1), IdRole); cur->setEditable(false); root->appendRow(cur); } QSqlQuery oQ(db); oQ.prepare("SELECT tname, iseriespart_id FROM files, files_origin WHERE tname ~ :fn AND files_origin.ifiles_id = files.ifiles_id ORDER BY tname"); oQ.bindValue(":fn", mSearch->text()); oQ.exec(); while(oQ.next()){ QStandardItem *cur = new QStandardItem(oQ.value(0).toString()); cur->setIcon(QIcon(":/french_maid_dress.png")); cur->setData(oQ.value(1), IdRole); cur->setEditable(false); root->appendRow(cur); } mResV->setSortingEnabled(true); mResV->sortByColumn(0); } void SearchDialog::doResultName(const QModelIndex &sel, int resType){ int seriesId = sel.data(IdRole).toInt(); QSqlDatabase db = QSqlDatabase::database("shemovdb"); QSqlQuery rQ(db); mDataM->clear(); mDataM->setColumnCount(1); QStandardItem *root = mDataM->invisibleRootItem(); if(resType == Title){ rQ.prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.iseriesparts_id, seriesparts.tsubtitle FROM series, seriesparts WHERE series.iseries_id = :id AND seriesparts.iseries_id = series.iseries_id"); mDataM->setHeaderData(0, Qt::Horizontal, tr("Parts")); }else if(resType == Filename){ rQ.prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.iseriesparts_id, seriesparts.tsubtitle FROM series, seriesparts WHERE seriesparts.iseriesparts_id = :id AND seriesparts.iseries_id = series.iseries_id"); mDataM->setHeaderData(0, Qt::Horizontal, tr("Series")); } rQ.bindValue(":id", seriesId); rQ.exec(); while(rQ.next()){ int sPart = rQ.value(1).toInt(); QString curDisp; if(sPart > 0){ curDisp = QString("%1 %2").arg(rQ.value(0).toString()).arg(sPart, 3, 10, QChar('0')); }else{ QString sub = rQ.value(3).toString(); if(sub.isEmpty()){ curDisp = QString("%1 - ").arg(rQ.value(0).toString()); }else{ curDisp = QString("%1 - %2").arg(rQ.value(0).toString()).arg(sub); } } QStandardItem *cur = new QStandardItem(curDisp); cur->setIcon(QIcon(":/butt_plug.png")); cur->setData(rQ.value(2), IdRole); cur->setEditable(false); doActors(cur); root->appendRow(cur); } mDataV->setSortingEnabled(true); mResV->sortByColumn(0); } void SearchDialog::doActors(QStandardItem *item){ QStringList actors; QIcon aIcon = Helper::icon(Qt::red, 'A'); QSqlDatabase db = QSqlDatabase::database("shemovdb"); QSqlQuery aQ(db); aQ.prepare("SELECT actors.tactorname FROM seriesparts_actormap, actors WHERE seriesparts_actormap.iseriesparts_id = :id and seriesparts_actormap.iactors_id = actors.iactors_id ORDER by tactorname"); aQ.bindValue(":id", item->data(IdRole)); aQ.exec(); while(aQ.next()){ actors << aQ.value(0).toString(); } if(!actors.isEmpty()){ QStandardItem *aItem = new QStandardItem(tr("actors")); aItem->setEditable(false); aItem->setIcon(aIcon); for(QString a : actors){ QStandardItem *curItem = new QStandardItem(a); aItem->appendRow(curItem); curItem->setEditable(false); } item->appendRow(aItem); } }