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