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