branchitem.cpp
author insilmaril
Mon, 04 Jan 2010 20:36:06 +0000
changeset 819 8f987e376035
parent 804 14f2b1b15242
child 822 c2ce9944148c
permissions -rw-r--r--
various fixes
     1 #include "branchitem.h"
     2 #include "branchobj.h"
     3 #include "vymmodel.h"
     4 #include "xlinkitem.h"
     5 
     6 #include <iostream>
     7 #include <QDir>
     8 
     9 using namespace std;
    10 
    11 BranchItem::BranchItem(const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
    12 {
    13 	//cout << "Constr. BranchItem\n";
    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 	//cout << "Destr. BranchItem "<<getHeadingStd()<<endl;
    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 	// Provide an ID for a branch makes export to XHTML easier
   105 	QString idAttr;
   106 	if (xlinkCount()>0)
   107 		idAttr=attribut ("id",model->getSelectString(this)); 
   108 	else
   109 		idAttr="";
   110 
   111 	QString elementName;
   112 	if (parentItem==rootItem)
   113 		elementName="mapcenter";
   114 	else	
   115 		elementName="branch";
   116 
   117     s=beginElement (elementName
   118 		+getMapAttr()
   119 		+getGeneralAttr()
   120 		+scrolledAttr 
   121 	//	+areaAttr	// FIXME-2
   122 		+idAttr 
   123 		+getIncludeImageAttr() 
   124 		);
   125     incIndent();
   126 
   127 	// save heading
   128     s+=valueElement("heading", getHeading(),
   129 		attribut ("textColor",QColor( bo->getColor()).name()));
   130 
   131 	// Save frame  //FIXME-4 not saved if there is no LMO
   132 	if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame) 
   133 		s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
   134 
   135 	// save names of flags set
   136 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   137 	
   138 	// Save Images
   139 	for (int i=0; i<imageCount(); ++i)
   140 		s+=getImageNum(i)->saveToDir (tmpdir,prefix);
   141 
   142 	// save note
   143 	if (!note.isEmpty() )
   144 		s+=note.saveToDir();
   145 	
   146 	// Save branches
   147 	int i=0;
   148 	TreeItem *ti=getBranchNum(i);
   149 	while (ti)
   150 	{
   151 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   152 		i++;
   153 		ti=getBranchNum(i);
   154 	}	
   155 
   156 	// Save XLinks 
   157 	QString ol;	// old link
   158 	QString cl;	// current link
   159 	for (int i=0; i<xlinkCount(); ++i)
   160 	{
   161 		cl=getXLinkNum(i)->saveToDir();
   162 		if (cl!=ol)
   163 		{
   164 			s+=cl;
   165 			ol=cl;
   166 		} else
   167 		{
   168 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   169 		}
   170 	}	
   171 
   172     decIndent();
   173     s+=endElement   (elementName);
   174     return s;
   175 }
   176 
   177 void BranchItem::updateVisibility()	
   178 {
   179 	// Needed to hide relinked branch, if parent is scrolled
   180 	if (lmo)
   181 	{
   182 		if (hasScrolledParent(this) || hidden)
   183 			lmo->setVisibility (false);
   184 		else	
   185 			lmo->setVisibility (true);
   186 	}
   187 }
   188 
   189 void BranchItem::setHeadingColor (QColor color)
   190 {
   191 	TreeItem::setHeadingColor (color);
   192 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   193 }
   194 
   195 void BranchItem::unScroll()
   196 {
   197 	if (tmpUnscrolled) resetTmpUnscroll();
   198 	if (scrolled) toggleScroll();
   199 }
   200 
   201 bool BranchItem::toggleScroll()	
   202 {
   203 	BranchObj *bo=NULL;
   204 	if (scrolled)
   205 	{
   206 		scrolled=false;
   207 		systemFlags.deactivate("system-scrolledright");
   208 		if (branchCounter>0)
   209 		{
   210 			for (int i=0;i<branchCounter;++i)
   211 			{
   212 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   213 				if (bo) bo->setVisibility(true);
   214 			}
   215 		}
   216 	} else
   217 	{
   218 		scrolled=true;
   219 		systemFlags.activate("system-scrolledright");
   220 		if (branchCounter>0)
   221 		{
   222 			for (int i=0;i<branchCounter;++i)
   223 			{
   224 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   225 				if (bo) bo->setVisibility(false);
   226 			}
   227 		}
   228 	}
   229 	//model->reposition();	// FIXME-3 we don't really want to update view from here...
   230 	return true;
   231 }
   232 
   233 bool BranchItem::isScrolled()
   234 {
   235 	return scrolled;
   236 }
   237 
   238 bool BranchItem::hasScrolledParent(BranchItem *start)
   239 {
   240 	// Calls parents recursivly to
   241 	// find out, if we are scrolled at all.
   242 	// But ignore myself, just look at parents.
   243 
   244 	if (this !=start && scrolled) return true;
   245 
   246 	BranchItem* bi=(BranchItem*)parentItem;
   247 	if (bi && bi!=rootItem ) 
   248 		return bi->hasScrolledParent(start);
   249 	else
   250 		return false;
   251 }
   252 
   253 bool BranchItem::tmpUnscroll()
   254 {
   255 	bool result=false;
   256 
   257 	// Unscroll parent (recursivly)
   258 	BranchItem * pi=(BranchItem*)parentItem;
   259 	if (pi && pi->isBranchLikeType() ) result=pi->tmpUnscroll();
   260 		
   261 	// Unscroll myself
   262 	if (scrolled)
   263 	{
   264 		tmpUnscrolled=true;
   265 		systemFlags.activate("system-tmpUnscrolledRight");
   266 		toggleScroll();
   267 		model->emitDataHasChanged (this);
   268 		result=true;
   269 	}	
   270 	return result;
   271 }
   272 
   273 bool BranchItem::resetTmpUnscroll()
   274 {
   275 	bool result=false;
   276 
   277 	// Unscroll parent (recursivly)
   278 	BranchItem * pi=(BranchItem*)parentItem;
   279 	if (pi && pi->isBranchLikeType() ) result=pi->resetTmpUnscroll();
   280 		
   281 	// Unscroll myself
   282 	if (tmpUnscrolled)
   283 	{
   284 		tmpUnscrolled=false;
   285 		systemFlags.deactivate("system-tmpUnscrolledRight");
   286 		toggleScroll();
   287 		model->emitDataHasChanged (this);
   288 		result=true;
   289 	}	
   290 	return result;
   291 }
   292 
   293 void BranchItem::sortChildren()
   294 {
   295 	int childCount=branchCounter; 
   296 	int curChildIndex;
   297 	bool madeChanges=false;
   298 	do
   299 	{
   300 		madeChanges=false;
   301 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
   302 			BranchItem* curChild =getBranchNum(curChildIndex);
   303 			BranchItem* prevChild=getBranchNum(curChildIndex-1);
   304 			if(prevChild->getHeading().compare(curChild->getHeading())>0)
   305 			{
   306 				model->moveUp(curChild);
   307 				madeChanges=true;
   308 			}
   309 		}
   310 	}while(madeChanges);
   311 }
   312 
   313 void BranchItem::setIncludeImagesVer(bool b)
   314 {
   315 	includeImagesVer=b;
   316 	/* calcBBoxSize(); FIXME-3
   317 	positionBBox();
   318 	requestReposition();
   319 	*/
   320 }
   321 
   322 bool BranchItem::getIncludeImagesVer()
   323 {
   324 	return includeImagesVer;
   325 }
   326 
   327 void BranchItem::setIncludeImagesHor(bool b)
   328 {
   329 	includeImagesHor=b;
   330 	/* calcBBoxSize(); FIXME-3
   331 	positionBBox();
   332 	requestReposition();
   333 	*/
   334 }
   335 
   336 bool BranchItem::getIncludeImagesHor()
   337 {
   338 	return includeImagesHor;
   339 }
   340 
   341 QString BranchItem::getIncludeImageAttr()
   342 {
   343 	QString a;
   344 	if (includeImagesVer)
   345 		a=attribut ("incImgV","true");
   346 	if (includeImagesHor)
   347 		a+=attribut ("incImgH","true");
   348 	return a;	
   349 }
   350 
   351 void BranchItem::setLastSelectedBranch()
   352 {
   353 	int d=depth();
   354 	if (d>=0)
   355 	{
   356 		if (d==1)
   357 			// Hack to save an additional lastSelected for mapcenters in MapEditor
   358 			// depending on orientation
   359 			// this allows to go both left and right from there
   360 			if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
   361 			{
   362 				((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
   363 				return;
   364 			}
   365 		((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
   366 	}
   367 }
   368 
   369 void BranchItem::setLastSelectedBranch(int i)
   370 {
   371 		lastSelectedBranchNum=i;
   372 }
   373 
   374 BranchItem* BranchItem::getLastSelectedBranch()
   375 {
   376 	if (lastSelectedBranchNum>=branchCounter)
   377 		return getBranchNum (branchCounter-1);
   378 	else	
   379 		return getBranchNum (lastSelectedBranchNum);
   380 }
   381 
   382 BranchItem* BranchItem::getLastSelectedBranchAlt()
   383 {
   384 	return getBranchNum (lastSelectedBranchNumAlt);
   385 }
   386 
   387 
   388 
   389 
   390 
   391 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   392 {
   393 	// Search branches
   394 	TreeItem *ti;
   395 	for (int i=0; i<branchCount(); ++i)
   396     {	
   397 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   398 		if (ti != NULL) return ti;
   399     }
   400 	
   401 
   402 	// Search myself
   403     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   404 		return this;
   405 
   406 	// Search images
   407 	ImageItem *ii;
   408     for (int i=0; i<imageCount(); ++i )
   409 	{
   410 		ii=getImageNum (i);
   411 		LinkableMapObj *mo=ii->getLMO();
   412 		if (mo && mo->isInClickBox(p) && 
   413 			(ii != excludeTI) && 
   414 			this!= excludeTI &&
   415 			mo->isVisibleObj() 
   416 		) return ii;
   417 	}
   418 	return NULL;
   419 }
   420 /*
   421 TreeItem* BranchItem::findID (QString sid)	//FIXME-3 move to TreeItem	//FIXME-4 search images
   422 {
   423 	// Search branches
   424     TreeItem *ti;
   425 	for (int i=0; i<branchCount(); ++i)
   426     {	
   427 		ti=getBranchNum(i)->findID (sid);
   428 		if (ti != NULL) return ti;
   429     }
   430 	
   431 	// Search myself
   432 	if (sid==objID) return this;
   433 
   434 
   435 	// Search float images 
   436     for (int i=0; i<floatimage.size(); ++i )
   437 		if (floatimage.at(i)->inBox(p) && 
   438 			(floatimage.at(i) != excludeLMO) && 
   439 			floatimage.at(i)->getParObj()!= excludeLMO &&
   440 			floatimage.at(i)->isVisibleObj() 
   441 		) return floatimage.at(i);
   442     return NULL;
   443 }
   444 */
   445 void BranchItem::updateStyles()
   446 {
   447 	// FIXME-5 compare also MapItem::initLMO...
   448 
   449 	if (lmo)
   450 	{ 
   451 		if ( parentItem != rootItem)
   452 			lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   453 		else
   454 			lmo->setParObj (NULL);
   455 		((BranchObj*)lmo)->setDefAttr(BranchObj::MovedBranch);
   456 	}
   457 }
   458 
   459 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   460 {
   461 	return (BranchObj*)lmo;
   462 }
   463 
   464 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   465 {
   466 	BranchObj *newbo;
   467 	newbo=new BranchObj(scene,this);
   468 	lmo=newbo;
   469 
   470 	if (parentItem==rootItem)
   471 	{
   472 		newbo->setParObj(NULL);
   473 		//newbo->setFrameType (FrameObj::Rectangle);	//FIXME-4 maybe call updateStyles?
   474 	} else
   475 	{
   476 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   477 		// Set visibility depending on parents
   478 		if (parentItem!=rootItem && 
   479 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   480 			newbo->setVisibility (false);
   481 	}
   482 	newbo->setDefAttr(BranchObj::NewBranch);
   483 	initLMO();
   484 
   485 	if (!getHeading().isEmpty() ) 
   486 	{
   487 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   488 		newbo->setColor (headingColor);
   489 	}	
   490 		
   491 	//newbo->updateLinkGeometry();	//FIXME-3
   492 
   493 	return newbo;
   494 }
   495