summaryrefslogtreecommitdiffstats
path: root/mappingtablemodel.h
blob: 8f3793bb5b8a51d3987c0073aa36f22efa3a7128 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
  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.
*/

#ifndef MAPPINGTABLEMODEL_H
#define MAPPINGTABLEMODEL_H

#include <QSqlDatabase>
#include <QList>

#include "smtreemodel.h"

class QString;
class QSqlQuery;
class QStringList;

class MappingTableModel : public SmTreeModel{
	Q_OBJECT
	public:
		enum CustomRoles { ItemNameRole = Qt::UserRole + 1, ItemIdRole = Qt::UserRole + 2 };
		enum Fields { ItemName = 0, ItemId = 1 };
		explicit MappingTableModel(QStringList &headers, const QString &table, QObject *parent = 0);

		//data + flags
		virtual Qt::ItemFlags flags(const QModelIndex &index) const;
		virtual QVariant data(const QModelIndex &index, int role) const;
		virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
		bool contains(const QString &value) const;
		void setMappings(const QStringList &itemNames, int seriesPartId = -1);
		QStringList findPartialName(const QString &value) const;

		//add and remove items
		bool addItem(const QVariant &name);
		bool removeItem(const QModelIndex &idx);

		//mappings
		bool addMapping(int seriesPartId, int itemId);
		bool removeMapping(int seriesId, int itemId);
		QList<QVariant> mappings(int seriesId);

		//database maintenance
		QHash<QString, int> zeroUsers();
		void deleteItems(const QList<int> &ids);

	signals:
		void needResort();

	private:
		void populate();
		QSqlDatabase mDb;
		QString mTable;
		QString mMappingTable;
		QString mIdColumnName;
		QString mNameColumnName;
		QString mSequenceName;
		QStringList mItemNames;

		//queries
		QSqlQuery *mUpdateItemNameQuery;
		QSqlQuery *mInsertItemQuery;
		QSqlQuery *mDeleteItemQuery;
		QSqlQuery *mAddMappingQuery;
		QSqlQuery *mRemoveMappingQuery;
		QSqlQuery *mMappingQuery;
		QSqlQuery *mAllItemsQuery;
		QSqlQuery *mItemCountQuery;
};

#endif // MAPPINGTABLEMODEL_H