branchitem.cpp
author insilmaril
Mon, 18 May 2009 11:22:41 +0000
changeset 771 01f2f6d6789d
parent 769 a6931cd6309a
child 772 e3f722759c7e
permissions -rw-r--r--
Fixed addBranchBefore
     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 
   209 	if (!branchCount()>0) return false;
   210 	BranchObj *bo=NULL;
   211 	if (scrolled)
   212 	{
   213 		scrolled=false;
   214 		systemFlags.deactivate("system-scrolledright");
   215 		if (branchCounter>0)
   216 		{
   217 			for (int i=0;i<branchCounter;++i)
   218 			{
   219 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   220 				if (bo) bo->setVisibility(true);
   221 			}
   222 		}
   223 		model->reposition();
   224 	} else
   225 	{
   226 		scrolled=true;
   227 		systemFlags.activate("system-scrolledright");
   228 		if (branchCounter>0)
   229 		{
   230 			for (int i=0;i<branchCounter;++i)
   231 			{
   232 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   233 				if (bo) bo->setVisibility(false);
   234 			}
   235 		}
   236 	}
   237 	return true;
   238 }
   239 
   240 bool BranchItem::isScrolled()
   241 {
   242 	return scrolled;
   243 }
   244 
   245 bool BranchItem::hasScrolledParent(BranchItem *start)
   246 {
   247 	// Calls parents recursivly to
   248 	// find out, if we are scrolled at all.
   249 	// But ignore myself, just look at parents.
   250 
   251 	if (this !=start && scrolled) return true;
   252 
   253 	BranchItem* bi=(BranchItem*)parentItem;
   254 	if (bi) 
   255 		return bi->hasScrolledParent(start);
   256 	else
   257 		return false;
   258 }
   259 
   260 void BranchItem::tmpUnscroll()
   261 {
   262 	// Unscroll parent (recursivly)
   263 	BranchItem * bi=(BranchItem*)parentItem;
   264 	if (bi) bi->tmpUnscroll();
   265 		
   266 	// Unscroll myself
   267 	if (scrolled)
   268 	{
   269 		tmpUnscrolled=true;
   270 		// FIXME-1 systemFlags->activate("tmpUnscrolledright");
   271 		toggleScroll();
   272 	}	
   273 }
   274 
   275 void BranchItem::resetTmpUnscroll()
   276 {
   277 	// Unscroll parent (recursivly)
   278 	BranchItem * bi=(BranchItem*)parentItem;
   279 	if (bi) bi->resetTmpUnscroll();
   280 		
   281 	// Unscroll myself
   282 	if (tmpUnscrolled)
   283 	{
   284 		tmpUnscrolled=false;
   285 		// FIXME-1 systemFlags->deactivate("tmpUnscrolledright");
   286 		toggleScroll();
   287 	}	
   288 }
   289 
   290 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   291 {
   292 	// Search branches
   293 	TreeItem *ti;
   294 	for (int i=0; i<branchCount(); ++i)
   295     {	
   296 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   297 		if (ti != NULL) return ti;
   298     }
   299 	
   300 
   301 	// Search myself
   302     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   303 		return this;
   304 
   305 /* FIXME-2 // Search float images
   306     for (int i=0; i<floatimage.size(); ++i )
   307 		if (floatimage.at(i)->isInClickBox(p) && 
   308 			(floatimage.at(i) != excludeTI) && 
   309 			floatimage.at(i)->getParObj()!= excludeTI &&
   310 			floatimage.at(i)->isVisibleObj() 
   311 		) return floatimage.at(i)->getTreeItem();
   312 */
   313     return NULL;
   314 }
   315 
   316 TreeItem* BranchItem::findID (QString sid)
   317 {
   318 	// Search branches
   319     TreeItem *ti;
   320 	for (int i=0; i<branchCount(); ++i)
   321     {	
   322 		ti=getBranchNum(i)->findID (sid);
   323 		if (ti != NULL) return ti;
   324     }
   325 	
   326 	// Search myself
   327 	if (sid==objID) return this;
   328 
   329 
   330 /*
   331 	// Search float images
   332     for (int i=0; i<floatimage.size(); ++i )
   333 		if (floatimage.at(i)->inBox(p) && 
   334 			(floatimage.at(i) != excludeLMO) && 
   335 			floatimage.at(i)->getParObj()!= excludeLMO &&
   336 			floatimage.at(i)->isVisibleObj() 
   337 		) return floatimage.at(i);
   338 */
   339     return NULL;
   340 }
   341 
   342 
   343 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   344 {
   345 	return (BranchObj*)lmo;
   346 }
   347 
   348 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   349 {
   350 	// Initialize BranchObj, order of things is important...
   351     BranchObj* newbo=new BranchObj(scene);
   352 	lmo=newbo;
   353     newbo->setParObj(parent()->getLMO() );
   354 	newbo->setTreeItem (this);
   355 	newbo->setDefAttr(BranchObj::NewBranch);
   356 	
   357 	initLMO();
   358 
   359 	if (!getHeading().isEmpty() ) 
   360 	{
   361 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   362 		newbo->setColor (headingColor);
   363 	}	
   364 
   365 		
   366 	//newbo->updateLink();	//FIXME-3
   367 
   368 	return newbo;
   369 }
   370