vymmodel.cpp
author insilmaril
Thu, 10 Apr 2008 19:56:11 +0000
changeset 687 1973a58f3900
parent 684 5f9a2771680d
child 696 0c2d74acf035
permissions -rw-r--r--
added typeinfo for gcc 4.3
     1 #include <QApplication>
     2 #include <typeinfo>
     3 
     4 #include "geometry.h"		// for addBBox
     5 #include "vymmodel.h"
     6 
     7 VymModel::VymModel() 
     8 {
     9 //    cout << "Const VymModel\n";
    10 }
    11 
    12 
    13 VymModel::~VymModel() 
    14 {
    15 //    cout << "Destr VymModel\n";
    16 }	
    17 
    18 void VymModel::clear() 
    19 {
    20 	while (!mapCenters.isEmpty())
    21 		delete mapCenters.takeFirst();
    22 }
    23 
    24 void VymModel::init () 
    25 {
    26 	addMapCenter();
    27 }
    28 
    29 void VymModel::setMapEditor(MapEditor *me)
    30 {
    31 	mapEditor=me;
    32 	for (int i=0; i<mapCenters.count(); i++)
    33 		mapCenters.at(i)->setMapEditor(mapEditor);
    34 }
    35 
    36 MapEditor* VymModel::getMapEditor()
    37 {
    38 	return mapEditor;
    39 }
    40 
    41 void VymModel::setVersion (const QString &s)
    42 {
    43 	version=s;
    44 }
    45 
    46 void VymModel::setAuthor (const QString &s)
    47 {
    48 	author=s;
    49 }
    50 
    51 QString VymModel::getAuthor()
    52 {
    53 	return author;
    54 }
    55 
    56 void VymModel::setComment (const QString &s)
    57 {
    58 	comment=s;
    59 }
    60 
    61 QString VymModel::getComment ()
    62 {
    63 	return comment;
    64 }
    65 
    66 QString VymModel::getDate ()
    67 {
    68 	return QDate::currentDate().toString ("yyyy-MM-dd");
    69 }
    70 
    71 void VymModel::setScene (QGraphicsScene *s)
    72 {
    73 	mapScene=s;
    74     init();	// Here we have a mapScene set, 
    75 			// which is (still) needed to create MapCenters
    76 }
    77 
    78 QGraphicsScene* VymModel::getScene ()
    79 {
    80 	return mapScene;
    81 }
    82 
    83 MapCenterObj* VymModel::addMapCenter()
    84 {
    85 	return addMapCenter (QPointF(0,0));
    86 }
    87 
    88 MapCenterObj* VymModel::addMapCenter(QPointF absPos)
    89 {
    90 	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
    91 	mapCenter->move (absPos);
    92     mapCenter->setVisibility (true);
    93 	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
    94 	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
    95 	mapCenters.append(mapCenter);
    96 	return mapCenter;
    97 }
    98 
    99 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
   100 {
   101 	int i=mapCenters.indexOf (mco);
   102 	if (i>=0)
   103 	{
   104 		mapCenters.removeAt (i);
   105 		delete (mco);
   106 		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
   107 	}
   108 	return NULL;
   109 }
   110 
   111 BranchObj* VymModel::first()
   112 {
   113 	if (mapCenters.count()>0) 
   114 		return mapCenters.first();
   115 	else	
   116 		return NULL;
   117 }
   118 	
   119 BranchObj* VymModel::next(BranchObj *bo_start)
   120 {
   121 	BranchObj *rbo;
   122 	BranchObj *bo=bo_start;
   123 	if (bo)
   124 	{
   125 		// Try to find next branch in current MapCenter
   126 		rbo=bo->next();
   127 		if (rbo) return rbo;
   128 
   129 		// Try to find MapCenter of bo
   130 		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   131 
   132 		// Try to find next MapCenter
   133 		int i=mapCenters.indexOf ((MapCenterObj*)bo);
   134 		if (i+2 > mapCenters.count() || i<0) return NULL;
   135 		if (mapCenters.at(i+1)!=bo_start)
   136 			return mapCenters.at(i+1);
   137 	} 
   138 	return NULL;
   139 }
   140 
   141 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   142 {
   143 	LinkableMapObj *lmo;
   144 
   145 	for (int i=0;i<mapCenters.count(); i++)
   146 	{
   147 		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   148 		if (lmo) return lmo;
   149 	}
   150 	return NULL;
   151 }
   152 
   153 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   154 {
   155 	LinkableMapObj *lmo;
   156 	if (!s.isEmpty() )
   157 	{
   158 		QString part;
   159 		QString typ;
   160 		QString num;
   161 		part=s.section(",",0,0);
   162 		typ=part.left (2);
   163 		num=part.right(part.length() - 3);
   164 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   165 			return mapCenters.at(num.toInt() );
   166 	}		
   167 
   168 	for (int i=0; i<mapCenters.count(); i++)
   169 	{
   170 		lmo=mapCenters.at(i)->findObjBySelect(s);
   171 		if (lmo) return lmo;
   172 	}	
   173 	return NULL;
   174 }
   175 
   176 LinkableMapObj* VymModel::findID (const QString &s)
   177 {
   178 	LinkableMapObj *lmo;
   179 	for (int i=0; i<mapCenters.count(); i++)
   180 	{
   181 		lmo=mapCenters.at(i)->findID (s);
   182 		if (lmo) return lmo;
   183 	}	
   184 	return NULL;
   185 }
   186 
   187 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   188 {
   189     QString s;
   190 
   191 	for (int i=0; i<mapCenters.count(); i++)
   192 		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   193     return s;
   194 }
   195 
   196 
   197 //////////////////////////////////////////////
   198 // View related
   199 //////////////////////////////////////////////
   200 
   201 void VymModel::updateRelPositions()
   202 {
   203 	for (int i=0; i<mapCenters.count(); i++)
   204 		mapCenters.at(i)->updateRelPositions();
   205 }
   206 
   207 void VymModel::reposition()
   208 {
   209 	for (int i=0;i<mapCenters.count(); i++)
   210 		mapCenters.at(i)->reposition();	//	for positioning heading
   211 }
   212 
   213 QPolygonF VymModel::shape(BranchObj *bo)
   214 {
   215 	// Creating (arbitrary) shapes
   216 
   217 	QPolygonF p;
   218 	QRectF rb=bo->getBBox();
   219 	if (bo->getDepth()==0)
   220 	{
   221 		// Just take BBox of this mapCenter
   222 		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
   223 		return p;
   224 	}
   225 
   226 	// Take union of BBox and TotalBBox 
   227 
   228 	QRectF ra=bo->getTotalBBox();
   229 	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
   230 		p   <<ra.bottomLeft()
   231 			<<ra.topLeft()
   232 			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
   233 			<<rb.topRight()
   234 			<<rb.bottomRight()
   235 			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
   236 	else		
   237 		p   <<ra.bottomRight()
   238 			<<ra.topRight()
   239 			<<QPointF (rb.topRight().x(), ra.topRight().y() )
   240 			<<rb.topLeft()
   241 			<<rb.bottomLeft()
   242 			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
   243 	return p;		
   244 
   245 }
   246 
   247 void VymModel::moveAway(LinkableMapObj *lmo)
   248 {
   249 	// Autolayout:
   250 	//
   251 	// Move all branches and MapCenters away from lmo 
   252 	// to avoid collisions 
   253 
   254 	// 
   255 
   256 
   257 	QPolygonF pA;
   258 	QPolygonF pB;
   259 
   260 	BranchObj *boA=(BranchObj*)lmo;
   261 	BranchObj *boB;
   262 	for (int i=0; i<mapCenters.count(); i++)
   263 	{
   264 		boB=mapCenters.at(i);
   265 		pA=shape (boA);
   266 		pB=shape (boB);
   267 		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
   268 		cout <<"------->"
   269 			<<"="<<r.intersect
   270 			<<"  ("<<qPrintable(boA->getHeading() )<<")"
   271 			<<"  with ("<< qPrintable (boB->getHeading() )
   272 			<<")  willIntersect"
   273 			<<r.willIntersect 
   274 			<<"  minT="<<r.minTranslation<<endl<<endl;
   275 	}
   276 }
   277 
   278 
   279 //////////////////////////////////////////////
   280 // Selection related
   281 //////////////////////////////////////////////
   282 
   283 
   284 // Only as long as we dont have Model/View yet
   285 LinkableMapObj* VymModel::getSelection()
   286 {
   287 	return mapEditor->getSelection();
   288 }
   289 BranchObj* VymModel::getSelectedBranch()
   290 {
   291 	return mapEditor->getSelectedBranch();
   292 }
   293 
   294 
   295 bool VymModel::select (const QString &s)
   296 {
   297 	return mapEditor->select (s);
   298 }
   299 
   300 QString VymModel::getSelectString (LinkableMapObj *lmo)
   301 {
   302 	QString s;
   303 	if (!lmo) return s;
   304 	if (typeid(*lmo)==typeid(BranchObj) ||
   305 		typeid(*lmo)==typeid(MapCenterObj) )
   306 	{	
   307 		LinkableMapObj *par=lmo->getParObj();
   308 		if (par)
   309 		{
   310 			if (lmo->getDepth() ==1)
   311 				// Mainbranch, return 
   312 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   313 			else	
   314 				// Branch, call myself recursively
   315 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   316 		} else
   317 		{
   318 			// MapCenter
   319 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   320 			if (i>=0) s=QString("mc:%1").arg(i);
   321 		}	
   322 	}	
   323 	return s;
   324 
   325 }
   326 
   327 	
   328 void VymModel::setHideTmp (HideTmpMode mode)
   329 {
   330 	for (int i=0;i<mapCenters.count(); i++)
   331 		mapCenters.at(i)->setHideTmp (mode);	
   332 }
   333 
   334 QRectF VymModel::getTotalBBox()
   335 {
   336 	QRectF r;
   337 	for (int i=0;i<mapCenters.count(); i++)
   338 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   339 	return r;	
   340 }
   341