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