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