branchitem.cpp
author insilmaril
Mon, 03 Aug 2009 10:42:12 +0000
changeset 785 5987f9f15bac
parent 781 6cbf3c9cbd21
child 786 6269016c9905
permissions -rw-r--r--
Fixed problem with images included in branches. Added missing adaptormodel.* files
     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):MapItem (data,parent)
    11 {
    12 	//cout << "Constr. BranchItem\n";
    13 
    14 	scrolled=false;
    15 	tmpUnscrolled=false;
    16 	type=Branch;
    17 
    18 	includeImagesVer=false;
    19 	includeImagesHor=false;
    20 }
    21 
    22 BranchItem::~BranchItem()
    23 {
    24 	cout << "Destr. BranchItem "<<getHeadingStd()<<endl;
    25 	if (lmo) 
    26 	{
    27 		delete lmo;
    28 		lmo=NULL;
    29 	}
    30 }
    31 
    32 void BranchItem::copy (BranchItem *other)
    33 {
    34 	scrolled=other->scrolled;
    35 	tmpUnscrolled=other->tmpUnscrolled;
    36 }
    37 
    38 void BranchItem::insertBranch (int pos, BranchItem *branch)
    39 {
    40 	if (pos<0) pos=0;
    41 	if (pos>branchCounter) pos=branchCounter;
    42     childItems.insert(pos+branchOffset,branch);
    43 	branch->parentItem=this;
    44 	branch->setModel (model);
    45 
    46 	if (branchCounter==0)
    47 		branchOffset=childItems.count()-1;
    48 	branchCounter++;
    49 }
    50 
    51 QString BranchItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset) //FIXME-3 Check if everything is saved...
    52 {
    53 	// Cloudy stuff can be hidden during exports
    54 	if (hidden) return QString();
    55 
    56     QString s,a;
    57 	BranchObj *bo=(BranchObj*)lmo;
    58 
    59 	// Update of note is usually done while unselecting a branch
    60 	
    61 	QString scrolledAttr;
    62 	if (scrolled) 
    63 		scrolledAttr=attribut ("scrolled","yes");
    64 	else
    65 		scrolledAttr="";
    66 
    67 	// save area, if not scrolled	// FIXME-5 not needed if HTML is rewritten...
    68 									// also we should check if _any_ of parents is scrolled
    69 	QString areaAttr;
    70 	if (lmo && parentItem->isBranchLikeType() && !((BranchItem*)parentItem)->isScrolled() )
    71 	{
    72 		qreal x=lmo->getAbsPos().x();
    73 		qreal y=lmo->getAbsPos().y();
    74 		areaAttr=
    75 			attribut("x1",QString().setNum(x-offset.x())) +
    76 			attribut("y1",QString().setNum(y-offset.y())) +
    77 			attribut("x2",QString().setNum(x+lmo->width()-offset.x())) +
    78 			attribut("y2",QString().setNum(y+lmo->height()-offset.y()));
    79 
    80 	} else
    81 		areaAttr="";
    82 	
    83 	/*	
    84 	// FIXME-3 Providing an ID for a branch makes export to XHTML easier
    85 	QString idAttr;
    86 	if (countXLinks()>0)
    87 		idAttr=attribut ("id",model->getSelectString(this)); //TODO directly access model
    88 	else
    89 		idAttr="";
    90 
    91 	*/
    92 	QString elementName;
    93 	if (parentItem==rootItem)
    94 		elementName="mapcenter";
    95 	else	
    96 		elementName="branch";
    97 
    98     s=beginElement (elementName
    99 		+getMapAttr()
   100 		+getGeneralAttr()
   101 		+scrolledAttr 
   102 	//	+areaAttr 
   103 	//	+idAttr 
   104 		+getIncludeImageAttr() 
   105 		);
   106     incIndent();
   107 
   108 	// save heading
   109     s+=valueElement("heading", getHeading(),
   110 		attribut ("textColor",QColor( bo->getColor()).name()));
   111 
   112 	// Save frame  //FIXME-4 not saved if there is no LMO
   113 	if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame) 
   114 		s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
   115 
   116 	// save names of flags set
   117 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   118 	
   119 	// Save Images
   120 	for (int i=0; i<imageCount(); ++i)
   121 		s+=getImageNum(i)->saveToDir (tmpdir,prefix);
   122 
   123 	// save note
   124 	if (!note.isEmpty() )
   125 		s+=note.saveToDir();
   126 	
   127 	// Save branches
   128 	int i=0;
   129 	TreeItem *ti=getBranchNum(i);
   130 	while (ti)
   131 	{
   132 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   133 		i++;
   134 		ti=getBranchNum(i);
   135 	}	
   136 
   137 	/*
   138 	// Save XLinks
   139 	QString ol;	// old link
   140 	QString cl;	// current link
   141 	for (int i=0; i<xlink.size(); ++i)
   142 	{
   143 		cl=xlink.at(i)->saveToDir();
   144 		if (cl!=ol)
   145 		{
   146 			s+=cl;
   147 			ol=cl;
   148 		} else
   149 		{
   150 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   151 		}
   152 	}	
   153 	*/
   154 
   155     decIndent();
   156     s+=endElement   (elementName);
   157     return s;
   158 }
   159 
   160 void BranchItem::updateVisibility()	
   161 {
   162 	// Needed to hide relinked branch, if parent is scrolled
   163 	if (lmo)
   164 		lmo->setVisibility(!((BranchItem*)parentItem)->isScrolled());
   165 }
   166 
   167 void BranchItem::setHeadingColor (QColor color)
   168 {
   169 	TreeItem::setHeadingColor (color);
   170 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   171 }
   172 
   173 void BranchItem::unScroll()
   174 {
   175 	if (tmpUnscrolled) resetTmpUnscroll();
   176 	if (scrolled) toggleScroll();
   177 }
   178 
   179 bool BranchItem::toggleScroll()	
   180 {
   181 	BranchObj *bo=NULL;
   182 	if (scrolled)
   183 	{
   184 		scrolled=false;
   185 		systemFlags.deactivate("system-scrolledright");
   186 		if (branchCounter>0)
   187 		{
   188 			for (int i=0;i<branchCounter;++i)
   189 			{
   190 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   191 				if (bo) bo->setVisibility(true);
   192 			}
   193 		}
   194 	} else
   195 	{
   196 		scrolled=true;
   197 		systemFlags.activate("system-scrolledright");
   198 		if (branchCounter>0)
   199 		{
   200 			for (int i=0;i<branchCounter;++i)
   201 			{
   202 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   203 				if (bo) bo->setVisibility(false);
   204 			}
   205 		}
   206 	}
   207 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   208 	return true;
   209 }
   210 
   211 bool BranchItem::isScrolled()
   212 {
   213 	return scrolled;
   214 }
   215 
   216 bool BranchItem::hasScrolledParent(BranchItem *start)
   217 {
   218 	// Calls parents recursivly to
   219 	// find out, if we are scrolled at all.
   220 	// But ignore myself, just look at parents.
   221 
   222 	//cout << "BI::hasScrolledParent this="<<this<<"  "<<getHeadingStd()<<endl;
   223 	if (this !=start && scrolled) return true;
   224 
   225 	BranchItem* bi=(BranchItem*)parentItem;
   226 	if (bi && bi!=rootItem && bi->isBranchLikeType() ) 
   227 		return bi->hasScrolledParent(start);
   228 	else
   229 		return false;
   230 }
   231 
   232 void BranchItem::tmpUnscroll()
   233 {
   234 	// Unscroll parent (recursivly)
   235 	BranchItem * pi=(BranchItem*)parentItem;
   236 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   237 		
   238 	// Unscroll myself
   239 	if (scrolled)
   240 	{
   241 		tmpUnscrolled=true;
   242 		systemFlags.activate("system-tmpUnscrolledRight");
   243 		toggleScroll();
   244 		model->emitDataHasChanged (this);
   245 	}	
   246 }
   247 
   248 void BranchItem::resetTmpUnscroll()
   249 {
   250 	// Unscroll parent (recursivly)
   251 	BranchItem * pi=(BranchItem*)parentItem;
   252 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   253 		
   254 	// Unscroll myself
   255 	if (tmpUnscrolled)
   256 	{
   257 		tmpUnscrolled=false;
   258 		systemFlags.deactivate("system-tmpUnscrolledRight");
   259 		toggleScroll();
   260 		model->emitDataHasChanged (this);
   261 	}	
   262 }
   263 
   264 void BranchItem::setIncludeImagesVer(bool b)
   265 {
   266 	includeImagesVer=b;
   267 	/* calcBBoxSize(); FIXME-2
   268 	positionBBox();
   269 	requestReposition();
   270 	*/
   271 }
   272 
   273 bool BranchItem::getIncludeImagesVer()
   274 {
   275 	return includeImagesVer;
   276 }
   277 
   278 void BranchItem::setIncludeImagesHor(bool b)
   279 {
   280 	includeImagesHor=b;
   281 	/* calcBBoxSize(); FIXME-2
   282 	positionBBox();
   283 	requestReposition();
   284 	*/
   285 }
   286 
   287 bool BranchItem::getIncludeImagesHor()
   288 {
   289 	return includeImagesHor;
   290 }
   291 
   292 QString BranchItem::getIncludeImageAttr()
   293 {
   294 	QString a;
   295 	if (includeImagesVer)
   296 		a=attribut ("incImgV","true");
   297 	if (includeImagesHor)
   298 		a+=attribut ("incImgH","true");
   299 	return a;	
   300 }
   301 
   302 
   303 
   304 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   305 {
   306 	// Search branches
   307 	TreeItem *ti;
   308 	for (int i=0; i<branchCount(); ++i)
   309     {	
   310 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   311 		if (ti != NULL) return ti;
   312     }
   313 	
   314 
   315 	// Search myself
   316     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   317 		return this;
   318 
   319 	// Search images
   320 	ImageItem *ii;
   321     for (int i=0; i<imageCount(); ++i )
   322 	{
   323 		ii=getImageNum (i);
   324 		LinkableMapObj *mo=ii->getLMO();
   325 		if (mo && mo->isInClickBox(p) && 
   326 			(ii != excludeTI) && 
   327 			this!= excludeTI &&
   328 			mo->isVisibleObj() 
   329 		) return ii;
   330 	}
   331 	return NULL;
   332 }
   333 
   334 TreeItem* BranchItem::findID (QString sid)
   335 {
   336 	// Search branches
   337     TreeItem *ti;
   338 	for (int i=0; i<branchCount(); ++i)
   339     {	
   340 		ti=getBranchNum(i)->findID (sid);
   341 		if (ti != NULL) return ti;
   342     }
   343 	
   344 	// Search myself
   345 	if (sid==objID) return this;
   346 
   347 
   348 /*
   349 	// Search float images //FIXME-4
   350     for (int i=0; i<floatimage.size(); ++i )
   351 		if (floatimage.at(i)->inBox(p) && 
   352 			(floatimage.at(i) != excludeLMO) && 
   353 			floatimage.at(i)->getParObj()!= excludeLMO &&
   354 			floatimage.at(i)->isVisibleObj() 
   355 		) return floatimage.at(i);
   356 */
   357     return NULL;
   358 }
   359 
   360 void BranchItem::updateStyles()
   361 {
   362 	// FIXME-5 compare also MapItem::initLMO...
   363 
   364 	if (lmo && parentItem != rootItem)
   365 	{
   366 		lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   367 	}
   368 }
   369 
   370 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   371 {
   372 	return (BranchObj*)lmo;
   373 }
   374 
   375 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   376 {
   377 	BranchObj *newbo;
   378 	newbo=new BranchObj(scene);
   379 	newbo->setTreeItem (this);
   380 	lmo=newbo;
   381 
   382 	if (parentItem==rootItem)
   383 	{
   384 		newbo->setParObj(NULL);
   385 		newbo->setFrameType (FrameObj::Rectangle);
   386 	} else
   387 	{
   388 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   389 		// Set visibility depending on parents
   390 		if (((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() )
   391 			newbo->setVisibility (false);
   392 	}
   393 	newbo->setDefAttr(BranchObj::NewBranch);
   394 	initLMO();
   395 
   396 	if (!getHeading().isEmpty() ) 
   397 	{
   398 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   399 		newbo->setColor (headingColor);
   400 	}	
   401 		
   402 	//newbo->updateLinkGeometry();	//FIXME-3
   403 
   404 	return newbo;
   405 }
   406