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