treemodel.cpp
author insilmaril
Fri, 06 Mar 2009 15:02:58 +0000
changeset 741 1b4d1ea6ea8c
parent 740 6dc0a20031f7
child 745 2d4cc445a86a
permissions -rw-r--r--
Iteration over map works now (again)
     1 #include <QtGui>
     2 
     3 #include <iostream>
     4 using namespace std;
     5 
     6 #include "treeitem.h"
     7 #include "treemodel.h"
     8 
     9 TreeModel::TreeModel(QObject *parent)
    10     : QAbstractItemModel(parent)
    11 {
    12     QList<QVariant> rootData;
    13     rootData << "Heading" << "Type" <<"Note";
    14     rootItem = new TreeItem(rootData);
    15 }
    16 
    17 TreeModel::~TreeModel()
    18 {
    19     delete rootItem;
    20 }
    21 
    22 QVariant TreeModel::data(const QModelIndex &index, int role) const
    23 {
    24     if (!index.isValid())
    25         return QVariant();
    26 
    27     if (role != Qt::DisplayRole)
    28         return QVariant();
    29 
    30     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    31 
    32     return item->data(index.column());
    33 }
    34 
    35 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    36 {
    37     if (!index.isValid())
    38         return Qt::ItemIsEnabled;
    39 
    40     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    41 }
    42 
    43 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
    44                                int role) const
    45 {
    46     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    47         return rootItem->data(section);
    48 
    49     return QVariant();
    50 }
    51 
    52 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    53             const
    54 {
    55     TreeItem *parentItem;
    56 
    57     if (!parent.isValid())
    58         parentItem = rootItem;
    59     else
    60         parentItem = static_cast<TreeItem*>(parent.internalPointer());
    61 
    62     TreeItem *childItem = parentItem->child(row);
    63     if (childItem)
    64         return createIndex(row, column, childItem);
    65     else
    66         return QModelIndex();
    67 }
    68 
    69 QModelIndex TreeModel::parent(const QModelIndex &index) const
    70 {
    71     if (!index.isValid())
    72         return QModelIndex();
    73 
    74     TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    75     TreeItem *parentItem = childItem->parent();
    76 
    77     if (parentItem == rootItem)
    78         return QModelIndex();
    79 
    80     return createIndex(parentItem->row(), 0, parentItem);
    81 }
    82 
    83 int TreeModel::rowCount(const QModelIndex &parent) const
    84 {
    85     TreeItem *parentItem;
    86 
    87     if (!parent.isValid())
    88         parentItem = rootItem;
    89     else
    90         parentItem = static_cast<TreeItem*>(parent.internalPointer());
    91 
    92     return parentItem->childCount();
    93 }
    94 
    95 int TreeModel::columnCount(const QModelIndex &parent) const
    96 {
    97     if (parent.isValid())
    98         return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    99     else
   100         return rootItem->columnCount();
   101 }
   102 
   103 TreeItem* TreeModel::next(TreeItem* &current, TreeItem* &previous, int &d0)
   104 {
   105 	// Walk through map beginning at current with previous==0
   106 	// Start at root, if current==NULL
   107 	if (!current) current=rootItem;
   108 
   109 	// Are we just beginning to walk the map?
   110 	if (!previous)
   111 	{
   112 		previous=current;
   113 		d0=current->depth();
   114 		current=current->getFirstBranch();
   115 		return current;
   116 	}
   117 
   118 	//std::cout << " cur="<<current->getHeading().toStdString();
   119 	//std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
   120 
   121 	// Going up or down (deeper)?
   122 	if (current->depth() > previous->depth() )
   123 	{	
   124 		// Coming from above
   125 		// Trying  to go down deeper
   126 		if (current->branchCount() >0 )
   127 		{
   128 			previous=current;
   129 			current=current->getFirstBranch();
   130 			return current;
   131 		}	
   132 		// turn around and go up again
   133 	}	
   134 
   135 	// Coming from below,
   136 	// Trying to go down again to siblings
   137 
   138 	TreeItem *sibling=current->getBranchNum (previous->num()+1);
   139 
   140 	if (sibling)
   141 	{	
   142 		// Found sibling of previous, go there
   143 		previous=current;
   144 		current=sibling;
   145 		return current;
   146 	} 
   147 
   148 	// Go up and try to find siblings of current
   149 	previous=current;
   150 	current=current->parent();
   151 
   152 	// Check if we still can go somewhere
   153 	if (!current) return current;
   154 	
   155 	while (current && current->depth() < previous->depth() )
   156 		next (current,previous,d0);
   157 		
   158 	return current;
   159 }
   160 
   161 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
   162 {
   163 	int last=row+count-1;
   164 	beginRemoveRows (parent,row,last);
   165 
   166     TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
   167 	for (int i=row; i<=last; i++)
   168 	{
   169 		std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
   170 		pi->removeChild (row);
   171 	}
   172 
   173 	endRemoveRows ();
   174 	
   175 }
   176 
   177 QModelIndex TreeModel::index (TreeItem* ti)
   178 {
   179 	return createIndex (ti->row(),ti->column(),ti);
   180 }
   181