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