branchitem.cpp
author insilmaril
Thu, 17 Sep 2009 09:41:09 +0000
changeset 795 6b0a5f4923d3
parent 791 f1006de05c54
child 796 cf634bbf9e04
permissions -rw-r--r--
Fixed sort children and addMapReplaceInt
     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 	//cout << "BI::hasScrolledParent this="<<this<<"  "<<getHeadingStd()<<endl;
   240 	if (this !=start && scrolled) return true;
   241 
   242 	BranchItem* bi=(BranchItem*)parentItem;
   243 	if (bi && bi!=rootItem && bi->isBranchLikeType() ) 
   244 		return bi->hasScrolledParent(start);
   245 	else
   246 		return false;
   247 }
   248 
   249 void BranchItem::tmpUnscroll()
   250 {
   251 	// Unscroll parent (recursivly)
   252 	BranchItem * pi=(BranchItem*)parentItem;
   253 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   254 		
   255 	// Unscroll myself
   256 	if (scrolled)
   257 	{
   258 		tmpUnscrolled=true;
   259 		systemFlags.activate("system-tmpUnscrolledRight");
   260 		toggleScroll();
   261 		model->emitDataHasChanged (this);
   262 	}	
   263 }
   264 
   265 void BranchItem::resetTmpUnscroll()
   266 {
   267 	// Unscroll parent (recursivly)
   268 	BranchItem * pi=(BranchItem*)parentItem;
   269 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   270 		
   271 	// Unscroll myself
   272 	if (tmpUnscrolled)
   273 	{
   274 		tmpUnscrolled=false;
   275 		systemFlags.deactivate("system-tmpUnscrolledRight");
   276 		toggleScroll();
   277 		model->emitDataHasChanged (this);
   278 	}	
   279 }
   280 
   281 void BranchItem::sortChildren()
   282 {
   283 	int childCount=branchCounter; 
   284 	int curChildIndex;
   285 	bool madeChanges=false;
   286 	do
   287 	{
   288 		madeChanges=false;
   289 		for(curChildIndex=1;curChildIndex<childCount;curChildIndex++){
   290 			BranchItem* curChild =getBranchNum(curChildIndex);
   291 			BranchItem* prevChild=getBranchNum(curChildIndex-1);
   292 			if(prevChild->getHeading().compare(curChild->getHeading())>0)
   293 			{
   294 				model->moveUp(curChild);
   295 				madeChanges=true;
   296 			}
   297 		}
   298 	}while(madeChanges);
   299 }
   300 
   301 void BranchItem::setIncludeImagesVer(bool b)
   302 {
   303 	includeImagesVer=b;
   304 	/* calcBBoxSize(); FIXME-3
   305 	positionBBox();
   306 	requestReposition();
   307 	*/
   308 }
   309 
   310 bool BranchItem::getIncludeImagesVer()
   311 {
   312 	return includeImagesVer;
   313 }
   314 
   315 void BranchItem::setIncludeImagesHor(bool b)
   316 {
   317 	includeImagesHor=b;
   318 	/* calcBBoxSize(); FIXME-3
   319 	positionBBox();
   320 	requestReposition();
   321 	*/
   322 }
   323 
   324 bool BranchItem::getIncludeImagesHor()
   325 {
   326 	return includeImagesHor;
   327 }
   328 
   329 QString BranchItem::getIncludeImageAttr()
   330 {
   331 	QString a;
   332 	if (includeImagesVer)
   333 		a=attribut ("incImgV","true");
   334 	if (includeImagesHor)
   335 		a+=attribut ("incImgH","true");
   336 	return a;	
   337 }
   338 
   339 void BranchItem::setLastSelectedBranch()
   340 {
   341 	int d=depth();
   342 	if (d>=0)
   343 	{
   344 		if (d==1)
   345 			// Hack to save an additional lastSelected for mapcenters in MapEditor
   346 			// depending on orientation
   347 			// this allows to go both left and right from there
   348 			if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
   349 			{
   350 				((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
   351 				return;
   352 			}
   353 		((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
   354 	}
   355 }
   356 
   357 void BranchItem::setLastSelectedBranch(int i)
   358 {
   359 		lastSelectedBranchNum=i;
   360 }
   361 
   362 BranchItem* BranchItem::getLastSelectedBranch()
   363 {
   364 	return getBranchNum (lastSelectedBranchNum);
   365 }
   366 
   367 BranchItem* BranchItem::getLastSelectedBranchAlt()
   368 {
   369 	return getBranchNum (lastSelectedBranchNumAlt);
   370 }
   371 
   372 
   373 
   374 
   375 
   376 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   377 {
   378 	// Search branches
   379 	TreeItem *ti;
   380 	for (int i=0; i<branchCount(); ++i)
   381     {	
   382 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   383 		if (ti != NULL) return ti;
   384     }
   385 	
   386 
   387 	// Search myself
   388     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   389 		return this;
   390 
   391 	// Search images
   392 	ImageItem *ii;
   393     for (int i=0; i<imageCount(); ++i )
   394 	{
   395 		ii=getImageNum (i);
   396 		LinkableMapObj *mo=ii->getLMO();
   397 		if (mo && mo->isInClickBox(p) && 
   398 			(ii != excludeTI) && 
   399 			this!= excludeTI &&
   400 			mo->isVisibleObj() 
   401 		) return ii;
   402 	}
   403 	return NULL;
   404 }
   405 /*
   406 TreeItem* BranchItem::findID (QString sid)	//FIXME-3 move to TreeItem	//FIXME-4 search images
   407 {
   408 	// Search branches
   409     TreeItem *ti;
   410 	for (int i=0; i<branchCount(); ++i)
   411     {	
   412 		ti=getBranchNum(i)->findID (sid);
   413 		if (ti != NULL) return ti;
   414     }
   415 	
   416 	// Search myself
   417 	if (sid==objID) return this;
   418 
   419 
   420 	// Search float images 
   421     for (int i=0; i<floatimage.size(); ++i )
   422 		if (floatimage.at(i)->inBox(p) && 
   423 			(floatimage.at(i) != excludeLMO) && 
   424 			floatimage.at(i)->getParObj()!= excludeLMO &&
   425 			floatimage.at(i)->isVisibleObj() 
   426 		) return floatimage.at(i);
   427     return NULL;
   428 }
   429 */
   430 void BranchItem::updateStyles()
   431 {
   432 	// FIXME-5 compare also MapItem::initLMO...
   433 
   434 	if (lmo && parentItem != rootItem)
   435 	{
   436 		lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   437 	}
   438 }
   439 
   440 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   441 {
   442 	return (BranchObj*)lmo;
   443 }
   444 
   445 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   446 {
   447 	BranchObj *newbo;
   448 	newbo=new BranchObj(scene,this);
   449 	lmo=newbo;
   450 
   451 	if (parentItem==rootItem)
   452 	{
   453 		newbo->setParObj(NULL);
   454 		newbo->setFrameType (FrameObj::Rectangle);
   455 	} else
   456 	{
   457 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   458 		// Set visibility depending on parents
   459 		if (parentItem!=rootItem && 
   460 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   461 			newbo->setVisibility (false);
   462 	}
   463 	newbo->setDefAttr(BranchObj::NewBranch);
   464 	initLMO();
   465 
   466 	if (!getHeading().isEmpty() ) 
   467 	{
   468 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   469 		newbo->setColor (headingColor);
   470 	}	
   471 		
   472 	//newbo->updateLinkGeometry();	//FIXME-3
   473 
   474 	return newbo;
   475 }
   476