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 cout << "MCO i="<<i<<" count="<<mapCenters.count()<<endl;
127 if (i>mapCenters.count()-1 || i<0)
130 return mapCenters.at(i);
133 int VymModel::countMapCenters()
135 return mapCenters.count();
138 BranchObj* VymModel::first()
140 if (mapCenters.count()>0)
141 return mapCenters.first();
146 BranchObj* VymModel::next(BranchObj *bo_start)
149 BranchObj *bo=bo_start;
152 // Try to find next branch in current MapCenter
156 // Try to find MapCenter of bo
157 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
159 // Try to find next MapCenter
160 int i=mapCenters.indexOf ((MapCenterObj*)bo);
161 if (i+2 > mapCenters.count() || i<0) return NULL;
162 if (mapCenters.at(i+1)!=bo_start)
163 return mapCenters.at(i+1);
168 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
172 for (int i=0;i<mapCenters.count(); i++)
174 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
180 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
188 part=s.section(",",0,0);
190 num=part.right(part.length() - 3);
191 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
192 return mapCenters.at(num.toInt() );
195 for (int i=0; i<mapCenters.count(); i++)
197 lmo=mapCenters.at(i)->findObjBySelect(s);
203 LinkableMapObj* VymModel::findID (const QString &s)
206 for (int i=0; i<mapCenters.count(); i++)
208 lmo=mapCenters.at(i)->findID (s);
214 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
218 for (int i=0; i<mapCenters.count(); i++)
219 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
224 //////////////////////////////////////////////
226 //////////////////////////////////////////////
228 void VymModel::updateRelPositions()
230 for (int i=0; i<mapCenters.count(); i++)
231 mapCenters.at(i)->updateRelPositions();
234 void VymModel::reposition()
236 for (int i=0;i<mapCenters.count(); i++)
237 mapCenters.at(i)->reposition(); // for positioning heading
240 QPolygonF VymModel::shape(BranchObj *bo)
242 // Creating (arbitrary) shapes
245 QRectF rb=bo->getBBox();
246 if (bo->getDepth()==0)
248 // Just take BBox of this mapCenter
249 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
253 // Take union of BBox and TotalBBox
255 QRectF ra=bo->getTotalBBox();
256 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
259 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
262 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
266 <<QPointF (rb.topRight().x(), ra.topRight().y() )
269 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
274 void VymModel::moveAway(LinkableMapObj *lmo)
278 // Move all branches and MapCenters away from lmo
279 // to avoid collisions
284 BranchObj *boA=(BranchObj*)lmo;
286 for (int i=0; i<mapCenters.count(); i++)
288 boB=mapCenters.at(i);
291 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
294 <<" ("<<qPrintable(boA->getHeading() )<<")"
295 <<" with ("<< qPrintable (boB->getHeading() )
298 <<" minT="<<r.minTranslation<<endl<<endl;
302 void VymModel::animate()
304 animationTimer->stop();
307 while (i<animObjList.size() )
309 bo=(BranchObj*)animObjList.at(i);
312 if (i>=0) animObjList.removeAt(i);
318 mapEditor->updateSelection();
320 if (!animObjList.isEmpty() ) animationTimer->start();
324 void VymModel::startAnimation(const QPointF &start, const QPointF &dest)
326 BranchObj *bo=getSelectedBranch();
327 if (bo && bo->getDepth()>0)
332 ap.setTicks (animationTicks);
333 ap.setAnimated (true);
334 bo->setAnimation (ap);
335 animObjList.append( bo );
336 animationTimer->setSingleShot (true);
337 animationTimer->start(animationInterval);
341 void VymModel::stopAnimation(MapObj *mo)
343 int i=animObjList.indexOf(mo);
345 animObjList.removeAt (i);
348 //////////////////////////////////////////////
350 //////////////////////////////////////////////
353 // Only as long as we dont have Model/View yet
354 LinkableMapObj* VymModel::getSelection()
356 return mapEditor->getSelection();
358 BranchObj* VymModel::getSelectedBranch()
360 return mapEditor->getSelectedBranch();
364 bool VymModel::select (const QString &s)
366 return mapEditor->select (s);
369 QString VymModel::getSelectString (LinkableMapObj *lmo)
373 if (typeid(*lmo)==typeid(BranchObj) ||
374 typeid(*lmo)==typeid(MapCenterObj) )
376 LinkableMapObj *par=lmo->getParObj();
379 if (lmo->getDepth() ==1)
380 // Mainbranch, return
381 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
383 // Branch, call myself recursively
384 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
388 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
389 if (i>=0) s=QString("mc:%1").arg(i);
397 void VymModel::setHideTmp (HideTmpMode mode)
399 for (int i=0;i<mapCenters.count(); i++)
400 mapCenters.at(i)->setHideTmp (mode);
403 QRectF VymModel::getTotalBBox()
406 for (int i=0;i<mapCenters.count(); i++)
407 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);