treemodel.cpp
author insilmaril
Fri, 30 Jan 2009 09:14:12 +0000
changeset 738 716a777c1c98
parent 727 96402b172173
child 740 6dc0a20031f7
permissions -rw-r--r--
fixes for selecting branches
insilmaril@725
     1
#include <QtGui>
insilmaril@725
     2
insilmaril@725
     3
#include "treeitem.h"
insilmaril@725
     4
#include "treemodel.h"
insilmaril@725
     5
insilmaril@726
     6
TreeItem* TreeModel::itFirst=NULL;
insilmaril@726
     7
TreeItem* TreeModel::itCur=NULL;
insilmaril@726
     8
insilmaril@725
     9
TreeModel::TreeModel(QObject *parent)
insilmaril@725
    10
    : QAbstractItemModel(parent)
insilmaril@725
    11
{
insilmaril@725
    12
    QList<QVariant> rootData;
insilmaril@725
    13
    rootData << "Heading" << "Type" <<"Note";
insilmaril@725
    14
    rootItem = new TreeItem(rootData);
insilmaril@725
    15
}
insilmaril@725
    16
insilmaril@725
    17
TreeModel::~TreeModel()
insilmaril@725
    18
{
insilmaril@725
    19
    delete rootItem;
insilmaril@725
    20
}
insilmaril@725
    21
insilmaril@725
    22
int TreeModel::columnCount(const QModelIndex &parent) const
insilmaril@725
    23
{
insilmaril@725
    24
    if (parent.isValid())
insilmaril@725
    25
        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
insilmaril@725
    26
    else
insilmaril@725
    27
        return rootItem->columnCount();
insilmaril@725
    28
}
insilmaril@725
    29
insilmaril@725
    30
QVariant TreeModel::data(const QModelIndex &index, int role) const
insilmaril@725
    31
{
insilmaril@725
    32
    if (!index.isValid())
insilmaril@725
    33
        return QVariant();
insilmaril@725
    34
insilmaril@725
    35
    if (role != Qt::DisplayRole)
insilmaril@725
    36
        return QVariant();
insilmaril@725
    37
insilmaril@725
    38
    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
insilmaril@725
    39
insilmaril@725
    40
    return item->data(index.column());
insilmaril@725
    41
}
insilmaril@725
    42
insilmaril@725
    43
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
insilmaril@725
    44
{
insilmaril@725
    45
    if (!index.isValid())
insilmaril@725
    46
        return Qt::ItemIsEnabled;
insilmaril@725
    47
insilmaril@725
    48
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
insilmaril@725
    49
}
insilmaril@725
    50
insilmaril@725
    51
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
insilmaril@725
    52
                               int role) const
insilmaril@725
    53
{
insilmaril@725
    54
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
insilmaril@725
    55
        return rootItem->data(section);
insilmaril@725
    56
insilmaril@725
    57
    return QVariant();
insilmaril@725
    58
}
insilmaril@725
    59
insilmaril@725
    60
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
insilmaril@725
    61
            const
insilmaril@725
    62
{
insilmaril@725
    63
    TreeItem *parentItem;
insilmaril@725
    64
insilmaril@725
    65
    if (!parent.isValid())
insilmaril@725
    66
        parentItem = rootItem;
insilmaril@725
    67
    else
insilmaril@725
    68
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
insilmaril@725
    69
insilmaril@725
    70
    TreeItem *childItem = parentItem->child(row);
insilmaril@725
    71
    if (childItem)
insilmaril@725
    72
        return createIndex(row, column, childItem);
insilmaril@725
    73
    else
insilmaril@725
    74
        return QModelIndex();
insilmaril@725
    75
}
insilmaril@725
    76
insilmaril@725
    77
QModelIndex TreeModel::parent(const QModelIndex &index) const
insilmaril@725
    78
{
insilmaril@725
    79
    if (!index.isValid())
insilmaril@725
    80
        return QModelIndex();
insilmaril@725
    81
insilmaril@725
    82
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
insilmaril@725
    83
    TreeItem *parentItem = childItem->parent();
insilmaril@725
    84
insilmaril@725
    85
    if (parentItem == rootItem)
insilmaril@725
    86
        return QModelIndex();
insilmaril@725
    87
insilmaril@725
    88
    return createIndex(parentItem->row(), 0, parentItem);
insilmaril@725
    89
}
insilmaril@725
    90
