branchitem.cpp
author insilmaril
Wed, 09 Sep 2009 12:57:06 +0000
changeset 793 cac93797c580
parent 791 f1006de05c54
child 795 6b0a5f4923d3
permissions -rw-r--r--
more fixes for collision detection
     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 void BranchItem::insertBranch (int pos, BranchItem *branch)
    49 {
    50 	if (pos<0) pos=0;
    51 	if (pos>branchCounter) pos=branchCounter;
    52     childItems.insert(pos+branchOffset,branch);
    53 	branch->parentItem=this;
    54 	branch->rootItem=rootItem;
    55 	branch->setModel (model);
    56 	if (parentItem==rootItem)
    57 		setType (MapCenter);
    58 	else
    59 		setType (Branch);
    60 
    61 
    62 	if (branchCounter==0)
    63 		branchOffset=childItems.count()-1;
    64 	branchCounter++;
    65 }
    66 
    67 QString BranchItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset) //FIXME-3 Check if everything is saved...
    68 {
    69 	// Cloudy stuff can be hidden during exports
    70 	if (hidden) return QString();
    71 
    72     QString s,a;
    73 	BranchObj *bo=(BranchObj*)lmo;
    74 
    75 	// Update of note is usually done while unselecting a branch
    76 	
    77 	QString scrolledAttr;
    78 	if (scrolled) 
    79 		scrolledAttr=attribut ("scrolled","yes");
    80 	else
    81 		scrolledAttr="";
    82 
    83 	// save area, if not scrolled	// FIXME-5 not needed if HTML is rewritten...
    84 									// also we should check if _any_ of parents is scrolled
    85 	QString areaAttr;
    86 	if (lmo && parentItem->isBranchLikeType() && !((BranchItem*)parentItem)->isScrolled() )
    87 	{
    88 		qreal x=lmo->getAbsPos().x();
    89 		qreal y=lmo->getAbsPos().y();
    90 		areaAttr=
    91 			attribut("x1",QString().setNum(x-offset.x())) +
    92 			attribut("y1",QString().setNum(y-offset.y())) +
    93 			attribut("x2",QString().setNum(x+lmo->width()-offset.x())) +
    94 			attribut("y2",QString().setNum(y+lmo->height()-offset.y()));
    95 
    96 	} else
    97 		areaAttr="";
    98 	
    99 	// Provide an ID for a branch makes export to XHTML easier
   100 	QString idAttr;
   101 	if (xlinkCount()>0)
   102 		idAttr=attribut ("id",model->getSelectString(this)); 
   103 	else
   104 		idAttr="";
   105 
   106 	QString elementName;
   107 	if (parentItem==rootItem)
   108 		elementName="mapcenter";
   109 	else	
   110 		elementName="branch";
   111 
   112     s=beginElement (elementName
   113 		+getMapAttr()
   114 		+getGeneralAttr()
   115 		+scrolledAttr 
   116 	//	+areaAttr	// FIXME-2
   117 		+idAttr 
   118 		+getIncludeImageAttr() 
   119 		);
   120     incIndent();
   121 
   122 	// save heading
   123     s+=valueElement("heading", getHeading(),
   124 		attribut ("textColor",QColor( bo->getColor()).name()));
   125 
   126 	// Save frame  //FIXME-4 not saved if there is no LMO
   127 	if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame) 
   128 		s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
   129 
   130 	// save names of flags set
   131 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   132 	
   133 	// Save Images
   134 	for (int i=0; i<imageCount(); ++i)
   135 		s+=getImageNum(i)->saveToDir (tmpdir,prefix);
   136 
   137 	// save note
   138 	if (!note.isEmpty() )
   139 		s+=note.saveToDir();
   140 	
   141 	// Save branches
   142 	int i=0;
   143 	TreeItem *ti=getBranchNum(i);
   144 	while (ti)
   145 	{
   146 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   147 		i++;
   148 		ti=getBranchNum(i);
   149 	}	
   150 
   151 	// Save XLinks 
   152 	QString ol;	// old link
   153 	QString cl;	// current link
   154 	for (int i=0; i<xlinkCount(); ++i)
   155 	{
   156 		cl=getXLinkNum(i)->saveToDir();
   157 		if (cl!=ol)
   158 		{
   159 			s+=cl;
   160 			ol=cl;
   161 		} else
   162 		{
   163 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   164 		}
   165 	}	
   166 
   167     decIndent();
   168     s+=endElement   (elementName);
   169     return s;
   170 }
   171 
   172 void BranchItem::updateVisibility()	
   173 {
   174 	// Needed to hide relinked branch, if parent is scrolled
   175 	if (lmo)
   176 		lmo->setVisibility(!((BranchItem*)parentItem)->isScrolled());
   177 }
   178 
   179 void BranchItem::setHeadingColor (QColor color)
   180 {
   181 	TreeItem::setHeadingColor (color);
   182 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   183 }
   184 
   185 void BranchItem::unScroll()
   186 {
   187 	if (tmpUnscrolled) resetTmpUnscroll();
   188 	if (scrolled) toggleScroll();
   189 }
   190 
   191 bool BranchItem::toggleScroll()	
   192 {
   193 	BranchObj *bo=NULL;
   194 	if (scrolled)
   195 	{
   196 		scrolled=false;
   197 		systemFlags.deactivate("system-scrolledright");
   198 		if (branchCounter>0)
   199 		{
   200 			for (int i=0;i<branchCounter;++i)
   201 			{
   202 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   203 				if (bo) bo->setVisibility(true);
   204 			}
   205 		}
   206 	} else
   207 	{
   208 		scrolled=true;
   209 		systemFlags.activate("system-scrolledright");
   210 		if (branchCounter>0)
   211 		{
   212 			for (int i=0;i<branchCounter;++i)
   213 			{
   214 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   215 				if (bo) bo->setVisibility(false);
   216 			}
   217 		}
   218 	}
   219 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   220 	return true;
   221 }
   222 
   223 bool BranchItem::isScrolled()
   224 {
   225 	return scrolled;
   226 }
   227 
   228 bool BranchItem::hasScrolledParent(BranchItem *start)
   229 {
   230 	// Calls parents recursivly to
   231 	// find out, if we are scrolled at all.
   232 	// But ignore myself, just look at parents.
   233 
   234 	//cout << "BI::hasScrolledParent this="<<this<<"  "<<getHeadingStd()<<endl;
   235 	if (this !=start && scrolled) return true;
   236 
   237 	BranchItem* bi=(BranchItem*)parentItem;
   238 	if (bi && bi!=rootItem && bi->isBranchLikeType() ) 
   239 		return bi->hasScrolledParent(start);
   240 	else
   241 		return false;
   242 }
   243 
   244 void BranchItem::tmpUnscroll()
   245 {
   246 	// Unscroll parent (recursivly)
   247 	BranchItem * pi=(BranchItem*)parentItem;
   248 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   249 		
   250 	// Unscroll myself
   251 	if (scrolled)
   252 	{
   253 		tmpUnscrolled=true;
   254 		systemFlags.activate("system-tmpUnscrolledRight");
   255 		toggleScroll();
   256 		model->emitDataHasChanged (this);
   257 	}	
   258 }
   259 
   260 void BranchItem::resetTmpUnscroll()
   261 {
   262 	// Unscroll parent (recursivly)
   263 	BranchItem * pi=(BranchItem*)parentItem;
   264 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   265 		
   266 	// Unscroll myself
   267 	if (tmpUnscrolled)
   268 	{
   269 		tmpUnscrolled=false;
   270 		systemFlags.deactivate("system-tmpUnscrolledRight");
   271 		toggleScroll();
   272 		model->emitDataHasChanged (this);
   273 	}	
   274 }
   275 
   276 void BranchItem::setIncludeImagesVer(bool b)
   277 {
   278 	includeImagesVer=b;
   279 	/* calcBBoxSize(); FIXME-2
   280 	positionBBox();
   281 	requestReposition();
   282 	*/
   283 }
   284 
   285 bool BranchItem::getIncludeImagesVer()
   286 {
   287 	return includeImagesVer;
   288 }
   289 
   290 void BranchItem::setIncludeImagesHor(bool b)
   291 {
   292 	includeImagesHor=b;
   293 	/* calcBBoxSize(); FIXME-2
   294 	positionBBox();
   295 	requestReposition();
   296 	*/
   297 }
   298 
   299 bool BranchItem::getIncludeImagesHor()
   300 {
   301 	return includeImagesHor;
   302 }
   303 
   304 QString BranchItem::getIncludeImageAttr()
   305 {
   306 	QString a;
   307 	if (includeImagesVer)
   308 		a=attribut ("incImgV","true");
   309 	if (includeImagesHor)
   310 		a+=attribut ("incImgH","true");
   311 	return a;	
   312 }
   313 
   314 void BranchItem::setLastSelectedBranch()
   315 {
   316 	int d=depth();
   317 	if (d>=0)
   318 	{
   319 		if (d==1)
   320 			// Hack to save an additional lastSelected for mapcenters in MapEditor
   321 			// depending on orientation
   322 			// this allows to go both left and right from there
   323 			if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
   324 			{
   325 				((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
   326 				return;
   327 			}
   328 		((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
   329 	}
   330 }
   331 
   332 void BranchItem::setLastSelectedBranch(int i)
   333 {
   334 		lastSelectedBranchNum=i;
   335 }
   336 
   337 BranchItem* BranchItem::getLastSelectedBranch()
   338 {
   339 	return getBranchNum (lastSelectedBranchNum);
   340 }
   341 
   342 BranchItem* BranchItem::getLastSelectedBranchAlt()
   343 {
   344 	return getBranchNum (lastSelectedBranchNumAlt);
   345 }
   346 
   347 
   348 
   349 
   350 
   351 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   352 {
   353 	// Search branches
   354 	TreeItem *ti;
   355 	for (int i=0; i<branchCount(); ++i)
   356     {	
   357 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   358 		if (ti != NULL) return ti;
   359     }
   360 	
   361 
   362 	// Search myself
   363     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   364 		return this;
   365 
   366 	// Search images
   367 	ImageItem *ii;
   368     for (int i=0; i<imageCount(); ++i )
   369 	{
   370 		ii=getImageNum (i);
   371 		LinkableMapObj *mo=ii->getLMO();
   372 		if (mo && mo->isInClickBox(p) && 
   373 			(ii != excludeTI) && 
   374 			this!= excludeTI &&
   375 			mo->isVisibleObj() 
   376 		) return ii;
   377 	}
   378 	return NULL;
   379 }
   380 
   381 TreeItem* BranchItem::findID (QString sid)	//FIXME-3 move to TreeItem	//FIXME-4 search images
   382 {
   383 	// Search branches
   384     TreeItem *ti;
   385 	for (int i=0; i<branchCount(); ++i)
   386     {	
   387 		ti=getBranchNum(i)->findID (sid);
   388 		if (ti != NULL) return ti;
   389     }
   390 	
   391 	// Search myself
   392 	if (sid==objID) return this;
   393 
   394 
   395 /*
   396 	// Search float images 
   397     for (int i=0; i<floatimage.size(); ++i )
   398 		if (floatimage.at(i)->inBox(p) && 
   399 			(floatimage.at(i) != excludeLMO) && 
   400 			floatimage.at(i)->getParObj()!= excludeLMO &&
   401 			floatimage.at(i)->isVisibleObj() 
   402 		) return floatimage.at(i);
   403 */
   404     return NULL;
   405 }
   406 
   407 void BranchItem::updateStyles()
   408 {
   409 	// FIXME-5 compare also MapItem::initLMO...
   410 
   411 	if (lmo && parentItem != rootItem)
   412 	{
   413 		lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   414 	}
   415 }
   416 
   417 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   418 {
   419 	return (BranchObj*)lmo;
   420 }
   421 
   422 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	// FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
   423 {
   424 	BranchObj *newbo;
   425 	newbo=new BranchObj(scene,this);
   426 	lmo=newbo;
   427 
   428 	if (parentItem==rootItem)
   429 	{
   430 		newbo->setParObj(NULL);
   431 		newbo->setFrameType (FrameObj::Rectangle);
   432 	} else
   433 	{
   434 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   435 		// Set visibility depending on parents
   436 		if (parentItem!=rootItem && 
   437 			( ((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() ) )
   438 			newbo->setVisibility (false);
   439 	}
   440 	newbo->setDefAttr(BranchObj::NewBranch);
   441 	initLMO();
   442 
   443 	if (!getHeading().isEmpty() ) 
   444 	{
   445 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   446 		newbo->setColor (headingColor);
   447 	}	
   448 		
   449 	//newbo->updateLinkGeometry();	//FIXME-3
   450 
   451 	return newbo;
   452 }
   453