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