/* 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 "dbanalyzer.h" #include "smtreemodel.h" #include "smtreeitem.h" DbAnalyzerDialog::DbAnalyzerDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){ //create tab widget mTab = new QTabWidget; //setup analyzer mAnalyzer = new DbAnalyzer(this); // no actors QWidget *noActorsT = new QWidget; QStringList noActorsHeaders = QStringList() << tr("Series") << tr("Part/Subtitle") << tr("Seriespart Id") << tr("Series Id") << tr("Seriespart"); mNoActorsV = new QTreeView; mNoActorsM = new SmTreeModel(noActorsHeaders, this); mNoActorsV->setModel(mNoActorsM); QVBoxLayout *noActorsL = new QVBoxLayout; noActorsL->addWidget(mNoActorsV); mNoActorsV->setColumnHidden(2, true); mNoActorsV->setColumnHidden(3, true); mNoActorsV->setColumnHidden(4, true); mNoActorsV->setEditTriggers(QTreeView::NoEditTriggers); mNoActorsV->setSelectionBehavior(QAbstractItemView::SelectRows); mNoActorsV->setSelectionMode(QAbstractItemView::SingleSelection); mNoActorsV->setAlternatingRowColors(true); connect(mNoActorsV, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(noActorsDoubleClicked(QModelIndex))); noActorsT->setLayout(noActorsL); //buttons mCancel = new QPushButton(tr("Cancel")); connect(mCancel, SIGNAL(clicked()), this, SLOT(cancelAnalyzer())); mClose = new QPushButton(tr("Close")); connect(mClose, SIGNAL(clicked()), this, SLOT(accept())); //setup dialog mTab->addTab(noActorsT, tr("No Actors")); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(mTab); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(); buttonLayout->addWidget(mCancel); buttonLayout->addWidget(mClose); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); setMinimumWidth(500); //get things going connect(mAnalyzer, SIGNAL(started()), this, SLOT(analyzerStarted())); connect(mAnalyzer, SIGNAL(finished()), this, SLOT(analyzerFinished())); mAnalyzer->start(); } void DbAnalyzerDialog::cancelAnalyzer(){ mAnalyzer->setCancel(true); } void DbAnalyzerDialog::analyzerStarted(){ mCancel->setEnabled(true); mClose->setEnabled(false); } void DbAnalyzerDialog::analyzerFinished(){ mCancel->setEnabled(false); mClose->setEnabled(true); populateNoActors(); } void DbAnalyzerDialog::noActorsDoubleClicked(const QModelIndex &idx){ if(!idx.isValid()){ return; } QModelIndex seriesPartIdx = mNoActorsM->index(idx.row(), 5, idx.parent()); QModelIndex seriesIdx = mNoActorsM->index(idx.row(), 4, idx.parent()); emit partClicked(seriesPartIdx.data().toInt(), seriesIdx.data().toInt()); } void DbAnalyzerDialog::populateNoActors(){ const int columns = 4; QList > noActors = mAnalyzer->noActors(); SmTreeItem *root = new SmTreeItem(columns); foreach(QList l, noActors){ SmTreeItem *child = new SmTreeItem(l, root); root->appendChild(child); } mNoActorsM->setRoot(root); mNoActorsV->resizeColumnToContents(0); } DbAnalyzer::DbAnalyzer(QObject *parent) : QThread(parent), mCanceled(false), mStatus(Fail) { mDb = QSqlDatabase::cloneDatabase(QSqlDatabase::database("treedb"), "analyzerDB"); mDb.open(); mStatus = mDb.isOpen() ? Ok : Fail; mNoActorQuery = new QSqlQuery(mDb); mNoActorQuery->prepare("SELECT series.tseries_name, seriesparts.iseriespart, seriesparts.tsubtitle, series.iseries_id, seriesparts.iseriesparts_id FROM series, seriesparts LEFT JOIN seriesparts_actormap ON seriesparts.iseriesparts_id = seriesparts_actormap.iseriesparts_id WHERE iactors_id IS NULL AND seriesparts.iseries_id = series.iseries_id ORDER BY tseries_name"); } DbAnalyzer::~DbAnalyzer(){ delete mNoActorQuery; mDb.close(); QSqlDatabase::removeDatabase("analyzerDb"); } void DbAnalyzer::setCancel(bool canceled){ QMutexLocker m(&mCancelMutex); mCanceled = canceled; } void DbAnalyzer::run(){ noActorsCheck(); } void DbAnalyzer::noActorsCheck(){ if(!mNoActorQuery->exec()){ mStatus = Fail; return; } emit message(tr("Fetching movies without actors")); while(mNoActorQuery->next()){ if(mCanceled){ break; } QList res; res << mNoActorQuery->value(0); if(!mNoActorQuery->value(2).isNull()){ res << mNoActorQuery->value(2); }else{ res << mNoActorQuery->value(1); } res << mNoActorQuery->value(1) << mNoActorQuery->value(2) << mNoActorQuery->value(3) << mNoActorQuery->value(4); mNoActorR << res; } emit message(tr("Done!")); }