branchitem.cpp
author insilmaril
Fri, 09 Apr 2010 14:16:02 +0000
changeset 845 b98c1793bb8b
parent 843 2d36a7bb0867
child 847 43268373032d
permissions -rw-r--r--
XHTML export obsoleted by HTML export
     1 #include "attributeitem.h"
     2 #include "branchitem.h"
     3 #include "branchobj.h"
     4 #include "vymmodel.h"
     5 #include "xlinkitem.h"
     6 
     7 #include <QDir>
     8 
     9 using namespace std;
    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 "<<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) //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);
   144 		i++;
   145 		ti=getBranchNum(i);
   146 	}	
   147 
   148 	// Save XLinks 
   149 	QString ol;	// old link
   150 	QString cl;	// current link
   151 	for (int i=0; i<xlinkCount(); ++i)
   152 	{
   153 		cl=getXLinkNum(i)->saveToDir();
   154 		if (cl!=ol)
   155 		{
   156 			s+=cl;
   157 			ol=cl;
   158 		} else
   159 		{
   160 			qWarning (QString("BranchItem::saveToDir  Ignoring of duplicate xLink in %1").arg(getHeading()));
   161 			qWarning ()<<"this="<<this<< " i="<<i<<" "<<getXLinkNum(i)<<"  "<<getXLinkNum(i-1);
   162 		}
   163 	}	
   164 
   165     decIndent();
   166     s+=endElement   (elementName);
   167     return s;
   168 }
   169 
   170 void BranchItem::updateVisibility()	
   171 {
   172 	// Needed to hide relinked branch, if parent is scrolled
   173 	if (lmo)
   174 	{
   175 		if (hasScrolledParent(this) || hidden)
   176 			lmo->setVisibility (false);
   177 		else	
   178 			lmo->setVisibility (true);
   179 	}
   180 }
   181 
   182 void BranchItem::setHeadingColor (QColor color)
   183 {
   184 	TreeItem::setHeadingColor (color);
   185 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   186 }
   187 
   188 void BranchItem::unScroll()
   189 {
   190 	if (tmpUnscrolled) resetTmpUnscroll();
   191 	if (scrolled) toggleScroll();
   192 }
   193 
   194 bool BranchItem::toggleScroll()	
   195 {
   196 	BranchObj *bo=NULL;
   197 	if (scrolled)
   198 	{
   199 		scrolled=false;
   200 		systemFlags.deactivate("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(true);
   207 			}
   208 		}
   209 	} else
   210 	{
   211 		scrolled=true;
   212 		systemFlags.activate("system-scrolledright");
   213 		if (branchCounter>0)
   214 		{
   215 			for (int i=0;i<branchCounter;++i)
   216 			{
   217 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   218 				if (bo) bo->setVisibility(false);
   219 			}
   220 		}
   221 	}
   222 	//model->reposition();	// FIXME-3 we don't really want to update view from here...
   223 	return true;
   224 }
   225 
   226 bool BranchItem::isScrolled()
   227 {
   228 	return scrolled;
   229 }
   230 
   231 bool BranchItem::hasScrolledParent(BranchItem *start)
   232 {
   233 	// Calls parents recursivly to
   234 	// find out, if we are scrolled at all.
   235 	// But ignore myself, just look at parents.
   236 
   237 	if (this !=start && scrolled) return true;
   238 
   239 	BranchItem* bi=(BranchItem*)parentItem;
   240 	if (bi && bi!=rootItem ) 
   241 		return bi->hasScrolledParent(start);
   242 	else
   243 		return false;
   244 }
   245 
   246 bool BranchItem::tmpUnscroll()
   247 {
   248 	bool result=false;
   249 
   250 	// Unscroll parent (recursivly)
   251 	BranchItem * pi=(BranchItem*)parentItem;
   252 	if (pi && pi->isBranchLikeType() ) result=pi->tmpUnscroll();
   253 		
   254 	// Unscroll myself
   255 	if (scrolled)
   256 	{
   257 		tmpUnscrolled=true;
   258 		systemFlags.activate("system-tmpUnscrolledRight");
   259 		toggleScroll();
   260 		model->emitDataHasChanged (this);
   261 		result=true;
   262 	}	
   263 	return result;
   264 }
   265 
   266 bool BranchItem::resetTmpUnscroll()
   267 {
   268 	bool result=false;
   269 
   270 	// Unscroll parent (recursivly)
   271 	BranchItem * pi=(BranchItem*)parentItem;
   272 	if (pi && pi->isBranchLikeType() ) result=pi->resetTmpUnscroll();
   273 		
   274 	// Unscroll myself
   275 	if (tmpUnscrolled)
   276 	{
   277 		tmpUnscrolled=false;
   278 		systemFlags.deactivate("system-tmpUnscrolledRight");
   279 		toggleScroll();
   280 		model->emitDataHasChanged (this);
   281 		result=true;
   282 	}	
   283 	return result;
   284 }
   285 
   286 void BranchItem::sortChildren(bool inverse)
   287 {
   288 	int childCount=branchCounter; 
   289 	int curChildIndex;
   290 	bool madeChanges=false;
   291 	do
   292 	{
   293 		madeChanges=false;
   294 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++)
   295 		{
   296 			BranchItem* curChild =getBranchNum(curChildIndex);
   297 			BranchItem* prevChild=getBranchNum(curChildIndex-1);
   298 			if (inverse)
   299 			{
   300 				if (prevChild->getHeading().compare(curChild->getHeading())<0)
   301 				{
   302 					model->moveUp (curChild);
   303 					madeChanges=true;
   304 				}	
   305 			} else	
   306 				if (prevChild->getHeading().compare(curChild->getHeading())>0)
   307 				{
   308 					model->moveUp (curChild);
   309 					madeChanges=true;
   310 				}	
   311 		} 
   312 	}while(madeChanges);
   313 }
   314 
   315 void BranchItem::setIncludeImagesVer(bool b)
   316 {
   317 	includeImagesVer=b;
   318 	/* calcBBoxSize(); FIXME-3
   319 	positionBBox();
   320 	requestReposition();
   321 	*/
   322 }
   323 
   324 bool BranchItem::getIncludeImagesVer()
   325 {
   326 	return includeImagesVer;
   327 }
   328 
   329 void BranchItem::setIncludeImagesHor(bool b)
   330 {
   331 	includeImagesHor=b;
   332 	/* calcBBoxSize(); FIXME-3
   333 	positionBBox();
   334 	requestReposition();
   335 	*/
   336 }
   337 
   338 bool BranchItem::getIncludeImagesHor()
   339 {
   340 	return includeImagesHor;
   341 }
   342 
   343 QString BranchItem::getIncludeImageAttr()
   344 {
   345 	QString a;
   346 	if (includeImagesVer)
   347 		a=attribut ("incImgV","true");
   348 	if (includeImagesHor)
   349 		a+=attribut ("incImgH","true");
   350 	return a;	
   351 }
   352 
   353 void BranchItem::setLastSelectedBranch()
   354 {
   355 	int d=depth();
   356 	if (d>=0)
   357 	{
   358 		if (d==1)
   359 			// Hack to save an additional lastSelected for mapcenters in MapEditor
   360 			// depending on orientation
   361 			// this allows to go both left and right from there
   362 			if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
   363 			{
   364 				((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
   365 				return;
   366 			}
   367 		((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
   368 	}
   369 }
   370 
   371 void BranchItem::setLastSelectedBranch(int i)
   372 {
   373 		lastSelectedBranchNum=i;
   374 }
   375 
   376 BranchItem* BranchItem::getLastSelectedBranch()
   377 {
   378 	if (lastSelectedBranchNum>=branchCounter)
   379 		return getBranchNum (branchCounter-1);
   380 	else	
   381 		return getBranchNum (lastSelectedBranchNum);
   382 }
   383 
   384 BranchItem* BranchItem::getLastSelectedBranchAlt()
   385 {
   386 	return getBranchNum (lastSelectedBranchNumAlt);
   387 }
   388 
   389 
   390 
   391 
   392 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   393 {
   394 	// Search branches
   395 	TreeItem *ti;
   396 	for (int i=0; i<branchCount(); ++i)
   397     {	
   398 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   399 		if (ti != NULL) return ti;
   400     }
   401 	
   402 
   403 	// Search myself
   404     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   405 		return this;
   406 
   407 	// Search images
   408 	ImageItem *ii;
   409     for (int i=0; i<imageCount(); ++i )
   410 	{
   411 		ii=getImageNum (i);
   412 		LinkableMapObj *mo=ii->getLMO();
   413 		if (mo && mo->isInClickBox(p) && 
   414 			(ii != excludeTI) && 
   415 			this!= excludeTI &&
   416 			mo->isVisibleObj() 
   417 		) return ii;
   418 	}
   419 
   420 	// Search attributes
   421 	AttributeItem *ai;
   422     for (int i=0; i<attributeCount(); ++i )
   423 	{
   424 		ai=getAttributeNum (i);
   425 		LinkableMapObj *mo=ai->getLMO();
   426 		if (mo && mo->isInClickBox(p) && 
   427 			(ii != excludeTI) && 
   428 			this!= excludeTI &&
   429 			mo->isVisibleObj() 
   430 		) return ai;
   431 	}
   432 
   433 	return NULL;
   434 }
   435 
   436 void BranchItem::updateStyles(const bool &keepFrame)
   437 {
   438 	// FIXME-5 compare also MapItem::initLMO...
   439 
   440 	if (lmo)
   441 	{ 
   442 		if ( parentItem != rootItem)
   443 			lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   444 		else
   445 			lmo->setParObj (NULL);
   446 		((BranchObj*)lmo)->setDefAttr(BranchObj::MovedBranch,keepFrame);
   447 	}
   448 }
   449 
   450 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   451 {
   452 	return (BranchObj*)lmo;
   453 }
   454 
   455 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   456 {
   457 	BranchObj *newbo;
   458 	newbo=new BranchObj(scene,this);
   459 	lmo=newbo;
   460 
   461 	if (parentItem==rootItem)
   462 	{
   463 		newbo->setParObj(NULL);
   464 		//newbo->setFrameType (FrameObj::Rectangle);	//FIXME-4 maybe call updateStyles?
   465 	} else
   466 	{
   467 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   468 		// Set visibility depending on parents
   469 		if (parentItem!=rootItem && 
   470 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   471 			newbo->setVisibility (false);
   472 	}
   473 	newbo->setDefAttr(BranchObj::NewBranch);
   474 	initLMO();
   475 
   476 	if (!getHeading().isEmpty() ) 
   477 	{
   478 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   479 		newbo->setColor (headingColor);
   480 	}	
   481 		
   482 	//newbo->updateLinkGeometry();	//FIXME-3
   483 
   484 	return newbo;
   485 }
   486