vymmodel.cpp
author insilmaril
Tue, 08 Apr 2008 08:28:37 +0000
changeset 683 7df0b7986274
parent 676 3dabc6424d73
child 684 5f9a2771680d
permissions -rw-r--r--
1.11.7: Fixed hanging find function
     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 	MapCenterObj *mapCenter = new MapCenterObj(mapScene);
    85     mapCenter->setVisibility (true);
    86 	mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
    87 	mapCenter->setMapEditor(mapEditor);		//FIXME needed to get defLinkStyle, mapLinkColorHint ... for later added objects
    88 	mapCenters.append(mapCenter);
    89 	return mapCenter;
    90 }
    91 
    92 MapCenterObj *VymModel::removeMapCenter(MapCenterObj* mco)
    93 {
    94 	int i=mapCenters.indexOf (mco);
    95 	if (i>=0)
    96 	{
    97 		mapCenters.removeAt (i);
    98 		delete (mco);
    99 		if (i>0) return mapCenters.at(i-1);	// Return previous MCO
   100 	}
   101 	return NULL;
   102 }
   103 
   104 BranchObj* VymModel::first()
   105 {
   106 	if (mapCenters.count()>0) 
   107 		return mapCenters.first();
   108 	else	
   109 		return NULL;
   110 }
   111 	
   112 BranchObj* VymModel::next(BranchObj *bo_start)
   113 {
   114 	BranchObj *rbo;
   115 	BranchObj *bo=bo_start;
   116 	if (bo)
   117 	{
   118 		// Try to find next branch in current MapCenter
   119 		rbo=bo->next();
   120 		if (rbo) return rbo;
   121 
   122 		// Try to find MapCenter of bo
   123 		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
   124 
   125 
   126 		// Try to find next MapCenter
   127 		int i=mapCenters.indexOf ((MapCenterObj*)bo);
   128 		if (i+1 > mapCenters.count() || i<0) return NULL;
   129 		if (mapCenters.at(i)!=bo_start)
   130 			return mapCenters.at(i);
   131 	} 
   132 	return NULL;
   133 }
   134 
   135 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
   136 {
   137 	LinkableMapObj *lmo;
   138 
   139 	for (int i=0;i<mapCenters.count(); i++)
   140 	{
   141 		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
   142 		if (lmo) return lmo;
   143 	}
   144 	return NULL;
   145 }
   146 
   147 LinkableMapObj* VymModel::findObjBySelect(const QString &s)
   148 {
   149 	LinkableMapObj *lmo;
   150 	if (!s.isEmpty() )
   151 	{
   152 		QString part;
   153 		QString typ;
   154 		QString num;
   155 		part=s.section(",",0,0);
   156 		typ=part.left (2);
   157 		num=part.right(part.length() - 3);
   158 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
   159 			return mapCenters.at(num.toInt() );
   160 	}		
   161 
   162 	for (int i=0; i<mapCenters.count(); i++)
   163 	{
   164 		lmo=mapCenters.at(i)->findObjBySelect(s);
   165 		if (lmo) return lmo;
   166 	}	
   167 	return NULL;
   168 }
   169 
   170 LinkableMapObj* VymModel::findID (const QString &s)
   171 {
   172 	LinkableMapObj *lmo;
   173 	for (int i=0; i<mapCenters.count(); i++)
   174 	{
   175 		lmo=mapCenters.at(i)->findID (s);
   176 		if (lmo) return lmo;
   177 	}	
   178 	return NULL;
   179 }
   180 
   181 QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
   182 {
   183     QString s;
   184 
   185 	for (int i=0; i<mapCenters.count(); i++)
   186 		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
   187     return s;
   188 }
   189 
   190 
   191 //////////////////////////////////////////////
   192 // View related
   193 //////////////////////////////////////////////
   194 
   195 void VymModel::updateRelPositions()
   196 {
   197 	for (int i=0; i<mapCenters.count(); i++)
   198 		mapCenters.at(i)->updateRelPositions();
   199 }
   200 
   201 void VymModel::reposition()
   202 {
   203 	for (int i=0;i<mapCenters.count(); i++)
   204 		mapCenters.at(i)->reposition();	//	for positioning heading
   205 }
   206 
   207 QPolygonF VymModel::shape(BranchObj *bo)
   208 {
   209 	// Creating (arbitrary) shapes
   210 
   211 	QPolygonF p;
   212 	QRectF rb=bo->getBBox();
   213 	if (bo->getDepth()==0)
   214 	{
   215 		// Just take BBox of this mapCenter
   216 		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
   217 		return p;
   218 	}
   219 
   220 	// Take union of BBox and TotalBBox 
   221 
   222 	QRectF ra=bo->getTotalBBox();
   223 	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
   224 		p   <<ra.bottomLeft()
   225 			<<ra.topLeft()
   226 			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
   227 			<<rb.topRight()
   228 			<<rb.bottomRight()
   229 			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
   230 	else		
   231 		p   <<ra.bottomRight()
   232 			<<ra.topRight()
   233 			<<QPointF (rb.topRight().x(), ra.topRight().y() )
   234 			<<rb.topLeft()
   235 			<<rb.bottomLeft()
   236 			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
   237 	return p;		
   238 
   239 }
   240 
   241 void VymModel::moveAway(LinkableMapObj *lmo)
   242 {
   243 	// Autolayout:
   244 	//
   245 	// Move all branches and MapCenters away from lmo 
   246 	// to avoid collisions 
   247 
   248 	// 
   249 
   250 
   251 	QPolygonF pA;
   252 	QPolygonF pB;
   253 
   254 	BranchObj *boA=(BranchObj*)lmo;
   255 	BranchObj *boB;
   256 	for (int i=0; i<mapCenters.count(); i++)
   257 	{
   258 		boB=mapCenters.at(i);
   259 		pA=shape (boA);
   260 		pB=shape (boB);
   261 		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
   262 		cout <<"------->"
   263 			<<"="<<r.intersect
   264 			<<"  ("<<qPrintable(boA->getHeading() )<<")"
   265 			<<"  with ("<< qPrintable (boB->getHeading() )
   266 			<<")  willIntersect"
   267 			<<r.willIntersect 
   268 			<<"  minT="<<r.minTranslation<<endl<<endl;
   269 	}
   270 }
   271 
   272 
   273 //////////////////////////////////////////////
   274 // Selection related
   275 //////////////////////////////////////////////
   276 
   277 
   278 // Only as long as we dont have Model/View yet
   279 LinkableMapObj* VymModel::getSelection()
   280 {
   281 	return mapEditor->getSelection();
   282 }
   283 BranchObj* VymModel::getSelectedBranch()
   284 {
   285 	return mapEditor->getSelectedBranch();
   286 }
   287 
   288 
   289 bool VymModel::select (const QString &s)
   290 {
   291 	return mapEditor->select (s);
   292 }
   293 
   294 QString VymModel::getSelectString (LinkableMapObj *lmo)
   295 {
   296 	QString s;
   297 	if (!lmo) return s;
   298 	if (typeid(*lmo)==typeid(BranchObj) ||
   299 		typeid(*lmo)==typeid(MapCenterObj) )
   300 	{	
   301 		LinkableMapObj *par=lmo->getParObj();
   302 		if (par)
   303 		{
   304 			if (lmo->getDepth() ==1)
   305 				// Mainbranch, return 
   306 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   307 			else	
   308 				// Branch, call myself recursively
   309 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   310 		} else
   311 		{
   312 			// MapCenter
   313 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   314 			if (i>=0) s=QString("mc:%1").arg(i);
   315 		}	
   316 	}	
   317 	return s;
   318 
   319 }
   320 
   321 	
   322 void VymModel::setHideTmp (HideTmpMode mode)
   323 {
   324 	for (int i=0;i<mapCenters.count(); i++)
   325 		mapCenters.at(i)->setHideTmp (mode);	
   326 }
   327 
   328 QRectF VymModel::getTotalBBox()
   329 {
   330 	QRectF r;
   331 	for (int i=0;i<mapCenters.count(); i++)
   332 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   333 	return r;	
   334 }
   335