summaryrefslogtreecommitdiffstats
path: root/smtreemodel.cpp
blob: 2c7a2d64ca23d4a10bcd7b25ba509acfcf46c664 (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
/*
  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 "smtreemodel.h"
#include "smtreeitem.h"

SmTreeModel::SmTreeModel(const QStringList &headers, QObject *parent) : QAbstractItemModel(parent), mRootItem(0){
	mHeaders = headers;
	mRootItem = new SmTreeItem(headers.count());
}

SmTreeModel::~SmTreeModel(){
	delete mRootItem;
}

int SmTreeModel::rowCount(const QModelIndex &parent) const{
	SmTreeItem *parentItem = itemAt(parent);
	return parentItem->childCount();
}

int SmTreeModel::columnCount(const QModelIndex &parent) const{
	Q_UNUSED(parent);
	return mRootItem->columnCount();
}

QModelIndex SmTreeModel::index(int row, int column, const QModelIndex &parent) const{
	if(parent.isValid() && (parent.column() != 0)){
		return QModelIndex();
	}
	SmTreeItem *parentItem = itemAt(parent);
	SmTreeItem *childItem = parentItem->child(row);
	if(childItem){
		return createIndex(row, column, childItem);
	}
	return QModelIndex();
}

Qt::ItemFlags SmTreeModel::flags(const QModelIndex &index) const{
	if(!index.isValid()){
		return 0;
	}
	return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant SmTreeModel::headerData(int section, Qt::Orientation orientation, int role) const{
	if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){
		return mRootItem->data(section);
	}
	return QVariant();
}

bool SmTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){
	if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){
		mRootItem->setData(section, value);
		emit headerDataChanged(orientation, section, section);
		return true;
	}
	return false;
}

QVariant SmTreeModel::data(const QModelIndex &index, int role) const{
	if(!index.isValid()){
		return QVariant();
	}
	SmTreeItem *item = itemAt(index);
	if(role == Qt::DisplayRole){
		return item->data(index.column());
	}
	if(role == Qt::EditRole){
		return item->data(index.column());
	}
	return QVariant();
}

bool SmTreeModel::setData(const QModelIndex &index, const QVariant &value, int role){
	if(role != Qt::EditRole){
		return false;
	}
	SmTreeItem *item = itemAt(index);
	item->setData(index.column(), value);
	emit dataChanged(index, index);
	return true;
}

bool SmTreeModel::setRoot(SmTreeItem *rootItem){
	if(mRootItem){
		beginResetModel();
		delete mRootItem;
		mRootItem = rootItem;
		endResetModel();
		return true;
	}
	return false;
}

bool SmTreeModel::insertRows(int row, int count, const QModelIndex &parent){
	SmTreeItem *parentItem = itemAt(parent);
	bool retval;

	if(row > parentItem->childCount()){
		return false;
	}

	beginInsertRows(parent, row, row + count - 1);
	for(int i = row; i < row + count; ++i){
		SmTreeItem *newItem = new SmTreeItem(mRootItem->columnCount());
		retval = parentItem->insertChild(i, newItem);
	}
	endInsertRows();
	return retval;
}

bool SmTreeModel::removeRows(int row, int count, const QModelIndex &parent){
	SmTreeItem *parentItem = itemAt(parent);
	bool retval;

	if(row > parentItem->childCount()){
		return false;
	}

	beginRemoveRows(parent, row, row + count - 1);
	for(int i = row + count; i > row; --i){
		retval = parentItem->removeChild(i);

	}
	endRemoveRows();
	return retval;
}

bool SmTreeModel::addRow(const QList<QVariant> &data, const QModelIndex &parent){
	if(data.count() != mRootItem->columnCount()){
		return false;
	}

	SmTreeItem *parentItem = itemAt(parent);

	if(insertRows(parentItem->childCount(), 1, parent)){
		SmTreeItem *child = parentItem->child(parentItem->childCount());
		delete child;
		SmTreeItem *newChild = new SmTreeItem(data, parentItem);
		child = newChild;
		QModelIndex start = index(parentItem->childCount() - 1, 0, parent);
		QModelIndex end = index(parentItem->childCount() - 1, parentItem->columnCount() - 1, parent);
		emit dataChanged(start, end);
		return true;
	}
	return false;
}

SmTreeItem *SmTreeModel::itemAt(const QModelIndex &index) const{
	if(index.isValid()){
		SmTreeItem *item = static_cast<SmTreeItem*>(index.internalPointer());
		if(item){
			return item;
		}
	}
	return mRootItem;
}