treemodel.cpp
author insilmaril
Mon, 23 Mar 2009 09:06:51 +0000
changeset 745 2d4cc445a86a
parent 741 1b4d1ea6ea8c
child 746 ee6b0f3a4c2f
permissions -rw-r--r--
still working on insert/remove of rows
     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 = getItem (index);
    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 = getItem (parent);
    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 *ti= getItem (index);
    75     TreeItem *parentItem = ti->parent();
    76 
    77 	//cout << "TreeModel::parent  ti="<<ti<<" "<<ti->getHeading().toStdString()<<"  pi="<<parentItem<<"  "<<endl;
    78     if (parentItem == rootItem)
    79         return QModelIndex();
    80 
    81 	if (!parentItem)
    82 	{
    83 		cout <<"TreeModel::parent      ti=="<<ti<<"  "<<ti->getHeading().toStdString()<<endl;
    84         return QModelIndex();	// FIXME do this to avoid segfault, but why?
    85 	}	
    86 
    87     return createIndex(parentItem->childNumber(), 0, parentItem);
    88 }
    89 
    90 int TreeModel::rowCount(const QModelIndex &parent) const
    91 {
    92     TreeItem *parentItem;
    93 
    94     if (!parent.isValid())
    95         parentItem = rootItem;
    96     else
    97         parentItem = getItem (parent);
    98 
    99     return parentItem->childCount();
   100 }
   101 
   102 int TreeModel::columnCount(const QModelIndex &parent) const
   103 {
   104     if (parent.isValid())
   105         return getItem (parent)->columnCount();
   106     else
   107         return rootItem->columnCount();
   108 }
   109 
   110 TreeItem* TreeModel::next(TreeItem* &current, TreeItem* &previous, int &d0)
   111 {
   112 	// Walk through map beginning at current with previous==0
   113 	// Start at root, if current==NULL
   114 	if (!current) current=rootItem;
   115 
   116 	// Are we just beginning to walk the map?
   117 	if (!previous)
   118 	{
   119 		previous=current;
   120 		d0=current->depth();
   121 		current=current->getFirstBranch();
   122 		return current;
   123 	}
   124 
   125 	//std::cout << " cur="<<current->getHeading().toStdString();
   126 	//std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
   127 
   128 	// Going up or down (deeper)?
   129 	if (current->depth() > previous->depth() )
   130 	{	
   131 		// Coming from above
   132 		// Trying  to go down deeper
   133 		if (current->branchCount() >0 )
   134 		{
   135 			previous=current;
   136 			current=current->getFirstBranch();
   137 			return current;
   138 		}	
   139 		// turn around and go up again
   140 	}	
   141 
   142 	// Coming from below,
   143 	// Trying to go down again to siblings
   144 
   145 	TreeItem *sibling=current->getBranchNum (previous->num()+1);
   146 
   147 	if (sibling)
   148 	{	
   149 		// Found sibling of previous, go there
   150 		previous=current;
   151 		current=sibling;
   152 		return current;
   153 	} 
   154 
   155 	// Go up and try to find siblings of current
   156 	previous=current;
   157 	current=current->parent();
   158 
   159 	// Check if we still can go somewhere
   160 	if (!current) return current;
   161 	
   162 	while (current && current->depth() < previous->depth() )
   163 		next (current,previous,d0);
   164 		
   165 	return current;
   166 }
   167 
   168 bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
   169 {
   170 	std::cout << "TreeModel insertRows()\n";
   171 	int last=row+count-1;
   172 	beginInsertRows (parent,row,last);
   173 
   174 	for (int i=row; i<=last; i++)
   175 	{
   176 		std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
   177 	}
   178 	endInsertRows ();
   179 	return true;
   180 }
   181 
   182 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
   183 {
   184 	int last=row+count-1;
   185 	cout << "TreeModel::removeRows row="<<row<<"  count="<<count<<"  last="<<last<<endl;
   186     TreeItem *pi= getItem (parent);
   187 	cout << "  pi="<<pi<<"  "<<pi->getHeading().toStdString()<<endl;
   188 	cout << "  ok0\n";
   189 	beginRemoveRows (parent,row,last);
   190 	cout << "  ok1\n";
   191 
   192 	for (int i=row; i<=last; i++)
   193 	{
   194 		cout << "TreeModel::removeRows removing i="<<i<<std::endl;
   195 		pi->removeChild (row);
   196 	}
   197 	endRemoveRows ();
   198 	return true;
   199 }
   200 
   201 TreeItem *TreeModel::getItem(const QModelIndex &index) const
   202 {
   203     if (index.isValid()) {
   204 		TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
   205 
   206         if (item) return item;
   207     }
   208     return rootItem;
   209 }
   210 
   211 QModelIndex TreeModel::index (TreeItem* ti)
   212 {
   213 	return createIndex (ti->row(),ti->column(),ti);
   214 }
   215