treemodel.cpp
author insilmaril
Wed, 09 Sep 2009 12:57:06 +0000
changeset 793 cac93797c580
parent 791 f1006de05c54
child 794 d922fb6ea482
permissions -rw-r--r--
more fixes for collision detection
     1 #include <QtGui>
     2 
     3 #include <iostream>
     4 using namespace std;
     5 
     6 #include "attributeitem.h"
     7 #include "branchitem.h"
     8 #include "imageitem.h"
     9 #include "treeitem.h"
    10 #include "treemodel.h"
    11 #include "xlinkitem.h"
    12 
    13 TreeModel::TreeModel(QObject *parent)
    14     : QAbstractItemModel(parent)
    15 {
    16     QList<QVariant> rootData;
    17     rootData << "Heading" << "Type";
    18     rootItem = new BranchItem(rootData);
    19 }
    20 
    21 TreeModel::~TreeModel()
    22 {
    23     delete rootItem;
    24 }
    25 
    26 QVariant TreeModel::data(const QModelIndex &index, int role) const
    27 {
    28     if (!index.isValid())
    29         return QVariant();
    30 
    31     if (role != Qt::DisplayRole)
    32         return QVariant();
    33 
    34     TreeItem *item = getItem (index);
    35 
    36     return item->data(index.column());
    37 }
    38 
    39 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    40 {
    41     if (!index.isValid())
    42         return Qt::NoItemFlags;
    43 
    44     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    45 }
    46 
    47 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
    48                                int role) const
    49 {
    50     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    51         return rootItem->data(section);
    52 
    53     return QVariant();
    54 }
    55 
    56 QModelIndex TreeModel::index (TreeItem* ti)
    57 {
    58 	if (!ti->parent())
    59 		return QModelIndex();
    60 	else	
    61 		return createIndex (ti->row(),0,ti);
    62 }
    63 
    64 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    65             const
    66 {
    67 	// Make sure to return invalid index for invalid values (see modeltest)
    68 	if (row<0 || column<0) return QModelIndex();
    69 	if (column!=0) return QModelIndex();
    70 
    71     TreeItem *parentItem;
    72 
    73     if (!parent.isValid())
    74 	{
    75         parentItem = rootItem;
    76 		/*
    77 		cout << "TM::index()  no parent?! xxx\n";
    78 		cout << "   row="<<row<<"  col="<<column<<endl;
    79 		cout << "   parent.internal="<< parent.internalPointer()<<endl;
    80 		*/
    81 		// Somehow index is requested where parentIndex is invalid.
    82 		// what's happening here...?
    83 		// Check if Qt examples also return index of rootIem then...
    84 
    85 	}	
    86     else
    87         parentItem = getItem (parent);
    88 
    89     TreeItem *childItem = parentItem->child(row);
    90 	//cout << "TM::index  parentItem="<<parentItem<<"  childItem="<<childItem<<"  row="<<row<<" col="<<column<<endl;
    91     if (childItem)
    92         return createIndex(row, column, childItem);
    93     else
    94         return QModelIndex();
    95 }
    96 
    97 QModelIndex TreeModel::parent(const QModelIndex &index) const
    98 {
    99     if (!index.isValid())
   100         return QModelIndex();
   101 
   102 	//FIXME-3 cout << "TM::parent  ri="<<rootItem<< "  row="<<index.row()<<"  col="<<index.column()<<endl;
   103     TreeItem *ti= getItem (index);
   104 	//cout << "            ti="<<ti<<endl;
   105 	//cout << "               "<<ti->getHeadingStd()<<endl;
   106     TreeItem *parentItem = ti->parent();
   107 	//cout << "            pi="<<parentItem<<endl;
   108 
   109 	//cout << "TreeModel::parent  ti="<<ti<<" "<<ti->getHeading().toStdString()<<"  pi="<<parentItem<<"  "<<endl;
   110     if (parentItem == rootItem)
   111         return QModelIndex();
   112 
   113 /*
   114 	if (!parentItem)
   115         return QModelIndex();	// FIXME-3 do this to avoid segfault, but why?
   116 		                        // see also my question on qt-interest in march
   117 */
   118     return createIndex(parentItem->childNumber(), 0, parentItem);
   119 }
   120 
   121 int TreeModel::rowCount(const QModelIndex &parent) const
   122 {
   123     TreeItem *parentItem;
   124 
   125     if (!parent.isValid())
   126         parentItem = rootItem;
   127     else
   128         parentItem = getItem (parent);
   129 
   130     return parentItem->childCount();
   131 }
   132 
   133 int TreeModel::columnCount(const QModelIndex &parent) const
   134 {
   135 	int c;
   136     if (parent.isValid())
   137 	{
   138         c= getItem (parent)->columnCount();
   139 		//cout << "TM::colCount  c="<<c<<"  parent="<<getItem (parent)<<endl;	
   140 	}
   141     else
   142 	{
   143         c= rootItem->columnCount();
   144 		//cout << "TM::colCount  c="<<c<<"  parent=invalid"<<endl;	
   145 	}
   146 	return c;
   147 }
   148 
   149 BranchItem* TreeModel::next(BranchItem* &current, BranchItem* &previous, BranchItem* start)	// FIXME-3 change this to nextBranch and use "next" for all TIs
   150 {
   151 /*FIXME-3	cout << "TM::next \n"; 
   152 	std::string ch="()"; if (current) ch=current->getHeadingStd();
   153 	std::string ph="()"; if (previous) ph=previous->getHeadingStd();
   154 	cout << "  cur="<<ch << " prev="<<ph<<endl;
   155 */
   156 
   157 	// Walk through map beginning at current with previous==0
   158 	// Start at root, if current==NULL
   159 	if (!current) current=(BranchItem*)rootItem;
   160 
   161 	// Are we just beginning to walk the map?
   162 	if (!previous)
   163 	{
   164 		if (!start) start=current;
   165 		previous=current;
   166 		current=current->getFirstBranch();
   167 		return current;
   168 	}
   169 
   170 	// Going up or down (deeper)?
   171 	if (current->depth() > previous->depth() )
   172 	{	
   173 		// Coming from above
   174 		// Trying  to go down deeper
   175 //		cout << "  trying to go deeper\n";
   176 		if (current->branchCount() >0 )
   177 		{
   178 //			cout << "  yes, going deeper\n";
   179 			previous=current;
   180 			current=current->getFirstBranch();
   181 			return current;
   182 		}	
   183 		// turn around and go up again
   184 //		cout << "  sorry, turn around\n";
   185 		BranchItem *bi=current;
   186 		current=previous;
   187 		previous=bi;
   188 	}	
   189 
   190 /*
   191 	cout << "  coming from below\n";
   192 	ch="()"; if (current) ch=current->getHeadingStd();
   193 	ph="()"; if (previous) ph=previous->getHeadingStd();
   194 	cout << "  cur="<<ch << " prev="<<ph<<endl;
   195 */	
   196 	// Coming from below
   197 	// Trying to go down again to siblings
   198 
   199 	BranchItem *sibling=current->getBranchNum (previous->num()+1);
   200 //	cout <<"    prev->num()="<<previous->num()<<endl;
   201 
   202 	if (sibling)
   203 	{	
   204 		// Found sibling of previous, go there
   205 //		cout << "  sib=cur="<<sibling->getHeadingStd()<<endl;
   206 		previous=current;
   207 		current=sibling;
   208 		return current;
   209 	} 
   210 
   211 	// If we only needed to go through subtree, we are done now
   212 	if (start==current) return NULL;
   213 
   214 	// Go up and try to find siblings of current
   215 //	cout <<"  going up again...\n";
   216 	previous=current;
   217 	current=(BranchItem*)current->parent();
   218 
   219 	// Check if we still can go somewhere
   220 	if (!current) return current;
   221 	
   222 	while (current && current->depth() < previous->depth() )
   223 		current=next (current,previous,start);
   224 		
   225 	return current;
   226 }
   227 
   228 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
   229 {
   230 	int last=row+count-1;
   231     TreeItem *pi= getItem (parent);
   232 	TreeItem *ti;
   233 
   234 	cout << "TM::removeRows  row="<<row<<"  count="<<count<<endl;
   235 	for (int i=row; i<=last; i++)
   236 	{
   237 		ti=pi->getChildNum (row);
   238 		cout << "   pi="<<pi<<"  ti="<<ti<<endl;
   239 		pi->removeChild (row);	// does not delete object!
   240 		delete ti;
   241 		/* FIXME-3
   242 		switch (ti->getType()) 
   243 		{
   244 			case TreeItem::MapCenter: 
   245 				delete (BranchItem*)ti; 
   246 				break;
   247 			case TreeItem::Branch:
   248 				delete (BranchItem*)ti; 
   249 				break;
   250 			case TreeItem::Image:
   251 				delete (ImageItem*)ti; 
   252 				break;
   253 			case TreeItem::Attribute:
   254 				delete (AttributeItem*)ti; 
   255 				break;
   256 			case TreeItem::XLink:
   257 				delete (XLinkItem*)ti; 
   258 				break;
   259 			default:
   260 				delete ti;
   261 				break;
   262 		}
   263 		*/
   264 	}
   265 	return true;
   266 }
   267 
   268 TreeItem *TreeModel::getItem(const QModelIndex &index) const
   269 {
   270 //FIXME-3	cout << "TM::getItem  "<<index.internalPointer()<<endl;
   271     if (index.isValid()) {
   272 		TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
   273 
   274 //		cout << "   item="<<item<<endl;
   275         if (item) return item;
   276     }
   277     return NULL;
   278 }
   279 
   280 BranchItem *TreeModel::getRootItem()
   281 {
   282 	return rootItem;
   283 }
   284