branchitem.cpp
author insilmaril
Thu, 01 Oct 2009 11:28:50 +0000
changeset 798 d251c7b2de54
parent 796 cf634bbf9e04
child 804 14f2b1b15242
permissions -rw-r--r--
Various fixes for relinking and selecting
     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 	return getBranchNum (lastSelectedBranchNum);
   372 }
   373 
   374 BranchItem* BranchItem::getLastSelectedBranchAlt()
   375 {
   376 	return getBranchNum (lastSelectedBranchNumAlt);
   377 }
   378 
   379 
   380 
   381 
   382 
   383 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   384 {
   385 	// Search branches
   386 	TreeItem *ti;
   387 	for (int i=0; i<branchCount(); ++i)
   388     {	
   389 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   390 		if (ti != NULL) return ti;
   391     }
   392 	
   393 
   394 	// Search myself
   395     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   396 		return this;
   397 
   398 	// Search images
   399 	ImageItem *ii;
   400     for (int i=0; i<imageCount(); ++i )
   401 	{
   402 		ii=getImageNum (i);
   403 		LinkableMapObj *mo=ii->getLMO();
   404 		if (mo && mo->isInClickBox(p) && 
   405 			(ii != excludeTI) && 
   406 			this!= excludeTI &&
   407 			mo->isVisibleObj() 
   408 		) return ii;
   409 	}
   410 	return NULL;
   411 }
   412 /*
   413 TreeItem* BranchItem::findID (QString sid)	//FIXME-3 move to TreeItem	//FIXME-4 search images
   414 {
   415 	// Search branches
   416     TreeItem *ti;
   417 	for (int i=0; i<branchCount(); ++i)
   418     {	
   419 		ti=getBranchNum(i)->findID (sid);
   420 		if (ti != NULL) return ti;
   421     }
   422 	
   423 	// Search myself
   424 	if (sid==objID) return this;
   425 
   426 
   427 	// Search float images 
   428     for (int i=0; i<floatimage.size(); ++i )
   429 		if (floatimage.at(i)->inBox(p) && 
   430 			(floatimage.at(i) != excludeLMO) && 
   431 			floatimage.at(i)->getParObj()!= excludeLMO &&
   432 			floatimage.at(i)->isVisibleObj() 
   433 		) return floatimage.at(i);
   434     return NULL;
   435 }
   436 */
   437 void BranchItem::updateStyles()
   438 {
   439 	// FIXME-5 compare also MapItem::initLMO...
   440 
   441 	if (lmo)
   442 	{ 
   443 		if ( parentItem != rootItem)
   444 			lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   445 		else
   446 			lmo->setParObj (NULL);
   447 		((BranchObj*)lmo)->setDefAttr(BranchObj::MovedBranch);
   448 	}
   449 }
   450 
   451 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   452 {
   453 	return (BranchObj*)lmo;
   454 }
   455 
   456 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   457 {
   458 	BranchObj *newbo;
   459 	newbo=new BranchObj(scene,this);
   460 	lmo=newbo;
   461 
   462 	if (parentItem==rootItem)
   463 	{
   464 		newbo->setParObj(NULL);
   465 		//newbo->setFrameType (FrameObj::Rectangle);	//FIXME-4 maybe call updateStyles?
   466 	} else
   467 	{
   468 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   469 		// Set visibility depending on parents
   470 		if (parentItem!=rootItem && 
   471 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   472 			newbo->setVisibility (false);
   473 	}
   474 	newbo->setDefAttr(BranchObj::NewBranch);
   475 	initLMO();
   476 
   477 	if (!getHeading().isEmpty() ) 
   478 	{
   479 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   480 		newbo->setColor (headingColor);
   481 	}	
   482 		
   483 	//newbo->updateLinkGeometry();	//FIXME-3
   484 
   485 	return newbo;
   486 }
   487