treemodel.cpp
author insilmaril
Mon, 08 Mar 2010 12:21:46 +0000
changeset 827 a591443f229c
parent 825 1ad892c1a709
child 847 43268373032d
permissions -rw-r--r--
Added missing flag for Bugzilla
insilmaril@725
     1
#include <QtGui>
insilmaril@725
     2
insilmaril@788
     3
#include "attributeitem.h"
insilmaril@750
     4
#include "branchitem.h"
insilmaril@788
     5
#include "imageitem.h"
insilmaril@725
     6
#include "treeitem.h"
insilmaril@725
     7
#include "treemodel.h"
insilmaril@790
     8
#include "xlinkitem.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@790
    14
    rootData << "Heading" << "Type";
insilmaril@791
    15
    rootItem = new BranchItem(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@791
    39
        return Qt::NoItemFlags;
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@788
    53
QModelIndex TreeModel::index (TreeItem* ti)
insilmaril@788
    54
{
insilmaril@788
    55
	if (!ti->parent())
insilmaril@788
    56
		return QModelIndex();
insilmaril@788
    57
	else	
insilmaril@791
    58
		return createIndex (ti->row(),0,ti);
insilmaril@788
    59
}
insilmaril@788
    60
insilmaril@725
    61
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
insilmaril@725
    62
            const
insilmaril@725
    63
{
insilmaril@791
    64
	// Make sure to return invalid index for invalid values (see modeltest)
insilmaril@791
    65
	if (row<0 || column<0) return QModelIndex();
insilmaril@791
    66
	if (column!=0) return QModelIndex();
insilmaril@791
    67
insilmaril@725
    68
    TreeItem *parentItem;
insilmaril@725
    69
insilmaril@725
    70
    if (!parent.isValid())
insilmaril@791
    71
	{
insilmaril@725
    72
        parentItem = rootItem;
insilmaril@788
    73
		/*
insilmaril@788
    74
		cout << "TM::index()  no parent?! xxx\n";
insilmaril@788
    75
		cout << "   row="<<row<<"  col="<<column<<endl;
insilmaril@788
    76
		cout << "   parent.internal="<< parent.internalPointer()<<endl;
insilmaril@788
    77
		*/
insilmaril@788
    78
		// Somehow index is requested where parentIndex is invalid.
insilmaril@788
    79
		// what's happening here...?
insilmaril@788
    80
		// Check if Qt examples also return index of rootIem then...
insilmaril@788
    81
insilmaril@788
    82
	}	
insilmaril@725
    83
    else
insilmaril@745
    84
        parentItem = getItem (parent);
insilmaril@725
    85
insilmaril@725
    86
    TreeItem *childItem = parentItem->child(row);
insilmaril@791
    87
	//cout << "TM::index  parentItem="<<parentItem<<"  childItem="<<childItem<<"  row="<<row<<" col="<<column<<endl;
insilmaril@725
    88
    if (childItem)
insilmaril@725
    89
        return createIndex(row, column, childItem);
insilmaril@725
    90
    else
insilmaril@725
    91
        return QModelIndex();
insilmaril@725
    92
}
insilmaril@725
    93
insilmaril@725
    94
QModelIndex TreeModel::parent(const QModelIndex &index) const
insilmaril@725
    95
{
insilmaril@725
    96
    if (!index.isValid())
insilmaril@725
    97
        return QModelIndex();
insilmaril@725
    98
insilmaril@788
    99
	//FIXME-3 cout << "TM::parent  ri="<<rootItem<< "  row="<<index.row()<<"  col="<<index.column()<<endl;
insilmaril@745
   100
    TreeItem *ti= getItem (index);
insilmaril@788
   101
	//cout << "            ti="<<ti<<endl;
insilmaril@788
   102
	//cout << "               "<<ti->getHeadingStd()<<endl;
insilmaril@745
   103
    TreeItem *parentItem = ti->parent();
insilmaril@788
   104
	//cout << "            pi="<<parentItem<<endl;
insilmaril@725
   105
insilmaril@745
   106
	//cout << "TreeModel::parent  ti="<<ti<<" "<<ti->getHeading().toStdString()<<"  pi="<<parentItem<<"  "<<endl;
insilmaril@725
   107
    if (parentItem == rootItem)
insilmaril@725
   108
        return QModelIndex();
insilmaril@725
   109
insilmaril@791
   110
/*
insilmaril@745
   111
	if (!parentItem)
insilmaril@746
   112
        return QModelIndex();	// FIXME-3 do this to avoid segfault, but why?
insilmaril@746
   113
		                        // see also my question on qt-interest in march
insilmaril@791
   114
*/
insilmaril@745
   115
    return createIndex(parentItem->childNumber(), 0, parentItem);
insilmaril@725
   116
}
insilmaril@725
   117
insilmaril@725
   118
int TreeModel::rowCount(const QModelIndex &parent) const
insilmaril@725
   119
{
insilmaril@725
   120
    TreeItem *parentItem;
insilmaril@725
   121
insilmaril@725
   122
    if (!parent.isValid())
insilmaril@725
   123
        parentItem = rootItem;
insilmaril@725
   124
    else
insilmaril@745
   125
        parentItem = getItem (parent);
insilmaril@725
   126
insilmaril@725
   127
    return parentItem->childCount();
insilmaril@725
   128
}
insilmaril@725
   129
insilmaril@741
   130
int TreeModel::columnCount(const QModelIndex &parent) const
insilmaril@726
   131
{
insilmaril@791
   132
	int c;
insilmaril@741
   133
    if (parent.isValid())
insilmaril@791
   134
	{
insilmaril@791
   135
        c= getItem (parent)->columnCount();
insilmaril@791
   136
		//cout << "TM::colCount  c="<<c<<"  parent="<<getItem (parent)<<endl;	
insilmaril@791
   137
	}
insilmaril@741
   138
    else
insilmaril@791
   139
	{
insilmaril@791
   140
        c= rootItem->columnCount();
insilmaril@791
   141
		//cout << "TM::colCount  c="<<c<<"  parent=invalid"<<endl;	
insilmaril@791
   142
	}
insilmaril@791
   143
	return c;
insilmaril@726
   144
}
insilmaril@726
   145
insilmaril@804
   146
BranchItem* TreeModel::nextBranch (BranchItem* &current, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)	
insilmaril@741
   147
{
insilmaril@741
   148
	// Walk through map beginning at current with previous==0
insilmaril@741
   149
	// Start at root, if current==NULL
insilmaril@750
   150
	if (!current) current=(BranchItem*)rootItem;
insilmaril@741
   151
insilmaril@741
   152
	// Are we just beginning to walk the map?
insilmaril@741
   153
	if (!previous)
insilmaril@741
   154
	{
insilmaril@773
   155
		if (!start) start=current;
insilmaril@741
   156
		previous=current;
insilmaril@741
   157
		current=current->getFirstBranch();
insilmaril@741
   158
		return current;
insilmaril@741
   159
	}
insilmaril@804
   160
	if (deepLevelsFirst)
insilmaril@804
   161
	{
insilmaril@804
   162
		// Going up or down (deeper)?
insilmaril@804
   163
		if (current->depth() > previous->depth() )
insilmaril@804
   164
		{	
insilmaril@804
   165
			// Coming from above
insilmaril@804
   166
			// Trying  to go down deeper
insilmaril@804
   167
			if (current->branchCount() >0 )
insilmaril@804
   168
			{
insilmaril@804
   169
				previous=current;
insilmaril@804
   170
				current=current->getFirstBranch();
insilmaril@804
   171
				return current;
insilmaril@804
   172
			}	
insilmaril@804
   173
			// turn around and go up again
insilmaril@804
   174
			BranchItem *bi=current;
insilmaril@804
   175
			current=previous;
insilmaril@804
   176
			previous=bi;
insilmaril@804
   177
		}	
insilmaril@741
   178
insilmaril@804
   179
		// Coming from below
insilmaril@804
   180
		// Trying to go down again to siblings
insilmaril@804
   181
insilmaril@804
   182
		BranchItem *sibling=current->getBranchNum (previous->num()+1);
insilmaril@804
   183
insilmaril@804
   184
		if (sibling)
insilmaril@804
   185
		{	
insilmaril@804
   186
			// Found sibling of previous, go there
insilmaril@741
   187
			previous=current;
insilmaril@804
   188
			current=sibling;
insilmaril@741
   189
			return current;
insilmaril@804
   190
		} 
insilmaril@741
   191
insilmaril@804
   192
		// If we only needed to go through subtree, we are done now
insilmaril@804
   193
		if (start==current) return NULL;
insilmaril@741
   194
insilmaril@804
   195
		// Go up and try to find siblings of current
insilmaril@804
   196
		previous=current;
insilmaril@804
   197
		current=(BranchItem*)current->parent();
insilmaril@741
   198
insilmaril@804
   199
		// Check if we still can go somewhere
insilmaril@804
   200
		if (!current) return current;
insilmaril@804
   201
		
insilmaril@804
   202
		while (current && current->depth() < previous->depth() )
insilmaril@804
   203
			current=nextBranch (current,previous,true,start);
insilmaril@804
   204
			
insilmaril@741
   205
		return current;
insilmaril@741
   206
insilmaril@804
   207
	} else
insilmaril@804
   208
	{
insilmaril@804
   209
/*FIXME-3
insilmaril@804
   210
		cout << "TM::nextBranch shallow\n"; 
insilmaril@804
   211
		std::string ch="()"; if (current) ch=current->getHeadingStd();
insilmaril@804
   212
		std::string ph="()"; if (previous) ph=previous->getHeadingStd();
insilmaril@804
   213
		cout << "  cur="<<ch << " prev="<<ph<<endl;
insilmaril@804
   214
*/
insilmaril@773
   215
insilmaril@804
   216
		// Try to find sibling with same depth
insilmaril@804
   217
		BranchItem *sibling=current->parent()->getBranchNum (current->num()+1);
insilmaril@804
   218
		if (sibling)
insilmaril@804
   219
		{	
insilmaril@804
   220
			// Found sibling of previous, go there
insilmaril@804
   221
			previous=current;
insilmaril@804
   222
			current=sibling;
insilmaril@804
   223
			return current;
insilmaril@804
   224
		}  else
insilmaril@804
   225
		{	
insilmaril@804
   226
			// Try to find next branch with same depth or greater
insilmaril@804
   227
			
insilmaril@741
   228
insilmaril@804
   229
			current=NULL;
insilmaril@804
   230
			return current;
insilmaril@804
   231
		}
insilmaril@804
   232
insilmaril@804
   233
insilmaril@804
   234
		/*
insilmaril@804
   235
	while (ix.isValid())
insilmaril@804
   236
	{
insilmaril@804
   237
		TreeItem *ti=model->getItem (ix);
insilmaril@804
   238
		cout << "  level="<<level<<"  ix=";
insilmaril@804
   239
		if (ti) cout << ti->getHeadingStd();
insilmaril@804
   240
		row=ix.row();
insilmaril@804
   241
		col=ix.column();
insilmaril@804
   242
		if (! treeEditor->isExpanded(ix))
insilmaril@804
   243
			cout <<"  expand!";
insilmaril@804
   244
		else	
insilmaril@804
   245
			cout <<"  is expanded.";
insilmaril@804
   246
		cout <<endl;
insilmaril@804
   247
		ix=ix.sibling(row+1,col);
insilmaril@804
   248
	}
insilmaril@804
   249
	*/
insilmaril@804
   250
insilmaril@804
   251
	}
insilmaril@741
   252
}
insilmaril@741
   253
insilmaril@727
   254
bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
insilmaril@727
   255
{
insilmaril@727
   256
	int last=row+count-1;
insilmaril@794
   257
    TreeItem *pi;
insilmaril@794
   258
	if (parent.isValid())
insilmaril@794
   259
		pi=getItem (parent);
insilmaril@794
   260
	else
insilmaril@794
   261
		pi=rootItem;
insilmaril@755
   262
	TreeItem *ti;
insilmaril@727
   263
insilmaril@795
   264
	//cout << "TM::removeRows  pi="<<pi<<"  row="<<row<<"  count="<<count<<endl;
insilmaril@727
   265
	for (int i=row; i<=last; i++)
insilmaril@727
   266
	{
insilmaril@755
   267
		ti=pi->getChildNum (row);
insilmaril@755
   268
		pi->removeChild (row);	// does not delete object!
insilmaril@791
   269
		delete ti;
insilmaril@727
   270
	}
insilmaril@745
   271
	return true;
insilmaril@745
   272
}
insilmaril@727
   273
insilmaril@745
   274
TreeItem *TreeModel::getItem(const QModelIndex &index) const
insilmaril@745
   275
{
insilmaril@745
   276
    if (index.isValid()) {
insilmaril@745
   277
		TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
insilmaril@745
   278
        if (item) return item;
insilmaril@745
   279
    }
insilmaril@791
   280
    return NULL;
insilmaril@727
   281
}
insilmaril@727
   282
insilmaril@793
   283
BranchItem *TreeModel::getRootItem()
insilmaril@754
   284
{
insilmaril@754
   285
	return rootItem;
insilmaril@754
   286
}
insilmaril@754
   287