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