treeitem.cpp
author insilmaril
Mon, 07 Sep 2009 15:36:57 +0000
changeset 791 f1006de05c54
parent 790 133e2ed6b9c5
child 794 d922fb6ea482
permissions -rw-r--r--
Fixed several Model errors using ModelTest
     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 /*
   179 void TreeItem::clear()	// FIXME-2 used in vymmodel addMapInsert, but needs to be used via model and emit layout changes...
   180 {
   181 	cout << "TI::clear\n";
   182 	while ( childItems.size()>0)
   183 	{
   184 		branchCounter--;
   185 		childItems.removeAt (0);
   186 	}
   187 }
   188 */
   189 
   190 TreeItem *TreeItem::child(int row)
   191 {
   192     return childItems.value(row);
   193 }
   194 
   195 int TreeItem::childCount() const
   196 {
   197     return childItems.count();
   198 }
   199 
   200 int TreeItem::childNumber() const
   201 {
   202     if (parentItem)
   203         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
   204 
   205     return 0;
   206 }
   207 
   208 int TreeItem::columnCount() const
   209 {
   210 	return 1;
   211 }
   212 
   213 int TreeItem::branchCount() const
   214 {
   215     return branchCounter;
   216 }
   217 
   218 int TreeItem::imageCount() const
   219 {
   220     return imageCounter; 
   221 }
   222 
   223 int TreeItem::xlinkCount() const
   224 {
   225     return xlinkCounter; 
   226 }
   227 
   228 int TreeItem::attributeCount() const 
   229 {
   230     return attributeCounter; 
   231 }
   232 
   233 int TreeItem::row() const
   234 {
   235     if (parentItem)
   236         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
   237 
   238 	cout << "TI::row() pI=NULL this="<<this<<"  ***************\n";
   239     return 0;
   240 }
   241 
   242 int TreeItem::depth() 
   243 {
   244 	// Rootitem d=-1
   245 	// MapCenter d=0
   246 	int d=-2;
   247 	TreeItem *ti=this;
   248 	while (ti!=NULL)
   249 	{
   250 		ti=ti->parent();
   251 		d++;
   252 	}
   253 	return d;
   254 }
   255 
   256 TreeItem *TreeItem::parent()
   257 {
   258 	//cout << "TI::parent of "<<getHeadingStd()<<"  is "<<parentItem<<endl;
   259     return parentItem;
   260 }
   261 
   262 int TreeItem::childNum()
   263 {
   264 	return parentItem->childItems.indexOf (this);
   265 }
   266 
   267 int TreeItem::num()
   268 {
   269 	if (!parentItem) return -1;
   270 	return parentItem->num (this);
   271 }
   272 
   273 int TreeItem::num (TreeItem *item)
   274 {
   275 	if (!item) return -1;
   276 	if (!childItems.contains(item)) return -1;
   277 	switch (item->getType())
   278 	{
   279 		case MapCenter: return childItems.indexOf (item) - branchOffset;
   280 		case Branch: return childItems.indexOf (item) - branchOffset;
   281 		case Image: return parentItem->childItems.indexOf (item) - imageOffset;
   282 		case Attribute: return parentItem->childItems.indexOf (item) - attributeOffset;
   283 		case XLink: return parentItem->childItems.indexOf (item) - xlinkOffset;
   284 		default: return -1;
   285 	}
   286 }
   287 void TreeItem::setType(const Type t)
   288 {
   289 	type=t;
   290 	itemData[1]=getTypeName();
   291 }
   292 
   293 TreeItem::Type TreeItem::getType()
   294 {
   295 	if (type==Branch && depth()==0) return MapCenter;	//FIXME-3 should not be necesssary
   296 	return type;
   297 }
   298 
   299 bool TreeItem::isBranchLikeType() const
   300 {
   301 	if (type==Branch ||type==MapCenter) return true;
   302 	return false;
   303 }
   304 
   305 QString TreeItem::getTypeName()
   306 {
   307 	switch (type)
   308 	{
   309 		case Undefined: return QString ("Undefined");
   310 		case MapCenter: return QString ("MapCenter");
   311 		case Branch: return QString ("Branch");
   312 		case Image: return QString ("Image");
   313 		case Attribute: return QString ("Attribute");
   314 		case XLink: return QString ("XLink");
   315 		default: return QString ("TreeItem::getTypeName no typename defined?!");
   316 	}
   317 }
   318 
   319 
   320 QVariant TreeItem::data(int column) const
   321 {
   322     return itemData.value(column);
   323 }
   324 
   325 void TreeItem::setHeading (const QString s)
   326 {
   327 	itemData[0]=s;
   328 }
   329 
   330 QString TreeItem::getHeading () const
   331 {
   332 	return itemData[0].toString();
   333 }
   334 
   335 std::string TreeItem::getHeadingStd () const
   336 {
   337 	return itemData[0].toString().toStdString();
   338 }
   339 
   340 void TreeItem::setHeadingColor (QColor color)
   341 {
   342 	headingColor=color;
   343 }
   344 
   345 QColor TreeItem::getHeadingColor ()
   346 {
   347 	return headingColor;
   348 }
   349 
   350 void TreeItem::setURL (const QString &u)
   351 {
   352 	url=u;
   353 	if (!url.isEmpty())
   354 		systemFlags.activate ("system-url");
   355 	else
   356 		systemFlags.deactivate ("system-url");
   357 }
   358 
   359 QString TreeItem::getURL ()
   360 {
   361 	return url;
   362 }
   363 
   364 void TreeItem::setVymLink (const QString &vl)
   365 {
   366 	if (!vl.isEmpty())
   367 	{
   368 		// We need the relative (from loading) 
   369 		// or absolute path (from User event)
   370 		// and build the absolute path.
   371 		// Note: If we have relative, use path of
   372 		// current map to build absolute path
   373 		QDir d(vl);
   374 		if (!d.path().startsWith ("/"))
   375 		{
   376 			QString p=model->getDestPath();
   377 			int i=p.findRev("/",-1);
   378 			d.setPath(p.left(i)+"/"+vl);
   379 			d.convertToAbs();
   380 		}
   381 		vymLink=d.path();
   382 		systemFlags.activate("system-vymLink");
   383 	}	
   384 	else	
   385 	{
   386 		systemFlags.deactivate("system-vymLink");
   387 		vymLink.clear();
   388 	}	
   389 }
   390 
   391 QString TreeItem::getVymLink ()
   392 {
   393 	return vymLink;
   394 }
   395 
   396 void TreeItem::setNote(const QString &s)
   397 {
   398 	NoteObj n;
   399 	n.setNote(s);
   400 	setNoteObj (n);
   401 }
   402 
   403 void TreeItem::clearNote()
   404 {
   405 	note.clear();
   406 	systemFlags.deactivate ("system-note");
   407 }
   408 
   409 void TreeItem::setNoteObj(const NoteObj &n){
   410 	note=n;
   411 	if (!note.isEmpty() && !systemFlags.isActive ("system-note"))
   412 		systemFlags.activate ("system-note");
   413 	if (note.isEmpty() && systemFlags.isActive ("system-note"))
   414 		systemFlags.deactivate ("system-note");
   415 }
   416 
   417 QString TreeItem::getNote()
   418 {
   419 	return note.getNote();
   420 }
   421 
   422 bool TreeItem::hasEmptyNote()
   423 {
   424 	return note.isEmpty();
   425 }
   426 
   427 NoteObj TreeItem::getNoteObj()
   428 {
   429 	return note;
   430 }
   431 
   432 QString TreeItem::getNoteASCII(const QString &indent, const int &width)
   433 {
   434     return note.getNoteASCII(indent,width);
   435 }
   436 
   437 QString TreeItem::getNoteASCII()
   438 {
   439     return note.getNoteASCII();
   440 }
   441 
   442 QString TreeItem::getNoteOpenDoc()
   443 {
   444     return note.getNoteOpenDoc();
   445 }
   446 
   447 void TreeItem::activateStandardFlag (const QString &name)
   448 {
   449 	standardFlags.activate (name);
   450 	model->emitDataHasChanged(this);
   451 }
   452 
   453 void TreeItem::deactivateStandardFlag (const QString &name)
   454 {
   455 	standardFlags.deactivate (name);
   456 	model->emitDataHasChanged(this);
   457 }
   458 
   459 void TreeItem::deactivateAllStandardFlags ()
   460 {
   461 	standardFlags.deactivateAll ();
   462 	model->emitDataHasChanged(this);
   463 }
   464 
   465 void TreeItem::toggleStandardFlag(const QString &name, FlagRow *master)
   466 {
   467 	standardFlags.toggle (name,master);
   468 	model->emitDataHasChanged(this);
   469 }
   470 
   471 bool TreeItem::isActiveStandardFlag (const QString &name)
   472 {
   473 	return standardFlags.isActive (name);
   474 }
   475 
   476 QStringList TreeItem::activeStandardFlagNames ()
   477 {
   478 	return standardFlags.activeFlagNames();
   479 }
   480 
   481 FlagRow* TreeItem::getStandardFlagRow()
   482 {
   483 	return &standardFlags;
   484 }
   485 
   486 QStringList TreeItem::activeSystemFlagNames ()
   487 {
   488 	return systemFlags.activeFlagNames();
   489 }
   490 
   491 bool TreeItem::canMoveDown()
   492 {
   493 	switch (type)
   494 	{
   495 		case Undefined: return false;
   496 		case MapCenter: 
   497 		case Branch: 
   498 			if (!parentItem) return false;
   499 			if (parentItem->num (this) < parentItem->branchCount()-1)
   500 				return true;
   501 			else
   502 				return false;
   503 			break;	
   504 		case Image: return false;
   505 		default: return false;
   506 	}
   507 }
   508 
   509 bool TreeItem::canMoveUp()
   510 {
   511 	switch (type)
   512 	{
   513 		case MapCenter: 
   514 		case Branch: 
   515 			if (!parentItem) return false;
   516 			if (parentItem->num (this) > 0)
   517 				return true;
   518 			else
   519 				return false;
   520 			break;	
   521 		default: return false;
   522 	}
   523 }
   524 
   525 void TreeItem::setID (const QString &s)
   526 {
   527 	objID=s;
   528 }
   529 
   530 QString TreeItem::getID()
   531 {
   532 	return objID;
   533 }
   534 
   535 
   536 TreeItem* TreeItem::getChildNum(const int &n)
   537 {
   538 	if (n>=0 && n<childItems.count() )
   539 		return childItems.at(n);
   540 	else
   541 		return NULL;
   542 }
   543 
   544 BranchItem* TreeItem::getFirstBranch()
   545 {
   546 	if (branchCounter>0)
   547 		return getBranchNum (branchOffset);
   548 	else
   549 		return NULL;
   550 }
   551 
   552 BranchItem* TreeItem::getLastBranch()
   553 {
   554 	if (branchCounter>0)
   555 		return getBranchNum (branchOffset + branchCounter-1);
   556 	else
   557 		return NULL;
   558 }
   559 
   560 BranchItem* TreeItem::getNextBranch(BranchItem *currentBranch)
   561 {
   562 	if (!currentBranch) return NULL;
   563 	int n=num (currentBranch)+1;
   564 	if (n<branchCounter)
   565 		return getBranchNum (branchOffset + n);
   566 	else
   567 		return NULL;
   568 }
   569 
   570 
   571 BranchItem* TreeItem::getBranchNum(const int &n)
   572 {
   573 	if (n>=0 && n<branchCounter)
   574 		return (BranchItem*)getChildNum (branchOffset + n);
   575 	else
   576 		return NULL;
   577 }
   578 
   579 BranchObj* TreeItem::getBranchObjNum(const int &n)
   580 {
   581 	if (n>=0 && n<branchCounter)
   582 	{
   583 		return (BranchObj*)(getBranchNum(n)->getLMO());
   584 	} else
   585 		return NULL;
   586 }
   587 
   588 ImageItem* TreeItem::getImageNum (const int &n)
   589 {
   590 	if (n>=0 && n<imageCounter)
   591 		return (ImageItem*)getChildNum (imageOffset + n);
   592 	else
   593 		return NULL;
   594 }
   595 
   596 FloatImageObj* TreeItem::getImageObjNum (const int &n)	// FIXME-5 what about SVGs later?
   597 {
   598 	if (imageCounter>0 )
   599 		return (FloatImageObj*)(getImageNum(n)->getLMO());
   600 	else
   601 		return NULL;
   602 }
   603 
   604 XLinkItem* TreeItem::getXLinkNum (const int &n)	
   605 {
   606 	if (n>=0 && n<xlinkCounter )
   607 		return (XLinkItem*)getChildNum (xlinkOffset +n);
   608 	else
   609 		return NULL;
   610 }
   611 
   612 
   613 XLinkObj* TreeItem::getXLinkObjNum (const int &n)	
   614 {
   615 	if (xlinkCounter>0 )
   616 	{
   617 		XLinkItem *xli=getXLinkNum (n);
   618 		if (!xli) return NULL;
   619 		if (xli->isBegin() )
   620 			return (XLinkObj*)(xli->getLMO());
   621 		else
   622 		{
   623 			xli=xli->getPartnerXLink();
   624 			if (!xli) return NULL;
   625 			return (XLinkObj*)(xli->getLMO());
   626 		}
   627 	}
   628 	return NULL;
   629 }
   630 
   631 
   632 void TreeItem::setHideTmp (HideTmpMode mode)  //FIXME-2	update visibility in derived objects...
   633 {
   634 		/*
   635 	if (type==Image)
   636 		((ImageItem*)this)->updateVisibility();
   637 		*/
   638 
   639 		if (mode==HideExport && (hideExport || hasHiddenExportParent() ) )
   640 		{
   641 			// Hide stuff according to hideExport flag and parents
   642 	//		if (lmo) lmo->setVisibility (false);
   643 			hidden=true;
   644 		}else
   645 		{
   646 			// Do not hide, but still take care of scrolled status
   647 			hidden=false;
   648 /*
   649 			XXXXXXXX treeItem should be THIS
   650 
   651 			move visible to TreeItem???
   652 
   653 			BranchObj now has updateContents
   654 */
   655 /*
   656 		if (isBranchLikeType() )
   657 			((BranchItem*)this)->updateVisibility();
   658 
   659 			if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem))
   660 				setVisibility (false);
   661 			else
   662 				setVisibility (true);
   663 */			
   664 		}	
   665 
   666 /*
   667 		// And take care of my children
   668 		for (int i=0; i<branchCount(); ++i)
   669 			getBranchNum(i)->setHideTmp (mode);	// FIXME-4 maybe also consider images and other types
   670 */
   671 }
   672 
   673 bool TreeItem::hasHiddenExportParent()
   674 {
   675 	// Calls parents recursivly to
   676 	// find out, if we or parents are temp. hidden
   677 
   678 	if (hidden || hideExport) return true;
   679 
   680 	if (parentItem) 
   681 		return parentItem->hasHiddenExportParent();
   682 	else
   683 		return false;
   684 }
   685 
   686 
   687 void TreeItem::setHideInExport(bool b) 
   688 {
   689 	if (type==Branch || type==Image)
   690 	{
   691 		hideExport=b;
   692 		if (b)
   693 			systemFlags.activate("system-hideInExport");
   694 		else	
   695 			systemFlags.deactivate("system-hideInExport");
   696 	}
   697 }	
   698 
   699 bool TreeItem::hideInExport()
   700 {
   701 	return hideExport;
   702 }	
   703 
   704 void TreeItem::updateVisibility()
   705 {
   706 	// overloaded in derived objects
   707 }	
   708 
   709 bool TreeItem::isHidden()
   710 {
   711 	return hidden;
   712 }	
   713 
   714 QString TreeItem::getGeneralAttr()
   715 {
   716 	QString s;
   717 	if (hideExport)
   718 		 s+=attribut("hideInExport","true");
   719 	if (!url.isEmpty())
   720 		s+=attribut ("url",url);
   721 	if (!vymLink.isEmpty())
   722 		s+=attribut ("vymLink",vymLink);
   723 	return s;	
   724 }
   725 
   726