treeitem.cpp
author insilmaril
Tue, 07 Apr 2009 16:15:53 +0000
changeset 753 25a77484ec72
parent 750 ff3b01ce0960
child 754 db0ec4bcf416
permissions -rw-r--r--
more work on screwing up the data structures
     1 #include <iostream>
     2 using namespace std;
     3 
     4 #include <QStringList>
     5 
     6 #include "treeitem.h"
     7 
     8 #include "branchobj.h"
     9 #include "branchitem.h"
    10 #include "vymmodel.h"
    11 
    12 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
    13 {
    14 	model=NULL;
    15 
    16     parentItem = parent;
    17     itemData = data;
    18 	lmo=NULL;
    19 
    20 	branchOffset=0;
    21 	branchCounter=0;
    22 	lastSelectedBranchNum=-1;
    23 
    24 	note.setNote(""); 
    25 	// note.setFontHint (textEditor->getFontHintDefault() );	//FIXME-2
    26 	// isNoteInEditor=false;
    27 
    28 	hidden=false;
    29 }
    30 
    31 TreeItem::~TreeItem()
    32 {
    33     qDeleteAll(childItems);
    34 }
    35 
    36 QString TreeItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset)
    37 {
    38 	qWarning ("TreeItem::saveToDir called directly");
    39 	return QString();
    40 }
    41 
    42 
    43 /*
    44 void TreeItem::copy (OrnamentedObj* other)	//FIXME-2	probably need deep copy of branches and data!
    45 
    46 {
    47 	note.copy (other->note);
    48 	model=other->model;
    49 	hideExport=other->hideExport;
    50 }
    51 */
    52 
    53 void TreeItem::setModel (VymModel *m)
    54 {
    55 	model=m;
    56 }
    57 
    58 VymModel* TreeItem::getModel ()
    59 {
    60 	return model;
    61 }
    62 
    63 void TreeItem::appendChild(TreeItem *item)
    64 {
    65     childItems.append(item);
    66 	item->setModel (model);
    67 
    68 	if (item->type == Branch || item->type ==MapCenter)
    69 	{
    70 		if (branchCounter==0)
    71 			branchOffset=childItems.count()-1;
    72 		branchCounter++;
    73 	}
    74 }
    75 
    76 void TreeItem::removeChild(int row)
    77 {
    78     if (row<0 || row > childItems.size()-1)
    79 		qWarning ("TreeItem::removeChild tried to remove non existing item?!\n");
    80 	else
    81 	{
    82 		cout << "TI::removeChild this="<<this<<"  row="<<row<<endl;
    83 		delete childItems.takeAt (row);
    84 	}
    85 }
    86 
    87 TreeItem *TreeItem::child(int row)
    88 {
    89     return childItems.value(row);
    90 }
    91 
    92 int TreeItem::childCount() const
    93 {
    94     return childItems.count();
    95 }
    96 
    97 int TreeItem::childNumber() const
    98 {
    99     if (parentItem)
   100         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
   101 
   102     return 0;
   103 }
   104 
   105 int TreeItem::columnCount() const
   106 {
   107     return itemData.count();
   108 }
   109 
   110 int TreeItem::branchCount() const
   111 {
   112     return branchCounter;
   113 }
   114 
   115 int TreeItem::imageCount() const
   116 {
   117 	int imageCounter=0;
   118     return imageCounter; // FIXME-1 imageCounter needs to be calculated...
   119 }
   120 
   121 int TreeItem::xlinkCount() const // FIXME-2 check if xlinks are stored in a different way (global to model?)
   122 {
   123 	int xlinkCounter=0;
   124     return xlinkCounter; // FIXME-1 xlinkCounter needs to be calculated...
   125 }
   126 
   127 int TreeItem::row() const
   128 {
   129     if (parentItem)
   130         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
   131 
   132     return 0;
   133 }
   134 
   135 int TreeItem::column() const
   136 {
   137     return 0;
   138 }
   139 
   140 int TreeItem::depth() 
   141 {
   142 	int d=-1;
   143 	TreeItem *ti=this;
   144 	while (ti!=NULL)
   145 	{
   146 		ti=ti->parent();
   147 		d++;
   148 	}
   149 	return d;
   150 }
   151 
   152 TreeItem *TreeItem::parent()
   153 {
   154     return parentItem;
   155 }
   156 
   157 int TreeItem::num()
   158 {
   159 	switch (type)
   160 	{
   161 		case Undefined: return -1;
   162 		case MapCenter: return parentItem->childItems.indexOf (this) - branchOffset;
   163 		case Branch: return parentItem->childItems.indexOf (this) - branchOffset;
   164 		case Image: return -1; // FIXME-2
   165 		default: return -1;
   166 	}
   167 }
   168 
   169 int TreeItem::num (TreeItem *item)
   170 {
   171 	if (!item) return -1;
   172 	switch (item->getType())
   173 	{
   174 		case Undefined: return -1;
   175 		case MapCenter: return childItems.indexOf (this) - branchOffset;
   176 		case Branch: return childItems.indexOf (this) - branchOffset;
   177 		case Image: return -1; // FIXME-2
   178 		default: return -1;
   179 	}
   180 }
   181 
   182 QVariant TreeItem::data(int column) const
   183 {
   184     return itemData.value(column);
   185 }
   186 
   187 void TreeItem::setHeading (const QString s)
   188 {
   189 	itemData[0]=s;
   190 }
   191 
   192 QString TreeItem::getHeading () const
   193 {
   194 	return itemData[0].toString();
   195 }
   196 
   197 std::string TreeItem::headingStd () const
   198 {
   199 	return itemData[0].toString().toStdString();
   200 }
   201 
   202 void TreeItem::setNote(const QString s)
   203 {
   204 	note.setNote(s);
   205 	// updateNoteFlag();  FIXME-2 needed?
   206 	if (model) 
   207 		model->emitNoteHasChanged (this);
   208 }
   209 
   210 void TreeItem::setNoteObj(const NoteObj &n, bool updateNoteEditor)
   211 {
   212 	note=n;
   213 	// updateNoteFlag();  FIXME-2 needed?
   214 	if (model && updateNoteEditor)	
   215 		model->emitNoteHasChanged (this);
   216 }
   217 
   218 QString TreeItem::getNote()
   219 {
   220 	return note.getNote();
   221 }
   222 
   223 NoteObj TreeItem::getNoteObj()
   224 {
   225 	return note;
   226 }
   227 
   228 QString TreeItem::getNoteASCII(const QString &indent, const int &width)
   229 {
   230     return note.getNoteASCII(indent,width);
   231 }
   232 
   233 QString TreeItem::getNoteASCII()
   234 {
   235     return note.getNoteASCII();
   236 }
   237 
   238 QString TreeItem::getNoteOpenDoc()
   239 {
   240     return note.getNoteOpenDoc();
   241 }
   242 
   243 void TreeItem::setType(const Type t)
   244 {
   245 	type=t;
   246 	itemData[1]=getTypeName();
   247 }
   248 
   249 TreeItem::Type TreeItem::getType()
   250 {
   251 	return type;
   252 }
   253 
   254 bool TreeItem::isBranchLikeType() const
   255 {
   256 	if (type==Branch ||type==MapCenter) return true;
   257 	return false;
   258 }
   259 
   260 QString TreeItem::getTypeName()
   261 {
   262 	switch (type)
   263 	{
   264 		case Undefined: return QString ("Undefined");
   265 		case MapCenter: return QString ("MapCenter");
   266 		case Branch: return QString ("Branch");
   267 		case Image: return QString ("Image");
   268 		default: return QString ("TreeItem::getTypeName no typename defined?!");
   269 	}
   270 }
   271 
   272 TreeItem* TreeItem::getChildNum(const int &n)
   273 {
   274 	if (n>=0 && n<childItems.count() )
   275 		return childItems.at(n);
   276 	else
   277 		return NULL;
   278 }
   279 
   280 BranchItem* TreeItem::getFirstBranch()
   281 {
   282 	if (branchCounter>0)
   283 		return getBranchNum (branchOffset);
   284 	else
   285 		return NULL;
   286 }
   287 
   288 BranchItem* TreeItem::getLastBranch()
   289 {
   290 	if (branchCounter>0)
   291 		return getBranchNum (branchOffset + branchCounter-1);
   292 	else
   293 		return NULL;
   294 }
   295 
   296 
   297 BranchItem* TreeItem::getBranchNum(const int &n)
   298 {
   299 	if (branchCounter>0)
   300 		return (BranchItem*)getChildNum (branchOffset + n);
   301 	else
   302 		return NULL;
   303 }
   304 
   305 BranchObj* TreeItem::getBranchObjNum(const int &n)
   306 {
   307 	if (branchCounter>0)
   308 	{
   309 		return (BranchObj*)(getChildNum (branchOffset+n)->lmo);
   310 	} else
   311 		return NULL;
   312 }
   313 
   314 void TreeItem::setLastSelectedBranch()
   315 {
   316 	if (parentItem)
   317 		parentItem->lastSelectedBranchNum=parentItem->childItems.indexOf(this);
   318 }
   319 
   320 TreeItem* TreeItem::getLastSelectedBranch()
   321 {
   322 	return getBranchNum (lastSelectedBranchNum);
   323 }
   324 
   325 
   326 void TreeItem::setHideTmp (HideTmpMode mode)
   327 {
   328 	if (isBranchLikeType() )
   329 		((BranchItem*)this)->updateVisibility();
   330 		/*
   331 	if (type==Image)
   332 		//updateVisibility();
   333 	*/
   334 
   335 /*
   336 
   337 		if (mode==HideExport && (hideExport || hasHiddenExportParent() ) )
   338 		{
   339 			// Hide stuff according to hideExport flag and parents
   340 			//setVisibility (false);
   341 			updateVisibility();
   342 			//FIXME-2 hidden=true;
   343 		}else
   344 		{
   345 			// Do not hide, but still take care of scrolled status
   346 			
   347 
   348 			XXXXXXXX treeItem should be THIS
   349 
   350 			move visible to TreeItem???
   351 
   352 			BranchObj now has updateContents
   353 
   354 			maybe also start "bool TreeItem::branchlikeType"
   355 
   356 
   357 
   358 			if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem))
   359 				setVisibility (false);
   360 			else
   361 				setVisibility (true);
   362 			//FIXME-2 hidden=false;
   363 		}	
   364 
   365 */
   366 		// And take care of my children
   367 		for (int i=0; i<branchCount(); ++i)
   368 			getBranchNum(i)->setHideTmp (mode);
   369 }
   370 
   371 bool TreeItem::hasHiddenExportParent()
   372 {
   373 	// Calls parents recursivly to
   374 	// find out, if we or parents are temp. hidden
   375 
   376 	if (hidden || hideExport) return true;
   377 
   378 	if (parentItem) 
   379 		return parentItem->hasHiddenExportParent();
   380 	else
   381 		return false;
   382 }
   383 
   384 
   385 void TreeItem::setHideInExport(bool b) 
   386 {
   387 	if (isBranchLikeType() )
   388 	{
   389 		hideExport=b;
   390 		/* FIXME-1 call setVis and updateContentsSize...
   391 		if (b)
   392 			systemFlags->activate("hideInExport");
   393 		else	
   394 			systemFlags->deactivate("hideInExport");
   395 		calcBBoxSize();
   396 		positionBBox();
   397 		requestReposition();	
   398 		*/
   399 	}
   400 }	
   401 
   402 bool TreeItem::hideInExport()
   403 {
   404 	return hideExport;
   405 }	
   406 
   407 bool TreeItem::isHidden()
   408 {
   409 	return hidden;
   410 }	
   411 
   412 
   413 LinkableMapObj* TreeItem::getLMO()	// FIXME-3  VM should be unnecessary in the end
   414 {
   415 	return lmo;
   416 }
   417 
   418 void TreeItem::setLMO(LinkableMapObj *l)
   419 {
   420 	lmo=l;
   421 }
   422