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