branchitem.cpp
author insilmaril
Mon, 29 Jun 2009 10:27:42 +0000
changeset 777 8acac4fade1b
parent 776 25e634a7e1dc
child 779 1fb50e79661c
permissions -rw-r--r--
more fixes
     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 	// save area, if not scrolled	// FIXME-5 not needed if HTML is rewritten...
    65 									// also we should check if _any_ of parents is scrolled
    66 	QString areaAttr;
    67 	if (lmo && parentItem->isBranchLikeType() && !((BranchItem*)parentItem)->isScrolled() )
    68 	{
    69 		qreal x=lmo->getAbsPos().x();
    70 		qreal y=lmo->getAbsPos().y();
    71 		areaAttr=
    72 			attribut("x1",QString().setNum(x-offset.x())) +
    73 			attribut("y1",QString().setNum(y-offset.y())) +
    74 			attribut("x2",QString().setNum(x+lmo->width()-offset.x())) +
    75 			attribut("y2",QString().setNum(y+lmo->height()-offset.y()));
    76 
    77 	} else
    78 		areaAttr="";
    79 	
    80 	/*	
    81 	// FIXME-3 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 	QString elementName;
    90 	if (parentItem==rootItem)
    91 		elementName="mapcenter";
    92 	else	
    93 		elementName="branch";
    94 
    95     s=beginElement (elementName
    96 		+getAttr()
    97 		+scrolledAttr 
    98 	//	+areaAttr 
    99 	//	+idAttr 
   100 	//	+getIncludeImageAttr() 
   101 		);
   102     incIndent();
   103 
   104 	// save heading
   105     s+=valueElement("heading", getHeading(),
   106 		attribut ("textColor",QColor( bo->getColor()).name()));
   107 
   108 	// Save frame  //FIXME-4 not saved if there is no LMO
   109 	if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame) 
   110 		s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
   111 
   112 	// save names of flags set
   113 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   114 	
   115 	// Save Images
   116 	for (int i=0; i<imageCount(); ++i)
   117 		s+=getImageNum(i)->saveToDir (tmpdir,prefix);
   118 
   119 	// save note
   120 	if (!note.isEmpty() )
   121 		s+=note.saveToDir();
   122 	
   123 	// Save branches
   124 	int i=0;
   125 	TreeItem *ti=getBranchNum(i);
   126 	while (ti)
   127 	{
   128 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   129 		i++;
   130 		ti=getBranchNum(i);
   131 	}	
   132 
   133 	/*
   134 	// Save XLinks
   135 	QString ol;	// old link
   136 	QString cl;	// current link
   137 	for (int i=0; i<xlink.size(); ++i)
   138 	{
   139 		cl=xlink.at(i)->saveToDir();
   140 		if (cl!=ol)
   141 		{
   142 			s+=cl;
   143 			ol=cl;
   144 		} else
   145 		{
   146 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   147 		}
   148 	}	
   149 	*/
   150 
   151     decIndent();
   152     s+=endElement   (elementName);
   153     return s;
   154 }
   155 
   156 void BranchItem::updateVisibility()	// FIXME-3	Check if this is needed after all...
   157 {
   158 }
   159 
   160 void BranchItem::setVymLink(QString s)
   161 {
   162 	if (!s.isEmpty())
   163 	{
   164 		// We need the relative (from loading) 
   165 		// or absolute path (from User event)
   166 		// and build the absolute path.
   167 		// Note: If we have relative, use path of
   168 		// current map to build absolute path
   169 		QDir d(s);
   170 		if (!d.path().startsWith ("/"))
   171 		{
   172 			QString p=model->getDestPath();
   173 			int i=p.findRev("/",-1);
   174 			d.setPath(p.left(i)+"/"+s);
   175 			d.convertToAbs();
   176 		}
   177 		vymLink=d.path();
   178 		//FIXME-2 systemFlags->activate("vymLink");
   179 	}	
   180 	else	
   181 	{
   182 		//FIXME-2 systemFlags->deactivate("vymLink");
   183 		vymLink="";
   184 	}	
   185 	/* FIXME-2
   186 	calcBBoxSize();			// recalculate bbox
   187     positionBBox();			// rearrange contents
   188 	forceReposition();
   189 	*/
   190 }
   191 
   192 QString BranchItem::getVymLink()
   193 {
   194 	return vymLink;
   195 }
   196 
   197 void BranchItem::setHeadingColor (QColor color)
   198 {
   199 	TreeItem::setHeadingColor (color);
   200 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   201 }
   202 
   203 void BranchItem::unScroll()
   204 {
   205 	if (tmpUnscrolled) resetTmpUnscroll();
   206 	if (scrolled) toggleScroll();
   207 }
   208 
   209 bool BranchItem::toggleScroll()	
   210 {
   211 	BranchObj *bo=NULL;
   212 	if (scrolled)
   213 	{
   214 		scrolled=false;
   215 		systemFlags.deactivate("system-scrolledright");
   216 		if (branchCounter>0)
   217 		{
   218 			for (int i=0;i<branchCounter;++i)
   219 			{
   220 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   221 				if (bo) bo->setVisibility(true);
   222 			}
   223 		}
   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 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   238 	return true;
   239 }
   240 
   241 bool BranchItem::isScrolled()
   242 {
   243 	return scrolled;
   244 }
   245 
   246 bool BranchItem::hasScrolledParent(BranchItem *start)
   247 {
   248 	// Calls parents recursivly to
   249 	// find out, if we are scrolled at all.
   250 	// But ignore myself, just look at parents.
   251 
   252 	if (this !=start && scrolled) return true;
   253 
   254 	BranchItem* bi=(BranchItem*)parentItem;
   255 	if (bi && bi->isBranchLikeType() ) 
   256 		return bi->hasScrolledParent(start);
   257 	else
   258 		return false;
   259 }
   260 
   261 void BranchItem::tmpUnscroll()
   262 {
   263 	// Unscroll parent (recursivly)
   264 	BranchItem * pi=(BranchItem*)parentItem;
   265 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   266 		
   267 	// Unscroll myself
   268 	if (scrolled)
   269 	{
   270 		tmpUnscrolled=true;
   271 		systemFlags.activate("system-tmpUnscrolledRight");
   272 		toggleScroll();
   273 		model->emitDataHasChanged (this);
   274 	}	
   275 }
   276 
   277 void BranchItem::resetTmpUnscroll()
   278 {
   279 	// Unscroll parent (recursivly)
   280 	BranchItem * pi=(BranchItem*)parentItem;
   281 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   282 		
   283 	// Unscroll myself
   284 	if (tmpUnscrolled)
   285 	{
   286 		tmpUnscrolled=false;
   287 		systemFlags.deactivate("system-tmpUnscrolledRight");
   288 		toggleScroll();
   289 		model->emitDataHasChanged (this);
   290 	}	
   291 }
   292 
   293 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   294 {
   295 	// Search branches
   296 	TreeItem *ti;
   297 	for (int i=0; i<branchCount(); ++i)
   298     {	
   299 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   300 		if (ti != NULL) return ti;
   301     }
   302 	
   303 
   304 	// Search myself
   305     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   306 		return this;
   307 
   308 	// Search images
   309 	ImageItem *ii;
   310     for (int i=0; i<imageCount(); ++i )
   311 	{
   312 		ii=getImageNum (i);
   313 		LinkableMapObj *mo=ii->getLMO();
   314 		if (mo && mo->isInClickBox(p) && 
   315 			(ii != excludeTI) && 
   316 			this!= excludeTI &&
   317 			mo->isVisibleObj() 
   318 		) return ii;
   319 	}
   320 	return NULL;
   321 }
   322 
   323 TreeItem* BranchItem::findID (QString sid)
   324 {
   325 	// Search branches
   326     TreeItem *ti;
   327 	for (int i=0; i<branchCount(); ++i)
   328     {	
   329 		ti=getBranchNum(i)->findID (sid);
   330 		if (ti != NULL) return ti;
   331     }
   332 	
   333 	// Search myself
   334 	if (sid==objID) return this;
   335 
   336 
   337 /*
   338 	// Search float images
   339     for (int i=0; i<floatimage.size(); ++i )
   340 		if (floatimage.at(i)->inBox(p) && 
   341 			(floatimage.at(i) != excludeLMO) && 
   342 			floatimage.at(i)->getParObj()!= excludeLMO &&
   343 			floatimage.at(i)->isVisibleObj() 
   344 		) return floatimage.at(i);
   345 */
   346     return NULL;
   347 }
   348 
   349 void BranchItem::updateStyles()
   350 {
   351 	// FIXME-5 compare also MapItem::initLMO...
   352 
   353 	if (lmo)
   354 	{
   355 		lmo->setParObj ( parentItem->getLMO() );
   356 	}
   357 }
   358 
   359 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   360 {
   361 	return (BranchObj*)lmo;
   362 }
   363 
   364 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   365 {
   366 	BranchObj *newbo;
   367 	newbo=new BranchObj(scene);
   368 	newbo->setTreeItem (this);
   369 	lmo=newbo;
   370 
   371 	if (parentItem==rootItem)
   372 	{
   373 		newbo->setParObj(NULL);
   374 		newbo->setFrameType (FrameObj::Rectangle);
   375 	} else
   376 	{
   377 		newbo->setParObj( parentItem->getLMO() );
   378 		// Set visibility depending on parents
   379 		if (((BranchItem*)parentItem)->scrolled || !parentItem->getLMO()->isVisibleObj() )
   380 			newbo->setVisibility (false);
   381 	}
   382 	newbo->setDefAttr(BranchObj::NewBranch);
   383 	initLMO();
   384 
   385 	if (!getHeading().isEmpty() ) 
   386 	{
   387 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   388 		newbo->setColor (headingColor);
   389 	}	
   390 		
   391 	//newbo->updateLink();	//FIXME-3
   392 
   393 	return newbo;
   394 }
   395