1 #include <QApplication>
4 #include "geometry.h" // for addBBox
9 // cout << "Const VymModel\n";
15 // cout << "Destr VymModel\n";
18 void VymModel::clear()
20 while (!mapCenters.isEmpty())
21 delete mapCenters.takeFirst();
24 void VymModel::init ()
29 void VymModel::setMapEditor(MapEditor *me)
32 for (int i=0; i<mapCenters.count(); i++)
33 mapCenters.at(i)->setMapEditor(mapEditor);
36 MapEditor* VymModel::getMapEditor()
41 void VymModel::setVersion (const QString &s)
46 void VymModel::setAuthor (const QString &s)
51 QString VymModel::getAuthor()
56 void VymModel::setComment (const QString &s)
61 QString VymModel::getComment ()
66 QString VymModel::getDate ()
68 return QDate::currentDate().toString ("yyyy-MM-dd");
71 void VymModel::setScene (QGraphicsScene *s)
74 init(); // Here we have a mapScene set,
75 // which is (still) needed to create MapCenters
78 QGraphicsScene* VymModel::getScene ()
83 MapCenterObj* VymModel::addMapCenter()
85 return addMapCenter (QPointF(0,0));
88 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
90 MapCenterObj *mapCenter = new MapCenterObj(mapScene);
91 mapCenter->move (absPos);
92 mapCenter->setVisibility (true);
93 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
94 mapCenter->setMapEditor(mapEditor); //FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
95 mapCenters.append(mapCenter);
99 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
101 int i=mapCenters.indexOf (mco);
104 mapCenters.removeAt (i);
106 if (i>0) return mapCenters.at(i-1); // Return previous MCO
111 BranchObj* VymModel::first()
113 if (mapCenters.count()>0)
114 return mapCenters.first();
119 BranchObj* VymModel::next(BranchObj *bo_start)
122 BranchObj *bo=bo_start;
125 // Try to find next branch in current MapCenter
129 // Try to find MapCenter of bo
130 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
132 // Try to find next MapCenter
133 int i=mapCenters.indexOf ((MapCenterObj*)bo);
134 if (i+2 > mapCenters.count() || i<0) return NULL;
135 if (mapCenters.at(i+1)!=bo_start)
136 return mapCenters.at(i+1);
141 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
145 for (int i=0;i<mapCenters.count(); i++)
147 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
153 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
161 part=s.section(",",0,0);
163 num=part.right(part.length() - 3);
164 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
165 return mapCenters.at(num.toInt() );
168 for (int i=0; i<mapCenters.count(); i++)
170 lmo=mapCenters.at(i)->findObjBySelect(s);
176 LinkableMapObj* VymModel::findID (const QString &s)
179 for (int i=0; i<mapCenters.count(); i++)
181 lmo=mapCenters.at(i)->findID (s);
187 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
191 for (int i=0; i<mapCenters.count(); i++)
192 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
197 //////////////////////////////////////////////
199 //////////////////////////////////////////////
201 void VymModel::updateRelPositions()
203 for (int i=0; i<mapCenters.count(); i++)
204 mapCenters.at(i)->updateRelPositions();
207 void VymModel::reposition()
209 for (int i=0;i<mapCenters.count(); i++)
210 mapCenters.at(i)->reposition(); // for positioning heading
213 QPolygonF VymModel::shape(BranchObj *bo)
215 // Creating (arbitrary) shapes
218 QRectF rb=bo->getBBox();
219 if (bo->getDepth()==0)
221 // Just take BBox of this mapCenter
222 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
226 // Take union of BBox and TotalBBox
228 QRectF ra=bo->getTotalBBox();
229 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
232 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
235 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
239 <<QPointF (rb.topRight().x(), ra.topRight().y() )
242 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
247 void VymModel::moveAway(LinkableMapObj *lmo)
251 // Move all branches and MapCenters away from lmo
252 // to avoid collisions
260 BranchObj *boA=(BranchObj*)lmo;
262 for (int i=0; i<mapCenters.count(); i++)
264 boB=mapCenters.at(i);
267 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
270 <<" ("<<qPrintable(boA->getHeading() )<<")"
271 <<" with ("<< qPrintable (boB->getHeading() )
274 <<" minT="<<r.minTranslation<<endl<<endl;
279 //////////////////////////////////////////////
281 //////////////////////////////////////////////
284 // Only as long as we dont have Model/View yet
285 LinkableMapObj* VymModel::getSelection()
287 return mapEditor->getSelection();
289 BranchObj* VymModel::getSelectedBranch()
291 return mapEditor->getSelectedBranch();
295 bool VymModel::select (const QString &s)
297 return mapEditor->select (s);
300 QString VymModel::getSelectString (LinkableMapObj *lmo)
304 if (typeid(*lmo)==typeid(BranchObj) ||
305 typeid(*lmo)==typeid(MapCenterObj) )
307 LinkableMapObj *par=lmo->getParObj();
310 if (lmo->getDepth() ==1)
311 // Mainbranch, return
312 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
314 // Branch, call myself recursively
315 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
319 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
320 if (i>=0) s=QString("mc:%1").arg(i);
328 void VymModel::setHideTmp (HideTmpMode mode)
330 for (int i=0;i<mapCenters.count(); i++)
331 mapCenters.at(i)->setHideTmp (mode);
334 QRectF VymModel::getTotalBBox()
337 for (int i=0;i<mapCenters.count(); i++)
338 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);