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