summaryrefslogtreecommitdiffstats
path: root/smubermodel.cpp
blob: 9bb8195a1b5b8e92c2d9c8f30551532d5796429b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
  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 <QSqlQuery>
#include <QList>
#include <QVariant>

#include "smubermodel.h"
#include "smtreemodel.h"
#include "smtreeitem.h"
#include "actormodel.h"

SmUberModel::SmUberModel(QObject *parent) : QObject(parent), mSeriesModel(0), mFileModel(0), mActorModel(0){
	//db setup
	mDb = QSqlDatabase::addDatabase("QPSQL", "treedb");
	mDb.setHostName("localhost");
	mDb.setUserName("shemov");
	mDb.setPassword("shemov");
	mDb.setDatabaseName("shemov2");
	mDb.open();
	mSeriesPartsQuery = new QSqlQuery(mDb);
	mSeriesPartsQuery->prepare("SELECT iseriesparts_id, iseriespart, iseries_id, iquality FROM seriesparts WHERE iseries_id = :id ORDER BY iseriespart");

	//series model
	QStringList seriesHeaders = QStringList() << tr("Name") << tr("Quality") << tr("Id");
	mSeriesModel = new SmTreeModel(seriesHeaders, this);
	populateSeriesmodel();
}

SmUberModel::~SmUberModel(){
	delete mSeriesPartsQuery;
}

void SmUberModel::populateSeriesmodel(){
	QSqlQuery seriesQuery("SELECT iseries_id, tseries_name FROM series ORDER BY tseries_name", mDb);
	SmTreeItem *root = new SmTreeItem(3);
	while(seriesQuery.next()){
		QList<QVariant> seriesData;
		seriesData << seriesQuery.value(1) << QVariant() << seriesQuery.value(0);
		SmTreeItem *seriesItem = new SmTreeItem(seriesData, root);
		root->appendChild(seriesItem);
		mSeriesPartsQuery->bindValue(":id", seriesQuery.value(0));
		mSeriesPartsQuery->exec();
		while(mSeriesPartsQuery->next()){
			QList<QVariant> partsData;
			QString title = QString("%1 %2").arg(seriesData.at(0).toString()).arg(mSeriesPartsQuery->value(1).toInt());
			partsData << title << mSeriesPartsQuery->value(3) << mSeriesPartsQuery->value(0);
			seriesItem->appendChild(new SmTreeItem(partsData, seriesItem));
		}
	}
	mSeriesModel->setRoot(root);
}