treemodel.cpp
author insilmaril
Mon, 06 Oct 2008 11:10:20 +0000
changeset 726 7f43b93242aa
parent 725 7ea31701156e
child 727 96402b172173
permissions -rw-r--r--
Various fixes, also from 1.12. branch
     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  //   setupModelData(rootItem);
    16 }
    17 
    18 TreeModel::~TreeModel()
    19 {
    20     delete rootItem;
    21 }
    22 
    23 int TreeModel::columnCount(const QModelIndex &parent) const
    24 {
    25     if (parent.isValid())
    26         return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    27     else
    28         return rootItem->columnCount();
    29 }
    30 
    31 QVariant TreeModel::data(const QModelIndex &index, int role) const
    32 {
    33     if (!index.isValid())
    34         return QVariant();
    35 
    36     if (role != Qt::DisplayRole)
    37         return QVariant();
    38 
    39     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    40 
    41     return item->data(index.column());
    42 }
    43 
    44 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    45 {
    46     if (!index.isValid())
    47         return Qt::ItemIsEnabled;
    48 
    49     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    50 }
    51 
    52 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
    53                                int role) const
    54 {
    55     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    56         return rootItem->data(section);
    57 
    58     return QVariant();
    59 }
    60 
    61 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    62             const
    63 {
    64     TreeItem *parentItem;
    65 
    66     if (!parent.isValid())
    67         parentItem = rootItem;
    68     else
    69         parentItem = static_cast<TreeItem*>(parent.internalPointer());
    70 
    71     TreeItem *childItem = parentItem->child(row);
    72     if (childItem)
    73         return createIndex(row, column, childItem);
    74     else
    75         return QModelIndex();
    76 }
    77 
    78 QModelIndex TreeModel::parent(const QModelIndex &index) const
    79 {
    80     if (!index.isValid())
    81         return QModelIndex();
    82 
    83     TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    84     TreeItem *parentItem = childItem->parent();
    85 
    86     if (parentItem == rootItem)
    87         return QModelIndex();
    88 
    89     return createIndex(parentItem->row(), 0, parentItem);
    90 }
    91 
    92 int TreeModel::rowCount(const QModelIndex &parent) const
    93 {
    94     TreeItem *parentItem;
    95 
    96     if (!parent.isValid())
    97         parentItem = rootItem;
    98     else
    99         parentItem = static_cast<TreeItem*>(parent.internalPointer());
   100 
   101     return parentItem->childCount();
   102 }
   103 
   104 TreeItem* TreeModel::first()
   105 {
   106 	itCur=NULL;	
   107 	itFirst=rootItem;
   108 	return rootItem; 
   109 }
   110 	
   111 TreeItem* TreeModel::next()
   112 {
   113 /*
   114 	BranchObj *bo;
   115 	BranchObj *lmo;
   116 	BranchObj *po=(BranchObj*)parObj;
   117 
   118 	if (branch.isEmpty())
   119 		bo=NULL;
   120 	else
   121 		bo=branch.first();
   122 
   123 	if (!itCur)
   124 	{
   125 		// no itCur, we are just beginning
   126 		if (bo) 
   127 		{
   128 			// we have children, return first one
   129 			itCur=this;
   130 			return bo;
   131 		}	
   132 		else
   133 		{
   134 			// No children, so there is no next
   135 			itCur=this;
   136 			return NULL;
   137 		}	
   138 	}
   139 
   140 	// We have an itCur
   141 	if (itCur==po)
   142 	{	// We come from parent
   143 		if (bo)
   144 		{
   145 			// there are children, go there
   146 			itCur=this;
   147 			return bo;
   148 		}	
   149 		else
   150 		{	// no children, try to go up again
   151 			if (po)
   152 			{
   153 				// go back to parent and try to find next there
   154 				itCur=this;
   155 				lmo=po->next();
   156 				itCur=this;
   157 				return lmo;
   158 
   159 			}	
   160 			else
   161 			{
   162 				// can't go up, I am mapCenter, no next
   163 				itCur=NULL;
   164 				return NULL;
   165 			}	
   166 		}
   167 	}
   168 
   169 	// We don't come from parent, but from brother or children
   170 
   171 	// Try to find last child, where we came from, in my own children
   172 	bool searching=true;
   173 	int i=0;
   174 	while (i<branch.size())
   175 	{
   176 		// Try to find itCur in my own children
   177 		if (itCur==branch.at(i))
   178 		{
   179 			// ok, we come from my own children
   180 			if (i<branch.size()-1)
   181 				bo=branch.at(i+1);
   182 			 else
   183 				bo=NULL;
   184 			searching=false;
   185 			i=branch.size();
   186 		} 	
   187 		++i;	
   188 	}
   189 	if (!searching)
   190 	{	// found itCur in my children
   191 		if (bo)
   192 		{
   193 			// found a brother of lastLMO 
   194 			itCur=this;
   195 			return bo;
   196 		}	
   197 		else
   198 		{
   199 			if (po)
   200 			{
   201 				if (this==itFirst) return NULL;	// Stop at starting point
   202 				// go up
   203 				itCur=this;
   204 				lmo=po->next();
   205 				itCur=this;
   206 				return lmo;
   207 			}
   208 			else
   209 			{
   210 				// can't go up, I am mapCenter
   211 				itCur=NULL;
   212 				return NULL;
   213 			}	
   214 		}
   215 	}
   216 
   217 	// couldn't find last child, it must be a nephew of mine
   218 	if (branch.size()>0)
   219 	{
   220 		// proceed with my first child
   221 		itCur=this;	
   222 		return branch.first();
   223 	}	
   224 	else
   225 	{
   226 		// or go back to my parents
   227 		if (po)
   228 		{
   229 			// go up
   230 			itCur=this;
   231 			lmo=po->next();
   232 			itCur=this;
   233 			return lmo;
   234 		}	
   235 		else
   236 		{
   237 			// can't go up, I am mapCenter
   238 			itCur=NULL;
   239 			return NULL;
   240 		}	
   241 	}	
   242 	*/
   243 }
   244 
   245 QModelIndex TreeModel::index (TreeItem* ti)
   246 {
   247 	return createIndex (ti->row(),ti->column(),ti);
   248 }
   249 
   250 TreeItem* TreeModel::LMOToItem (LinkableMapObj *lmo)// FIXME not used so far!!!
   251 {
   252 	TreeItem *ti=rootItem;		
   253 }
   254 
   255 void TreeModel::setupModelData(TreeItem *root)	// FIXME not needed anymore
   256 {
   257 	QList<QVariant> cData;
   258 
   259 	cData << "Center of map" << "MapCenter"<<"Data 1";
   260 	TreeItem *mco=new TreeItem (cData,root);
   261 	root->appendChild (mco);
   262 
   263 	cData.clear();
   264 	cData << "Main A" << "Branch"<<"Data 2";
   265 	TreeItem *bo=new TreeItem (cData,mco);
   266 	mco->appendChild (bo);
   267 	TreeItem *mainA=bo;
   268 
   269 	cData.clear();
   270 	cData << "Sub a" << "Branch"<<"Data";
   271 	bo=new TreeItem (cData,mainA);
   272 	mainA->appendChild (bo);
   273 
   274 	cData.clear();
   275 	cData << "Sub b" << "Branch"<<"Data";
   276 	bo=new TreeItem (cData,mainA);
   277 	mainA->appendChild (bo);
   278 
   279 	cData.clear();
   280 	cData << "Main B"<<"Branch" <<"Data 3";
   281 	mco->appendChild(new TreeItem(cData, mco));
   282 }