summaryrefslogtreecommitdiffstats
path: root/smtreemodel.cpp
blob: 138617958989a7fdda6621e774a63c02dc471bca (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
  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 <QIcon>
#include <QSettings>
#include <QBrush>

#include "smtreemodel.h"
#include "smtreeitem.h"
#include "smglobals.h"

SmTreeModel::SmTreeModel(const QStringList &headers, QObject *parent) : QAbstractItemModel(parent), mRootItem(0){
	mHeaders = headers;
	for(int i = 0; i < mHeaders.size(); ++i){
		mHeaderData.insert(mHeaders.at(i), i);
	}
	mRootItem = new SmTreeItem(headers.count());
    mDecorationIcon = SmGlobals::instance()->iconFor("folder");
}

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(!hasIndex(row, column, parent)){
        return QModelIndex();
    }
    SmTreeItem *parentItem;
    if(!parent.isValid()){
        parentItem = mRootItem;
    }else{
        parentItem = static_cast<SmTreeItem*>(parent.internalPointer());
    }
    SmTreeItem *childItem = parentItem->child(row);
    if(childItem){
        return createIndex(row, column, childItem);
    }
    return QModelIndex();
}

QModelIndex SmTreeModel::parent(const QModelIndex &child) const{
	if(!child.isValid()){
		return QModelIndex();
	}

    SmTreeItem *childItem = static_cast<SmTreeItem*>(child.internalPointer());
    if(childItem == mRootItem){
        return QModelIndex();
    }

	SmTreeItem *parentItem = childItem->parent();
    if(parentItem == mRootItem){
        return QModelIndex();
    }
    return createIndex(parentItem->row(), 0, parentItem);
}

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) && (section > -1) && (section < mHeaders.count())){
		return mHeaders.at(section);
	}
	return QVariant();
}

const QHash<QString, int> SmTreeModel::headerData() const{
	return mHeaderData;
}

bool SmTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){
	if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){
		mHeaders[section] = value.toString();
		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());
	}
	if(role == Qt::DecorationRole){
		if(index.column() == 0){
			return mDecorationIcon;
        }
	}
	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;
}

QModelIndex SmTreeModel::find(const QVariant &value, int column, const QModelIndex &pIdx) const{
	SmTreeItem *parentItem = 0;
    if(!pIdx.isValid()){
		parentItem = root();
	}else{
        parentItem = static_cast<SmTreeItem*>(pIdx.internalPointer());
	}
	for(int i = 0; i < parentItem->childCount(); ++i){
		SmTreeItem *child = parentItem->child(i);
		if(child->data(column) == value){
            return index(i, column, pIdx);
		}
	}
	return QModelIndex();
}

QModelIndex SmTreeModel::findRecursive(const QVariant &value, int column, const QModelIndex &start) const{
    if(!start.isValid()){
        return QModelIndex();
    }
    SmTreeItem *startItem = static_cast<SmTreeItem*>(start.internalPointer());
    if(startItem->data(column) == value){
        return start;
    }
    for(int i = 0; i < startItem->childCount(); ++i){
        SmTreeItem *child = startItem->child(i);
        if(child->data(column) == value){
            return createIndex(i, column, child);
        }
        if(child->childCount()){
            return findRecursive(value, column, createIndex(i, column, child));
        }
    }
    SmTreeItem *next = startItem->next();
    if(next){
        return findRecursive(value, column, createIndex(next->row(), column,  next));
    }
    return QModelIndex();
}

bool SmTreeModel::matchRecursive(const QModelIndex &pIdx, const QRegExp &regex, int column) const {
    if(!pIdx.isValid()){
        return false;
    }
    // first try the parent itself
    QString value = pIdx.data().toString();
    if(value.isEmpty()){
        return false;
    }
    if(value.contains(regex)){
        return true;
    }

    // no match, check for children
    SmTreeItem *item = static_cast<SmTreeItem*>(pIdx.internalPointer());
    if(!item->childCount()){
        //leaf, chech parents
        return checkParents(item, regex, column);
    }

    // we have children, traverse them
    bool retval = false;
    for(int i = 0; i < item->childCount(); ++i){
        QModelIndex newIdx = createIndex(i, column, item->child(i));
        retval = matchRecursive(newIdx, regex, column);
        if(retval){
            break;
        }
    }
    return retval;
}

bool SmTreeModel::checkParents(const SmTreeItem *item, const QRegExp &regex, int column) const {
    while(item != root()){
        QString value = item->data(column).toString();
        if(value.contains(regex)){
            return true;
        }
        item = item->parent();
    }
    return false;
}

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

QModelIndex SmTreeModel::rootIndex() const {
    return createIndex(0, 0, mRootItem);
}

SmTreeItem *SmTreeModel::parentItem(const QModelIndex &child) const{
	QModelIndex parent = child.parent();
	if(parent == QModelIndex()){
		return mRootItem;
	}
	return static_cast<SmTreeItem*>(child.parent().internalPointer());
}

void SmTreeModel::reparent(const QModelIndex &idx, const QModelIndex &newParent, bool sorted){
	if(!idx.isValid()){
		return;
	}
    SmTreeItem *item = static_cast<SmTreeItem*>(idx.internalPointer());
    QList<QVariant> data;
    for(int i = 0; i < item->columnCount(); ++i){
        data << item->data(i);
    }
    QPersistentModelIndex pNewParent = newParent;
	removeRows(idx.row(), 1, idx.parent());
    addRow(data, pNewParent, sorted);
}

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

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

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

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

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

    beginRemoveRows(pIdx, row, row + count - 1);
	for(int i = row + count; i > row; --i){
		retval = parentItem->removeChild(i - 1);
	}
	endRemoveRows();
	return retval;
}

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

    SmTreeItem *parentItem = itemAt(pIdx);
    int where = 0;
    if(sorted){
        for(int i = 0; i < parentItem->childCount(); ++i){
            if(parentItem->child(i)->data(0).toString() > data.at(0).toString()){
                where = i;
                break;
            }
        }
    }

    if(insertRows(where, 1, pIdx)){
        SmTreeItem *child = parentItem->child(where);
		for(int i = 0; i < data.count(); ++i){
			child->setData(i, data.at(i));
		}
        QModelIndex start = index(parentItem->childCount() - 1, 0, pIdx);
        QModelIndex end = index(parentItem->childCount() - 1, parentItem->columnCount() - 1, pIdx);
        emit dataChanged(start, end);
		return true;
    }
    return false;
}

bool SmTreeModel::appendRow(const QList<QVariant> &data, const QModelIndex &pIdx){
    SmTreeItem *pItem = itemAt(pIdx);
    SmTreeItem *newItem = new SmTreeItem(data, pItem);
    beginResetModel();
    pItem->appendChild(newItem);
    endResetModel();
    return true;
}

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