branchitem.cpp
author insilmaril
Wed, 03 Jun 2009 20:37:17 +0000
changeset 775 6e4b586aa88a
parent 773 340bc29da9a0
child 776 25e634a7e1dc
permissions -rw-r--r--
Unscrolling temporary 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):TreeItem (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 		+getAttr()
    97 	//	+getOrnXMLAttr() 
    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 /*	
   110 	// Save frame  //FIXME-2
   111 	if (frame->getFrameType()!=FrameObj::NoFrame) 
   112 		s+=frame->saveToDir ();
   113 */		
   114 
   115 	// save names of flags set
   116 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   117 	
   118 /*	
   119 	// Save FloatImages
   120 	for (int i=0; i<floatimage.size(); ++i)
   121 		s+=floatimage.at(i)->saveToDir (tmpdir,prefix);
   122 */
   123 
   124 	// save note
   125 	if (!note.isEmpty() )
   126 		s+=note.saveToDir();
   127 	
   128 	// Save branches
   129 	int i=0;
   130 	TreeItem *ti=getBranchNum(i);
   131 	while (ti)
   132 	{
   133 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   134 		i++;
   135 		ti=getBranchNum(i);
   136 	}	
   137 
   138 	/*
   139 	// Save XLinks
   140 	QString ol;	// old link
   141 	QString cl;	// current link
   142 	for (int i=0; i<xlink.size(); ++i)
   143 	{
   144 		cl=xlink.at(i)->saveToDir();
   145 		if (cl!=ol)
   146 		{
   147 			s+=cl;
   148 			ol=cl;
   149 		} else
   150 		{
   151 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   152 		}
   153 	}	
   154 	*/
   155 
   156     decIndent();
   157     s+=endElement   (elementName);
   158     return s;
   159 }
   160 
   161 void BranchItem::updateVisibility()	// FIXME-3	Check if this is needed after all...
   162 {
   163 }
   164 
   165 void BranchItem::setVymLink(QString s)
   166 {
   167 	if (!s.isEmpty())
   168 	{
   169 		// We need the relative (from loading) 
   170 		// or absolute path (from User event)
   171 		// and build the absolute path.
   172 		// Note: If we have relative, use path of
   173 		// current map to build absolute path
   174 		QDir d(s);
   175 		if (!d.path().startsWith ("/"))
   176 		{
   177 			QString p=model->getDestPath();
   178 			int i=p.findRev("/",-1);
   179 			d.setPath(p.left(i)+"/"+s);
   180 			d.convertToAbs();
   181 		}
   182 		vymLink=d.path();
   183 		//FIXME-2 systemFlags->activate("vymLink");
   184 	}	
   185 	else	
   186 	{
   187 		//FIXME-2 systemFlags->deactivate("vymLink");
   188 		vymLink="";
   189 	}	
   190 	/* FIXME-2
   191 	calcBBoxSize();			// recalculate bbox
   192     positionBBox();			// rearrange contents
   193 	forceReposition();
   194 	*/
   195 }
   196 
   197 QString BranchItem::getVymLink()
   198 {
   199 	return vymLink;
   200 }
   201 
   202 void BranchItem::setHeadingColor (QColor color)
   203 {
   204 	TreeItem::setHeadingColor (color);
   205 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   206 }
   207 
   208 void BranchItem::unScroll()
   209 {
   210 	if (tmpUnscrolled) resetTmpUnscroll();
   211 	if (scrolled) toggleScroll();
   212 }
   213 
   214 bool BranchItem::toggleScroll()	
   215 {
   216 	BranchObj *bo=NULL;
   217 	if (scrolled)
   218 	{
   219 		scrolled=false;
   220 		systemFlags.deactivate("system-scrolledright");
   221 		if (branchCounter>0)
   222 		{
   223 			for (int i=0;i<branchCounter;++i)
   224 			{
   225 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   226 				if (bo) bo->setVisibility(true);
   227 			}
   228 		}
   229 	} else
   230 	{
   231 		scrolled=true;
   232 		systemFlags.activate("system-scrolledright");
   233 		if (branchCounter>0)
   234 		{
   235 			for (int i=0;i<branchCounter;++i)
   236 			{
   237 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   238 				if (bo) bo->setVisibility(false);
   239 			}
   240 		}
   241 	}
   242 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   243 	return true;
   244 }
   245 
   246 bool BranchItem::isScrolled()
   247 {
   248 	return scrolled;
   249 }
   250 
   251 bool BranchItem::hasScrolledParent(BranchItem *start)
   252 {
   253 	// Calls parents recursivly to
   254 	// find out, if we are scrolled at all.
   255 	// But ignore myself, just look at parents.
   256 
   257 	if (this !=start && scrolled) return true;
   258 
   259 	BranchItem* bi=(BranchItem*)parentItem;
   260 	if (bi && bi->isBranchLikeType() ) 
   261 		return bi->hasScrolledParent(start);
   262 	else
   263 		return false;
   264 }
   265 
   266 void BranchItem::tmpUnscroll()
   267 {
   268 	// Unscroll parent (recursivly)
   269 	BranchItem * pi=(BranchItem*)parentItem;
   270 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   271 		
   272 	// Unscroll myself
   273 	if (scrolled)
   274 	{
   275 		tmpUnscrolled=true;
   276 		systemFlags.activate("system-tmpUnscrolledRight");
   277 		toggleScroll();
   278 		model->emitDataHasChanged (this);
   279 	}	
   280 }
   281 
   282 void BranchItem::resetTmpUnscroll()
   283 {
   284 	// Unscroll parent (recursivly)
   285 	BranchItem * pi=(BranchItem*)parentItem;
   286 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   287 		
   288 	// Unscroll myself
   289 	if (tmpUnscrolled)
   290 	{
   291 		tmpUnscrolled=false;
   292 		systemFlags.deactivate("system-tmpUnscrolledRight");
   293 		toggleScroll();
   294 		model->emitDataHasChanged (this);
   295 	}	
   296 }
   297 
   298 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   299 {
   300 	// Search branches
   301 	TreeItem *ti;
   302 	for (int i=0; i<branchCount(); ++i)
   303     {	
   304 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   305 		if (ti != NULL) return ti;
   306     }
   307 	
   308 
   309 	// Search myself
   310     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   311 		return this;
   312 
   313 /* FIXME-2 // Search float images
   314     for (int i=0; i<floatimage.size(); ++i )
   315 		if (floatimage.at(i)->isInClickBox(p) && 
   316 			(floatimage.at(i) != excludeTI) && 
   317 			floatimage.at(i)->getParObj()!= excludeTI &&
   318 			floatimage.at(i)->isVisibleObj() 
   319 		) return floatimage.at(i)->getTreeItem();
   320 */
   321     return NULL;
   322 }
   323 
   324 TreeItem* BranchItem::findID (QString sid)
   325 {
   326 	// Search branches
   327     TreeItem *ti;
   328 	for (int i=0; i<branchCount(); ++i)
   329     {	
   330 		ti=getBranchNum(i)->findID (sid);
   331 		if (ti != NULL) return ti;
   332     }
   333 	
   334 	// Search myself
   335 	if (sid==objID) return this;
   336 
   337 
   338 /*
   339 	// Search float images
   340     for (int i=0; i<floatimage.size(); ++i )
   341 		if (floatimage.at(i)->inBox(p) && 
   342 			(floatimage.at(i) != excludeLMO) && 
   343 			floatimage.at(i)->getParObj()!= excludeLMO &&
   344 			floatimage.at(i)->isVisibleObj() 
   345 		) return floatimage.at(i);
   346 */
   347     return NULL;
   348 }
   349 
   350 void BranchItem::updateStyles()
   351 {
   352 	// FIXME-5 compare also MapItem::initLMO...
   353 
   354 	if (lmo)
   355 	{
   356 		lmo->setParObj (parentItem->getLMO() );
   357 	}
   358 }
   359 
   360 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   361 {
   362 	return (BranchObj*)lmo;
   363 }
   364 
   365 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   366 {
   367 	BranchObj *newbo;
   368 	newbo=new BranchObj(scene);
   369 	newbo->setTreeItem (this);
   370 	lmo=newbo;
   371 
   372 	if (parentItem==rootItem)
   373 	{
   374 		newbo->setParObj(NULL);
   375 		newbo->setFrameType (FrameObj::Rectangle);
   376 	} else
   377 	{
   378 		newbo->setParObj(parentItem->getLMO() );
   379 		// Set visibility depending on parents
   380 		if (((BranchItem*)parentItem)->scrolled || !parentItem->getLMO()->isVisibleObj() )
   381 			newbo->setVisibility (false);
   382 	}
   383 	newbo->setDefAttr(BranchObj::NewBranch);
   384 	initLMO();
   385 
   386 	if (!getHeading().isEmpty() ) 
   387 	{
   388 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   389 		newbo->setColor (headingColor);
   390 	}	
   391 		
   392 	//newbo->updateLink();	//FIXME-3
   393 
   394 	return newbo;
   395 }
   396