branchitem.cpp
author insilmaril
Wed, 29 Apr 2009 18:46:31 +0000
changeset 760 59614eaf5fbb
parent 758 04039e47ac74
child 762 ffb95cd03156
permissions -rw-r--r--
started to save data like positions outside of MapObj & Co
     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 	//	+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 	// save names of flags set
   107 	s+=standardFlags->saveToDir(tmpdir,prefix,0);
   108 	
   109 	// Save FloatImages
   110 	for (int i=0; i<floatimage.size(); ++i)
   111 		s+=floatimage.at(i)->saveToDir (tmpdir,prefix);
   112 */
   113 
   114 	// save note
   115 	if (!note.isEmpty() )
   116 		s+=note.saveToDir();
   117 	
   118 	// Save branches
   119 	int i=0;
   120 	TreeItem *ti=getBranchNum(i);
   121 	while (ti)
   122 	{
   123 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   124 		i++;
   125 		ti=getBranchNum(i);
   126 	}	
   127 
   128 	/*
   129 	// Save XLinks
   130 	QString ol;	// old link
   131 	QString cl;	// current link
   132 	for (int i=0; i<xlink.size(); ++i)
   133 	{
   134 		cl=xlink.at(i)->saveToDir();
   135 		if (cl!=ol)
   136 		{
   137 			s+=cl;
   138 			ol=cl;
   139 		} else
   140 		{
   141 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   142 		}
   143 	}	
   144 	*/
   145 
   146     decIndent();
   147     s+=endElement   ("branch");
   148     return s;
   149 }
   150 
   151 void BranchItem::updateVisibility()	// FIXME-1
   152 {
   153 }
   154 
   155 void BranchItem::setVymLink(QString s)
   156 {
   157 	if (!s.isEmpty())
   158 	{
   159 		// We need the relative (from loading) 
   160 		// or absolute path (from User event)
   161 		// and build the absolute path.
   162 		// Note: If we have relative, use path of
   163 		// current map to build absolute path
   164 		QDir d(s);
   165 		if (!d.path().startsWith ("/"))
   166 		{
   167 			QString p=model->getDestPath();
   168 			int i=p.findRev("/",-1);
   169 			d.setPath(p.left(i)+"/"+s);
   170 			d.convertToAbs();
   171 		}
   172 		vymLink=d.path();
   173 		//FIXME-2 systemFlags->activate("vymLink");
   174 	}	
   175 	else	
   176 	{
   177 		//FIXME-2 systemFlags->deactivate("vymLink");
   178 		vymLink="";
   179 	}	
   180 	/* FIXME-2
   181 	calcBBoxSize();			// recalculate bbox
   182     positionBBox();			// rearrange contents
   183 	forceReposition();
   184 	*/
   185 }
   186 
   187 QString BranchItem::getVymLink()
   188 {
   189 	return vymLink;
   190 }
   191 
   192 void BranchItem::setHeadingColor (QColor color)
   193 {
   194 	TreeItem::setHeadingColor (color);
   195 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   196 }
   197 
   198 void BranchItem::unScroll()
   199 {
   200 	if (tmpUnscrolled) resetTmpUnscroll();
   201 	if (scrolled) toggleScroll();
   202 }
   203 
   204 void BranchItem::toggleScroll()
   205 {
   206 	if (scrolled)
   207 	{
   208 		scrolled=false;
   209 		//FIXME-1 systemFlags->deactivate("scrolledright");
   210 		/*
   211 		for (int i=0; i<branch.size(); ++i)
   212 			branch.at(i)->setVisibility(true);
   213 		*/	
   214 	} else
   215 	{
   216 		scrolled=true;
   217 		/*
   218 		//FIXME-1 systemFlags->activate("scrolledright");
   219 		for (int i=0; i<branch.size(); ++i)
   220 			branch.at(i)->setVisibility(false);
   221 		*/	
   222 	}
   223 	/*	
   224 	calcBBoxSize();
   225 	positionBBox();	
   226 	move (absPos.x(), absPos.y() );
   227 	forceReposition();
   228 	*/
   229 }
   230 
   231 bool BranchItem::isScrolled()
   232 {
   233 	return scrolled;
   234 }
   235 
   236 bool BranchItem::hasScrolledParent(BranchItem *start)
   237 {
   238 	// Calls parents recursivly to
   239 	// find out, if we are scrolled at all.
   240 	// But ignore myself, just look at parents.
   241 
   242 	if (this !=start && scrolled) return true;
   243 
   244 	BranchItem* bi=(BranchItem*)parentItem;
   245 	if (bi) 
   246 		return bi->hasScrolledParent(start);
   247 	else
   248 		return false;
   249 }
   250 
   251 void BranchItem::tmpUnscroll()
   252 {
   253 	// Unscroll parent (recursivly)
   254 	BranchItem * bi=(BranchItem*)parentItem;
   255 	if (bi) bi->tmpUnscroll();
   256 		
   257 	// Unscroll myself
   258 	if (scrolled)
   259 	{
   260 		tmpUnscrolled=true;
   261 		// FIXME-1 systemFlags->activate("tmpUnscrolledright");
   262 		toggleScroll();
   263 	}	
   264 }
   265 
   266 void BranchItem::resetTmpUnscroll()
   267 {
   268 	// Unscroll parent (recursivly)
   269 	BranchItem * bi=(BranchItem*)parentItem;
   270 	if (bi) bi->resetTmpUnscroll();
   271 		
   272 	// Unscroll myself
   273 	if (tmpUnscrolled)
   274 	{
   275 		tmpUnscrolled=false;
   276 		// FIXME-1 systemFlags->deactivate("tmpUnscrolledright");
   277 		toggleScroll();
   278 	}	
   279 }
   280 
   281 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   282 {
   283 	// Search branches
   284 	TreeItem *ti;
   285 	for (int i=0; i<branchCount(); ++i)
   286     {	
   287 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   288 		if (ti != NULL) return ti;
   289     }
   290 	
   291 
   292 	// Search myself
   293     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   294 		return this;
   295 
   296 /* FIXME-2 // Search float images
   297     for (int i=0; i<floatimage.size(); ++i )
   298 		if (floatimage.at(i)->isInClickBox(p) && 
   299 			(floatimage.at(i) != excludeTI) && 
   300 			floatimage.at(i)->getParObj()!= excludeTI &&
   301 			floatimage.at(i)->isVisibleObj() 
   302 		) return floatimage.at(i)->getTreeItem();
   303 */
   304     return NULL;
   305 }
   306 
   307 TreeItem* BranchItem::findID (QString sid)
   308 {
   309 	// Search branches
   310     TreeItem *ti;
   311 	for (int i=0; i<branchCount(); ++i)
   312     {	
   313 		ti=getBranchNum(i)->findID (sid);
   314 		if (ti != NULL) return ti;
   315     }
   316 	
   317 	// Search myself
   318 	if (sid==objID) return this;
   319 
   320 
   321 /*
   322 	// Search float images
   323     for (int i=0; i<floatimage.size(); ++i )
   324 		if (floatimage.at(i)->inBox(p) && 
   325 			(floatimage.at(i) != excludeLMO) && 
   326 			floatimage.at(i)->getParObj()!= excludeLMO &&
   327 			floatimage.at(i)->isVisibleObj() 
   328 		) return floatimage.at(i);
   329 */
   330     return NULL;
   331 }
   332 
   333 
   334 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   335 {
   336 	return (BranchObj*)lmo;
   337 }
   338 
   339 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   340 {
   341 	// Initialize BranchObj, order of things is important...
   342     BranchObj* newbo=new BranchObj(scene);
   343 	lmo=newbo;
   344     newbo->setParObj(parent()->getLMO() );
   345 	newbo->setTreeItem (this);
   346 	newbo->setDefAttr(BranchObj::NewBranch);
   347 	
   348 	initLMO();
   349 
   350 	if (!getHeading().isEmpty() ) 
   351 	{
   352 		newbo->updateHeading();
   353 		newbo->setColor (headingColor);
   354 	}	
   355 
   356 		
   357 	//newbo->updateLink();	//FIXME-3
   358 
   359 	return newbo;
   360 }
   361