branchitem.cpp
author insilmaril
Wed, 20 May 2009 15:40:14 +0000
changeset 772 e3f722759c7e
parent 771 01f2f6d6789d
child 773 340bc29da9a0
permissions -rw-r--r--
Fixed segfault when closing a map
     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 	/* 
    65 	// save area, if not scrolled	// FIXME-3 not needed if HTML is rewritten...
    66 									// also we should check if _any_ of parents is scrolled
    67 	QString areaAttr;
    68 	if (!((BranchObj*)(parObj))->isScrolled() )
    69 	{
    70 		areaAttr=
    71 			attribut("x1",QString().setNum(absPos.x()-offset.x())) +
    72 			attribut("y1",QString().setNum(absPos.y()-offset.y())) +
    73 			attribut("x2",QString().setNum(absPos.x()+width()-offset.x())) +
    74 			attribut("y2",QString().setNum(absPos.y()+height()-offset.y()));
    75 
    76 	} else
    77 		areaAttr="";
    78 	
    79 	// Providing an ID for a branch makes export to XHTML easier
    80 	QString idAttr;
    81 	if (countXLinks()>0)
    82 		idAttr=attribut ("id",model->getSelectString(this)); //TODO directly access model
    83 	else
    84 		idAttr="";
    85 
    86 	*/
    87     s=beginElement ("branch" 
    88 		+getAttr()
    89 	//	+getOrnXMLAttr() 
    90 		+scrolledAttr 
    91 	//	+areaAttr 
    92 	//	+idAttr 
    93 	//	+getIncludeImageAttr() 
    94 		);
    95     incIndent();
    96 
    97 	// save heading
    98     s+=valueElement("heading", getHeading(),
    99 		attribut ("textColor",QColor( bo->getColor()).name()));
   100 
   101 /*
   102 	// Save frame
   103 	if (frame->getFrameType()!=FrameObj::NoFrame) 
   104 		s+=frame->saveToDir ();
   105 */		
   106 
   107 	// save names of flags set
   108 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   109 	
   110 /*	
   111 	// Save FloatImages
   112 	for (int i=0; i<floatimage.size(); ++i)
   113 		s+=floatimage.at(i)->saveToDir (tmpdir,prefix);
   114 */
   115 
   116 	// save note
   117 	if (!note.isEmpty() )
   118 		s+=note.saveToDir();
   119 	
   120 	// Save branches
   121 	int i=0;
   122 	TreeItem *ti=getBranchNum(i);
   123 	while (ti)
   124 	{
   125 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   126 		i++;
   127 		ti=getBranchNum(i);
   128 	}	
   129 
   130 	/*
   131 	// Save XLinks
   132 	QString ol;	// old link
   133 	QString cl;	// current link
   134 	for (int i=0; i<xlink.size(); ++i)
   135 	{
   136 		cl=xlink.at(i)->saveToDir();
   137 		if (cl!=ol)
   138 		{
   139 			s+=cl;
   140 			ol=cl;
   141 		} else
   142 		{
   143 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   144 		}
   145 	}	
   146 	*/
   147 
   148     decIndent();
   149     s+=endElement   ("branch");
   150     return s;
   151 }
   152 
   153 void BranchItem::updateVisibility()	// FIXME-3	Check if this is needed after all...
   154 {
   155 }
   156 
   157 void BranchItem::setVymLink(QString s)
   158 {
   159 	if (!s.isEmpty())
   160 	{
   161 		// We need the relative (from loading) 
   162 		// or absolute path (from User event)
   163 		// and build the absolute path.
   164 		// Note: If we have relative, use path of
   165 		// current map to build absolute path
   166 		QDir d(s);
   167 		if (!d.path().startsWith ("/"))
   168 		{
   169 			QString p=model->getDestPath();
   170 			int i=p.findRev("/",-1);
   171 			d.setPath(p.left(i)+"/"+s);
   172 			d.convertToAbs();
   173 		}
   174 		vymLink=d.path();
   175 		//FIXME-2 systemFlags->activate("vymLink");
   176 	}	
   177 	else	
   178 	{
   179 		//FIXME-2 systemFlags->deactivate("vymLink");
   180 		vymLink="";
   181 	}	
   182 	/* FIXME-2
   183 	calcBBoxSize();			// recalculate bbox
   184     positionBBox();			// rearrange contents
   185 	forceReposition();
   186 	*/
   187 }
   188 
   189 QString BranchItem::getVymLink()
   190 {
   191 	return vymLink;
   192 }
   193 
   194 void BranchItem::setHeadingColor (QColor color)
   195 {
   196 	TreeItem::setHeadingColor (color);
   197 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   198 }
   199 
   200 void BranchItem::unScroll()
   201 {
   202 	if (tmpUnscrolled) resetTmpUnscroll();
   203 	if (scrolled) toggleScroll();
   204 }
   205 
   206 bool BranchItem::toggleScroll()	
   207 {
   208 	BranchObj *bo=NULL;
   209 	if (scrolled)
   210 	{
   211 		scrolled=false;
   212 		systemFlags.deactivate("system-scrolledright");
   213 		if (branchCounter>0)
   214 		{
   215 			for (int i=0;i<branchCounter;++i)
   216 			{
   217 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   218 				if (bo) bo->setVisibility(true);
   219 			}
   220 		}
   221 	} else
   222 	{
   223 		scrolled=true;
   224 		systemFlags.activate("system-scrolledright");
   225 		if (branchCounter>0)
   226 		{
   227 			for (int i=0;i<branchCounter;++i)
   228 			{
   229 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   230 				if (bo) bo->setVisibility(false);
   231 			}
   232 		}
   233 	}
   234 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   235 	return true;
   236 }
   237 
   238 bool BranchItem::isScrolled()
   239 {
   240 	return scrolled;
   241 }
   242 
   243 bool BranchItem::hasScrolledParent(BranchItem *start)
   244 {
   245 	// Calls parents recursivly to
   246 	// find out, if we are scrolled at all.
   247 	// But ignore myself, just look at parents.
   248 
   249 	if (this !=start && scrolled) return true;
   250 
   251 	BranchItem* bi=(BranchItem*)parentItem;
   252 	if (bi) 
   253 		return bi->hasScrolledParent(start);
   254 	else
   255 		return false;
   256 }
   257 
   258 void BranchItem::tmpUnscroll()
   259 {
   260 	// Unscroll parent (recursivly)
   261 	BranchItem * bi=(BranchItem*)parentItem;
   262 	if (bi) bi->tmpUnscroll();
   263 		
   264 	// Unscroll myself
   265 	if (scrolled)
   266 	{
   267 		tmpUnscrolled=true;
   268 		// FIXME-1 systemFlags->activate("tmpUnscrolledright");
   269 		toggleScroll();
   270 	}	
   271 }
   272 
   273 void BranchItem::resetTmpUnscroll()
   274 {
   275 	// Unscroll parent (recursivly)
   276 	BranchItem * bi=(BranchItem*)parentItem;
   277 	if (bi) bi->resetTmpUnscroll();
   278 		
   279 	// Unscroll myself
   280 	if (tmpUnscrolled)
   281 	{
   282 		tmpUnscrolled=false;
   283 		// FIXME-1 systemFlags->deactivate("tmpUnscrolledright");
   284 		toggleScroll();
   285 	}	
   286 }
   287 
   288 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   289 {
   290 	// Search branches
   291 	TreeItem *ti;
   292 	for (int i=0; i<branchCount(); ++i)
   293     {	
   294 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   295 		if (ti != NULL) return ti;
   296     }
   297 	
   298 
   299 	// Search myself
   300     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   301 		return this;
   302 
   303 /* FIXME-2 // Search float images
   304     for (int i=0; i<floatimage.size(); ++i )
   305 		if (floatimage.at(i)->isInClickBox(p) && 
   306 			(floatimage.at(i) != excludeTI) && 
   307 			floatimage.at(i)->getParObj()!= excludeTI &&
   308 			floatimage.at(i)->isVisibleObj() 
   309 		) return floatimage.at(i)->getTreeItem();
   310 */
   311     return NULL;
   312 }
   313 
   314 TreeItem* BranchItem::findID (QString sid)
   315 {
   316 	// Search branches
   317     TreeItem *ti;
   318 	for (int i=0; i<branchCount(); ++i)
   319     {	
   320 		ti=getBranchNum(i)->findID (sid);
   321 		if (ti != NULL) return ti;
   322     }
   323 	
   324 	// Search myself
   325 	if (sid==objID) return this;
   326 
   327 
   328 /*
   329 	// Search float images
   330     for (int i=0; i<floatimage.size(); ++i )
   331 		if (floatimage.at(i)->inBox(p) && 
   332 			(floatimage.at(i) != excludeLMO) && 
   333 			floatimage.at(i)->getParObj()!= excludeLMO &&
   334 			floatimage.at(i)->isVisibleObj() 
   335 		) return floatimage.at(i);
   336 */
   337     return NULL;
   338 }
   339 
   340 
   341 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   342 {
   343 	return (BranchObj*)lmo;
   344 }
   345 
   346 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   347 {
   348 	// Initialize BranchObj, order of things is important...
   349     BranchObj* newbo=new BranchObj(scene);
   350 	lmo=newbo;
   351 	BranchObj* parbo=(BranchObj*)(parentItem->getLMO());
   352     newbo->setParObj(parbo);
   353 	newbo->setTreeItem (this);
   354 	newbo->setDefAttr(BranchObj::NewBranch);
   355 	if (((BranchItem*)parentItem)->scrolled || !parbo->isVisibleObj() )
   356 		newbo->setVisibility (false);
   357 	
   358 	initLMO();
   359 
   360 	if (!getHeading().isEmpty() ) 
   361 	{
   362 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   363 		newbo->setColor (headingColor);
   364 	}	
   365 
   366 		
   367 	//newbo->updateLink();	//FIXME-3
   368 
   369 	return newbo;
   370 }
   371