treemodel.cpp
changeset 791 f1006de05c54
parent 790 133e2ed6b9c5
child 793 cac93797c580
     1.1 --- a/treemodel.cpp	Thu Sep 03 08:52:00 2009 +0000
     1.2 +++ b/treemodel.cpp	Mon Sep 07 15:36:57 2009 +0000
     1.3 @@ -15,7 +15,7 @@
     1.4  {
     1.5      QList<QVariant> rootData;
     1.6      rootData << "Heading" << "Type";
     1.7 -    rootItem = new TreeItem(rootData);
     1.8 +    rootItem = new BranchItem(rootData);
     1.9  }
    1.10  
    1.11  TreeModel::~TreeModel()
    1.12 @@ -39,7 +39,7 @@
    1.13  Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    1.14  {
    1.15      if (!index.isValid())
    1.16 -        return Qt::ItemIsEnabled;
    1.17 +        return Qt::NoItemFlags;
    1.18  
    1.19      return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    1.20  }
    1.21 @@ -58,23 +58,26 @@
    1.22  	if (!ti->parent())
    1.23  		return QModelIndex();
    1.24  	else	
    1.25 -		return createIndex (ti->row(),ti->column(),ti);
    1.26 +		return createIndex (ti->row(),0,ti);
    1.27  }
    1.28  
    1.29  QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    1.30              const
    1.31  {
    1.32 +	// Make sure to return invalid index for invalid values (see modeltest)
    1.33 +	if (row<0 || column<0) return QModelIndex();
    1.34 +	if (column!=0) return QModelIndex();
    1.35 +
    1.36      TreeItem *parentItem;
    1.37  
    1.38      if (!parent.isValid())
    1.39 -	{	//FIXME-3 left here for testing only, seems to work now...
    1.40 +	{
    1.41          parentItem = rootItem;
    1.42  		/*
    1.43  		cout << "TM::index()  no parent?! xxx\n";
    1.44  		cout << "   row="<<row<<"  col="<<column<<endl;
    1.45  		cout << "   parent.internal="<< parent.internalPointer()<<endl;
    1.46  		*/
    1.47 -		//return QModelIndex();	//FIXME-3 this line is new (testing)
    1.48  		// Somehow index is requested where parentIndex is invalid.
    1.49  		// what's happening here...?
    1.50  		// Check if Qt examples also return index of rootIem then...
    1.51 @@ -84,6 +87,7 @@
    1.52          parentItem = getItem (parent);
    1.53  
    1.54      TreeItem *childItem = parentItem->child(row);
    1.55 +	//cout << "TM::index  parentItem="<<parentItem<<"  childItem="<<childItem<<"  row="<<row<<" col="<<column<<endl;
    1.56      if (childItem)
    1.57          return createIndex(row, column, childItem);
    1.58      else
    1.59 @@ -106,10 +110,11 @@
    1.60      if (parentItem == rootItem)
    1.61          return QModelIndex();
    1.62  
    1.63 +/*
    1.64  	if (!parentItem)
    1.65          return QModelIndex();	// FIXME-3 do this to avoid segfault, but why?
    1.66  		                        // see also my question on qt-interest in march
    1.67 -
    1.68 +*/
    1.69      return createIndex(parentItem->childNumber(), 0, parentItem);
    1.70  }
    1.71  
    1.72 @@ -127,13 +132,21 @@
    1.73  
    1.74  int TreeModel::columnCount(const QModelIndex &parent) const
    1.75  {
    1.76 +	int c;
    1.77      if (parent.isValid())
    1.78 -        return getItem (parent)->columnCount();
    1.79 +	{
    1.80 +        c= getItem (parent)->columnCount();
    1.81 +		//cout << "TM::colCount  c="<<c<<"  parent="<<getItem (parent)<<endl;	
    1.82 +	}
    1.83      else
    1.84 -        return rootItem->columnCount();
    1.85 +	{
    1.86 +        c= rootItem->columnCount();
    1.87 +		//cout << "TM::colCount  c="<<c<<"  parent=invalid"<<endl;	
    1.88 +	}
    1.89 +	return c;
    1.90  }
    1.91  
    1.92 -BranchItem* TreeModel::next(BranchItem* &current, BranchItem* &previous, BranchItem* start)
    1.93 +BranchItem* TreeModel::next(BranchItem* &current, BranchItem* &previous, BranchItem* start)	// FIXME-3 change this to nextBranch and use "next" for all TIs
    1.94  {
    1.95  /*FIXME-3	cout << "TM::next \n"; 
    1.96  	std::string ch="()"; if (current) ch=current->getHeadingStd();
    1.97 @@ -224,6 +237,8 @@
    1.98  		ti=pi->getChildNum (row);
    1.99  		cout << "   pi="<<pi<<"  ti="<<ti<<endl;
   1.100  		pi->removeChild (row);	// does not delete object!
   1.101 +		delete ti;
   1.102 +		/* FIXME-3
   1.103  		switch (ti->getType()) 
   1.104  		{
   1.105  			case TreeItem::MapCenter: 
   1.106 @@ -245,6 +260,7 @@
   1.107  				delete ti;
   1.108  				break;
   1.109  		}
   1.110 +		*/
   1.111  	}
   1.112  	return true;
   1.113  }
   1.114 @@ -258,7 +274,7 @@
   1.115  //		cout << "   item="<<item<<endl;
   1.116          if (item) return item;
   1.117      }
   1.118 -    return rootItem;
   1.119 +    return NULL;
   1.120  }
   1.121  
   1.122  TreeItem *TreeModel::getRootItem()