branchitem.cpp
author insilmaril
Mon, 07 Dec 2009 21:56:46 +0000
changeset 815 2881c4424190
parent 804 14f2b1b15242
child 819 8f987e376035
permissions -rw-r--r--
bugfixes, added flag-wip
     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 		lmo->setVisibility(!((BranchItem*)parentItem)->isScrolled());
   182 }
   183 
   184 void BranchItem::setHeadingColor (QColor color)
   185 {
   186 	TreeItem::setHeadingColor (color);
   187 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   188 }
   189 
   190 void BranchItem::unScroll()
   191 {
   192 	if (tmpUnscrolled) resetTmpUnscroll();
   193 	if (scrolled) toggleScroll();
   194 }
   195 
   196 bool BranchItem::toggleScroll()	
   197 {
   198 	BranchObj *bo=NULL;
   199 	if (scrolled)
   200 	{
   201 		scrolled=false;
   202 		systemFlags.deactivate("system-scrolledright");
   203 		if (branchCounter>0)
   204 		{
   205 			for (int i=0;i<branchCounter;++i)
   206 			{
   207 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   208 				if (bo) bo->setVisibility(true);
   209 			}
   210 		}
   211 	} else
   212 	{
   213 		scrolled=true;
   214 		systemFlags.activate("system-scrolledright");
   215 		if (branchCounter>0)
   216 		{
   217 			for (int i=0;i<branchCounter;++i)
   218 			{
   219 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   220 				if (bo) bo->setVisibility(false);
   221 			}
   222 		}
   223 	}
   224 	//model->reposition();	// FIXME-3 we don't really want to update view from here...
   225 	return true;
   226 }
   227 
   228 bool BranchItem::isScrolled()
   229 {
   230 	return scrolled;
   231 }
   232 
   233 bool BranchItem::hasScrolledParent(BranchItem *start)
   234 {
   235 	// Calls parents recursivly to
   236 	// find out, if we are scrolled at all.
   237 	// But ignore myself, just look at parents.
   238 
   239 	if (this !=start && scrolled) return true;
   240 
   241 	BranchItem* bi=(BranchItem*)parentItem;
   242 	if (bi && bi!=rootItem ) 
   243 		return bi->hasScrolledParent(start);
   244 	else
   245 		return false;
   246 }
   247 
   248 bool BranchItem::tmpUnscroll()
   249 {
   250 	bool result=false;
   251 
   252 	// Unscroll parent (recursivly)
   253 	BranchItem * pi=(BranchItem*)parentItem;
   254 	if (pi && pi->isBranchLikeType() ) result=pi->tmpUnscroll();
   255 		
   256 	// Unscroll myself
   257 	if (scrolled)
   258 	{
   259 		tmpUnscrolled=true;
   260 		systemFlags.activate("system-tmpUnscrolledRight");
   261 		toggleScroll();
   262 		model->emitDataHasChanged (this);
   263 		result=true;
   264 	}	
   265 	return result;
   266 }
   267 
   268 bool BranchItem::resetTmpUnscroll()
   269 {
   270 	bool result=false;
   271 
   272 	// Unscroll parent (recursivly)
   273 	BranchItem * pi=(BranchItem*)parentItem;
   274 	if (pi && pi->isBranchLikeType() ) result=pi->resetTmpUnscroll();
   275 		
   276 	// Unscroll myself
   277 	if (tmpUnscrolled)
   278 	{
   279 		tmpUnscrolled=false;
   280 		systemFlags.deactivate("system-tmpUnscrolledRight");
   281 		toggleScroll();
   282 		model->emitDataHasChanged (this);
   283 		result=true;
   284 	}	
   285 	return result;
   286 }
   287 
   288 void BranchItem::sortChildren()
   289 {
   290 	int childCount=branchCounter; 
   291 	int curChildIndex;
   292 	bool madeChanges=false;
   293 	do
   294 	{
   295 		madeChanges=false;
   296 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
   297 			BranchItem* curChild =getBranchNum(curChildIndex);
   298 			BranchItem* prevChild=getBranchNum(curChildIndex-1);
   299 			if(prevChild->getHeading().compare(curChild->getHeading())>0)
   300 			{
   301 				model->moveUp(curChild);
   302 				madeChanges=true;
   303 			}
   304 		}
   305 	}while(madeChanges);
   306 }
   307 
   308 void BranchItem::setIncludeImagesVer(bool b)
   309 {
   310 	includeImagesVer=b;
   311 	/* calcBBoxSize(); FIXME-3
   312 	positionBBox();
   313 	requestReposition();
   314 	*/
   315 }
   316 
   317 bool BranchItem::getIncludeImagesVer()
   318 {
   319 	return includeImagesVer;
   320 }
   321 
   322 void BranchItem::setIncludeImagesHor(bool b)
   323 {
   324 	includeImagesHor=b;
   325 	/* calcBBoxSize(); FIXME-3
   326 	positionBBox();
   327 	requestReposition();
   328 	*/
   329 }
   330 
   331 bool BranchItem::getIncludeImagesHor()
   332 {
   333 	return includeImagesHor;
   334 }
   335 
   336 QString BranchItem::getIncludeImageAttr()
   337 {
   338 	QString a;
   339 	if (includeImagesVer)
   340 		a=attribut ("incImgV","true");
   341 	if (includeImagesHor)
   342 		a+=attribut ("incImgH","true");
   343 	return a;	
   344 }
   345 
   346 void BranchItem::setLastSelectedBranch()
   347 {
   348 	int d=depth();
   349 	if (d>=0)
   350 	{
   351 		if (d==1)
   352 			// Hack to save an additional lastSelected for mapcenters in MapEditor
   353 			// depending on orientation
   354 			// this allows to go both left and right from there
   355 			if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
   356 			{
   357 				((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
   358 				return;
   359 			}
   360 		((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
   361 	}
   362 }
   363 
   364 void BranchItem::setLastSelectedBranch(int i)
   365 {
   366 		lastSelectedBranchNum=i;
   367 }
   368 
   369 BranchItem* BranchItem::getLastSelectedBranch()
   370 {
   371 	if (lastSelectedBranchNum>=branchCounter)
   372 		return getBranchNum (branchCounter-1);
   373 	else	
   374 		return getBranchNum (lastSelectedBranchNum);
   375 }
   376 
   377 BranchItem* BranchItem::getLastSelectedBranchAlt()
   378 {
   379 	return getBranchNum (lastSelectedBranchNumAlt);
   380 }
   381 
   382 
   383 
   384 
   385 
   386 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   387 {
   388 	// Search branches
   389 	TreeItem *ti;
   390 	for (int i=0; i<branchCount(); ++i)
   391     {	
   392 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   393 		if (ti != NULL) return ti;
   394     }
   395 	
   396 
   397 	// Search myself
   398     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   399 		return this;
   400 
   401 	// Search images
   402 	ImageItem *ii;
   403     for (int i=0; i<imageCount(); ++i )
   404 	{
   405 		ii=getImageNum (i);
   406 		LinkableMapObj *mo=ii->getLMO();
   407 		if (mo && mo->isInClickBox(p) && 
   408 			(ii != excludeTI) && 
   409 			this!= excludeTI &&
   410 			mo->isVisibleObj() 
   411 		) return ii;
   412 	}
   413 	return NULL;
   414 }
   415 /*
   416 TreeItem* BranchItem::findID (QString sid)	//FIXME-3 move to TreeItem	//FIXME-4 search images
   417 {
   418 	// Search branches
   419     TreeItem *ti;
   420 	for (int i=0; i<branchCount(); ++i)
   421     {	
   422 		ti=getBranchNum(i)->findID (sid);
   423 		if (ti != NULL) return ti;
   424     }
   425 	
   426 	// Search myself
   427 	if (sid==objID) return this;
   428 
   429 
   430 	// Search float images 
   431     for (int i=0; i<floatimage.size(); ++i )
   432 		if (floatimage.at(i)->inBox(p) && 
   433 			(floatimage.at(i) != excludeLMO) && 
   434 			floatimage.at(i)->getParObj()!= excludeLMO &&
   435 			floatimage.at(i)->isVisibleObj() 
   436 		) return floatimage.at(i);
   437     return NULL;
   438 }
   439 */
   440 void BranchItem::updateStyles()
   441 {
   442 	// FIXME-5 compare also MapItem::initLMO...
   443 
   444 	if (lmo)
   445 	{ 
   446 		if ( parentItem != rootItem)
   447 			lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   448 		else
   449 			lmo->setParObj (NULL);
   450 		((BranchObj*)lmo)->setDefAttr(BranchObj::MovedBranch);
   451 	}
   452 }
   453 
   454 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   455 {
   456 	return (BranchObj*)lmo;
   457 }
   458 
   459 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   460 {
   461 	BranchObj *newbo;
   462 	newbo=new BranchObj(scene,this);
   463 	lmo=newbo;
   464 
   465 	if (parentItem==rootItem)
   466 	{
   467 		newbo->setParObj(NULL);
   468 		//newbo->setFrameType (FrameObj::Rectangle);	//FIXME-4 maybe call updateStyles?
   469 	} else
   470 	{
   471 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   472 		// Set visibility depending on parents
   473 		if (parentItem!=rootItem && 
   474 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   475 			newbo->setVisibility (false);
   476 	}
   477 	newbo->setDefAttr(BranchObj::NewBranch);
   478 	initLMO();
   479 
   480 	if (!getHeading().isEmpty() ) 
   481 	{
   482 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   483 		newbo->setColor (headingColor);
   484 	}	
   485 		
   486 	//newbo->updateLinkGeometry();	//FIXME-3
   487 
   488 	return newbo;
   489 }
   490