insilmaril@725
|
1 |
#include <QtGui>
|
insilmaril@725
|
2 |
|
insilmaril@741
|
3 |
#include <iostream>
|
insilmaril@741
|
4 |
using namespace std;
|
insilmaril@741
|
5 |
|
insilmaril@750
|
6 |
#include "branchitem.h"
|
insilmaril@725
|
7 |
#include "treeitem.h"
|
insilmaril@725
|
8 |
#include "treemodel.h"
|
insilmaril@725
|
9 |
|
insilmaril@725
|
10 |
TreeModel::TreeModel(QObject *parent)
|
insilmaril@725
|
11 |
: QAbstractItemModel(parent)
|
insilmaril@725
|
12 |
{
|
insilmaril@725
|
13 |
QList<QVariant> rootData;
|
insilmaril@725
|
14 |
rootData << "Heading" << "Type" <<"Note";
|
insilmaril@725
|
15 |
rootItem = new TreeItem(rootData);
|
insilmaril@725
|
16 |
}
|
insilmaril@725
|
17 |
|
insilmaril@725
|
18 |
TreeModel::~TreeModel()
|
insilmaril@725
|
19 |
{
|
insilmaril@725
|
20 |
delete rootItem;
|
insilmaril@725
|
21 |
}
|
insilmaril@725
|
22 |
|
insilmaril@725
|
23 |
QVariant TreeModel::data(const QModelIndex &index, int role) const
|
insilmaril@725
|
24 |
{
|
insilmaril@725
|
25 |
if (!index.isValid())
|
insilmaril@725
|
26 |
return QVariant();
|
insilmaril@725
|
27 |
|
insilmaril@725
|
28 |
if (role != Qt::DisplayRole)
|
insilmaril@725
|
29 |
return QVariant();
|
insilmaril@725
|
30 |
|
insilmaril@745
|
31 |
TreeItem *item = getItem (index);
|
insilmaril@725
|
32 |
|
insilmaril@725
|
33 |
return item->data(index.column());
|
insilmaril@725
|
34 |
}
|
insilmaril@725
|
35 |
|
insilmaril@725
|
36 |
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
insilmaril@725
|
37 |
{
|
insilmaril@725
|
38 |
if (!index.isValid())
|
insilmaril@725
|
39 |
return Qt::ItemIsEnabled;
|
insilmaril@725
|
40 |
|
insilmaril@725
|
41 |
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
insilmaril@725
|
42 |
}
|
insilmaril@725
|
43 |
|
insilmaril@725
|
44 |
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
insilmaril@725
|
45 |
int role) const
|
insilmaril@725
|
46 |
{
|
insilmaril@725
|
47 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
insilmaril@725
|
48 |
return rootItem->data(section);
|
insilmaril@725
|
49 |
|
insilmaril@725
|
50 |
return QVariant();
|
insilmaril@725
|
51 |
}
|
insilmaril@725
|
52 |
|
insilmaril@725
|
53 |
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
|
insilmaril@725
|
54 |
const
|
insilmaril@725
|
55 |
{
|
insilmaril@725
|
56 |
TreeItem *parentItem;
|
insilmaril@725
|
57 |
|
insilmaril@725
|
58 |
if (!parent.isValid())
|
insilmaril@725
|
59 |
parentItem = rootItem;
|
insilmaril@725
|
60 |
else
|
insilmaril@745
|
61 |
parentItem = getItem (parent);
|
insilmaril@725
|
62 |
|
insilmaril@725
|
63 |
TreeItem *childItem = parentItem->child(row);
|
insilmaril@725
|
64 |
if (childItem)
|
insilmaril@725
|
65 |
return createIndex(row, column, childItem);
|
insilmaril@725
|
66 |
else
|
insilmaril@725
|
67 |
return QModelIndex();
|
insilmaril@725
|
68 |
}
|
insilmaril@725
|
69 |
|
insilmaril@725
|
70 |
QModelIndex TreeModel::parent(const QModelIndex &index) const
|
insilmaril@725
|
71 |
{
|
insilmaril@725
|
72 |
if (!index.isValid())
|
insilmaril@725
|
73 |
return QModelIndex();
|
insilmaril@725
|
74 |
|
insilmaril@745
|
75 |
TreeItem *ti= getItem (index);
|
insilmaril@745
|
76 |
TreeItem *parentItem = ti->parent();
|
insilmaril@725
|
77 |
|
insilmaril@745
|
78 |
//cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
|
insilmaril@725
|
79 |
if (parentItem == rootItem)
|
insilmaril@725
|
80 |
return QModelIndex();
|
insilmaril@725
|
81 |
|
insilmaril@745
|
82 |
if (!parentItem)
|
insilmaril@746
|
83 |
return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
|
insilmaril@746
|
84 |
// see also my question on qt-interest in march
|
insilmaril@745
|
85 |
|
insilmaril@745
|
86 |
return createIndex(parentItem->childNumber(), 0, parentItem);
|
insilmaril@725
|
87 |
}
|
insilmaril@725
|
88 |
|
insilmaril@725
|
89 |
int TreeModel::rowCount(const QModelIndex &parent) const
|
insilmaril@725
|
90 |
{
|
insilmaril@725
|
91 |
TreeItem *parentItem;
|
insilmaril@725
|
92 |
|
insilmaril@725
|
93 |
if (!parent.isValid())
|
insilmaril@725
|
94 |
parentItem = rootItem;
|
insilmaril@725
|
95 |
else
|
insilmaril@745
|
96 |
parentItem = getItem (parent);
|
insilmaril@725
|
97 |
|
insilmaril@725
|
98 |
return parentItem->childCount();
|
insilmaril@725
|
99 |
}
|
insilmaril@725
|
100 |
|
insilmaril@741
|
101 |
int TreeModel::columnCount(const QModelIndex &parent) const
|
insilmaril@726
|
102 |
{
|
insilmaril@741
|
103 |
if (parent.isValid())
|
insilmaril@745
|
104 |
return getItem (parent)->columnCount();
|
insilmaril@741
|
105 |
else
|
insilmaril@741
|
106 |
return rootItem->columnCount();
|
insilmaril@726
|
107 |
}
|
insilmaril@726
|
108 |
|
insilmaril@750
|
109 |
BranchItem* TreeModel::next(BranchItem* ¤t, BranchItem* &previous, int &d0)
|
insilmaril@741
|
110 |
{
|
insilmaril@741
|
111 |
// Walk through map beginning at current with previous==0
|
insilmaril@741
|
112 |
// Start at root, if current==NULL
|
insilmaril@750
|
113 |
if (!current) current=(BranchItem*)rootItem;
|
insilmaril@741
|
114 |
|
insilmaril@741
|
115 |
// Are we just beginning to walk the map?
|
insilmaril@741
|
116 |
if (!previous)
|
insilmaril@741
|
117 |
{
|
insilmaril@741
|
118 |
previous=current;
|
insilmaril@741
|
119 |
d0=current->depth();
|
insilmaril@741
|
120 |
current=current->getFirstBranch();
|
insilmaril@741
|
121 |
return current;
|
insilmaril@741
|
122 |
}
|
insilmaril@741
|
123 |
|
insilmaril@741
|
124 |
//std::cout << " cur="<<current->getHeading().toStdString();
|
insilmaril@741
|
125 |
//std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
|
insilmaril@741
|
126 |
|
insilmaril@741
|
127 |
// Going up or down (deeper)?
|
insilmaril@741
|
128 |
if (current->depth() > previous->depth() )
|
insilmaril@741
|
129 |
{
|
insilmaril@741
|
130 |
// Coming from above
|
insilmaril@741
|
131 |
// Trying to go down deeper
|
insilmaril@741
|
132 |
if (current->branchCount() >0 )
|
insilmaril@741
|
133 |
{
|
insilmaril@741
|
134 |
previous=current;
|
insilmaril@741
|
135 |
current=current->getFirstBranch();
|
insilmaril@741
|
136 |
return current;
|
insilmaril@741
|
137 |
}
|
insilmaril@741
|
138 |
// turn around and go up again
|
insilmaril@741
|
139 |
}
|
insilmaril@741
|
140 |
|
insilmaril@741
|
141 |
// Coming from below,
|
insilmaril@741
|
142 |
// Trying to go down again to siblings
|
insilmaril@741
|
143 |
|
insilmaril@750
|
144 |
BranchItem *sibling=current->getBranchNum (previous->num()+1);
|
insilmaril@741
|
145 |
|
insilmaril@741
|
146 |
if (sibling)
|
insilmaril@741
|
147 |
{
|
insilmaril@741
|
148 |
// Found sibling of previous, go there
|
insilmaril@741
|
149 |
previous=current;
|
insilmaril@741
|
150 |
current=sibling;
|
insilmaril@741
|
151 |
return current;
|
insilmaril@741
|
152 |
}
|
insilmaril@741
|
153 |
|
insilmaril@741
|
154 |
// Go up and try to find siblings of current
|
insilmaril@741
|
155 |
previous=current;
|
insilmaril@750
|
156 |
current=(BranchItem*)current->parent();
|
insilmaril@741
|
157 |
|
insilmaril@741
|
158 |
// Check if we still can go somewhere
|
insilmaril@741
|
159 |
if (!current) return current;
|
insilmaril@741
|
160 |
|
insilmaril@741
|
161 |
while (current && current->depth() < previous->depth() )
|
insilmaril@741
|
162 |
next (current,previous,d0);
|
insilmaril@741
|
163 |
|
insilmaril@741
|
164 |
return current;
|
insilmaril@741
|
165 |
}
|
insilmaril@741
|
166 |
|
insilmaril@745
|
167 |
bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
|
insilmaril@745
|
168 |
{
|
insilmaril@745
|
169 |
std::cout << "TreeModel insertRows()\n";
|
insilmaril@745
|
170 |
int last=row+count-1;
|
insilmaril@745
|
171 |
beginInsertRows (parent,row,last);
|
insilmaril@745
|
172 |
|
insilmaril@745
|
173 |
for (int i=row; i<=last; i++)
|
insilmaril@745
|
174 |
{
|
insilmaril@745
|
175 |
std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
|
insilmaril@745
|
176 |
}
|
insilmaril@745
|
177 |
endInsertRows ();
|
insilmaril@745
|
178 |
return true;
|
insilmaril@745
|
179 |
}
|
insilmaril@745
|
180 |
|
insilmaril@727
|
181 |
bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
|
insilmaril@727
|
182 |
{
|
insilmaril@727
|
183 |
int last=row+count-1;
|
insilmaril@745
|
184 |
TreeItem *pi= getItem (parent);
|
insilmaril@755
|
185 |
TreeItem *ti;
|
insilmaril@727
|
186 |
|
insilmaril@727
|
187 |
for (int i=row; i<=last; i++)
|
insilmaril@727
|
188 |
{
|
insilmaril@755
|
189 |
ti=pi->getChildNum (row);
|
insilmaril@755
|
190 |
pi->removeChild (row); // does not delete object!
|
insilmaril@756
|
191 |
switch (ti->getType())
|
insilmaril@756
|
192 |
{
|
insilmaril@756
|
193 |
case TreeItem::MapCenter:
|
insilmaril@756
|
194 |
delete (BranchItem*)ti;
|
insilmaril@756
|
195 |
break;
|
insilmaril@756
|
196 |
case TreeItem::Branch:
|
insilmaril@756
|
197 |
delete (BranchItem*)ti;
|
insilmaril@756
|
198 |
break;
|
insilmaril@756
|
199 |
default:
|
insilmaril@756
|
200 |
delete ti;
|
insilmaril@756
|
201 |
break;
|
insilmaril@756
|
202 |
}
|
insilmaril@727
|
203 |
}
|
insilmaril@745
|
204 |
return true;
|
insilmaril@745
|
205 |
}
|
insilmaril@727
|
206 |
|
insilmaril@745
|
207 |
TreeItem *TreeModel::getItem(const QModelIndex &index) const
|
insilmaril@745
|
208 |
{
|
insilmaril@745
|
209 |
if (index.isValid()) {
|
insilmaril@745
|
210 |
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
insilmaril@745
|
211 |
|
insilmaril@745
|
212 |
if (item) return item;
|
insilmaril@745
|
213 |
}
|
insilmaril@745
|
214 |
return rootItem;
|
insilmaril@727
|
215 |
}
|
insilmaril@727
|
216 |
|
insilmaril@754
|
217 |
TreeItem *TreeModel::getRootItem()
|
insilmaril@754
|
218 |
{
|
insilmaril@754
|
219 |
return rootItem;
|
insilmaril@754
|
220 |
}
|
insilmaril@754
|
221 |
|
insilmaril@726
|
222 |
QModelIndex TreeModel::index (TreeItem* ti)
|
insilmaril@726
|
223 |
{
|
insilmaril@726
|
224 |
return createIndex (ti->row(),ti->column(),ti);
|
insilmaril@726
|
225 |
}
|
insilmaril@726
|
226 |
|