vymmodel.cpp
author insilmaril
Wed Jan 16 15:45:19 2008 +0000 (2008-01-16)
changeset 655 d4b49c6c6069
parent 650 65c5a0c28d20
child 660 d0e047b8d412
permissions -rw-r--r--
Fixed missing MapCenter
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@650
    19
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
    20
		mapCenters.at(i)->clear();
insilmaril@650
    21
	mapCenters.clear();	
insilmaril@650
    22
}
insilmaril@650
    23
insilmaril@650
    24
void VymModel::init () 
insilmaril@650
    25
{
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@650
   118
		rbo=bo->next();
insilmaril@650
   119
		if (rbo) return rbo;
insilmaril@650
   120
insilmaril@650
   121
		// Try to find MapCenter of bo
insilmaril@650
   122
		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
insilmaril@650
   123
insilmaril@650
   124
insilmaril@650
   125
		// Try to find next MapCenter
insilmaril@650
   126
		int i=mapCenters.indexOf ((MapCenterObj*)bo);
insilmaril@650
   127
		if (i+1 > mapCenters.count() || i<0) return NULL;
insilmaril@650
   128
		if (mapCenters.at(i)!=bo_start)
insilmaril@650
   129
			return mapCenters.at(i);
insilmaril@650
   130
	} 
insilmaril@650
   131
	return NULL;
insilmaril@650
   132
}
insilmaril@650
   133
insilmaril@650
   134
LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
insilmaril@650
   135
{
insilmaril@650
   136
	LinkableMapObj *lmo;
insilmaril@650
   137
insilmaril@650
   138
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@650
   139
	{
insilmaril@650
   140
		lmo=mapCenters.at(i)->findMapObj (p,excludeLMO);
insilmaril@650
   141
		if (lmo) return lmo;
insilmaril@650
   142
	}
insilmaril@650
   143
	return NULL;
insilmaril@650
   144
}
insilmaril@650
   145
insilmaril@650
   146
LinkableMapObj* VymModel::findObjBySelect(const QString &s)
insilmaril@650
   147
{
insilmaril@650
   148
	LinkableMapObj *lmo;
insilmaril@650
   149
	if (!s.isEmpty() )
insilmaril@650
   150
	{
insilmaril@650
   151
		QString part;
insilmaril@650
   152
		QString typ;
insilmaril@650
   153
		QString num;
insilmaril@650
   154
		part=s.section(",",0,0);
insilmaril@652
   155
		typ=part.left (2);
insilmaril@650
   156
		num=part.right(part.length() - 3);
insilmaril@650
   157
		if (typ=="mc" && num.toInt()>=0 && num.toInt() <mapCenters.count() )
insilmaril@650
   158
			return mapCenters.at(num.toInt() );
insilmaril@650
   159
	}		
insilmaril@650
   160
insilmaril@650
   161
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
   162
	{
insilmaril@650
   163
		lmo=mapCenters.at(i)->findObjBySelect(s);
insilmaril@650
   164
		if (lmo) return lmo;
insilmaril@650
   165
	}	
insilmaril@650
   166
	return NULL;
insilmaril@650
   167
}
insilmaril@650
   168
insilmaril@650
   169
LinkableMapObj* VymModel::findID (const QString &s)
insilmaril@650
   170
{
insilmaril@650
   171
	LinkableMapObj *lmo;
insilmaril@650
   172
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
   173
	{
insilmaril@650
   174
		lmo=mapCenters.at(i)->findID (s);
insilmaril@650
   175
		if (lmo) return lmo;
insilmaril@650
   176
	}	
insilmaril@650
   177
	return NULL;
insilmaril@650
   178
}
insilmaril@650
   179
insilmaril@650
   180
QString VymModel::saveToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)
insilmaril@650
   181
{
insilmaril@650
   182
    QString s;
insilmaril@650
   183
insilmaril@650
   184
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@650
   185
		s+=mapCenters.at(i)->saveToDir (tmpdir,prefix,verbose,offset);
insilmaril@650
   186
    return s;
insilmaril@650
   187
}
insilmaril@650
   188
insilmaril@650
   189
insilmaril@652
   190
//////////////////////////////////////////////
insilmaril@652
   191
// View related
insilmaril@652
   192
//////////////////////////////////////////////
insilmaril@652
   193
