vymmodel.cpp
branchrelease-1-12-maintained
changeset 40 394b2f297e1d
child 43 cf274b28e5fe
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vymmodel.cpp	Thu Jul 17 11:11:55 2008 +0000
     1.3 @@ -0,0 +1,395 @@
     1.4 +#include <QApplication>
     1.5 +#include <typeinfo>
     1.6 +
     1.7 +#include "geometry.h"		// for addBBox
     1.8 +#include "vymmodel.h"
     1.9 +
    1.10 +
    1.11 +extern Settings settings;
    1.12 +
    1.13 +VymModel::VymModel() 
    1.14 +{
    1.15 +//    cout << "Const VymModel\n";
    1.16 +}
    1.17 +
    1.18 +
    1.19 +VymModel::~VymModel() 
    1.20 +{
    1.21 +//    cout << "Destr VymModel\n";
    1.22 +}	
    1.23 +
    1.24 +void VymModel::clear() 
    1.25 +{
    1.26 +	while (!mapCenters.isEmpty())
    1.27 +		delete mapCenters.takeFirst();
    1.28 +}
    1.29 +
    1.30 +void VymModel::init () 
    1.31 +{
    1.32 +	addMapCenter();
    1.33 +
    1.34 +	// animations
    1.35 +	animationUse=settings.readBoolEntry("/animation/use",false);
    1.36 +	animationTicks=settings.readNumEntry("/animation/ticks",10);
    1.37 +	animationInterval=settings.readNumEntry("/animation/interval",50);
    1.38 +	animObjList.clear();
    1.39 +	animationTimer=new QTimer (this);
    1.40 +	connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
    1.41 +
    1.42 +}
    1.43 +
    1.44 +void VymModel::setMapEditor(MapEditor *me)
    1.45 +{
    1.46 +	mapEditor=me;
    1.47 +	for (int i=0; i<mapCenters.count(); i++)
    1.48 +		mapCenters.at(i)->setMapEditor(mapEditor);
    1.49 +}
    1.50 +
    1.51 +MapEditor* VymModel::getMapEditor()
    1.52 +{
    1.53 +	return mapEditor;
    1.54 +}
    1.55 +
    1.56 +void VymModel::setVersion (const QString &s)
    1.57 +{
    1.58 +	version=s;
    1.59 +}
    1.60 +
    1.61 +void VymModel::setAuthor (const QString &s)
    1.62 +{
    1.63 +	author=s;
    1.64 +}
    1.65 +
    1.66 +QString VymModel::getAuthor()
    1.67 +{
    1.68 +	return author;
    1.69 +}
    1.70 +
    1.71 +void VymModel::setComment (const QString &s)
    1.72 +{
    1.73 +	comment=s;
    1.74 +}
    1.75 +
    1.76 +QString VymModel::getComment ()
    1.77 +{
    1.78 +	return comment;
    1.79 +}
    1.80 +
    1.81 +QString VymModel::getDate ()
    1.82 +{
    1.83 +	return QDate::currentDate().toString ("yyyy-MM-dd");
    1.84 +}
    1.85 +
    1.86 +void VymModel::setScene (QGraphicsScene *s)
    1.87 +{
    1.88 +	mapScene=s;
    1.89 +    init();	// Here we have a mapScene set, 
    1.90 +			// which is (still) needed to create MapCenters
    1.91 +}
    1.92 +
    1.93 +QGraphicsScene* VymModel::getScene ()
    1.94 +{
    1.95 +	return mapScene;
    1.96 +}
    1.97 +
    1.98 +MapCenterObj* VymModel::addMapCenter()
    1.99 +{
   1.100 +	return addMapCenter (QPointF(0,0));
   1.101 +}
   1.102 +
   1.103 +MapCenterObj* VymModel::addMapCenter(QPointF absPos)
   1.104 +{
   1.105 +	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
   1.106 +	mapCenter->move (absPos);
   1.107 +    mapCenter->setVisibility (true);
   1.108 +	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
   1.109 +	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
   1.110 +	mapCenters.append(mapCenter);
   1.111 +	return mapCenter;
   1.112 +}
   1.113 +
   1.114 +MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
   1.115 +{
   1.116 +	int i=mapCenters.indexOf (mco);
   1.117 +	if (i>=0)
   1.118 +	{
   1.119 +		mapCenters.removeAt (i);
   1.120 +		delete (mco);
   1.121 +		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
   1.122 +	}
   1.123 +	return NULL;
   1.124 +}
   1.125 +
   1.126 +BranchObj* VymModel::first()
   1.127 +{
   1.128 +	if (mapCenters.count()>0) 
   1.129 +		return mapCenters.first();
   1.130 +	else	
   1.131 +		return NULL;
   1.132 +}
   1.133 +	
   1.134 +BranchObj* VymModel::next(BranchObj *bo_start)
   1.135 +{
   1.136 +	BranchObj *rbo;
   1.137 +	BranchObj *bo=bo_start;
   1.138 +	if (bo)
   1.139 +	{
   1.140 +		// Try to find next branch in current MapCenter
   1.141 +		rbo=bo->next();
   1.142 +		if (rbo) return rbo;
   1.143 +
   1.144 +		// Try to find MapCenter of bo
   1.145 +		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   1.146 +
   1.147 +		// Try to find next MapCenter
   1.148 +		int i=mapCenters.indexOf ((MapCenterObj*)bo);
   1.149 +		if (i+2 > mapCenters.count() || i<0) return NULL;
   1.150 +		if (mapCenters.at(i+1)!=bo_start)
   1.151 +			return mapCenters.at(i+1);
   1.152 +	} 
   1.153 +	return NULL;
   1.154 +}
   1.155 +
   1.156 +LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   1.157 +{
   1.158 +	LinkableMapObj *lmo;
   1.159 +
   1.160 +	for (int i=0;i<mapCenters.count(); i++)
   1.161 +	{
   1.162 +		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   1.163 +		if (lmo) return lmo;
   1.164 +	}
   1.165 +	return NULL;
   1.166 +}
   1.167 +
   1.168 +LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   1.169 +{
   1.170 +	LinkableMapObj *lmo;
   1.171 +	if (!s.isEmpty() )
   1.172 +	{
   1.173 +		QString part;
   1.174 +		QString typ;
   1.175 +		QString num;
   1.176 +		part=s.section(",",0,0);
   1.177 +		typ=part.left (2);
   1.178 +		num=part.right(part.length() - 3);
   1.179 +		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   1.180 +			return mapCenters.at(num.toInt() );
   1.181 +	}		
   1.182 +
   1.183 +	for (int i=0; i<mapCenters.count(); i++)
   1.184 +	{
   1.185 +		lmo=mapCenters.at(i)->findObjBySelect(s);
   1.186 +		if (lmo) return lmo;
   1.187 +	}	
   1.188 +	return NULL;
   1.189 +}
   1.190 +
   1.191 +LinkableMapObj* VymModel::findID (const QString &s)
   1.192 +{
   1.193 +	LinkableMapObj *lmo;
   1.194 +	for (int i=0; i<mapCenters.count(); i++)
   1.195 +	{
   1.196 +		lmo=mapCenters.at(i)->findID (s);
   1.197 +		if (lmo) return lmo;
   1.198 +	}	
   1.199 +	return NULL;
   1.200 +}
   1.201 +
   1.202 +QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   1.203 +{
   1.204 +    QString s;
   1.205 +
   1.206 +	for (int i=0; i<mapCenters.count(); i++)
   1.207 +		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   1.208 +    return s;
   1.209 +}
   1.210 +
   1.211 +
   1.212 +//////////////////////////////////////////////
   1.213 +// View related
   1.214 +//////////////////////////////////////////////
   1.215 +
   1.216 +void VymModel::updateRelPositions()
   1.217 +{
   1.218 +	for (int i=0; i<mapCenters.count(); i++)
   1.219 +		mapCenters.at(i)->updateRelPositions();
   1.220 +}
   1.221 +
   1.222 +void VymModel::reposition()
   1.223 +{
   1.224 +	for (int i=0;i<mapCenters.count(); i++)
   1.225 +		mapCenters.at(i)->reposition();	//	for positioning heading
   1.226 +}
   1.227 +
   1.228 +QPolygonF VymModel::shape(BranchObj *bo)
   1.229 +{
   1.230 +	// Creating (arbitrary) shapes
   1.231 +
   1.232 +	QPolygonF p;
   1.233 +	QRectF rb=bo->getBBox();
   1.234 +	if (bo->getDepth()==0)
   1.235 +	{
   1.236 +		// Just take BBox of this mapCenter
   1.237 +		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
   1.238 +		return p;
   1.239 +	}
   1.240 +
   1.241 +	// Take union of BBox and TotalBBox 
   1.242 +
   1.243 +	QRectF ra=bo->getTotalBBox();
   1.244 +	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
   1.245 +		p   <<ra.bottomLeft()
   1.246 +			<<ra.topLeft()
   1.247 +			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
   1.248 +			<<rb.topRight()
   1.249 +			<<rb.bottomRight()
   1.250 +			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
   1.251 +	else		
   1.252 +		p   <<ra.bottomRight()
   1.253 +			<<ra.topRight()
   1.254 +			<<QPointF (rb.topRight().x(), ra.topRight().y() )
   1.255 +			<<rb.topLeft()
   1.256 +			<<rb.bottomLeft()
   1.257 +			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
   1.258 +	return p;		
   1.259 +
   1.260 +}
   1.261 +
   1.262 +void VymModel::moveAway(LinkableMapObj *lmo)
   1.263 +{
   1.264 +	// Autolayout:
   1.265 +	//
   1.266 +	// Move all branches and MapCenters away from lmo 
   1.267 +	// to avoid collisions 
   1.268 +
   1.269 +	QPolygonF pA;
   1.270 +	QPolygonF pB;
   1.271 +
   1.272 +	BranchObj *boA=(BranchObj*)lmo;
   1.273 +	BranchObj *boB;
   1.274 +	for (int i=0; i<mapCenters.count(); i++)
   1.275 +	{
   1.276 +		boB=mapCenters.at(i);
   1.277 +		pA=shape (boA);
   1.278 +		pB=shape (boB);
   1.279 +		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
   1.280 +		cout <<"------->"
   1.281 +			<<"="<<r.intersect
   1.282 +			<<"  ("<<qPrintable(boA->getHeading() )<<")"
   1.283 +			<<"  with ("<< qPrintable (boB->getHeading() )
   1.284 +			<<")  willIntersect"
   1.285 +			<<r.willIntersect 
   1.286 +			<<"  minT="<<r.minTranslation<<endl<<endl;
   1.287 +	}
   1.288 +}
   1.289 +
   1.290 +void VymModel::animate()
   1.291 +{
   1.292 +	animationTimer->stop();
   1.293 +	BranchObj *bo;
   1.294 +	int i=0;
   1.295 +	while (i<animObjList.size() )
   1.296 +	{
   1.297 +		bo=(BranchObj*)animObjList.at(i);
   1.298 +		if (!bo->animate())
   1.299 +		{
   1.300 +			if (i>=0) animObjList.removeAt(i);
   1.301 +			i--;
   1.302 +		}
   1.303 +		bo->reposition();
   1.304 +		i++;
   1.305 +	} 
   1.306 +	mapEditor->updateSelection();
   1.307 +	mapScene->update();
   1.308 +	animationTimer->start();
   1.309 +}
   1.310 +
   1.311 +
   1.312 +void VymModel::startAnimation(const QPointF &start, const QPointF &dest)
   1.313 +{
   1.314 +	BranchObj *bo=getSelectedBranch();
   1.315 +	if (bo && bo->getDepth()>0) 
   1.316 +	{
   1.317 +		AnimPoint ap;
   1.318 +		ap.setStart (start);
   1.319 +		ap.setDest  (dest);
   1.320 +		ap.setTicks (animationTicks);
   1.321 +		ap.setAnimated (true);
   1.322 +		bo->setAnimation (ap);
   1.323 +		animObjList.append( bo );
   1.324 +		animationTimer->setSingleShot (true);
   1.325 +		animationTimer->start(animationInterval);
   1.326 +	}
   1.327 +}
   1.328 +
   1.329 +void VymModel::stopAnimation(MapObj *mo)
   1.330 +{
   1.331 +	int i=animObjList.indexOf(mo);
   1.332 +    if (i>=0)
   1.333 +		animObjList.removeAt (i);
   1.334 +}
   1.335 +
   1.336 +//////////////////////////////////////////////
   1.337 +// Selection related
   1.338 +//////////////////////////////////////////////
   1.339 +
   1.340 +
   1.341 +// Only as long as we dont have Model/View yet
   1.342 +LinkableMapObj* VymModel::getSelection()
   1.343 +{
   1.344 +	return mapEditor->getSelection();
   1.345 +}
   1.346 +BranchObj* VymModel::getSelectedBranch()
   1.347 +{
   1.348 +	return mapEditor->getSelectedBranch();
   1.349 +}
   1.350 +
   1.351 +
   1.352 +bool VymModel::select (const QString &s)
   1.353 +{
   1.354 +	return mapEditor->select (s);
   1.355 +}
   1.356 +
   1.357 +QString VymModel::getSelectString (LinkableMapObj *lmo)
   1.358 +{
   1.359 +	QString s;
   1.360 +	if (!lmo) return s;
   1.361 +	if (typeid(*lmo)==typeid(BranchObj) ||
   1.362 +		typeid(*lmo)==typeid(MapCenterObj) )
   1.363 +	{	
   1.364 +		LinkableMapObj *par=lmo->getParObj();
   1.365 +		if (par)
   1.366 +		{
   1.367 +			if (lmo->getDepth() ==1)
   1.368 +				// Mainbranch, return 
   1.369 +				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   1.370 +			else	
   1.371 +				// Branch, call myself recursively
   1.372 +				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   1.373 +		} else
   1.374 +		{
   1.375 +			// MapCenter
   1.376 +			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   1.377 +			if (i>=0) s=QString("mc:%1").arg(i);
   1.378 +		}	
   1.379 +	}	
   1.380 +	return s;
   1.381 +
   1.382 +}
   1.383 +
   1.384 +	
   1.385 +void VymModel::setHideTmp (HideTmpMode mode)
   1.386 +{
   1.387 +	for (int i=0;i<mapCenters.count(); i++)
   1.388 +		mapCenters.at(i)->setHideTmp (mode);	
   1.389 +}
   1.390 +
   1.391 +QRectF VymModel::getTotalBBox()
   1.392 +{
   1.393 +	QRectF r;
   1.394 +	for (int i=0;i<mapCenters.count(); i++)
   1.395 +		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   1.396 +	return r;	
   1.397 +}
   1.398 +