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