treeitem.cpp
author insilmaril
Tue, 26 May 2009 11:23:44 +0000
changeset 773 340bc29da9a0
parent 772 e3f722759c7e
child 776 25e634a7e1dc
permissions -rw-r--r--
Various fixes and simplifications
     1 #include <iostream>
     2 #include <QStringList>
     3 
     4 #include "treeitem.h"
     5 
     6 #include "branchobj.h"
     7 #include "branchitem.h"
     8 #include "vymmodel.h"
     9 
    10 using namespace std;
    11 
    12 extern FlagRow* standardFlagsMaster;
    13 
    14 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent):MapItem()
    15 {
    16 	//cout << "Constructor TreeItem this="<<this<<"  parent="<<parent<<endl;
    17 	init();
    18     parentItem = parent;
    19     itemData = data;
    20 	
    21 	rootItem=this;
    22 	if (parentItem )
    23 		rootItem=parentItem->rootItem;
    24 }
    25 
    26 TreeItem::~TreeItem()
    27 {
    28 	//cout << "Destructor TreeItem "<<getHeadingStd()<<endl;
    29 	TreeItem *ti;
    30 	while (!childItems.isEmpty())
    31 	{
    32 		ti=childItems.takeFirst();
    33 		switch (ti->getType() )
    34 		{
    35 			case TreeItem::MapCenter:
    36 				delete (BranchItem*)ti;
    37 				break;
    38 			case TreeItem::Branch:
    39 				delete (BranchItem*)ti;
    40 				break;
    41 			default:
    42 				delete ti;
    43 				break;
    44 		}
    45 	}	
    46 }
    47 
    48 
    49 void TreeItem::init()
    50 {
    51 	model=NULL;
    52 
    53     parentItem = NULL;
    54     itemData.clear();
    55 
    56 	branchOffset=0;
    57 	branchCounter=0;
    58 	lastSelectedBranchNum=-1;
    59 
    60 	imageOffset=0;
    61 	imageCounter=0;
    62 
    63 	note.setNote(""); 
    64 	// note.setFontHint (textEditor->getFontHintDefault() );	//FIXME-2
    65 	// isNoteInEditor=false;
    66 
    67 	hidden=false;
    68 
    69 	// Reset ID
    70 	objID="";
    71 
    72 	standardFlags.setMasterRow (standardFlagsMaster);
    73 }
    74 
    75 
    76 /*
    77 void TreeItem::copy (OrnamentedObj* other)	//FIXME-2	probably need deep copy of branches and data!
    78 
    79 {
    80 	note.copy (other->note);
    81 	model=other->model;
    82 	hideExport=officially 
    83 	other->hideExport;
    84 }
    85 */
    86 
    87 void TreeItem::setModel (VymModel *m)
    88 {
    89 	model=m;
    90 }
    91 
    92 VymModel* TreeItem::getModel ()
    93 {
    94 	return model;
    95 }
    96 
    97 void TreeItem::appendChild(TreeItem *item)
    98 {
    99     childItems.append(item);
   100 	item->parentItem=this;
   101 	item->rootItem=rootItem;
   102 	item->setModel (model);
   103 
   104 	if (item->type == Branch || item->type ==MapCenter)
   105 	{
   106 		if (branchCounter==0)
   107 			branchOffset=childItems.count()-1;
   108 		branchCounter++;
   109 	}
   110 }
   111 
   112 void TreeItem::removeChild(int row)
   113 {
   114     if (row<0 || row > childItems.size()-1)
   115 		qWarning ("TreeItem::removeChild tried to remove non existing item?!\n");
   116 	else
   117 	{
   118 		if (childItems.at(row)->isBranchLikeType())
   119 			branchCounter--;
   120 		childItems.removeAt (row);
   121 	}
   122 }
   123 
   124 void TreeItem::removeChildBranches()
   125 {
   126 	while ( childItems.size()>0)
   127 	{
   128 		branchCounter--;
   129 		childItems.removeAt (0);
   130 	}
   131 }
   132 
   133 TreeItem *TreeItem::child(int row)
   134 {
   135     return childItems.value(row);
   136 }
   137 
   138 int TreeItem::childCount() const
   139 {
   140     return childItems.count();
   141 }
   142 
   143 int TreeItem::childNumber() const
   144 {
   145     if (parentItem)
   146         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
   147 
   148     return 0;
   149 }
   150 
   151 int TreeItem::columnCount() const
   152 {
   153     return itemData.count();
   154 }
   155 
   156 int TreeItem::branchCount() const
   157 {
   158     return branchCounter;
   159 }
   160 
   161 int TreeItem::imageCount() const
   162 {
   163 	int imageCounter=0;
   164     return imageCounter; // FIXME-1 imageCounter needs to be calculated...
   165 }
   166 
   167 int TreeItem::xlinkCount() const // FIXME-2 check if xlinks are stored in a different way (global to model?)
   168 {
   169 	int xlinkCounter=0;
   170     return xlinkCounter; // FIXME-1 xlinkCounter needs to be calculated...
   171 }
   172 
   173 int TreeItem::row() const
   174 {
   175     if (parentItem)
   176         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
   177 
   178     return 0;
   179 }
   180 
   181 int TreeItem::column() const
   182 {
   183     return 0;
   184 }
   185 
   186 int TreeItem::depth() 
   187 {
   188 	int d=-2;
   189 	TreeItem *ti=this;
   190 	while (ti!=NULL)
   191 	{
   192 		ti=ti->parent();
   193 		d++;
   194 	}
   195 	return d;
   196 }
   197 
   198 TreeItem *TreeItem::parent()
   199 {
   200     return parentItem;
   201 }
   202 
   203 int TreeItem::childNum()
   204 {
   205 	return parentItem->childItems.indexOf (this);
   206 }
   207 
   208 int TreeItem::num()
   209 {
   210 	switch (type)
   211 	{
   212 		case Undefined: return -1;
   213 		case MapCenter: return parentItem->childItems.indexOf (this) - branchOffset;
   214 		case Branch: return parentItem->childItems.indexOf (this) - branchOffset;
   215 		case Image: return parentItem->childItems.indexOf (this) - imageOffset;
   216 		default: return -1;
   217 	}
   218 }
   219 
   220 int TreeItem::num (TreeItem *item)
   221 {
   222 	if (!item) return -1;
   223 	switch (item->getType())
   224 	{
   225 		case Undefined: return -1;
   226 		case MapCenter: return childItems.indexOf (item) - branchOffset;
   227 		case Branch: return childItems.indexOf (item) - branchOffset;
   228 		case Image: return parentItem->childItems.indexOf (item) - imageOffset;
   229 		default: return -1;
   230 	}
   231 }
   232 void TreeItem::setType(const Type t)
   233 {
   234 	type=t;
   235 	itemData[1]=getTypeName();
   236 }
   237 
   238 TreeItem::Type TreeItem::getType()
   239 {
   240 	return type;
   241 }
   242 
   243 bool TreeItem::isBranchLikeType() const
   244 {
   245 	if (type==Branch ||type==MapCenter) return true;
   246 	return false;
   247 }
   248 
   249 QString TreeItem::getTypeName()
   250 {
   251 	switch (type)
   252 	{
   253 		case Undefined: return QString ("Undefined");
   254 		case MapCenter: return QString ("MapCenter");
   255 		case Branch: return QString ("Branch");
   256 		case Image: return QString ("Image");
   257 		default: return QString ("TreeItem::getTypeName no typename defined?!");
   258 	}
   259 }
   260 
   261 
   262 QVariant TreeItem::data(int column) const
   263 {
   264     return itemData.value(column);
   265 }
   266 
   267 void TreeItem::setHeading (const QString s)
   268 {
   269 	itemData[0]=s;
   270 }
   271 
   272 QString TreeItem::getHeading () const
   273 {
   274 	return itemData[0].toString();
   275 }
   276 
   277 std::string TreeItem::getHeadingStd () const
   278 {
   279 	return itemData[0].toString().toStdString();
   280 }
   281 
   282 void TreeItem::setHeadingColor (QColor color)
   283 {
   284 	headingColor=color;
   285 }
   286 
   287 QColor TreeItem::getHeadingColor ()
   288 {
   289 	return headingColor;
   290 }
   291 
   292 void TreeItem::setURL (const QString &u)
   293 {
   294 	url=u;
   295 	if (!url.isEmpty())
   296 		systemFlags.activate ("system-url");
   297 	else
   298 		systemFlags.deactivate ("system-url");
   299 }
   300 
   301 QString TreeItem::getURL ()
   302 {
   303 	return url;
   304 }
   305 
   306 void TreeItem::setVymLink (const QString &vl)
   307 {
   308 	if (!vl.isEmpty())
   309 	{
   310 		// We need the relative (from loading) 
   311 		// or absolute path (from User event)
   312 		// and build the absolute path.
   313 		// Note: If we have relative, use path of
   314 		// current map to build absolute path
   315 		QDir d(vl);
   316 		if (!d.path().startsWith ("/"))
   317 		{
   318 			QString p=model->getDestPath();
   319 			int i=p.findRev("/",-1);
   320 			d.setPath(p.left(i)+"/"+vl);
   321 			d.convertToAbs();
   322 		}
   323 		vymLink=d.path();
   324 		systemFlags.activate("system-vymLink");
   325 	}	
   326 	else	
   327 	{
   328 		systemFlags.deactivate("system-vymLink");
   329 		vymLink.clear();
   330 	}	
   331 }
   332 
   333 QString TreeItem::getVymLink ()
   334 {
   335 	return vymLink;
   336 }
   337 
   338 void TreeItem::setNote(const QString &s)
   339 {
   340 	NoteObj n;
   341 	n.setNote(s);
   342 	setNoteObj (n,false);
   343 }
   344 
   345 void TreeItem::clearNote()
   346 {
   347 	note.clear();
   348 	systemFlags.deactivate ("system-note");
   349 }
   350 
   351 void TreeItem::setNoteObj(const NoteObj &n, bool updateNoteEditor) //FIXME-1 setNoteObj is called for every select or so???
   352 {
   353 	note=n;
   354 	if (!note.isEmpty() && !systemFlags.isActive ("system-note"))
   355 		systemFlags.activate ("system-note");
   356 	if (note.isEmpty() && systemFlags.isActive ("system-note"))
   357 		systemFlags.deactivate ("system-note");
   358 }
   359 
   360 QString TreeItem::getNote()
   361 {
   362 	return note.getNote();
   363 }
   364 
   365 bool TreeItem::hasEmptyNote()
   366 {
   367 	return note.isEmpty();
   368 }
   369 
   370 NoteObj TreeItem::getNoteObj()
   371 {
   372 	return note;
   373 }
   374 
   375 QString TreeItem::getNoteASCII(const QString &indent, const int &width)
   376 {
   377     return note.getNoteASCII(indent,width);
   378 }
   379 
   380 QString TreeItem::getNoteASCII()
   381 {
   382     return note.getNoteASCII();
   383 }
   384 
   385 QString TreeItem::getNoteOpenDoc()
   386 {
   387     return note.getNoteOpenDoc();
   388 }
   389 
   390 void TreeItem::activateStandardFlag (const QString &name)
   391 {
   392 	standardFlags.activate (name);
   393 	model->emitDataHasChanged(this);
   394 }
   395 
   396 void TreeItem::deactivateStandardFlag (const QString &name)
   397 {
   398 	standardFlags.deactivate (name);
   399 	model->emitDataHasChanged(this);
   400 }
   401 
   402 void TreeItem::deactivateAllStandardFlags ()
   403 {
   404 	standardFlags.deactivateAll ();
   405 	model->emitDataHasChanged(this);
   406 }
   407 
   408 void TreeItem::toggleStandardFlag(const QString &name, FlagRow *master)
   409 {
   410 	standardFlags.toggle (name,master);
   411 	model->emitDataHasChanged(this);
   412 }
   413 
   414 bool TreeItem::isActiveStandardFlag (const QString &name)
   415 {
   416 	return standardFlags.isActive (name);
   417 }
   418 
   419 QStringList TreeItem::activeStandardFlagNames ()
   420 {
   421 	return standardFlags.activeFlagNames();
   422 }
   423 
   424 FlagRow* TreeItem::getStandardFlagRow()
   425 {
   426 	return &standardFlags;
   427 }
   428 
   429 /*
   430 void TreeItem::updateToolBar()
   431 {
   432 	standardFlags.updateToolBar();
   433 }
   434 */
   435 QStringList TreeItem::activeSystemFlagNames ()	//FIXME-1 missing: scrolled-tmp,hideInExport
   436 {
   437 	return systemFlags.activeFlagNames();
   438 }
   439 
   440 bool TreeItem::canMoveDown()
   441 {
   442 	switch (type)
   443 	{
   444 		case Undefined: return false;
   445 		case MapCenter: 
   446 		case Branch: 
   447 			if (!parentItem) return false;
   448 			if (parentItem->num (this) < parentItem->branchCount()-1)
   449 				return true;
   450 			else
   451 				return false;
   452 			break;	
   453 		case Image: return false;
   454 		default: return false;
   455 	}
   456 }
   457 
   458 bool TreeItem::canMoveUp()
   459 {
   460 	switch (type)
   461 	{
   462 		case Undefined: return false;
   463 		case MapCenter: 
   464 		case Branch: 
   465 			if (!parentItem) return false;
   466 			if (parentItem->num (this) > 0)
   467 				return true;
   468 			else
   469 				return false;
   470 			break;	
   471 		case Image: return false;
   472 		default: return false;
   473 	}
   474 }
   475 
   476 void TreeItem::setID (const QString &s)
   477 {
   478 	objID=s;
   479 }
   480 
   481 QString TreeItem::getID()
   482 {
   483 	return objID;
   484 }
   485 
   486 
   487 TreeItem* TreeItem::getChildNum(const int &n)
   488 {
   489 	if (n>=0 && n<childItems.count() )
   490 		return childItems.at(n);
   491 	else
   492 		return NULL;
   493 }
   494 
   495 BranchItem* TreeItem::getFirstBranch()
   496 {
   497 	if (branchCounter>0)
   498 		return getBranchNum (branchOffset);
   499 	else
   500 		return NULL;
   501 }
   502 
   503 BranchItem* TreeItem::getLastBranch()
   504 {
   505 	if (branchCounter>0)
   506 		return getBranchNum (branchOffset + branchCounter-1);
   507 	else
   508 		return NULL;
   509 }
   510 
   511 BranchItem* TreeItem::getNextBranch(BranchItem *currentBranch)
   512 {
   513 	if (!currentBranch) return NULL;
   514 	int n=num (currentBranch)+1;
   515 	if (n<branchCounter)
   516 		return getBranchNum (branchOffset + n);
   517 	else
   518 		return NULL;
   519 }
   520 
   521 
   522 BranchItem* TreeItem::getBranchNum(const int &n)
   523 {
   524 	if (branchCounter>0)
   525 		return (BranchItem*)getChildNum (branchOffset + n);
   526 	else
   527 		return NULL;
   528 }
   529 
   530 BranchObj* TreeItem::getBranchObjNum(const int &n)
   531 {
   532 	if (branchCounter>0)
   533 	{
   534 		return (BranchObj*)(getChildNum (branchOffset+n)->lmo);
   535 	} else
   536 		return NULL;
   537 }
   538 
   539 void TreeItem::setLastSelectedBranch()
   540 {
   541 	if (parentItem)
   542 		parentItem->lastSelectedBranchNum=parentItem->childItems.indexOf(this);
   543 }
   544 
   545 void TreeItem::setLastSelectedBranch(int i)
   546 {
   547 		lastSelectedBranchNum=i;
   548 }
   549 
   550 TreeItem* TreeItem::getLastSelectedBranch()
   551 {
   552 	return getBranchNum (lastSelectedBranchNum);
   553 }
   554 
   555 
   556 void TreeItem::setHideTmp (HideTmpMode mode)
   557 {
   558 	if (isBranchLikeType() )
   559 		((BranchItem*)this)->updateVisibility();
   560 		/*
   561 	if (type==Image)
   562 		//updateVisibility();
   563 	*/
   564 
   565 /*
   566 
   567 		if (mode==HideExport && (hideExport || hasHiddenExportParent() ) )
   568 		{
   569 			// Hide stuff according to hideExport flag and parents
   570 			//setVisibility (false);
   571 			updateVisibility();
   572 			//FIXME-2 hidden=true;
   573 		}else
   574 		{
   575 			// Do not hide, but still take care of scrolled status
   576 			
   577 
   578 			XXXXXXXX treeItem should be THIS
   579 
   580 			move visible to TreeItem???
   581 
   582 			BranchObj now has updateContents
   583 
   584 			maybe also start "bool TreeItem::branchlikeType"
   585 
   586 
   587 
   588 			if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem))
   589 				setVisibility (false);
   590 			else
   591 				setVisibility (true);
   592 			//FIXME-2 hidden=false;
   593 		}	
   594 
   595 */
   596 		// And take care of my children
   597 		for (int i=0; i<branchCount(); ++i)
   598 			getBranchNum(i)->setHideTmp (mode);
   599 }
   600 
   601 bool TreeItem::hasHiddenExportParent()
   602 {
   603 	// Calls parents recursivly to
   604 	// find out, if we or parents are temp. hidden
   605 
   606 	if (hidden || hideExport) return true;
   607 
   608 	if (parentItem) 
   609 		return parentItem->hasHiddenExportParent();
   610 	else
   611 		return false;
   612 }
   613 
   614 
   615 void TreeItem::setHideInExport(bool b) 
   616 {
   617 	if (isBranchLikeType() )
   618 	{
   619 		hideExport=b;
   620 		/* FIXME-1 call setVis and updateContentsSize...
   621 		if (b)
   622 			systemFlags->activate("hideInExport");
   623 		else	
   624 			systemFlags->deactivate("hideInExport");
   625 		calcBBoxSize();
   626 		positionBBox();
   627 		requestReposition();	
   628 		*/
   629 	}
   630 }	
   631 
   632 bool TreeItem::hideInExport()
   633 {
   634 	return hideExport;
   635 }	
   636 
   637 bool TreeItem::isHidden()
   638 {
   639 	return hidden;
   640 }	
   641 
   642 
   643