branchitem.cpp
author insilmaril
Tue, 07 Jul 2009 11:21:27 +0000
changeset 780 fe839bdfd10c
parent 779 1fb50e79661c
child 781 6cbf3c9cbd21
permissions -rw-r--r--
vymLinks working 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):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 		+getMapAttr()
    97 		+getGeneralAttr()
    98 		+scrolledAttr 
    99 	//	+areaAttr 
   100 	//	+idAttr 
   101 	//	+getIncludeImageAttr() 
   102 		);
   103     incIndent();
   104 
   105 	// save heading
   106     s+=valueElement("heading", getHeading(),
   107 		attribut ("textColor",QColor( bo->getColor()).name()));
   108 
   109 	// Save frame  //FIXME-4 not saved if there is no LMO
   110 	if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame) 
   111 		s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
   112 
   113 	// save names of flags set
   114 	s+=standardFlags.saveToDir(tmpdir,prefix,0);
   115 	
   116 	// Save Images
   117 	for (int i=0; i<imageCount(); ++i)
   118 		s+=getImageNum(i)->saveToDir (tmpdir,prefix);
   119 
   120 	// save note
   121 	if (!note.isEmpty() )
   122 		s+=note.saveToDir();
   123 	
   124 	// Save branches
   125 	int i=0;
   126 	TreeItem *ti=getBranchNum(i);
   127 	while (ti)
   128 	{
   129 		s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
   130 		i++;
   131 		ti=getBranchNum(i);
   132 	}	
   133 
   134 	/*
   135 	// Save XLinks
   136 	QString ol;	// old link
   137 	QString cl;	// current link
   138 	for (int i=0; i<xlink.size(); ++i)
   139 	{
   140 		cl=xlink.at(i)->saveToDir();
   141 		if (cl!=ol)
   142 		{
   143 			s+=cl;
   144 			ol=cl;
   145 		} else
   146 		{
   147 			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
   148 		}
   149 	}	
   150 	*/
   151 
   152     decIndent();
   153     s+=endElement   (elementName);
   154     return s;
   155 }
   156 
   157 void BranchItem::updateVisibility()	// FIXME-3	Check if this is needed after all...
   158 {
   159 }
   160 
   161 void BranchItem::setHeadingColor (QColor color)
   162 {
   163 	TreeItem::setHeadingColor (color);
   164 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   165 }
   166 
   167 void BranchItem::unScroll()
   168 {
   169 	if (tmpUnscrolled) resetTmpUnscroll();
   170 	if (scrolled) toggleScroll();
   171 }
   172 
   173 bool BranchItem::toggleScroll()	
   174 {
   175 	BranchObj *bo=NULL;
   176 	if (scrolled)
   177 	{
   178 		scrolled=false;
   179 		systemFlags.deactivate("system-scrolledright");
   180 		if (branchCounter>0)
   181 		{
   182 			for (int i=0;i<branchCounter;++i)
   183 			{
   184 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   185 				if (bo) bo->setVisibility(true);
   186 			}
   187 		}
   188 	} else
   189 	{
   190 		scrolled=true;
   191 		systemFlags.activate("system-scrolledright");
   192 		if (branchCounter>0)
   193 		{
   194 			for (int i=0;i<branchCounter;++i)
   195 			{
   196 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   197 				if (bo) bo->setVisibility(false);
   198 			}
   199 		}
   200 	}
   201 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   202 	return true;
   203 }
   204 
   205 bool BranchItem::isScrolled()
   206 {
   207 	return scrolled;
   208 }
   209 
   210 bool BranchItem::hasScrolledParent(BranchItem *start)
   211 {
   212 	// Calls parents recursivly to
   213 	// find out, if we are scrolled at all.
   214 	// But ignore myself, just look at parents.
   215 
   216 	//cout << "BI::hasScrolledParent this="<<this<<"  "<<getHeadingStd()<<endl;
   217 	if (this !=start && scrolled) return true;
   218 
   219 	BranchItem* bi=(BranchItem*)parentItem;
   220 	if (bi && bi!=rootItem && bi->isBranchLikeType() ) 
   221 		return bi->hasScrolledParent(start);
   222 	else
   223 		return false;
   224 }
   225 
   226 void BranchItem::tmpUnscroll()
   227 {
   228 	// Unscroll parent (recursivly)
   229 	BranchItem * pi=(BranchItem*)parentItem;
   230 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   231 		
   232 	// Unscroll myself
   233 	if (scrolled)
   234 	{
   235 		tmpUnscrolled=true;
   236 		systemFlags.activate("system-tmpUnscrolledRight");
   237 		toggleScroll();
   238 		model->emitDataHasChanged (this);
   239 	}	
   240 }
   241 
   242 void BranchItem::resetTmpUnscroll()
   243 {
   244 	// Unscroll parent (recursivly)
   245 	BranchItem * pi=(BranchItem*)parentItem;
   246 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   247 		
   248 	// Unscroll myself
   249 	if (tmpUnscrolled)
   250 	{
   251 		tmpUnscrolled=false;
   252 		systemFlags.deactivate("system-tmpUnscrolledRight");
   253 		toggleScroll();
   254 		model->emitDataHasChanged (this);
   255 	}	
   256 }
   257 
   258 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   259 {
   260 	// Search branches
   261 	TreeItem *ti;
   262 	for (int i=0; i<branchCount(); ++i)
   263     {	
   264 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   265 		if (ti != NULL) return ti;
   266     }
   267 	
   268 
   269 	// Search myself
   270     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   271 		return this;
   272 
   273 	// Search images
   274 	ImageItem *ii;
   275     for (int i=0; i<imageCount(); ++i )
   276 	{
   277 		ii=getImageNum (i);
   278 		LinkableMapObj *mo=ii->getLMO();
   279 		if (mo && mo->isInClickBox(p) && 
   280 			(ii != excludeTI) && 
   281 			this!= excludeTI &&
   282 			mo->isVisibleObj() 
   283 		) return ii;
   284 	}
   285 	return NULL;
   286 }
   287 
   288 TreeItem* BranchItem::findID (QString sid)
   289 {
   290 	// Search branches
   291     TreeItem *ti;
   292 	for (int i=0; i<branchCount(); ++i)
   293     {	
   294 		ti=getBranchNum(i)->findID (sid);
   295 		if (ti != NULL) return ti;
   296     }
   297 	
   298 	// Search myself
   299 	if (sid==objID) return this;
   300 
   301 
   302 /*
   303 	// Search float images
   304     for (int i=0; i<floatimage.size(); ++i )
   305 		if (floatimage.at(i)->inBox(p) && 
   306 			(floatimage.at(i) != excludeLMO) && 
   307 			floatimage.at(i)->getParObj()!= excludeLMO &&
   308 			floatimage.at(i)->isVisibleObj() 
   309 		) return floatimage.at(i);
   310 */
   311     return NULL;
   312 }
   313 
   314 void BranchItem::updateStyles()
   315 {
   316 	// FIXME-5 compare also MapItem::initLMO...
   317 
   318 	if (lmo && parentItem != rootItem)
   319 	{
   320 		lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   321 	}
   322 }
   323 
   324 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   325 {
   326 	return (BranchObj*)lmo;
   327 }
   328 
   329 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   330 {
   331 	BranchObj *newbo;
   332 	newbo=new BranchObj(scene);
   333 	newbo->setTreeItem (this);
   334 	lmo=newbo;
   335 
   336 	if (parentItem==rootItem)
   337 	{
   338 		newbo->setParObj(NULL);
   339 		newbo->setFrameType (FrameObj::Rectangle);
   340 	} else
   341 	{
   342 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   343 		// Set visibility depending on parents
   344 		if (((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() )
   345 			newbo->setVisibility (false);
   346 	}
   347 	newbo->setDefAttr(BranchObj::NewBranch);
   348 	initLMO();
   349 
   350 	if (!getHeading().isEmpty() ) 
   351 	{
   352 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   353 		newbo->setColor (headingColor);
   354 	}	
   355 		
   356 	//newbo->updateLinkGeometry();	//FIXME-3
   357 
   358 	return newbo;
   359 }
   360