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