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