branchitem.cpp
author insilmaril
Mon Jul 27 12:53:17 2009 +0000 (2009-07-27)
changeset 783 85dec6a8c12c
parent 780 fe839bdfd10c
child 785 5987f9f15bac
permissions -rw-r--r--
Playing with dbus, fixed name of TW translation file
     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()	
   158 {
   159 	// Needed to hide relinked branch, if parent is scrolled
   160 	if (lmo)
   161 		lmo->setVisibility(!((BranchItem*)parentItem)->isScrolled());
   162 }
   163 
   164 void BranchItem::setHeadingColor (QColor color)
   165 {
   166 	TreeItem::setHeadingColor (color);
   167 	if (lmo) ((BranchObj*)lmo)->setColor (color);
   168 }
   169 
   170 void BranchItem::unScroll()
   171 {
   172 	if (tmpUnscrolled) resetTmpUnscroll();
   173 	if (scrolled) toggleScroll();
   174 }
   175 
   176 bool BranchItem::toggleScroll()	
   177 {
   178 	BranchObj *bo=NULL;
   179 	if (scrolled)
   180 	{
   181 		scrolled=false;
   182 		systemFlags.deactivate("system-scrolledright");
   183 		if (branchCounter>0)
   184 		{
   185 			for (int i=0;i<branchCounter;++i)
   186 			{
   187 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   188 				if (bo) bo->setVisibility(true);
   189 			}
   190 		}
   191 	} else
   192 	{
   193 		scrolled=true;
   194 		systemFlags.activate("system-scrolledright");
   195 		if (branchCounter>0)
   196 		{
   197 			for (int i=0;i<branchCounter;++i)
   198 			{
   199 				bo=(BranchObj*)(getBranchNum(i)->getLMO());
   200 				if (bo) bo->setVisibility(false);
   201 			}
   202 		}
   203 	}
   204 	model->reposition();	// FIXME-3 we don't really want to update view from here...
   205 	return true;
   206 }
   207 
   208 bool BranchItem::isScrolled()
   209 {
   210 	return scrolled;
   211 }
   212 
   213 bool BranchItem::hasScrolledParent(BranchItem *start)
   214 {
   215 	// Calls parents recursivly to
   216 	// find out, if we are scrolled at all.
   217 	// But ignore myself, just look at parents.
   218 
   219 	//cout << "BI::hasScrolledParent this="<<this<<"  "<<getHeadingStd()<<endl;
   220 	if (this !=start && scrolled) return true;
   221 
   222 	BranchItem* bi=(BranchItem*)parentItem;
   223 	if (bi && bi!=rootItem && bi->isBranchLikeType() ) 
   224 		return bi->hasScrolledParent(start);
   225 	else
   226 		return false;
   227 }
   228 
   229 void BranchItem::tmpUnscroll()
   230 {
   231 	// Unscroll parent (recursivly)
   232 	BranchItem * pi=(BranchItem*)parentItem;
   233 	if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
   234 		
   235 	// Unscroll myself
   236 	if (scrolled)
   237 	{
   238 		tmpUnscrolled=true;
   239 		systemFlags.activate("system-tmpUnscrolledRight");
   240 		toggleScroll();
   241 		model->emitDataHasChanged (this);
   242 	}	
   243 }
   244 
   245 void BranchItem::resetTmpUnscroll()
   246 {
   247 	// Unscroll parent (recursivly)
   248 	BranchItem * pi=(BranchItem*)parentItem;
   249 	if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
   250 		
   251 	// Unscroll myself
   252 	if (tmpUnscrolled)
   253 	{
   254 		tmpUnscrolled=false;
   255 		systemFlags.deactivate("system-tmpUnscrolledRight");
   256 		toggleScroll();
   257 		model->emitDataHasChanged (this);
   258 	}	
   259 }
   260 
   261 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
   262 {
   263 	// Search branches
   264 	TreeItem *ti;
   265 	for (int i=0; i<branchCount(); ++i)
   266     {	
   267 		ti=getBranchNum(i)->findMapItem(p, excludeTI);
   268 		if (ti != NULL) return ti;
   269     }
   270 	
   271 
   272 	// Search myself
   273     if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() ) 
   274 		return this;
   275 
   276 	// Search images
   277 	ImageItem *ii;
   278     for (int i=0; i<imageCount(); ++i )
   279 	{
   280 		ii=getImageNum (i);
   281 		LinkableMapObj *mo=ii->getLMO();
   282 		if (mo && mo->isInClickBox(p) && 
   283 			(ii != excludeTI) && 
   284 			this!= excludeTI &&
   285 			mo->isVisibleObj() 
   286 		) return ii;
   287 	}
   288 	return NULL;
   289 }
   290 
   291 TreeItem* BranchItem::findID (QString sid)
   292 {
   293 	// Search branches
   294     TreeItem *ti;
   295 	for (int i=0; i<branchCount(); ++i)
   296     {	
   297 		ti=getBranchNum(i)->findID (sid);
   298 		if (ti != NULL) return ti;
   299     }
   300 	
   301 	// Search myself
   302 	if (sid==objID) return this;
   303 
   304 
   305 /*
   306 	// Search float images
   307     for (int i=0; i<floatimage.size(); ++i )
   308 		if (floatimage.at(i)->inBox(p) && 
   309 			(floatimage.at(i) != excludeLMO) && 
   310 			floatimage.at(i)->getParObj()!= excludeLMO &&
   311 			floatimage.at(i)->isVisibleObj() 
   312 		) return floatimage.at(i);
   313 */
   314     return NULL;
   315 }
   316 
   317 void BranchItem::updateStyles()
   318 {
   319 	// FIXME-5 compare also MapItem::initLMO...
   320 
   321 	if (lmo && parentItem != rootItem)
   322 	{
   323 		lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
   324 	}
   325 }
   326 
   327 BranchObj* BranchItem::getBranchObj()	// FIXME-3 only for transition BO->BI
   328 {
   329 	return (BranchObj*)lmo;
   330 }
   331 
   332 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene)	
   333 {
   334 	BranchObj *newbo;
   335 	newbo=new BranchObj(scene);
   336 	newbo->setTreeItem (this);
   337 	lmo=newbo;
   338 
   339 	if (parentItem==rootItem)
   340 	{
   341 		newbo->setParObj(NULL);
   342 		newbo->setFrameType (FrameObj::Rectangle);
   343 	} else
   344 	{
   345 		newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
   346 		// Set visibility depending on parents
   347 		if (((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() )
   348 			newbo->setVisibility (false);
   349 	}
   350 	newbo->setDefAttr(BranchObj::NewBranch);
   351 	initLMO();
   352 
   353 	if (!getHeading().isEmpty() ) 
   354 	{
   355 		newbo->updateData();	//FIXME-3 maybe better model->emitDataHasChanged()?
   356 		newbo->setColor (headingColor);
   357 	}	
   358 		
   359 	//newbo->updateLinkGeometry();	//FIXME-3
   360 
   361 	return newbo;
   362 }
   363