summaryrefslogtreecommitdiffstats
path: root/listmodel.cpp
blob: 71a50409350d873ce5e5f8d99cf55be4dd170c4f (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

/*
  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 <QString>
#include <QSqlQuery>

#include <QSqlError>
#include <QDebug>

#include "listmodel.h"
#include "listitem.h"

ListModel::ListModel(const QString table, QObject *parent) : QAbstractItemModel(parent), mTable(table){
	QString updateQuery = QString("UPDATE %1 SET t%1name = :name WHERE i%1id = :id").arg(mTable);
	mUpdateQuery = new QSqlQuery;
	mUpdateQuery->prepare(updateQuery);
	QString insertQuery = QString("INSERT INTO %1 (t%1name) VALUES(:name)").arg(mTable);
	mInsertQuery = new QSqlQuery;
	mInsertQuery->prepare(insertQuery);
	QString deleteQuery = QString("DELETE FROM %1 WHERE t%1name = :name").arg(mTable);
	mDeleteQuery = new QSqlQuery;
	mDeleteQuery->prepare(deleteQuery);
	QString idQuery = QString("SELECT i%1id FROM %1 WHERE t%1name = :name").arg(mTable);
	mIdQuery = new QSqlQuery;
	mIdQuery->prepare(idQuery);
	populate();
}

ListModel::~ListModel(){
	qDeleteAll(mItems);
}

QModelIndex ListModel::index(int row, int column, const QModelIndex &) const{
	if((column != 0) || (row >= mItems.size()) || (row < 0)){
		return QModelIndex();
	}
	return createIndex(row, 0, mItems.at(row));
}

QModelIndex ListModel::index(const QVariant &data) const{
	for(int i = 0; i < rowCount(); ++i){
		ListItem *item = mItems.at(i);
		if(data.toString() == item->name()){
			return createIndex(i, 0, item);
		}
	}
	return QModelIndex();
}

int ListModel::rowCount(const QModelIndex &) const{
	return mItems.size();
}

QVariant ListModel::data(const QModelIndex &index, int role) const{
	if(!index.isValid()){
		return QVariant();
	}
	if(index.column() > 0){
		return QVariant();
	}
	if(index.row() > mItems.size()){
		return QVariant();
	}
	ListItem *item = static_cast<ListItem*>(index.internalPointer());
	Q_ASSERT(item != 0);
	switch (role){
		case Qt::DisplayRole:
			return item->name();
			break;
		case IdRole:
			return item->id();
	}
	return QVariant();
}

bool ListModel::insertRows(int row, int count, const QModelIndex &){
	beginInsertRows(QModelIndex(), row, row + count - 1);
	for(int i = row; i < (row + count); ++i){
		ListItem *newItem = new ListItem(QString(), -1);
		mItems.insert(i, newItem);
	}
	endInsertRows();
	return true;
}

bool ListModel::removeRows(int row, int count, const QModelIndex&){
	if(row > mItems.size()){
		return false;
	}
	if((row + count) > rowCount()){
		return false;
	}
	beginRemoveRows(QModelIndex(), row, row + (count - 1));
	for(int i = row; i < (row + count); ++i){
		ListItem *delItem = mItems.at(i);
		delete delItem;
		mItems.removeAt(i);
	}
	endRemoveRows();
	return true;
}

bool ListModel::setData(const QModelIndex &idx, const QVariant &data, int role){
	if((!idx.isValid()) || (role != Qt::EditRole) || (idx.column() != 0)){
		return false;
	}
	ListItem *item = static_cast<ListItem*>(idx.internalPointer());
	Q_ASSERT(item != 0);
	int id = item->id();
	bool success = false;
	if(id > -1){
		//this is an update
		mUpdateQuery->bindValue(":id", id);
		success = mUpdateQuery->exec();
	}else{
		//this is an insert
		mInsertQuery->bindValue(":name", data);
		success = mInsertQuery->exec();
		if(success){
			mIdQuery->bindValue(":name", data);
			success = mIdQuery->exec();
			int count = 0;
			while(mIdQuery->next()){
				id = mIdQuery->value(0).toInt();
				++count;
			}
			Q_ASSERT(count == 1);
			item->setName(data.toString());
			item->setId(id);
		}
	}
	if(success){
		qSort(mItems.begin(), mItems.end(), sortListItems);
		dataChanged(index(0, 0), index((mItems.size() - 1), 0));
	}
	return success;
}

bool ListModel::addItem(const QVariant &item){
	bool success = false;
	if(insertRows(0, 1, QModelIndex())){
		QModelIndex idx = index(0, 0);
		if(idx.isValid()){
			success = setData(idx, item, Qt::EditRole);
		}
	}
	return success;
}

bool ListModel::removeItem(const QVariant &item){
	QString text = item.toString();
	int rowToDelete = -1;
	for(int row = 0; row < rowCount(); ++row){
		QString data = index(row, 0).data().toString();
		if(data == text){
			rowToDelete = row;
			break;
		}
	}
	if(rowToDelete == -1){
		return false;
	}
	mDeleteQuery->bindValue(":name", item.toString());
	bool success = mDeleteQuery->exec();
	if(success){
		success = removeRows(rowToDelete, 1, QModelIndex());
	}
	return success;
}

bool ListModel::renameItem(const QVariant &oldName, const QVariant &newName){
	QModelIndex idx = QModelIndex();
	bool success = false;
	for(int i = 0; i < rowCount(); ++i){
		idx = index(i, 0);
		if(idx.data().toString() == oldName.toString()){
			success = setData(idx, newName);
		}
	}
	return success;
}

int ListModel::defaultId(){
	if(!mItems.isEmpty()){
		return mItems.at(0)->id();
	}
	addItem("(default)");
	return mItems.at(0)->id();
}

void ListModel::populate(){
	QString query = QString("SELECT i%1id, t%1name FROM %1 ORDER BY t%1name").arg(mTable);
	QSqlQuery q(query);
	while(q.next()){
		ListItem *newItem = new ListItem(q.value(1).toString(), q.value(0).toInt());
		mItems << newItem;
	}
}

bool sortListItems(const ListItem *lhs, const ListItem *rhs){
	return lhs->name().toLower() < rhs->name().toLower();
}