insilmaril@652
   194
	/* FIXME copied from MCO, still needed?
insilmaril@652
   195
void VymModel::updateLink()
insilmaril@652
   196
{
insilmaril@652
   197
	// set childPos to middle of MapCenterObj
insilmaril@652
   198
	childPos.setX( clickBox.topLeft().x() + (int)(clickBox.width())/2 );
insilmaril@652
   199
	childPos.setY( clickBox.topLeft().y() + (int)(clickBox.height())/2 );
insilmaril@652
   200
	parPos=childPos;		
insilmaril@652
   201
	for (int i=0; i<branch.size(); ++i)
insilmaril@652
   202
		branch.at(i)->updateLink();
insilmaril@652
   203
}
insilmaril@652
   204
insilmaril@652
   205
*/
insilmaril@652
   206
void VymModel::updateRelPositions()
insilmaril@652
   207
{
insilmaril@652
   208
	for (int i=0; i<mapCenters.count(); i++)
insilmaril@652
   209
		mapCenters.at(i)->updateRelPositions();
insilmaril@652
   210
}
insilmaril@652
   211
insilmaril@652
   212
void VymModel::reposition()
insilmaril@652
   213
{
insilmaril@652
   214
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@652
   215
		mapCenters.at(i)->reposition();	//	for positioning heading
insilmaril@652
   216
}
insilmaril@652
   217
insilmaril@652
   218
insilmaril@652
   219
insilmaril@652
   220
//////////////////////////////////////////////
insilmaril@652
   221
// Selection related
insilmaril@652
   222
//////////////////////////////////////////////
insilmaril@652
   223
insilmaril@652
   224
insilmaril@652
   225
// Only as long as we dont have Model/View yet
insilmaril@652
   226
LinkableMapObj* VymModel::getSelection()
insilmaril@652
   227
{
insilmaril@652
   228
	return mapEditor->getSelection();
insilmaril@652
   229
}
insilmaril@652
   230
BranchObj* VymModel::getSelectedBranch()
insilmaril@652
   231
{
insilmaril@652
   232
	return mapEditor->getSelectedBranch();
insilmaril@652
   233
}
insilmaril@652
   234
insilmaril@652
   235
insilmaril@652
   236
bool VymModel::select (const QString &s)
insilmaril@652
   237
{
insilmaril@652
   238
	return mapEditor->select (s);
insilmaril@652
   239
}
insilmaril@652
   240
insilmaril@652
   241
QString VymModel::getSelectString (LinkableMapObj *lmo)
insilmaril@652
   242
{
insilmaril@652
   243
	QString s;
insilmaril@652
   244
	if (!lmo) return s;
insilmaril@652
   245
	if (typeid(*lmo)==typeid(BranchObj) ||
insilmaril@652
   246
		typeid(*lmo)==typeid(MapCenterObj) )
insilmaril@652
   247
	{	
insilmaril@652
   248
		LinkableMapObj *par=lmo->getParObj();
insilmaril@652
   249
		if (par)
insilmaril@652
   250
		{
insilmaril@652
   251
			if (lmo->getDepth() ==1)
insilmaril@652
   252
				// Mainbranch, return 
insilmaril@652
   253
				s= "bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
insilmaril@652
   254
			else	
insilmaril@652
   255
				// Branch, call myself recursively
insilmaril@652
   256
				s= getSelectString(par) + ",bo:" + QString("%1").arg(((BranchObj*)lmo)->getNum());
insilmaril@652
   257
		} else
insilmaril@652
   258
		{
insilmaril@652
   259
			// MapCenter
insilmaril@652
   260
			int i=mapCenters.indexOf ((MapCenterObj*)lmo);
insilmaril@652
   261
			if (i>=0) s=QString("mc:%1").arg(i);
insilmaril@652
   262
		}	
insilmaril@652
   263
	}	
insilmaril@652
   264
	return s;
insilmaril@652
   265
insilmaril@652
   266
}
insilmaril@652
   267
insilmaril@652
   268
	
insilmaril@652
   269
void VymModel::setHideTmp (HideTmpMode mode)
insilmaril@652
   270
{
insilmaril@652
   271
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@652
   272
		mapCenters.at(i)->setHideTmp (mode);	
insilmaril@652
   273
}
insilmaril@652
   274
insilmaril@652
   275
QRectF VymModel::getTotalBBox()
insilmaril@652
   276
{
insilmaril@652
   277
	QRectF r;
insilmaril@652
   278
	for (int i=0;i<mapCenters.count(); i++)
insilmaril@652
   279
		r=addBBox (mapCenters.at(i)->getTotalBBox(), r);
insilmaril@652
   280
	return r;	
insilmaril@652
   281
}
insilmaril@652
   282