treemodel.cpp
author insilmaril
Thu, 19 Mar 2009 11:45:28 +0000
changeset 743 5b635536ca7d
parent 741 1b4d1ea6ea8c
child 745 2d4cc445a86a
permissions -rw-r--r--
ProgressBar during load and more fixes
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@725
    30
    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
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@725
    60
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
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@725
    74
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
insilmaril@725
    75
    TreeItem *parentItem = childItem->parent();
insilmaril@725
    76
insilmaril@725
    77
    if (parentItem == rootItem)
insilmaril@725
    78
        return QModelIndex();
insilmaril@725
    79
insilmaril@725
    80
    return createIndex(parentItem->row(), 0, parentItem);
insilmaril@725
    81
}
insilmaril@725
    82
insilmaril@725
    83
int TreeModel::rowCount(const QModelIndex &parent) const
insilmaril@725
    84
{
insilmaril@725
    85
    TreeItem *parentItem;
insilmaril@725
    86
insilmaril@725
    87
    if (!parent.isValid())
insilmaril@725
    88
        parentItem = rootItem;
insilmaril@725
    89
    else
insilmaril@725
    90
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
insilmaril@725
    91
insilmaril@725
    92
    return parentItem->childCount();
insilmaril@725
    93
}
insilmaril@725
    94
insilmaril@741
    95
int TreeModel::columnCount(const QModelIndex &parent) const
insilmaril@726
    96
{
insilmaril@741
    97
    if (parent.isValid())
insilmaril@741
    98
        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
insilmaril@741
    99
    else
insilmaril@741
   100
        return rootItem->columnCount();
insilmaril@726
   101
}
insilmaril@726
   102
insilmaril@741
   103
TreeItem* TreeModel::next(TreeItem* &current, TreeItem* &previous, int &d0)
insilmaril@741
   104
{
insilmaril@741
   105
	// Walk through map beginning at current with previous==0
insilmaril@741
   106
	// Start at root, if current==NULL
insilmaril@741
   107
	if (!current) current=rootItem;
insilmaril@741
   108
insilmaril@741
   109
	// Are we just beginning to walk the map?
insilmaril@741
   110
	if (!previous)
insilmaril@741
   111
	{
insilmaril@741
   112
		previous=current;
insilmaril@741
   113
		d0=current->depth();
insilmaril@741
   114
		current=current->getFirstBranch();
insilmaril@741
   115
		return current;
insilmaril@741
   116
	}
insilmaril@741
   117
insilmaril@741
   118
	//std::cout << " cur="<<current->getHeading().toStdString();
insilmaril@741
   119
	//std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
insilmaril@741
   120
insilmaril@741
   121
	// Going up or down (deeper)?
insilmaril@741
   122
	if (current->depth() > previous->depth() )
insilmaril@741
   123
	{	
insilmaril@741
   124
		// Coming from above
insilmaril@741
   125
		// Trying  to go down deeper
insilmaril@741
   126
		if (current->branchCount() >0 )
insilmaril@741
   127
		{
insilmaril@741
   128
			previous=current;
insilmaril@741
   129
			current=current->getFirstBranch();
insilmaril@741
   130
			return current;
insilmaril@741
   131
		}	
insilmaril@741
   132
		// turn around and go up again
insilmaril@741
   133
	}	
insilmaril@741
   134
insilmaril@741
   135
	// Coming from below,
insilmaril@741
   136
	// Trying to go down again to siblings
insilmaril@741
   137
insilmaril@741
   138
	TreeItem *sibling=current->getBranchNum (previous->num()+1);
insilmaril@741
   139
insilmaril@741
   140
	if (sibling)
insilmaril@741
   141
	{	
insilmaril@741
   142
		// Found sibling of previous, go there
insilmaril@741
   143
		previous=current;
insilmaril@741
   144
		current=sibling;
insilmaril@741
   145
		return current;
insilmaril@741
   146
	} 
insilmaril@741
   147
insilmaril@741
   148
	// Go up and try to find siblings of current
insilmaril@741
   149
	previous=current;
insilmaril@741
   150
	current=current->parent();
insilmaril@741
   151
insilmaril@741
   152
	// Check if we still can go somewhere
insilmaril@741
   153
	if (!current) return current;
insilmaril@741
   154
	
insilmaril@741
   155
	while (current && current->depth() < previous->depth() )
insilmaril@741
   156
		next (current,previous,d0);
insilmaril@741
   157
		
insilmaril@741
   158
	return current;
insilmaril@741
   159
}
insilmaril@741
   160
insilmaril@727
   161
bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
insilmaril@727
   162
{
insilmaril@727
   163
	int last=row+count-1;
insilmaril@727
   164
	beginRemoveRows (parent,row,last);
insilmaril@727
   165
insilmaril@727
   166
    TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
insilmaril@727
   167
	for (int i=row; i<=last; i++)
insilmaril@727
   168
	{
insilmaril@727
   169
		std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
insilmaril@727
   170
		pi->removeChild (row);
insilmaril@727
   171
	}
insilmaril@727
   172
insilmaril@727
   173
	endRemoveRows ();
insilmaril@727
   174
	
insilmaril@727
   175
}
insilmaril@727
   176
insilmaril@726
   177
QModelIndex TreeModel::index (TreeItem* ti)
insilmaril@726
   178
{
insilmaril@726
   179
	return createIndex (ti->row(),ti->column(),ti);
insilmaril@726
   180
}
insilmaril@726
   181