insilmaril@725
    91
int TreeModel::rowCount(const QModelIndex &parent) const
insilmaril@725
    92
{
insilmaril@725
    93
    TreeItem *parentItem;
insilmaril@725
    94
insilmaril@725
    95
    if (!parent.isValid())
insilmaril@725
    96
        parentItem = rootItem;
insilmaril@725
    97
    else
insilmaril@725
    98
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
insilmaril@725
    99
insilmaril@725
   100
    return parentItem->childCount();
insilmaril@725
   101
}
insilmaril@725
   102
insilmaril@726
   103
TreeItem* TreeModel::first()
insilmaril@726
   104
{
insilmaril@726
   105
	itCur=NULL;	
insilmaril@726
   106
	itFirst=rootItem;
insilmaril@726
   107
	return rootItem; 
insilmaril@726
   108
}
insilmaril@726
   109
	
insilmaril@726
   110
TreeItem* TreeModel::next()
insilmaril@726
   111
{
insilmaril@726
   112
/*
insilmaril@726
   113
	BranchObj *bo;
insilmaril@726
   114
	BranchObj *lmo;
insilmaril@726
   115
	BranchObj *po=(BranchObj*)parObj;
insilmaril@726
   116
insilmaril@726
   117
	if (branch.isEmpty())
insilmaril@726
   118
		bo=NULL;
insilmaril@726
   119
	else
insilmaril@726
   120
		bo=branch.first();
insilmaril@726
   121
insilmaril@726
   122
	if (!itCur)
insilmaril@726
   123
	{
insilmaril@726
   124
		// no itCur, we are just beginning
insilmaril@726
   125
		if (bo) 
insilmaril@726
   126
		{
insilmaril@726
   127
			// we have children, return first one
insilmaril@726
   128
			itCur=this;
insilmaril@726
   129
			return bo;
insilmaril@726
   130
		}	
insilmaril@726
   131
		else
insilmaril@726
   132
		{
insilmaril@726
   133
			// No children, so there is no next
insilmaril@726
   134
			itCur=this;
insilmaril@726
   135
			return NULL;
insilmaril@726
   136
		}	
insilmaril@726
   137
	}
insilmaril@726
   138
insilmaril@726
   139
	// We have an itCur
insilmaril@726
   140
	if (itCur==po)
insilmaril@726
   141
	{	// We come from parent
insilmaril@726
   142
		if (bo)
insilmaril@726
   143
		{
insilmaril@726
   144
			// there are children, go there
insilmaril@726
   145
			itCur=this;
insilmaril@726
   146
			return bo;
insilmaril@726
   147
		}	
insilmaril@726
   148
		else
insilmaril@726
   149
		{	// no children, try to go up again
insilmaril@726
   150
			if (po)
insilmaril@726
   151
			{
insilmaril@726
   152
				// go back to parent and try to find next there
insilmaril@726
   153
				itCur=this;
insilmaril@726
   154
				lmo=po->next();
insilmaril@726
   155
				itCur=this;
insilmaril@726
   156
				return lmo;
insilmaril@726
   157
insilmaril@726
   158
			}	
insilmaril@726
   159
			else
insilmaril@726
   160
			{
insilmaril@726
   161
				// can't go up, I am mapCenter, no next
insilmaril@726
   162
				itCur=NULL;
insilmaril@726
   163
				return NULL;
insilmaril@726
   164
			}	
insilmaril@726
   165
		}
insilmaril@726
   166
	}
insilmaril@726
   167
insilmaril@726
   168
	// We don't come from parent, but from brother or children
insilmaril@726
   169
insilmaril@726
   170
	// Try to find last child, where we came from, in my own children
insilmaril@726
   171
	bool searching=true;
insilmaril@726
   172
	int i=0;
insilmaril@726
   173
	while (i<branch.size())
insilmaril@726
   174
	{
insilmaril@726
   175
		// Try to find itCur in my own children
insilmaril@726
   176
		if (itCur==branch.at(i))
insilmaril@726
   177
		{
insilmaril@726
   178
			// ok, we come from my own children
insilmaril@726
   179
			if (i<branch.size()-1)
insilmaril@726
   180
				bo=branch.at(i+1);
insilmaril@726
   181
			 else
insilmaril@726
   182
				bo=NULL;
insilmaril@726
   183
			searching=false;
insilmaril@726
   184
			i=branch.size();
insilmaril@726
   185
		} 	
insilmaril@726
   186
		++i;	
insilmaril@726
   187
	}
insilmaril@726
   188
	if (!searching)
insilmaril@726
   189
	{	// found itCur in my children
insilmaril@726
   190
		if (bo)
insilmaril@726
   191
		{
insilmaril@726
   192
			// found a brother of lastLMO 
insilmaril@726
   193
			itCur=this;
insilmaril@726
   194
			return bo;
insilmaril@726
   195
		}	
insilmaril@726
   196
		else
insilmaril@726
   197
		{
insilmaril@726
   198
			if (po)
insilmaril@726
   199
			{
insilmaril@726
   200
				if (this==itFirst) return NULL;	// Stop at starting point
insilmaril@726
   201
				// go up
insilmaril@726
   202
				itCur=this;
insilmaril@726
   203
				lmo=po->next();
insilmaril@726
   204
				itCur=this;
insilmaril@726
   205
				return lmo;
insilmaril@726
   206
			}
insilmaril@726
   207
			else
insilmaril@726
   208
			{
insilmaril@726
   209
				// can't go up, I am mapCenter
insilmaril@726
   210
				itCur=NULL;
insilmaril@726
   211
				return NULL;
insilmaril@726
   212
			}	
insilmaril@726
   213
		}
insilmaril@726
   214
	}
insilmaril@726
   215
insilmaril@726
   216
	// couldn't find last child, it must be a nephew of mine
insilmaril@726
   217
	if (branch.size()>0)
insilmaril@726
   218
	{
insilmaril@726
   219
		// proceed with my first child
insilmaril@726
   220
		itCur=this;	
insilmaril@726
   221
		return branch.first();
insilmaril@726
   222
	}	
insilmaril@726
   223
	else
insilmaril@726
   224
	{
insilmaril@726
   225
		// or go back to my parents
insilmaril@726
   226
		if (po)
insilmaril@726
   227
		{
insilmaril@726
   228
			// go up
insilmaril@726
   229
			itCur=this;
insilmaril@726
   230
			lmo=po->next();
insilmaril@726
   231
			itCur=this;
insilmaril@726
   232
			return lmo;
insilmaril@726
   233
		}	
insilmaril@726
   234
		else
insilmaril@726
   235
		{
insilmaril@726
   236
			// can't go up, I am mapCenter
insilmaril@726
   237
			itCur=NULL;
insilmaril@726
   238
			return NULL;
insilmaril@726
   239
		}	
insilmaril@726
   240
	}	
insilmaril@726
   241
	*/
insilmaril@726
   242
}
insilmaril@726
   243
insilmaril@727
   244
#include <iostream>
insilmaril@727
   245
bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
insilmaril@727
   246
{
insilmaril@727
   247
	int last=row+count-1;
insilmaril@727
   248
	beginRemoveRows (parent,row,last);
insilmaril@727
   249
insilmaril@727
   250
    TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
insilmaril@727
   251
    TreeItem *ti;
insilmaril@727
   252
	for (int i=row; i<=last; i++)
insilmaril@727
   253
	{
insilmaril@727
   254
		std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
insilmaril@727
   255
		pi->removeChild (row);
insilmaril@727
   256
	}
insilmaril@727
   257
insilmaril@727
   258
	endRemoveRows ();
insilmaril@727
   259
	
insilmaril@727
   260
}
insilmaril@727
   261
insilmaril@726
   262
QModelIndex TreeModel::index (TreeItem* ti)
insilmaril@726
   263
{
insilmaril@726
   264
	return createIndex (ti->row(),ti->column(),ti);
insilmaril@726
   265
}
insilmaril@726
   266