1 #include <QApplication>
4 #include "geometry.h" // for addBBox
8 extern Settings settings;
12 // cout << "Const VymModel\n";
18 //cout << "Destr VymModel\n";
22 void VymModel::clear()
24 while (!mapCenters.isEmpty())
25 delete mapCenters.takeFirst();
28 void VymModel::init ()
33 animationUse=settings.readBoolEntry("/animation/use",false);
34 animationTicks=settings.readNumEntry("/animation/ticks",10);
35 animationInterval=settings.readNumEntry("/animation/interval",50);
37 animationTimer=new QTimer (this);
38 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
42 void VymModel::setMapEditor(MapEditor *me)
45 for (int i=0; i<mapCenters.count(); i++)
46 mapCenters.at(i)->setMapEditor(mapEditor);
49 MapEditor* VymModel::getMapEditor()
54 void VymModel::setVersion (const QString &s)
59 void VymModel::setAuthor (const QString &s)
64 QString VymModel::getAuthor()
69 void VymModel::setComment (const QString &s)
74 QString VymModel::getComment ()
79 QString VymModel::getDate ()
81 return QDate::currentDate().toString ("yyyy-MM-dd");
84 void VymModel::setScene (QGraphicsScene *s)
87 init(); // Here we have a mapScene set,
88 // which is (still) needed to create MapCenters
91 QGraphicsScene* VymModel::getScene ()
96 MapCenterObj* VymModel::addMapCenter()
98 return addMapCenter (QPointF(0,0));
101 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
103 MapCenterObj *mapCenter = new MapCenterObj(mapScene);
104 mapCenter->move (absPos);
105 mapCenter->setVisibility (true);
106 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
107 mapCenter->setMapEditor(mapEditor); //FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
108 mapCenters.append(mapCenter);
112 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
114 int i=mapCenters.indexOf (mco);
117 mapCenters.removeAt (i);
119 if (i>0) return mapCenters.at(i-1); // Return previous MCO
124 MapCenterObj *VymModel::getMapCenterNum (int i)
126 if (i>mapCenters.count()-1 || i<0)
129 return mapCenters.at(i);
132 int VymModel::countMapCenters()
134 return mapCenters.count();
137 int VymModel::countBranches()
140 BranchObj *bo=first();
149 BranchObj* VymModel::first()
151 if (mapCenters.count()>0)
152 return mapCenters.first();
157 BranchObj* VymModel::next(BranchObj *bo_start)
160 BranchObj *bo=bo_start;
163 // Try to find next branch in current MapCenter
167 // Try to find MapCenter of bo
168 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
170 // Try to find next MapCenter
171 int i=mapCenters.indexOf ((MapCenterObj*)bo);
172 if (i+2 > mapCenters.count() || i<0) return NULL;
173 if (mapCenters.at(i+1)!=bo_start)
174 return mapCenters.at(i+1);
179 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
183 for (int i=0;i<mapCenters.count(); i++)
185 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
191 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
199 part=s.section(",",0,0);
201 num=part.right(part.length() - 3);
202 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
203 return mapCenters.at(num.toInt() );
206 for (int i=0; i<mapCenters.count(); i++)
208 lmo=mapCenters.at(i)->findObjBySelect(s);
214 LinkableMapObj* VymModel::findID (const QString &s)
217 for (int i=0; i<mapCenters.count(); i++)
219 lmo=mapCenters.at(i)->findID (s);
225 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
229 for (int i=0; i<mapCenters.count(); i++)
230 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
235 //////////////////////////////////////////////
237 //////////////////////////////////////////////
239 void VymModel::updateRelPositions()
241 for (int i=0; i<mapCenters.count(); i++)
242 mapCenters.at(i)->updateRelPositions();
245 void VymModel::reposition()
247 for (int i=0;i<mapCenters.count(); i++)
248 mapCenters.at(i)->reposition(); // for positioning heading
251 QPolygonF VymModel::shape(BranchObj *bo)
253 // Creating (arbitrary) shapes
256 QRectF rb=bo->getBBox();
257 if (bo->getDepth()==0)
259 // Just take BBox of this mapCenter
260 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
264 // Take union of BBox and TotalBBox
266 QRectF ra=bo->getTotalBBox();
267 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
270 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
273 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
277 <<QPointF (rb.topRight().x(), ra.topRight().y() )
280 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
285 void VymModel::moveAway(LinkableMapObj *lmo)
289 // Move all branches and MapCenters away from lmo
290 // to avoid collisions
295 BranchObj *boA=(BranchObj*)lmo;
297 for (int i=0; i<mapCenters.count(); i++)
299 boB=mapCenters.at(i);
302 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
305 <<" ("<<qPrintable(boA->getHeading() )<<")"
306 <<" with ("<< qPrintable (boB->getHeading() )
309 <<" minT="<<r.minTranslation<<endl<<endl;
313 void VymModel::animate()
315 animationTimer->stop();
318 while (i<animObjList.size() )
320 bo=(BranchObj*)animObjList.at(i);
323 if (i>=0) animObjList.removeAt(i);
329 mapEditor->updateSelection();
331 if (!animObjList.isEmpty() ) animationTimer->start();
335 void VymModel::startAnimation(const QPointF &start, const QPointF &dest)
337 BranchObj *bo=getSelectedBranch();
338 if (bo && bo->getDepth()>0)
343 ap.setTicks (animationTicks);
344 ap.setAnimated (true);
345 bo->setAnimation (ap);
346 animObjList.append( bo );
347 animationTimer->setSingleShot (true);
348 animationTimer->start(animationInterval);
352 void VymModel::stopAnimation(MapObj *mo)
354 int i=animObjList.indexOf(mo);
356 animObjList.removeAt (i);
359 //////////////////////////////////////////////
361 //////////////////////////////////////////////
364 // Only as long as we dont have Model/View yet
365 LinkableMapObj* VymModel::getSelection()
367 return mapEditor->getSelection();
369 BranchObj* VymModel::getSelectedBranch()
371 return mapEditor->getSelectedBranch();
375 bool VymModel::select (const QString &s)
377 return mapEditor->select (s);
380 QString VymModel::getSelectString (LinkableMapObj *lmo)
384 if (typeid(*lmo)==typeid(BranchObj) ||
385 typeid(*lmo)==typeid(MapCenterObj) )
387 LinkableMapObj *par=lmo->getParObj();
390 if (lmo->getDepth() ==1)
391 // Mainbranch, return
392 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
394 // Branch, call myself recursively
395 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
399 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
400 if (i>=0) s=QString("mc:%1").arg(i);
408 void VymModel::setHideTmp (HideTmpMode mode)
410 for (int i=0;i<mapCenters.count(); i++)
411 mapCenters.at(i)->setHideTmp (mode);
414 QRectF VymModel::getTotalBBox()
417 for (int i=0;i<mapCenters.count(); i++)
418 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);