From ba8b78eb0b7b364f6ecc954f8b99b138dfc13981 Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 1 Jun 2013 05:06:04 +0200 Subject: First draft of new ArchiveModel The new ArchiveModel allows sorting and display by the series name, actors and genres. Hopefully it's generic enough to easily add other sort orders like file location or file type. For now, it's just there. Not included anywhere. No view, no nothing. --- archivemodel.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 archivemodel.cpp (limited to 'archivemodel.cpp') diff --git a/archivemodel.cpp b/archivemodel.cpp new file mode 100644 index 0000000..874b252 --- /dev/null +++ b/archivemodel.cpp @@ -0,0 +1,133 @@ +/* + 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 "smtreeitem.h" +#include "archivemodel.h" + +ArchiveModel::ArchiveModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mOrder(SeriesName){ + mDb = QSqlDatabase::database("treedb"); + mAvailableOrders.insert(SeriesName, "Series Name"); + mAvailableOrders.insert(Actor, "Actor"); + mAvailableOrders.insert(Genre, "Genre"); + setOrder(mOrder); +} + +const QStringList ArchiveModel::availableOrders() const { + QStringList retval = mAvailableOrders.values(); + qSort(retval); + return retval; +} + +void ArchiveModel::setOrder(int order) { + switch (order) { + case SeriesName: + populateBySeriesName(); + break; + case Actor: + populateByActor(); + break; + case Genre: + populateByGenre(); + break; + default: + return; + } +} + +void ArchiveModel::populateBySeriesName() { + SmTreeItem *rootItem = new SmTreeItem(NumFields); + fetchSeries(QVariant(), rootItem); + fetchParts(rootItem); + setRoot(rootItem); +} + +void ArchiveModel::populateByGenre(){ + QSqlQuery genreIdQuery = QSqlQuery("SELECT igenres_id, tgenrename FROM genres ORDER BY tgenrename", mDb); + SmTreeItem *rootItem = new SmTreeItem(NumFields); + while(genreIdQuery.next()){ + QList genreIdData; + genreIdData << genreIdQuery.value(1) << genreIdQuery.value(0) << QVariant() << QVariant() << GenreNode << false << QVariant() << QVariant() << QVariant(); + SmTreeItem *genreIdItem = new SmTreeItem(genreIdData, rootItem); + rootItem->appendChild(genreIdItem); + } + fetchChildren(rootItem); + setRoot(rootItem); +} + +void ArchiveModel::populateByActor(){ + QSqlQuery actorIdQuery = QSqlQuery("SELECT iactors_id, tactorname FROM actors ORDER BY tactorname", mDb); + SmTreeItem *rootItem = new SmTreeItem(NumFields); + while(actorIdQuery.next()){ + QList actorIdData; + actorIdData << actorIdQuery.value(1) << actorIdQuery.value(0) << QVariant() << QVariant() << GenreNode << false << QVariant() << QVariant() << QVariant(); + SmTreeItem *actorIdItem = new SmTreeItem(actorIdData, rootItem); + rootItem->appendChild(actorIdItem); + } + fetchChildren(rootItem); + setRoot(rootItem); +} + +void ArchiveModel::fetchChildren(SmTreeItem *parent){ + for(int i = 0; i < parent->childCount(); ++i){ + fetchSeries(parent->child(i)->data(SeriesId), parent->child(i)); + for(int j = 0; j < parent->child(i)->childCount(); ++j){ + SmTreeItem *seriesItem = parent->child(i)->child(j); + fetchParts(seriesItem); + } + } +} + +void ArchiveModel::fetchSeries(const QVariant &id, SmTreeItem *parent){ + QSqlQuery seriesIdQuery(mDb); + switch (mOrder) { + case SeriesName: + seriesIdQuery.prepare("SELECT iseries_id, tseries_name FROM series ORDER BY tseries_name"); + break; + case Genre: + seriesIdQuery.prepare("SELECT distinct(series.iseries_id), tseries_name FROM series, seriesparts, seriesparts_genremap WHERE seriesparts_genremap.igenres_id = :genreid AND seriesparts_genremap.iseriesparts_id = seriesparts.iseriesparts_id AND seriesparts.iseries_id = series.iseries_id ORDER BY tseries_name"); + seriesIdQuery.bindValue(":genreid", id); + break; + case Actor: + seriesIdQuery.prepare("SELECT distinct(series.iseries_id), tseries_name FROM series, seriesparts, seriesparts_actormap WHERE seriesparts_actormap.iactors_id = :actorid AND seriesparts_actormap.iseriesparts_id = seriesparts.iseriesparts_id AND seriesparts.iseries_id = series.iseries_id ORDER BY tseries_name"); + seriesIdQuery.bindValue(":actorid", id); + break; + default: + qFatal("ArchiveTreeModel::fetchSeries: invalid order!"); + return; + } + if(!seriesIdQuery.exec()){ + qWarning("ArchiveTreeModel::fetchSeries: failed to exectue query!"); + return; + } + while(seriesIdQuery.next()){ + QList seriesData; + seriesData << seriesIdQuery.value(1) << seriesIdQuery.value(0) << QVariant() << QVariant() << SeriesNode << false << QVariant() << QVariant() << QVariant(); + SmTreeItem *seriesItem = new SmTreeItem(seriesData, parent); + parent->appendChild(seriesItem); + } + parent->setData(Count, parent->childCount()); +} + + +void ArchiveModel::fetchParts(SmTreeItem *parent){ + QSqlQuery partsQuery(mDb); + partsQuery.prepare("SELECT iseriesparts_id, iseriespart, bfavorite, tsubtitle FROM seriesparts WHERE iseries_id = :id ORDER BY iseriespart"); + for(int i = 0; i < parent->childCount(); ++i){ + QVariant id = parent->child(i)->data(SeriesId); + partsQuery.bindValue(":id", id); + partsQuery.exec(); + while(partsQuery.next()){ + QList partData; + partData << parent->data(Name) << parent->data(SeriesId) << partsQuery.value(0) << partsQuery.value(1) << SeriesPartNode << partsQuery.value(2) << partsQuery.value(3) << QVariant(); + SmTreeItem *partItem = new SmTreeItem(partData, parent->child(i)); + parent->child(i)->appendChild(partItem); + } + parent->child(i)->setData(Count, parent->child(i)->childCount()); + } +} -- cgit v1.2.3-70-g09d2