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