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