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