treeitem.cpp
author insilmaril
Thu, 07 May 2009 08:48:53 +0000
changeset 766 7a71a914afdb
parent 763 8c028a5d9083
child 767 6d2b32f305f9
permissions -rw-r--r--
Started to reanimate flags
     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::setNote(const QString s)
   282 {
   283 	note.setNote(s);
   284 	// updateNoteFlag();  FIXME-2 needed?
   285 	if (model) 
   286 		model->emitNoteHasChanged (this);
   287 }
   288 
   289 void TreeItem::setNoteObj(const NoteObj &n, bool updateNoteEditor)
   290 {
   291 	note=n;
   292 	// updateNoteFlag();  FIXME-2 needed?
   293 	if (model && updateNoteEditor)	
   294 		model->emitNoteHasChanged (this);
   295 }
   296 
   297 QString TreeItem::getNote()
   298 {
   299 	return note.getNote();
   300 }
   301 
   302 NoteObj TreeItem::getNoteObj()
   303 {
   304 	return note;
   305 }
   306 
   307 QString TreeItem::getNoteASCII(const QString &indent, const int &width)
   308 {
   309     return note.getNoteASCII(indent,width);
   310 }
   311 
   312 QString TreeItem::getNoteASCII()
   313 {
   314     return note.getNoteASCII();
   315 }
   316 
   317 QString TreeItem::getNoteOpenDoc()
   318 {
   319     return note.getNoteOpenDoc();
   320 }
   321 
   322 void TreeItem::activateStandardFlag (const QString &name)
   323 {
   324 	cout << "TI::activateStandardFlag "<<name.toStdString()<<" for item "<<getHeadingStd()<<endl;
   325 	cout << "   lmo="<<lmo<< "  "<<isBranchLikeType()<<endl;
   326 	standardFlags.activate (name);
   327 	if (lmo &&isBranchLikeType() ) 
   328 		((BranchObj*)lmo)->activateStandardFlag (name);
   329 }
   330 
   331 void TreeItem::deactivateStandardFlag (const QString &name)
   332 {
   333 	cout << "TI::deactivateStandardFlag "<<name.toStdString()<<" for item "<<getHeadingStd()<<endl;
   334 	standardFlags.deactivate (name);
   335 	if (lmo &&isBranchLikeType() ) 
   336 		((BranchObj*)lmo)->deactivateStandardFlag (name);
   337 }
   338 
   339 void TreeItem::toggleStandardFlag(const QString &name)
   340 {
   341 	cout << "TI::toggleStandardFlag "<<name.toStdString()<<" for item "<<getHeadingStd()<<endl;
   342 	if (standardFlags.isActive (name))
   343 		deactivateStandardFlag (name);
   344 	else	
   345 		activateStandardFlag (name);
   346 }
   347 
   348 bool TreeItem::isActiveStandardFlag (const QString &name)
   349 {
   350 	return standardFlags.isActive (name);
   351 }
   352 
   353 void TreeItem::setID (const QString &s)
   354 {
   355 	objID=s;
   356 }
   357 
   358 QString TreeItem::getID()
   359 {
   360 	return objID;
   361 }
   362 
   363 
   364 TreeItem* TreeItem::getChildNum(const int &n)
   365 {
   366 	if (n>=0 && n<childItems.count() )
   367 		return childItems.at(n);
   368 	else
   369 		return NULL;
   370 }
   371 
   372 BranchItem* TreeItem::getFirstBranch()
   373 {
   374 	if (branchCounter>0)
   375 		return getBranchNum (branchOffset);
   376 	else
   377 		return NULL;
   378 }
   379 
   380 BranchItem* TreeItem::getLastBranch()
   381 {
   382 	if (branchCounter>0)
   383 		return getBranchNum (branchOffset + branchCounter-1);
   384 	else
   385 		return NULL;
   386 }
   387 
   388 BranchItem* TreeItem::getNextBranch(BranchItem *currentBranch)
   389 {
   390 	if (!currentBranch) return NULL;
   391 	int n=num (currentBranch)+1;
   392 	if (n<branchCounter)
   393 		return getBranchNum (branchOffset + n);
   394 	else
   395 		return NULL;
   396 }
   397 
   398 
   399 BranchItem* TreeItem::getBranchNum(const int &n)
   400 {
   401 	if (branchCounter>0)
   402 		return (BranchItem*)getChildNum (branchOffset + n);
   403 	else
   404 		return NULL;
   405 }
   406 
   407 BranchObj* TreeItem::getBranchObjNum(const int &n)
   408 {
   409 	if (branchCounter>0)
   410 	{
   411 		return (BranchObj*)(getChildNum (branchOffset+n)->lmo);
   412 	} else
   413 		return NULL;
   414 }
   415 
   416 void TreeItem::setLastSelectedBranch()
   417 {
   418 	if (parentItem)
   419 		parentItem->lastSelectedBranchNum=parentItem->childItems.indexOf(this);
   420 }
   421 
   422 void TreeItem::setLastSelectedBranch(int i)
   423 {
   424 		lastSelectedBranchNum=i;
   425 }
   426 
   427 TreeItem* TreeItem::getLastSelectedBranch()
   428 {
   429 	return getBranchNum (lastSelectedBranchNum);
   430 }
   431 
   432 
   433 void TreeItem::setHideTmp (HideTmpMode mode)
   434 {
   435 	if (isBranchLikeType() )
   436 		((BranchItem*)this)->updateVisibility();
   437 		/*
   438 	if (type==Image)
   439 		//updateVisibility();
   440 	*/
   441 
   442 /*
   443 
   444 		if (mode==HideExport && (hideExport || hasHiddenExportParent() ) )
   445 		{
   446 			// Hide stuff according to hideExport flag and parents
   447 			//setVisibility (false);
   448 			updateVisibility();
   449 			//FIXME-2 hidden=true;
   450 		}else
   451 		{
   452 			// Do not hide, but still take care of scrolled status
   453 			
   454 
   455 			XXXXXXXX treeItem should be THIS
   456 
   457 			move visible to TreeItem???
   458 
   459 			BranchObj now has updateContents
   460 
   461 			maybe also start "bool TreeItem::branchlikeType"
   462 
   463 
   464 
   465 			if ( ((BranchItem*)treeItem)->hasScrolledParent((BranchItem*)treeItem))
   466 				setVisibility (false);
   467 			else
   468 				setVisibility (true);
   469 			//FIXME-2 hidden=false;
   470 		}	
   471 
   472 */
   473 		// And take care of my children
   474 		for (int i=0; i<branchCount(); ++i)
   475 			getBranchNum(i)->setHideTmp (mode);
   476 }
   477 
   478 bool TreeItem::hasHiddenExportParent()
   479 {
   480 	// Calls parents recursivly to
   481 	// find out, if we or parents are temp. hidden
   482 
   483 	if (hidden || hideExport) return true;
   484 
   485 	if (parentItem) 
   486 		return parentItem->hasHiddenExportParent();
   487 	else
   488 		return false;
   489 }
   490 
   491 
   492 void TreeItem::setHideInExport(bool b) 
   493 {
   494 	if (isBranchLikeType() )
   495 	{
   496 		hideExport=b;
   497 		/* FIXME-1 call setVis and updateContentsSize...
   498 		if (b)
   499 			systemFlags->activate("hideInExport");
   500 		else	
   501 			systemFlags->deactivate("hideInExport");
   502 		calcBBoxSize();
   503 		positionBBox();
   504 		requestReposition();	
   505 		*/
   506 	}
   507 }	
   508 
   509 bool TreeItem::hideInExport()
   510 {
   511 	return hideExport;
   512 }	
   513 
   514 bool TreeItem::isHidden()
   515 {
   516 	return hidden;
   517 }	
   518 
   519 
   520