vymmodel.cpp
author insilmaril
Fri, 01 Feb 2008 15:28:35 +0000
changeset 665 a7db20d79c32
parent 660 d0e047b8d412
child 676 3dabc6424d73
permissions -rw-r--r--
Added brasilian translation by Amadeu Júnior
     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 	/* FIXME copied from MCO, still needed?
   196 void VymModel::updateLink()
   197 {
   198 	// set childPos to middle of MapCenterObj
   199 	childPos.setX( clickBox.topLeft().x() + (int)(clickBox.width())/2 );
   200 	childPos.setY( clickBox.topLeft().y() + (int)(clickBox.height())/2 );
   201 	parPos=childPos;		
   202 	for (int i=0; i<branch.size(); ++i)
   203 		branch.at(i)->updateLink();
   204 }
   205 
   206 */
   207 void VymModel::updateRelPositions()
   208 {
   209 	for (int i=0; i<mapCenters.count(); i++)
   210 		mapCenters.at(i)->updateRelPositions();
   211 }
   212 
   213 void VymModel::reposition()
   214 {
   215 	for (int i=0;i<mapCenters.count(); i++)
   216 		mapCenters.at(i)->reposition();	//	for positioning heading
   217 }
   218 
   219 QPolygonF VymModel::shape(BranchObj *bo)
   220 {
   221 	// Creating (arbitrary) shapes
   222 
   223 	QPolygonF p;
   224 	QRectF rb=bo->getBBox();
   225 	if (bo->getDepth()==0)
   226 	{
   227 		// Just take BBox of this mapCenter
   228 		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
   229 		return p;
   230 	}
   231 
   232 	// Take union of BBox and TotalBBox 
   233 
   234 	QRectF ra=bo->getTotalBBox();
   235 	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
   236 		p   <<ra.bottomLeft()
   237 			<<ra.topLeft()
   238 			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
   239 			<<rb.topRight()
   240 			<<rb.bottomRight()
   241 			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
   242 	else		
   243 		p   <<ra.bottomRight()
   244 			<<ra.topRight()
   245 			<<QPointF (rb.topRight().x(), ra.topRight().y() )
   246 			<<rb.topLeft()
   247 			<<rb.bottomLeft()
   248 			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
   249 	return p;		
   250 
   251 }
   252 
   253 void VymModel::moveAway(LinkableMapObj *lmo)
   254 {
   255 	// Autolayout:
   256 	//
   257 	// Move all branches and MapCenters away from lmo 
   258 	// to avoid collisions 
   259 
   260 	// 
   261 
   262 
   263 	QPolygonF pA;
   264 	QPolygonF pB;
   265 
   266 	BranchObj *boA=(BranchObj*)lmo;
   267 	BranchObj *boB;
   268 	for (int i=0; i<mapCenters.count(); i++)
   269 	{
   270 		boB=mapCenters.at(i);
   271 		pA=shape (boA);
   272 		pB=shape (boB);
   273 		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
   274 		cout <<"------->"
   275 			<<"="<<r.intersect
   276 			<<"  ("<<qPrintable(boA->getHeading() )<<")"
   277 			<<"  with ("<< qPrintable (boB->getHeading() )
   278 			<<")  willIntersect"
   279 			<<r.willIntersect 
   280 			<<"  minT="<<r.minTranslation<<endl<<endl;
   281 	}
   282 }
   283 
   284 
   285 //////////////////////////////////////////////
   286 // Selection related
   287 //////////////////////////////////////////////
   288 
   289 
   290 // Only as long as we dont have Model/View yet
   291 LinkableMapObj* VymModel::getSelection()
   292 {
   293 	return mapEditor->getSelection();
   294 }
   295 BranchObj* VymModel::getSelectedBranch()
   296 {
   297 	return mapEditor->getSelectedBranch();
   298 }
   299 
   300 
   301 bool VymModel::select (const QString &s)
   302 {
   303 	return mapEditor->select (s);
   304 }
   305 
   306 QString VymModel::getSelectString (LinkableMapObj *lmo)
   307 {
   308 	QString s;
   309 	if (!lmo) return s;
   310 	if (typeid(*lmo)==typeid(BranchObj) ||
   311 		typeid(*lmo)==typeid(MapCenterObj) )
   312 	{	
   313 		LinkableMapObj *par=lmo->getParObj();
   314 		if (par)
   315 		{
   316 			if (lmo->getDepth() ==1)
   317 				// Mainbranch, return 
   318 				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   319 			else	
   320 				// Branch, call myself recursively
   321 				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
   322 		} else
   323 		{
   324 			// MapCenter
   325 			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
   326 			if (i>=0) s=QString("mc:%1").arg(i);
   327 		}	
   328 	}	
   329 	return s;
   330 
   331 }
   332 
   333 	
   334 void VymModel::setHideTmp (HideTmpMode mode)
   335 {
   336 	for (int i=0;i<mapCenters.count(); i++)
   337 		mapCenters.at(i)->setHideTmp (mode);	
   338 }
   339 
   340 QRectF VymModel::getTotalBBox()
   341 {
   342 	QRectF r;
   343 	for (int i=0;i<mapCenters.count(); i++)
   344 		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
   345 	return r;	
   346 }
   347