treemodel.cpp
author insilmaril
Tue, 04 Nov 2008 12:09:10 +0000
changeset 728 a8a98a94899a
parent 727 96402b172173
child 740 6dc0a20031f7
permissions -rw-r--r--
Selections work (a little bit)
     1 #include <QtGui>
     2 
     3 #include "treeitem.h"
     4 #include "treemodel.h"
     5 
     6 TreeItem* TreeModel::itFirst=NULL;
     7 TreeItem* TreeModel::itCur=NULL;
     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 int TreeModel::columnCount(const QModelIndex &parent) const
    23 {
    24     if (parent.isValid())
    25         return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    26     else
    27         return rootItem->columnCount();
    28 }
    29 
    30 QVariant TreeModel::data(const QModelIndex &index, int role) const
    31 {
    32     if (!index.isValid())
    33         return QVariant();
    34 
    35     if (role != Qt::DisplayRole)
    36         return QVariant();
    37 
    38     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    39 
    40     return item->data(index.column());
    41 }
    42 
    43 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    44 {
    45     if (!index.isValid())
    46         return Qt::ItemIsEnabled;
    47 
    48     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    49 }
    50 
    51 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
    52                                int role) const
    53 {
    54     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    55         return rootItem->data(section);
    56 
    57     return QVariant();
    58 }
    59 
    60 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    61             const
    62 {
    63     TreeItem *parentItem;
    64 
    65     if (!parent.isValid())
    66         parentItem = rootItem;
    67     else
    68         parentItem = static_cast<TreeItem*>(parent.internalPointer());
    69 
    70     TreeItem *childItem = parentItem->child(row);
    71     if (childItem)
    72         return createIndex(row, column, childItem);
    73     else
    74         return QModelIndex();
    75 }
    76 
    77 QModelIndex TreeModel::parent(const QModelIndex &index) const
    78 {
    79     if (!index.isValid())
    80         return QModelIndex();
    81 
    82     TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    83     TreeItem *parentItem = childItem->parent();
    84 
    85     if (parentItem == rootItem)
    86         return QModelIndex();
    87 
    88     return createIndex(parentItem->row(), 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 = static_cast<TreeItem*>(parent.internalPointer());
    99 
   100     return parentItem->childCount();
   101 }
   102 
   103 TreeItem* TreeModel::first()
   104 {
   105 	itCur=NULL;	
   106 	itFirst=rootItem;
   107 	return rootItem; 
   108 }
   109 	
   110 TreeItem* TreeModel::next()
   111 {
   112 /*
   113 	BranchObj *bo;
   114 	BranchObj *lmo;
   115 	BranchObj *po=(BranchObj*)parObj;
   116 
   117 	if (branch.isEmpty())
   118 		bo=NULL;
   119 	else
   120 		bo=branch.first();
   121 
   122 	if (!itCur)
   123 	{
   124 		// no itCur, we are just beginning
   125 		if (bo) 
   126 		{
   127 			// we have children, return first one
   128 			itCur=this;
   129 			return bo;
   130 		}	
   131 		else
   132 		{
   133 			// No children, so there is no next
   134 			itCur=this;
   135 			return NULL;
   136 		}	
   137 	}
   138 
   139 	// We have an itCur
   140 	if (itCur==po)
   141 	{	// We come from parent
   142 		if (bo)
   143 		{
   144 			// there are children, go there
   145 			itCur=this;
   146 			return bo;
   147 		}	
   148 		else
   149 		{	// no children, try to go up again
   150 			if (po)
   151 			{
   152 				// go back to parent and try to find next there
   153 				itCur=this;
   154 				lmo=po->next();
   155 				itCur=this;
   156 				return lmo;
   157 
   158 			}	
   159 			else
   160 			{
   161 				// can't go up, I am mapCenter, no next
   162 				itCur=NULL;
   163 				return NULL;
   164 			}	
   165 		}
   166 	}
   167 
   168 	// We don't come from parent, but from brother or children
   169 
   170 	// Try to find last child, where we came from, in my own children
   171 	bool searching=true;
   172 	int i=0;
   173 	while (i<branch.size())
   174 	{
   175 		// Try to find itCur in my own children
   176 		if (itCur==branch.at(i))
   177 		{
   178 			// ok, we come from my own children
   179 			if (i<branch.size()-1)
   180 				bo=branch.at(i+1);
   181 			 else
   182 				bo=NULL;
   183 			searching=false;
   184 			i=branch.size();
   185 		} 	
   186 		++i;	
   187 	}
   188 	if (!searching)
   189 	{	// found itCur in my children
   190 		if (bo)
   191 		{
   192 			// found a brother of lastLMO 
   193 			itCur=this;
   194 			return bo;
   195 		}	
   196 		else
   197 		{
   198 			if (po)
   199 			{
   200 				if (this==itFirst) return NULL;	// Stop at starting point
   201 				// go up
   202 				itCur=this;
   203 				lmo=po->next();
   204 				itCur=this;
   205 				return lmo;
   206 			}
   207 			else
   208 			{
   209 				// can't go up, I am mapCenter
   210 				itCur=NULL;
   211 				return NULL;
   212 			}	
   213 		}
   214 	}
   215 
   216 	// couldn't find last child, it must be a nephew of mine
   217 	if (branch.size()>0)
   218 	{
   219 		// proceed with my first child
   220 		itCur=this;	
   221 		return branch.first();
   222 	}	
   223 	else
   224 	{
   225 		// or go back to my parents
   226 		if (po)
   227 		{
   228 			// go up
   229 			itCur=this;
   230 			lmo=po->next();
   231 			itCur=this;
   232 			return lmo;
   233 		}	
   234 		else
   235 		{
   236 			// can't go up, I am mapCenter
   237 			itCur=NULL;
   238 			return NULL;
   239 		}	
   240 	}	
   241 	*/
   242 }
   243 
   244 #include <iostream>
   245 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
   246 {
   247 	int last=row+count-1;
   248 	beginRemoveRows (parent,row,last);
   249 
   250     TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
   251     TreeItem *ti;
   252 	for (int i=row; i<=last; i++)
   253 	{
   254 		std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
   255 		pi->removeChild (row);
   256 	}
   257 
   258 	endRemoveRows ();
   259 	
   260 }
   261 
   262 QModelIndex TreeModel::index (TreeItem* ti)
   263 {
   264 	return createIndex (ti->row(),ti->column(),ti);
   265 }
   266