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 MapCenterObj *VymModel::getMapCenterNum (int i)
125 cout << "MCO i="<<i<<" count="<<mapCenters.count()<<endl;
126 if (i>mapCenters.count()-1 || i<0)
129 return mapCenters.at(i);
132 int VymModel::countMapCenters()
134 return mapCenters.count();
137 BranchObj* VymModel::first()
139 if (mapCenters.count()>0)
140 return mapCenters.first();
145 BranchObj* VymModel::next(BranchObj *bo_start)
148 BranchObj *bo=bo_start;
151 // Try to find next branch in current MapCenter
155 // Try to find MapCenter of bo
156 while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
158 // Try to find next MapCenter
159 int i=mapCenters.indexOf ((MapCenterObj*)bo);
160 if (i+2 > mapCenters.count() || i<0) return NULL;
161 if (mapCenters.at(i+1)!=bo_start)
162 return mapCenters.at(i+1);
167 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
171 for (int i=0;i<mapCenters.count(); i++)
173 lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
179 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
187 part=s.section(",",0,0);
189 num=part.right(part.length() - 3);
190 if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
191 return mapCenters.at(num.toInt() );
194 for (int i=0; i<mapCenters.count(); i++)
196 lmo=mapCenters.at(i)->findObjBySelect(s);
202 LinkableMapObj* VymModel::findID (const QString &s)
205 for (int i=0; i<mapCenters.count(); i++)
207 lmo=mapCenters.at(i)->findID (s);
213 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
217 for (int i=0; i<mapCenters.count(); i++)
218 s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
223 //////////////////////////////////////////////
225 //////////////////////////////////////////////
227 void VymModel::updateRelPositions()
229 for (int i=0; i<mapCenters.count(); i++)
230 mapCenters.at(i)->updateRelPositions();
233 void VymModel::reposition()
235 for (int i=0;i<mapCenters.count(); i++)
236 mapCenters.at(i)->reposition(); // for positioning heading
239 QPolygonF VymModel::shape(BranchObj *bo)
241 // Creating (arbitrary) shapes
244 QRectF rb=bo->getBBox();
245 if (bo->getDepth()==0)
247 // Just take BBox of this mapCenter
248 p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
252 // Take union of BBox and TotalBBox
254 QRectF ra=bo->getTotalBBox();
255 if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
258 <<QPointF (rb.topLeft().x(), ra.topLeft().y() )
261 <<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
265 <<QPointF (rb.topRight().x(), ra.topRight().y() )
268 <<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
273 void VymModel::moveAway(LinkableMapObj *lmo)
277 // Move all branches and MapCenters away from lmo
278 // to avoid collisions
283 BranchObj *boA=(BranchObj*)lmo;
285 for (int i=0; i<mapCenters.count(); i++)
287 boB=mapCenters.at(i);
290 PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
293 <<" ("<<qPrintable(boA->getHeading() )<<")"
294 <<" with ("<< qPrintable (boB->getHeading() )
297 <<" minT="<<r.minTranslation<<endl<<endl;
301 void VymModel::animate()
303 animationTimer->stop();
306 while (i<animObjList.size() )
308 bo=(BranchObj*)animObjList.at(i);
311 if (i>=0) animObjList.removeAt(i);
317 mapEditor->updateSelection();
319 if (!animObjList.isEmpty() ) animationTimer->start();
323 void VymModel::startAnimation(const QPointF &start, const QPointF &dest)
325 BranchObj *bo=getSelectedBranch();
326 if (bo && bo->getDepth()>0)
331 ap.setTicks (animationTicks);
332 ap.setAnimated (true);
333 bo->setAnimation (ap);
334 animObjList.append( bo );
335 animationTimer->setSingleShot (true);
336 animationTimer->start(animationInterval);
340 void VymModel::stopAnimation(MapObj *mo)
342 int i=animObjList.indexOf(mo);
344 animObjList.removeAt (i);
347 //////////////////////////////////////////////
349 //////////////////////////////////////////////
352 // Only as long as we dont have Model/View yet
353 LinkableMapObj* VymModel::getSelection()
355 return mapEditor->getSelection();
357 BranchObj* VymModel::getSelectedBranch()
359 return mapEditor->getSelectedBranch();
363 bool VymModel::select (const QString &s)
365 return mapEditor->select (s);
368 QString VymModel::getSelectString (LinkableMapObj *lmo)
372 if (typeid(*lmo)==typeid(BranchObj) ||
373 typeid(*lmo)==typeid(MapCenterObj) )
375 LinkableMapObj *par=lmo->getParObj();
378 if (lmo->getDepth() ==1)
379 // Mainbranch, return
380 s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
382 // Branch, call myself recursively
383 s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
387 int i=mapCenters.indexOf ((MapCenterObj*)lmo);
388 if (i>=0) s=QString("mc:%1").arg(i);
396 void VymModel::setHideTmp (HideTmpMode mode)
398 for (int i=0;i<mapCenters.count(); i++)
399 mapCenters.at(i)->setHideTmp (mode);
402 QRectF VymModel::getTotalBBox()
405 for (int i=0;i<mapCenters.count(); i++)
406 r=addBBox (mapCenters.at(i)->getTotalBBox(), r);