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