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