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