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