1 #include <QApplication>
4 #include "geometry.h" // for addBBox
8 extern Settings settings;
12 // cout << "Const VymModel\n";
18 // cout << "Destr VymModel\n";
21 void VymModel::clear()
23 while (!mapCenters.isEmpty())
24 delete mapCenters.takeFirst();
27 void VymModel::init ()
32 animationUse=settings.readBoolEntry("/animation/use",false);
33 animationTicks=settings.readNumEntry("/animation/ticks",10);
34 animationInterval=settings.readNumEntry("/animation/interval",50);
36 animationTimer=new QTimer (this);
37 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
41 void VymModel::setMapEditor(MapEditor *me)
44 for (int i=0; i<mapCenters.count(); i++)
45 mapCenters.at(i)->setMapEditor(mapEditor);
48 MapEditor* VymModel::getMapEditor()
53 void VymModel::setVersion (const QString &s)
58 void VymModel::setAuthor (const QString &s)
63 QString VymModel::getAuthor()
68 void VymModel::setComment (const QString &s)
73 QString VymModel::getComment ()
78 QString VymModel::getDate ()
80 return QDate::currentDate().toString ("yyyy-MM-dd");
83 void VymModel::setScene (QGraphicsScene *s)
86 init(); // Here we have a mapScene set,
87 // which is (still) needed to create MapCenters
90 QGraphicsScene* VymModel::getScene ()
95 MapCenterObj* VymModel::addMapCenter()
97 return addMapCenter (QPointF(0,0));
100 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
102 MapCenterObj *mapCenter = new MapCenterObj(mapScene);
103 mapCenter->move (absPos);
104 mapCenter->setVisibility (true);
105 mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
106 mapCenter->setMapEditor(mapEditor); //FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
107 mapCenters.append(mapCenter);
111 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
113 int i=mapCenters.indexOf (mco);
116 mapCenters.removeAt (i);
118 if (i>0) return mapCenters.at(i-1); // Return previous MCO
123 BranchObj* VymModel::first()
125 if (mapCenters.count()>0)
126 return mapCenters.first();
131 BranchObj* VymModel::next(BranchObj *bo_start)
134 BranchObj *bo=bo_start;
137 // Try to find next branch in current MapCenter
141 // Try to find MapCenter of bo
142 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
144 // Try to find next MapCenter
145 int i=mapCenters.indexOf ((MapCenterObj*)bo);
146 if (i+2 > mapCenters.count() || i<0) return NULL;
147 if (mapCenters.at(i+1)!=bo_start)
148 return mapCenters.at(i+1);
153 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
157 for (int i=0;i<mapCenters.count(); i++)
159 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
165 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
173 part=s.section(",",0,0);
175 num=part.right(part.length() - 3);
176 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
177 return mapCenters.at(num.toInt() );
180 for (int i=0; i<mapCenters.count(); i++)
182 lmo=mapCenters.at(i)->findObjBySelect(s);
188 LinkableMapObj* VymModel::findID (const QString &s)
191 for (int i=0; i<mapCenters.count(); i++)
193 lmo=mapCenters.at(i)->findID (s);
199 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
203 for (int i=0; i<mapCenters.count(); i++)
204 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
209 //////////////////////////////////////////////
211 //////////////////////////////////////////////
213 void VymModel::updateRelPositions()
215 for (int i=0; i<mapCenters.count(); i++)
216 mapCenters.at(i)->updateRelPositions();
219 void VymModel::reposition()
221 for (int i=0;i<mapCenters.count(); i++)
222 mapCenters.at(i)->reposition(); // for positioning heading
225 QPolygonF VymModel::shape(BranchObj *bo)
227 // Creating (arbitrary) shapes
230 QRectF rb=bo->getBBox();
231 if (bo->getDepth()==0)
233 // Just take BBox of this mapCenter
234 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
238 // Take union of BBox and TotalBBox
240 QRectF ra=bo->getTotalBBox();
241 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
244 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
247 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
251 <<QPointF (rb.topRight().x(), ra.topRight().y() )
254 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
259 void VymModel::moveAway(LinkableMapObj *lmo)
263 // Move all branches and MapCenters away from lmo
264 // to avoid collisions
269 BranchObj *boA=(BranchObj*)lmo;
271 for (int i=0; i<mapCenters.count(); i++)
273 boB=mapCenters.at(i);
276 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
279 <<" ("<<qPrintable(boA->getHeading() )<<")"
280 <<" with ("<< qPrintable (boB->getHeading() )
283 <<" minT="<<r.minTranslation<<endl<<endl;
287 void VymModel::animate()
289 animationTimer->stop();
292 while (i<animObjList.size() )
294 bo=(BranchObj*)animObjList.at(i);
297 if (i>=0) animObjList.removeAt(i);
303 mapEditor->updateSelection();
305 animationTimer->start();
309 void VymModel::startAnimation(const QPointF &start, const QPointF &dest)
311 BranchObj *bo=getSelectedBranch();
312 if (bo && bo->getDepth()>0)
317 ap.setTicks (animationTicks);
318 ap.setAnimated (true);
319 bo->setAnimation (ap);
320 animObjList.append( bo );
321 animationTimer->setSingleShot (true);
322 animationTimer->start(animationInterval);
326 void VymModel::stopAnimation(MapObj *mo)
328 int i=animObjList.indexOf(mo);
330 animObjList.removeAt (i);
333 //////////////////////////////////////////////
335 //////////////////////////////////////////////
338 // Only as long as we dont have Model/View yet
339 LinkableMapObj* VymModel::getSelection()
341 return mapEditor->getSelection();
343 BranchObj* VymModel::getSelectedBranch()
345 return mapEditor->getSelectedBranch();
349 bool VymModel::select (const QString &s)
351 return mapEditor->select (s);
354 QString VymModel::getSelectString (LinkableMapObj *lmo)
358 if (typeid(*lmo)==typeid(BranchObj) ||
359 typeid(*lmo)==typeid(MapCenterObj) )
361 LinkableMapObj *par=lmo->getParObj();
364 if (lmo->getDepth() ==1)
365 // Mainbranch, return
366 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
368 // Branch, call myself recursively
369 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
373 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
374 if (i>=0) s=QString("mc:%1").arg(i);
382 void VymModel::setHideTmp (HideTmpMode mode)
384 for (int i=0;i<mapCenters.count(); i++)
385 mapCenters.at(i)->setHideTmp (mode);
388 QRectF VymModel::getTotalBBox()
391 for (int i=0;i<mapCenters.count(); i++)
392 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);