branchitem.cpp
author convert-repo
Fri, 23 Jul 2010 16:43:49 +0000
changeset 849 988f1908a7c4
parent 847 43268373032d
permissions -rw-r--r--
update tags
     1 #include "branchitem.h"
     2 
     3 #include "attributeitem.h"
     4 #include "branchobj.h"
     5 #include "vymmodel.h"
     6 #include "xlink.h"
     7 #include "xlinkitem.h"
     8 
     9 #include <QDir>
    10 
    11 BranchItem::BranchItem(const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
    12 {
    13 	//qDebug()<< "Constr. BranchItem";
    14 
    15 	// Set type if parent is known yet 
    16 	// if not, type is set in insertBranch or TreeItem::appendChild
    17 	if (parent==rootItem)
    18 		setType (MapCenter);
    19 	else
    20 		setType (Branch);
    21 
    22 	scrolled=false;
    23 	tmpUnscrolled=false;
    24 
    25 	includeImagesVer=false;
    26 	includeImagesHor=false;
    27 	 
    28 	lastSelectedBranchNum=-1;
    29 	lastSelectedBranchNumAlt=-1;
    30 }
    31 
    32 BranchItem::~BranchItem()
    33 {
    34 	//qDebug()<< "Destr. BranchItem this="<<this<<"  "<<getHeading();
    35 	if (lmo) 
    36 	{
    37 		delete lmo;
    38 		lmo=NULL;
    39 	}
    40 }
    41 
    42 void BranchItem::copy (BranchItem *other)
    43 {
    44 	scrolled=other->scrolled;
    45 	tmpUnscrolled=other->tmpUnscrolled;
    46 }
    47 
    48 BranchItem* BranchItem::parentBranch ()
    49 {
    50 	return (BranchItem*) parentItem;
    51 }
    52 
    53 void BranchItem::insertBranch (int pos, BranchItem *branch)
    54 {
    55 	if (pos<0) pos=0;
    56 	if (pos>branchCounter) pos=branchCounter;
    57     childItems.insert(pos+branchOffset,branch);
    58 	branch->parentItem=this;
    59 	branch->rootItem=rootItem;
    60 	branch->setModel (model);
    61 	if (parentItem==rootItem)
    62 		setType (MapCenter);
    63 	else
    64 		setType (Branch);
    65 
    66 
    67 	if (branchCounter==0)
    68 		branchOffset=childItems.count()-1;
    69 	branchCounter++;
    70 }
    71 
    72 QString BranchItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset, QList <Link*> &tmpLinks ) //FIXME-3 Check if everything is saved...
    73 {
    74 	// Cloudy stuff can be hidden during exports
    75 	if (hidden) return QString();
    76 
    77     QString s,a;
    78 	BranchObj *bo=(BranchObj*)lmo;
    79 
    80 	// Update of note is usually done while unselecting a branch
    81 	
    82 	QString scrolledAttr;
    83 	if (scrolled) 
    84 		scrolledAttr=attribut ("scrolled","yes");
    85 	else
    86 		scrolledAttr="";
    87 
    88 	// save area, if not scrolled	// FIXME-5 not needed if HTML is rewritten...
    89 									// also we should check if _any_ of parents is scrolled
    90 	QString areaAttr;
    91 	if (lmo && parentItem->isBranchLikeType() && !((BranchItem*)parentItem)->isScrolled() )
    92 	{
    93 		qreal x=lmo->getAbsPos().x();
    94 		qreal y=lmo->getAbsPos().y();
    95 		areaAttr=
    96 			attribut("x1",QString().setNum(x-offset.x())) +
    97 			attribut("y1",QString().setNum(y-offset.y())) +
    98 			attribut("x2",QString().setNum(x+lmo->width()-offset.x())) +
    99 			attribut("y2",QString().setNum(y+lmo->height()-offset.y()));
   100 
   101 	} else
   102 		areaAttr="";
   103 	
   104 	QString elementName;
   105 	if (parentItem==rootItem)
   106 		elementName="mapcenter";
   107 	else	
   108 		elementName="branch";
   109 
   110     s=beginElement (elementName
   111 		+getMapAttr()
   112 		+getGeneralAttr()
   113 		+scrolledAttr 
   114 	//	+areaAttr	// FIXME-4 not needed anymore. Wait until end of 2010 before removing...
   115 		+getIncludeImageAttr() 
   116 		);
   117     incIndent();
   118 
   119 	// save heading
   120     s+=valueElement("heading", getHeading(),
   121 		attribut ("textColor",QColor( bo->getColor()).name()));
   122 
   123 	// Save frame  //FIXME-4 not saved if there is no LMO
   124 	if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame) 
   125 		s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
   126 
   127 	// save names of flags set
   128 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   129 	
   130 	// Save Images
   131 	for (int i=0; i<imageCount(); ++i)
   132 		s+=getImageNum(i)->saveToDir (tmpdir,prefix);
   133 
   134 	// save note
   135 	if (!note.isEmpty() )
   136 		s+=note.saveToDir();
   137 	
   138 	// Save branches
   139 	int i=0;
   140 	TreeItem *ti=getBranchNum(i);
   141 	while (ti)
   142 	{
   143 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset,tmpLinks);
   144 		i++;
   145 		ti=getBranchNum(i);
   146 	}	
   147 
   148 	// Mark Links for save
   149 	for (int i=0; i<xlinkCount(); ++i)
   150 		if (!tmpLinks.contains (getXLinkNum (i)->getLink() )) 
   151 			tmpLinks.append (getXLinkNum(i)->getLink() );
   152 
   153     decIndent();
   154     s+=endElement   (elementName);
   155     return s;
   156 }
   157 
   158 void BranchItem::updateVisibility()	
   159 {
   160 	// Needed to hide relinked branch, if parent is scrolled
   161 	if (lmo)
   162 	{
   163 		if (hasScrolledParent(this) || hidden)
   164 			lmo->setVisibility (false);
   165 		else	
   166 			lmo->setVisibility (true);
   167 	}
   168 }
   169 
   170 void BranchItem::setHeadingColor (QColor color)
   171 {
   172 	TreeItem::setHeadingColor (color);
   173 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   174 }
   175 
   176 void BranchItem::unScroll()
   177 {
   178 	if (tmpUnscrolled) resetTmpUnscroll();
   179 	if (scrolled) toggleScroll();
   180 }
   181 
   182 bool BranchItem::toggleScroll()	
   183 {
   184 	BranchObj *bo=NULL;
   185 	if (scrolled)
   186 	{
   187 		scrolled=false;
   188 		systemFlags.deactivate("system-scrolledright");
   189 		if (branchCounter>0)
   190 		{
   191 			for (int i=0;i<branchCounter;++i)
   192 			{
   193 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   194 				if (bo) bo->setVisibility(true);
   195 			}
   196 		}
   197 	} else
   198 	{
   199 		scrolled=true;
   200 		systemFlags.activate("system-scrolledright");
   201 		if (branchCounter>0)
   202 		{
   203 			for (int i=0;i<branchCounter;++i)
   204 			{
   205 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   206 				if (bo) bo->setVisibility(false);
   207 			}
   208 		}
   209 	}
   210 	//model->reposition();	// FIXME-3 we don't really want to update view from here...
   211 	return true;
   212 }
   213 
   214 bool BranchItem::isScrolled()
   215 {
   216 	return scrolled;
   217 }
   218 
   219 bool BranchItem::hasScrolledParent(BranchItem *start)
   220 {
   221 	// Calls parents recursivly to
   222 	// find out, if we are scrolled at all.
   223 	// But ignore myself, just look at parents.
   224 
   225 	if (this !=start && scrolled) return true;
   226 
   227 	BranchItem* bi=(BranchItem*)parentItem;
   228 	if (bi && bi!=rootItem ) 
   229 		return bi->hasScrolledParent(start);
   230 	else
   231 		return false;
   232 }
   233 
   234 bool BranchItem::tmpUnscroll()
   235 {
   236 	bool result=false;
   237 
   238 	// Unscroll parent (recursivly)
   239 	BranchItem * pi=(BranchItem*)parentItem;
   240 	if (pi && pi->isBranchLikeType() ) result=pi->tmpUnscroll();
   241 		
   242 	// Unscroll myself
   243 	if (scrolled)
   244 	{
   245 		tmpUnscrolled=true;
   246 		systemFlags.activate("system-tmpUnscrolledRight");
   247 		toggleScroll();
   248 		model->emitDataHasChanged (this);
   249 		result=true;
   250 	}	
   251 	return result;
   252 }
   253 
   254 bool BranchItem::resetTmpUnscroll()
   255 {
   256 	bool result=false;
   257 
   258 	// Unscroll parent (recursivly)
   259 	BranchItem * pi=(BranchItem*)parentItem;
   260 	if (pi && pi->isBranchLikeType() ) result=pi->resetTmpUnscroll();
   261 		
   262 	// Unscroll myself
   263 	if (tmpUnscrolled)
   264 	{
   265 		tmpUnscrolled=false;
   266 		systemFlags.deactivate("system-tmpUnscrolledRight");
   267 		toggleScroll();
   268 		model->emitDataHasChanged (this);
   269 		result=true;
   270 	}	
   271 	return result;
   272 }
   273 
   274 void BranchItem::sortChildren(bool inverse)
   275 {
   276 	int childCount=branchCounter; 
   277 	int curChildIndex;
   278 	bool madeChanges=false;
   279 	do
   280 	{
   281 		madeChanges=false;
   282 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++)
   283 		{
   284 			BranchItem* curChild =getBranchNum(curChildIndex);
   285 			BranchItem* prevChild=getBranchNum(curChildIndex-1);
   286 			if (inverse)
   287 			{
   288 				if (prevChild->getHeading().compare(curChild->getHeading())<0)
   289 				{
   290 					model->moveUp (curChild);
   291 					madeChanges=true;
   292 				}	
   293 			} else	
   294 				if (prevChild->getHeading().compare(curChild->getHeading())>0)
   295 				{
   296 					model->moveUp (curChild);
   297 					madeChanges=true;
   298 				}	
   299 		} 
   300 	}while(madeChanges);
   301 }
   302 
   303 void BranchItem::setIncludeImagesVer(bool b)
   304 {
   305 	includeImagesVer=b;
   306 	/* calcBBoxSize(); FIXME-3
   307 	positionBBox();
   308 	requestReposition();
   309 	*/
   310 }
   311 
   312 bool BranchItem::getIncludeImagesVer()
   313 {
   314 	return includeImagesVer;
   315 }
   316 
   317 void BranchItem::setIncludeImagesHor(bool b)
   318 {
   319 	includeImagesHor=b;
   320 	/* calcBBoxSize(); FIXME-3
   321 	positionBBox();
   322 	requestReposition();
   323 	*/
   324 }
   325 
   326 bool BranchItem::getIncludeImagesHor()
   327 {
   328 	return includeImagesHor;
   329 }
   330 
   331 QString BranchItem::getIncludeImageAttr()
   332 {
   333 	QString a;
   334 	if (includeImagesVer)
   335 		a=attribut ("incImgV","true");
   336 	if (includeImagesHor)
   337 		a+=attribut ("incImgH","true");
   338 	return a;	
   339 }
   340 
   341 void BranchItem::setLastSelectedBranch()
   342 {
   343 	int d=depth();
   344 	if (d>=0)
   345 	{
   346 		if (d==1)
   347 			// Hack to save an additional lastSelected for mapcenters in MapEditor
   348 			// depending on orientation
   349 			// this allows to go both left and right from there
   350 			if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
   351 			{
   352 				((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
   353 				return;
   354 			}
   355 		((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
   356 	}
   357 }
   358 
   359 void BranchItem::setLastSelectedBranch(int i)
   360 {
   361 		lastSelectedBranchNum=i;
   362 }
   363 
   364 BranchItem* BranchItem::getLastSelectedBranch()
   365 {
   366 	if (lastSelectedBranchNum>=branchCounter)
   367 		return getBranchNum (branchCounter-1);
   368 	else	
   369 		return getBranchNum (lastSelectedBranchNum);
   370 }
   371 
   372 BranchItem* BranchItem::getLastSelectedBranchAlt()
   373 {
   374 	return getBranchNum (lastSelectedBranchNumAlt);
   375 }
   376 
   377 
   378 
   379 
   380 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   381 {
   382 	// Search branches
   383 	TreeItem *ti;
   384 	for (int i=0; i<branchCount(); ++i)
   385     {	
   386 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   387 		if (ti != NULL) return ti;
   388     }
   389 	
   390 
   391 	// Search myself
   392     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   393 		return this;
   394 
   395 	// Search images
   396 	ImageItem *ii;
   397     for (int i=0; i<imageCount(); ++i )
   398 	{
   399 		ii=getImageNum (i);
   400 		LinkableMapObj *mo=ii->getLMO();
   401 		if (mo && mo->isInClickBox(p) && 
   402 			(ii != excludeTI) && 
   403 			this!= excludeTI &&
   404 			mo->isVisibleObj() 
   405 		) return ii;
   406 	}
   407 
   408 	// Search attributes
   409 	AttributeItem *ai;
   410     for (int i=0; i<attributeCount(); ++i )
   411 	{
   412 		ai=getAttributeNum (i);
   413 		LinkableMapObj *mo=ai->getLMO();
   414 		if (mo && mo->isInClickBox(p) && 
   415 			(ii != excludeTI) && 
   416 			this!= excludeTI &&
   417 			mo->isVisibleObj() 
   418 		) return ai;
   419 	}
   420 
   421 	return NULL;
   422 }
   423 
   424 void BranchItem::updateStyles(const bool &keepFrame)
   425 {
   426 	// FIXME-5 compare also MapItem::initLMO...
   427 
   428 	if (lmo)
   429 	{ 
   430 		if ( parentItem != rootItem)
   431 			lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   432 		else
   433 			lmo->setParObj (NULL);
   434 		((BranchObj*)lmo)->setDefAttr(BranchObj::MovedBranch,keepFrame);
   435 	}
   436 }
   437 
   438 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   439 {
   440 	return (BranchObj*)lmo;
   441 }
   442 
   443 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   444 {
   445 	BranchObj *newbo;
   446 	newbo=new BranchObj(scene,this);
   447 	lmo=newbo;
   448 
   449 	if (parentItem==rootItem)
   450 	{
   451 		newbo->setParObj(NULL);
   452 		//newbo->setFrameType (FrameObj::Rectangle);	//FIXME-4 maybe call updateStyles?
   453 	} else
   454 	{
   455 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   456 		// Set visibility depending on parents
   457 		if (parentItem!=rootItem && 
   458 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   459 			newbo->setVisibility (false);
   460 	}
   461 	newbo->setDefAttr(BranchObj::NewBranch);
   462 	initLMO();
   463 
   464 	if (!getHeading().isEmpty() ) 
   465 	{
   466 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   467 		newbo->setColor (headingColor);
   468 	}	
   469 		
   470 	//newbo->updateLinkGeometry();	//FIXME-3
   471 
   472 	return newbo;
   473 }
   474