vymmodel.cpp
author insilmaril
Mon, 16 Mar 2009 15:40:49 +0000
branchrelease-1-12-maintained
changeset 61 bd98be838da9
parent 59 1c550f80c43b
child 62 85683324f94a
permissions -rw-r--r--
updated changelog file
     1 #include <QApplication>
     2 #include <typeinfo>
     3 
     4 #include "geometry.h"		// for addBBox
     5 #include "vymmodel.h"
     6 
     7 
     8 extern Settings settings;
     9 
    10 VymModel::VymModel() 
    11 {
    12 //    cout << "Const VymModel\n";
    13 }
    14 
    15 
    16 VymModel::~VymModel() 
    17 {
    18     //cout << "Destr VymModel\n";
    19 	clear();
    20 }	
    21 
    22 void VymModel::clear() 
    23 {
    24 	while (!mapCenters.isEmpty())
    25 		delete mapCenters.takeFirst();
    26 }
    27 
    28 void VymModel::init () 
    29 {
    30 	addMapCenter();
    31 
    32 	// animations
    33 	animationUse=settings.readBoolEntry("/animation/use",false);
    34 	animationTicks=settings.readNumEntry("/animation/ticks",10);
    35 	animationInterval=settings.readNumEntry("/animation/interval",50);
    36 	animObjList.clear();
    37 	animationTimer=new QTimer (this);
    38 	connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
    39 
    40 }
    41 
    42 void VymModel::setMapEditor(MapEditor *me)
    43 {
    44 	mapEditor=me;
    45 	for (int i=0; i<mapCenters.count(); i++)
    46 		mapCenters.at(i)->setMapEditor(mapEditor);
    47 }
    48 
    49 MapEditor* VymModel::getMapEditor()
    50 {
    51 	return mapEditor;
    52 }
    53 
    54 void VymModel::setVersion (const QString &s)
    55 {
    56 	version=s;
    57 }
    58 
    59 void VymModel::setAuthor (const QString &s)
    60 {
    61 	author=s;
    62 }
    63 
    64 QString VymModel::getAuthor()
    65 {
    66 	return author;
    67 }
    68 
    69 void VymModel::setComment (const QString &s)
    70 {
    71 	comment=s;
    72 }
    73 
    74 QString VymModel::getComment ()
    75 {
    76 	return comment;
    77 }
    78 
    79 QString VymModel::getDate ()
    80 {
    81 	return QDate::currentDate().toString ("yyyy-MM-dd");
    82 }
    83 
    84 void VymModel::setScene (QGraphicsScene *s)
    85 {
    86 	mapScene=s;
    87     init();	// Here we have a mapScene set, 
    88 			// which is (still) needed to create MapCenters
    89 }
    90 
    91 QGraphicsScene* VymModel::getScene ()
    92 {
    93 	return mapScene;
    94 }
    95 
    96 MapCenterObj* VymModel::addMapCenter()
    97 {
    98 	return addMapCenter (QPointF(0,0));
    99 }
   100 
   101 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
   102 {
   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);
   109 	return mapCenter;
   110 }
   111 
   112 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
   113 {
   114 	int i=mapCenters.indexOf (mco);
   115 	if (i>=0)
   116 	{
   117 		mapCenters.removeAt (i);
   118 		delete (mco);
   119 		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
   120 	}
   121 	return NULL;
   122 }
   123 
   124 MapCenterObj *VymModel::getMapCenterNum (int i)
   125 {
   126 	cout << "MCO i="<<i<<"  count="<<mapCenters.count()<<endl;
   127 	if (i>mapCenters.count()-1 || i<0)
   128 		return NULL;
   129 	else
   130 		return mapCenters.at(i);
   131 }
   132 
   133 int VymModel::countMapCenters()
   134 {
   135 	return mapCenters.count();
   136 }
   137 
   138 BranchObj* VymModel::first()
   139 {
   140 	if (mapCenters.count()>0) 
   141 		return mapCenters.first();
   142 	else	
   143 		return NULL;
   144 }
   145 	
   146 BranchObj* VymModel::next(BranchObj *bo_start)
   147 {
   148 	BranchObj *rbo;
   149 	BranchObj *bo=bo_start;
   150 	if (bo)
   151 	{
   152 		// Try to find next branch in current MapCenter
   153 		rbo=bo->next();
   154 		if (rbo) return rbo;
   155 
   156 		// Try to find MapCenter of bo
   157 		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   158 
   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);
   164 	} 
   165 	return NULL;
   166 }
   167 
   168 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   169 {
   170 	LinkableMapObj *lmo;
   171 
   172 	for (int i=0;i<mapCenters.count(); i++)
   173 	{
   174 		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   175 		if (lmo) return lmo;
   176 	}
   177 	return NULL;
   178 }
   179 
   180 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   181 {
   182 	LinkableMapObj *lmo;
   183 	if (!s.isEmpty() )
   184 	{
   185 		QString part;
   186 		QString typ;
   187 		QString num;
   188 		part=s.section(",",0,0);
   189 		typ=part.left (2);
   190 		num=part.right(part.length() - 3);
   191 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   192 			return mapCenters.at(num.toInt() );
   193 	}		
   194 
   195 	for (int i=0; i<mapCenters.count(); i++)
   196 	{
   197 		lmo=mapCenters.at(i)->findObjBySelect(s);
   198 		if (lmo) return lmo;
   199 	}	
   200 	return NULL;
   201 }
   202 
   203 LinkableMapObj* VymModel::findID (const QString &s)
   204 {
   205 	LinkableMapObj *lmo;
   206 	for (int i=0; i<mapCenters.count(); i++)
   207 	{
   208 		lmo=mapCenters.at(i)->findID (s);
   209 		if (lmo) return lmo;
   210 	}	
   211 	return NULL;
   212 }
   213 
   214 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   215 {
   216     QString s;
   217 
   218 	for (int i=0; i<mapCenters.count(); i++)
   219 		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   220     return s;
   221 }
   222 
   223 
   224 //////////////////////////////////////////////
   225 // View related
   226 //////////////////////////////////////////////
   227 
   228 void VymModel::updateRelPositions()
   229 {
   230 	for (int i=0; i<mapCenters.count(); i++)
   231 		mapCenters.at(i)->updateRelPositions();
   232 }
   233 
   234 void VymModel::reposition()
   235 {
   236 	for (int i=0;i<mapCenters.count(); i++)
   237 		mapCenters.at(i)->reposition();	//	for positioning heading
   238 }
   239 
   240 QPolygonF VymModel::shape(BranchObj *bo)
   241 {
   242 	// Creating (arbitrary) shapes
   243 
   244 	QPolygonF p;
   245 	QRectF rb=bo->getBBox();
   246 	if (bo->getDepth()==0)
   247 	{
   248 		// Just take BBox of this mapCenter
   249 		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
   250 		return p;
   251 	}
   252 
   253 	// Take union of BBox and TotalBBox 
   254 
   255 	QRectF ra=bo->getTotalBBox();
   256 	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
   257 		p   <<ra.bottomLeft()
   258 			<<ra.topLeft()
   259 			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
   260 			<<rb.topRight()
   261 			<<rb.bottomRight()
   262 			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
   263 	else		
   264 		p   <<ra.bottomRight()
   265 			<<ra.topRight()
   266 			<<QPointF (rb.topRight().x(), ra.topRight().y() )
   267 			<<rb.topLeft()
   268 			<<rb.bottomLeft()
   269 			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
   270 	return p;		
   271 
   272 }
   273 
   274 void VymModel::moveAway(LinkableMapObj *lmo)
   275 {
   276 	// Autolayout:
   277 	//
   278 	// Move all branches and MapCenters away from lmo 
   279 	// to avoid collisions 
   280 
   281 	QPolygonF pA;
   282 	QPolygonF pB;
   283 
   284 	BranchObj *boA=(BranchObj*)lmo;
   285 	BranchObj *boB;
   286 	for (int i=0; i<mapCenters.count(); i++)
   287 	{
   288 		boB=mapCenters.at(i);
   289 		pA=shape (boA);
   290 		pB=shape (boB);
   291 		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
   292 		cout <<"------->"
   293 			<<"="<<r.intersect
   294 			<<"  ("<<qPrintable(boA->getHeading() )<<")"
   295 			<<"  with ("<< qPrintable (boB->getHeading() )
   296 			<<")  willIntersect"
   297 			<<r.willIntersect 
   298 			<<"  minT="<<r.minTranslation<<endl<<endl;
   299 	}
   300 }
   301 
   302 void VymModel::animate()
   303 {
   304 	animationTimer->stop();
   305 	BranchObj *bo;
   306 	int i=0;
   307 	while (i<animObjList.size() )
   308 	{
   309 		bo=(BranchObj*)animObjList.at(i);
   310 		if (!bo->animate())
   311 		{
   312 			if (i>=0) animObjList.removeAt(i);
   313 			i--;
   314 		}
   315 		bo->reposition();
   316 		i++;
   317 	} 
   318 	mapEditor->updateSelection();
   319 	mapScene->update();
   320 	if (!animObjList.isEmpty() ) animationTimer->start();
   321 }
   322 
   323 
   324 void VymModel::startAnimation(const QPointF &start, const QPointF &dest)
   325 {
   326 	BranchObj *bo=getSelectedBranch();
   327 	if (bo && bo->getDepth()>0) 
   328 	{
   329 		AnimPoint ap;
   330 		ap.setStart (start);
   331 		ap.setDest  (dest);
   332 		ap.setTicks (animationTicks);
   333 		ap.setAnimated (true);
   334 		bo->setAnimation (ap);
   335 		animObjList.append( bo );
   336 		animationTimer->setSingleShot (true);
   337 		animationTimer->start(animationInterval);
   338 	}
   339 }
   340 
   341 void VymModel::stopAnimation(MapObj *mo)
   342 {
   343 	int i=animObjList.indexOf(mo);
   344     if (i>=0)
   345 		animObjList.removeAt (i);
   346 }
   347 
   348 //////////////////////////////////////////////
   349 // Selection related
   350 //////////////////////////////////////////////
   351 
   352 
   353 // Only as long as we dont have Model/View yet
   354 LinkableMapObj* VymModel::getSelection()
   355 {
   356 	return mapEditor->getSelection();
   357 }
   358 BranchObj* VymModel::getSelectedBranch()
   359 {
   360 	return mapEditor->getSelectedBranch();
   361 }
   362 
   363 
   364 bool VymModel::select (const QString &s)
   365 {
   366 	return mapEditor->select (s);
   367 }
   368 
   369 QString VymModel::getSelectString (LinkableMapObj *lmo)
   370 {
   371 	QString s;
   372 	if (!lmo) return s;
   373 	if (typeid(*lmo)==typeid(BranchObj) ||
   374 		typeid(*lmo)==typeid(MapCenterObj) )
   375 	{	
   376 		LinkableMapObj *par=lmo->getParObj();
   377 		if (par)
   378 		{
   379 			if (lmo->getDepth() ==1)
   380 				// Mainbranch, return 
   381 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   382 			else	
   383 				// Branch, call myself recursively
   384 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   385 		} else
   386 		{
   387 			// MapCenter
   388 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   389 			if (i>=0) s=QString("mc:%1").arg(i);
   390 		}	
   391 	}	
   392 	return s;
   393 
   394 }
   395 
   396 	
   397 void VymModel::setHideTmp (HideTmpMode mode)
   398 {
   399 	for (int i=0;i<mapCenters.count(); i++)
   400 		mapCenters.at(i)->setHideTmp (mode);	
   401 }
   402 
   403 QRectF VymModel::getTotalBBox()
   404 {
   405 	QRectF r;
   406 	for (int i=0;i<mapCenters.count(); i++)
   407 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   408 	return r;	
   409 }
   410