Fix for segfault when deleting MCO (invalid QModelIndex needs to be returned in index(TreeItem*) )
1 #include "branchitem.h"
10 BranchItem::BranchItem(const QList<QVariant> &data, TreeItem *parent):MapItem (data,parent)
12 //cout << "Constr. BranchItem\n";
22 includeImagesVer=false;
23 includeImagesHor=false;
25 lastSelectedBranchNum=-1;
26 lastSelectedBranchNumAlt=-1;
29 BranchItem::~BranchItem()
31 //cout << "Destr. BranchItem "<<getHeadingStd()<<endl;
39 void BranchItem::copy (BranchItem *other)
41 scrolled=other->scrolled;
42 tmpUnscrolled=other->tmpUnscrolled;
45 void BranchItem::insertBranch (int pos, BranchItem *branch)
48 if (pos>branchCounter) pos=branchCounter;
49 childItems.insert(pos+branchOffset,branch);
50 branch->parentItem=this;
51 branch->setModel (model);
54 branchOffset=childItems.count()-1;
58 QString BranchItem::saveToDir (const QString &tmpdir,const QString &prefix, const QPointF& offset) //FIXME-3 Check if everything is saved...
60 // Cloudy stuff can be hidden during exports
61 if (hidden) return QString();
64 BranchObj *bo=(BranchObj*)lmo;
66 // Update of note is usually done while unselecting a branch
70 scrolledAttr=attribut ("scrolled","yes");
74 // save area, if not scrolled // FIXME-5 not needed if HTML is rewritten...
75 // also we should check if _any_ of parents is scrolled
77 if (lmo && parentItem->isBranchLikeType() && !((BranchItem*)parentItem)->isScrolled() )
79 qreal x=lmo->getAbsPos().x();
80 qreal y=lmo->getAbsPos().y();
82 attribut("x1",QString().setNum(x-offset.x())) +
83 attribut("y1",QString().setNum(y-offset.y())) +
84 attribut("x2",QString().setNum(x+lmo->width()-offset.x())) +
85 attribut("y2",QString().setNum(y+lmo->height()-offset.y()));
91 // FIXME-3 Providing an ID for a branch makes export to XHTML easier
94 idAttr=attribut ("id",model->getSelectString(this)); //TODO directly access model
100 if (parentItem==rootItem)
101 elementName="mapcenter";
103 elementName="branch";
105 s=beginElement (elementName
111 +getIncludeImageAttr()
116 s+=valueElement("heading", getHeading(),
117 attribut ("textColor",QColor( bo->getColor()).name()));
119 // Save frame //FIXME-4 not saved if there is no LMO
120 if (lmo && ((OrnamentedObj*)lmo)->getFrame()->getFrameType()!=FrameObj::NoFrame)
121 s+=((OrnamentedObj*)lmo)->getFrame()->saveToDir ();
123 // save names of flags set
124 s+=standardFlags.saveToDir(tmpdir,prefix,0);
127 for (int i=0; i<imageCount(); ++i)
128 s+=getImageNum(i)->saveToDir (tmpdir,prefix);
131 if (!note.isEmpty() )
136 TreeItem *ti=getBranchNum(i);
139 s+=getBranchNum(i)->saveToDir(tmpdir,prefix,offset);
146 QString ol; // old link
147 QString cl; // current link
148 for (int i=0; i<xlink.size(); ++i)
150 cl=xlink.at(i)->saveToDir();
157 qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
163 s+=endElement (elementName);
167 void BranchItem::updateVisibility()
169 // Needed to hide relinked branch, if parent is scrolled
171 lmo->setVisibility(!((BranchItem*)parentItem)->isScrolled());
174 void BranchItem::setHeadingColor (QColor color)
176 TreeItem::setHeadingColor (color);
177 if (lmo) ((BranchObj*)lmo)->setColor (color);
180 void BranchItem::unScroll()
182 if (tmpUnscrolled) resetTmpUnscroll();
183 if (scrolled) toggleScroll();
186 bool BranchItem::toggleScroll()
192 systemFlags.deactivate("system-scrolledright");
195 for (int i=0;i<branchCounter;++i)
197 bo=(BranchObj*)(getBranchNum(i)->getLMO());
198 if (bo) bo->setVisibility(true);
204 systemFlags.activate("system-scrolledright");
207 for (int i=0;i<branchCounter;++i)
209 bo=(BranchObj*)(getBranchNum(i)->getLMO());
210 if (bo) bo->setVisibility(false);
214 model->reposition(); // FIXME-3 we don't really want to update view from here...
218 bool BranchItem::isScrolled()
223 bool BranchItem::hasScrolledParent(BranchItem *start)
225 // Calls parents recursivly to
226 // find out, if we are scrolled at all.
227 // But ignore myself, just look at parents.
229 //cout << "BI::hasScrolledParent this="<<this<<" "<<getHeadingStd()<<endl;
230 if (this !=start && scrolled) return true;
232 BranchItem* bi=(BranchItem*)parentItem;
233 if (bi && bi!=rootItem && bi->isBranchLikeType() )
234 return bi->hasScrolledParent(start);
239 void BranchItem::tmpUnscroll()
241 // Unscroll parent (recursivly)
242 BranchItem * pi=(BranchItem*)parentItem;
243 if (pi && pi->isBranchLikeType() ) pi->tmpUnscroll();
249 systemFlags.activate("system-tmpUnscrolledRight");
251 model->emitDataHasChanged (this);
255 void BranchItem::resetTmpUnscroll()
257 // Unscroll parent (recursivly)
258 BranchItem * pi=(BranchItem*)parentItem;
259 if (pi && pi->isBranchLikeType() ) pi->resetTmpUnscroll();
265 systemFlags.deactivate("system-tmpUnscrolledRight");
267 model->emitDataHasChanged (this);
271 void BranchItem::setIncludeImagesVer(bool b)
274 /* calcBBoxSize(); FIXME-2
280 bool BranchItem::getIncludeImagesVer()
282 return includeImagesVer;
285 void BranchItem::setIncludeImagesHor(bool b)
288 /* calcBBoxSize(); FIXME-2
294 bool BranchItem::getIncludeImagesHor()
296 return includeImagesHor;
299 QString BranchItem::getIncludeImageAttr()
302 if (includeImagesVer)
303 a=attribut ("incImgV","true");
304 if (includeImagesHor)
305 a+=attribut ("incImgH","true");
309 void BranchItem::setLastSelectedBranch()
315 // Hack to save an additional lastSelected for mapcenters in MapEditor
316 // depending on orientation
317 // this allows to go both left and right from there
318 if (lmo && lmo->getOrientation()==LinkableMapObj::LeftOfCenter)
320 ((BranchItem*)parentItem)->lastSelectedBranchNumAlt=parentItem->num(this);
323 ((BranchItem*)parentItem)->lastSelectedBranchNum=parentItem->num(this);
327 void BranchItem::setLastSelectedBranch(int i)
329 lastSelectedBranchNum=i;
332 BranchItem* BranchItem::getLastSelectedBranch()
334 return getBranchNum (lastSelectedBranchNum);
337 BranchItem* BranchItem::getLastSelectedBranchAlt()
339 return getBranchNum (lastSelectedBranchNumAlt);
346 TreeItem* BranchItem::findMapItem (QPointF p, TreeItem* excludeTI)
350 for (int i=0; i<branchCount(); ++i)
352 ti=getBranchNum(i)->findMapItem(p, excludeTI);
353 if (ti != NULL) return ti;
358 if (getBranchObj()->isInClickBox (p) && (this != excludeTI) && getBranchObj()->isVisibleObj() )
363 for (int i=0; i<imageCount(); ++i )
366 LinkableMapObj *mo=ii->getLMO();
367 if (mo && mo->isInClickBox(p) &&
376 TreeItem* BranchItem::findID (QString sid)
380 for (int i=0; i<branchCount(); ++i)
382 ti=getBranchNum(i)->findID (sid);
383 if (ti != NULL) return ti;
387 if (sid==objID) return this;
391 // Search float images //FIXME-4
392 for (int i=0; i<floatimage.size(); ++i )
393 if (floatimage.at(i)->inBox(p) &&
394 (floatimage.at(i) != excludeLMO) &&
395 floatimage.at(i)->getParObj()!= excludeLMO &&
396 floatimage.at(i)->isVisibleObj()
397 ) return floatimage.at(i);
402 void BranchItem::updateStyles()
404 // FIXME-5 compare also MapItem::initLMO...
406 if (lmo && parentItem != rootItem)
408 lmo->setParObj ( ((MapItem*)parentItem)->getLMO() );
412 BranchObj* BranchItem::getBranchObj() // FIXME-3 only for transition BO->BI
414 return (BranchObj*)lmo;
417 BranchObj* BranchItem::createMapObj(QGraphicsScene *scene) // FIXME-4 maybe move this into MapEditor to get rid of scene in VymModel?
420 newbo=new BranchObj(scene);
421 newbo->setTreeItem (this);
424 if (parentItem==rootItem)
426 newbo->setParObj(NULL);
427 newbo->setFrameType (FrameObj::Rectangle);
430 newbo->setParObj( ((MapItem*)parentItem)->getLMO() );
431 // Set visibility depending on parents
432 if (((BranchItem*)parentItem)->scrolled || !((MapItem*)parentItem)->getLMO()->isVisibleObj() )
433 newbo->setVisibility (false);
435 newbo->setDefAttr(BranchObj::NewBranch);
438 if (!getHeading().isEmpty() )
440 newbo->updateData(); //FIXME-3 maybe better model->emitDataHasChanged()?
441 newbo->setColor (headingColor);
444 //newbo->updateLinkGeometry(); //FIXME-3