flagrowobj.cpp
author insilmaril
Fri Feb 01 15:28:35 2008 +0000 (2008-02-01)
changeset 662 9abdd5e6c3dc
parent 446 c56ce5d81cc3
child 742 54d44ecd6097
permissions -rw-r--r--
Added brasilian translation by Amadeu Júnior
insilmaril@0
     1
#include "flagrowobj.h"
insilmaril@644
     2
#include "geometry.h"
insilmaril@0
     3
insilmaril@366
     4
#include <QToolBar>
insilmaril@366
     5
insilmaril@0
     6
/////////////////////////////////////////////////////////////////
insilmaril@0
     7
// FlagRowObj
insilmaril@0
     8
/////////////////////////////////////////////////////////////////
insilmaril@0
     9
FlagRowObj::FlagRowObj()
insilmaril@0
    10
{
insilmaril@166
    11
//    cout << "Const FlagRowObj ()\n";
insilmaril@0
    12
    init ();
insilmaril@0
    13
}
insilmaril@0
    14
insilmaril@408
    15
FlagRowObj::FlagRowObj(QGraphicsScene* s):MapObj(s) 
insilmaril@0
    16
{
insilmaril@408
    17
//    cout << "Const FlagRowObj (s)\n";
insilmaril@0
    18
    init ();
insilmaril@0
    19
}
insilmaril@0
    20
insilmaril@0
    21
FlagRowObj::~FlagRowObj()
insilmaril@0
    22
{
insilmaril@425
    23
	//cout << "Destr FlagRowObj\n";
insilmaril@406
    24
	while (!flag.isEmpty())
insilmaril@425
    25
		delete (flag.takeFirst() );
insilmaril@0
    26
}
insilmaril@0
    27
insilmaril@0
    28
void FlagRowObj::init ()
insilmaril@0
    29
{
insilmaril@0
    30
	parentRow=NULL;
insilmaril@256
    31
	showFlags=true;
insilmaril@0
    32
}
insilmaril@0
    33
insilmaril@0
    34
void FlagRowObj::copy (FlagRowObj* other)
insilmaril@0
    35
{
insilmaril@0
    36
    MapObj::copy(other);
insilmaril@0
    37
	parentRow=other->parentRow;
insilmaril@0
    38
	flag.clear();
insilmaril@406
    39
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
    40
		addFlag (flag.at(i));
insilmaril@0
    41
}
insilmaril@0
    42
insilmaril@0
    43
void FlagRowObj::clone (FlagRowObj* pr)
insilmaril@0
    44
{
insilmaril@0
    45
	// Difference to copy:
insilmaril@0
    46
	// We don't copy the flags here, they
insilmaril@0
    47
	// are created on the fly by toggle and activate
insilmaril@0
    48
	// This saves lots of canvas objects.
insilmaril@0
    49
	MapObj::copy(pr);
insilmaril@0
    50
	flag.clear();
insilmaril@0
    51
	parentRow=pr;
insilmaril@0
    52
}
insilmaril@0
    53
insilmaril@0
    54
void FlagRowObj::move(double x, double y)
insilmaril@0
    55
{
insilmaril@0
    56
    MapObj::move(x,y);
insilmaril@408
    57
	qreal dx=0;
insilmaril@406
    58
	for (int i=0; i<flag.size(); ++i)
insilmaril@0
    59
	{
insilmaril@406
    60
		flag.at(i)->move(x+dx,y);
insilmaril@408
    61
		dx+=QSizeF(flag.at(i)->getSize() ).width();
insilmaril@0
    62
	}
insilmaril@0
    63
}
insilmaril@0
    64
insilmaril@0
    65
void FlagRowObj::moveBy(double x, double y)
insilmaril@0
    66
{
insilmaril@0
    67
    move (x+absPos.x(),y+absPos.y() );
insilmaril@0
    68
}
insilmaril@0
    69
insilmaril@0
    70
void FlagRowObj::setVisibility (bool v)
insilmaril@0
    71
{
insilmaril@0
    72
	MapObj::setVisibility(v);
insilmaril@406
    73
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
    74
		flag.at(i)->setVisibility (v);
insilmaril@0
    75
}
insilmaril@0
    76
insilmaril@0
    77
FlagObj* FlagRowObj::addFlag (FlagObj *fo)
insilmaril@0
    78
{
insilmaril@408
    79
	FlagObj *newfo=new FlagObj (scene);
insilmaril@408
    80
	newfo->copy (fo);	// create a deep copy of fo
insilmaril@0
    81
	newfo->move (absPos.x() + bbox.width(), absPos.y() );
insilmaril@0
    82
	flag.append(newfo);
insilmaril@0
    83
	calcBBoxSize();
insilmaril@0
    84
	positionBBox();
insilmaril@0
    85
	return newfo;
insilmaril@0
    86
}
insilmaril@0
    87
insilmaril@0
    88
void FlagRowObj::positionBBox()
insilmaril@0
    89
{
insilmaril@173
    90
    bbox.moveTopLeft(absPos );
insilmaril@173
    91
    clickBox.moveTopLeft(absPos );
insilmaril@0
    92
}
insilmaril@0
    93
insilmaril@0
    94
void FlagRowObj::calcBBoxSize()
insilmaril@0
    95
{
insilmaril@408
    96
	QSizeF size(0,0);
insilmaril@408
    97
	QSizeF boxsize(0,0);
insilmaril@406
    98
	for (int i=0; i<flag.size(); ++i)
insilmaril@0
    99
	{
insilmaril@406
   100
		size=flag.at(i)->getSize();
insilmaril@0
   101
		// add widths
insilmaril@0
   102
		boxsize.setWidth(boxsize.width() + size.width() );
insilmaril@0
   103
		// maximize height
insilmaril@0
   104
		if (size.height() > boxsize.height() ) 
insilmaril@0
   105
			boxsize.setHeight(size.height() );
insilmaril@0
   106
	}
insilmaril@173
   107
	bbox.setSize (boxsize);
insilmaril@173
   108
	clickBox.setSize (boxsize);
insilmaril@0
   109
}
insilmaril@0
   110
insilmaril@408
   111
QString FlagRowObj::getFlagName (const QPointF &p)
insilmaril@0
   112
{
insilmaril@644
   113
	if (!inBox (p,clickBox)) return "";
insilmaril@406
   114
	for (int i=0; i<flag.size(); ++i)
insilmaril@644
   115
		if (inBox (p,flag.at(i)->getClickBox ())) return flag.at(i)->getName();
insilmaril@0
   116
	return "";	
insilmaril@0
   117
insilmaril@0
   118
	
insilmaril@0
   119
}
insilmaril@0
   120
insilmaril@0
   121
bool FlagRowObj::isActive (const QString &foname)
insilmaril@0
   122
{
insilmaril@0
   123
	FlagObj *fo=findFlag (foname);
insilmaril@236
   124
	if (parentRow && fo)
insilmaril@236
   125
		return fo->isActive();
insilmaril@236
   126
	else
insilmaril@0
   127
		if (fo) return true;
insilmaril@0
   128
	return false;
insilmaril@0
   129
}
insilmaril@0
   130
insilmaril@103
   131
void FlagRowObj::toggle (const QString &foname, bool exclusive)
insilmaril@0
   132
{
insilmaril@0
   133
	FlagObj *fo=findFlag (foname);
insilmaril@0
   134
	if (fo)
insilmaril@0
   135
	{
insilmaril@0
   136
		// FlagObj is here, it will be active, too.
insilmaril@0
   137
		// Deactivate it by removing it from this row.
insilmaril@0
   138
		flag.remove (fo);
insilmaril@408
   139
		delete (fo);
insilmaril@0
   140
	} else
insilmaril@0
   141
	{
insilmaril@0
   142
		// FlagObj is not present in this row.
insilmaril@0
   143
		// Copy it from parentRow
insilmaril@0
   144
		fo=parentRow->findFlag (foname);
insilmaril@0
   145
		if (fo)
insilmaril@0
   146
		{
insilmaril@0
   147
			fo=addFlag (fo);
insilmaril@0
   148
			fo->activate();
insilmaril@103
   149
			if (exclusive) 
insilmaril@103
   150
			{
insilmaril@103
   151
				deactivateGroup (fo);
insilmaril@103
   152
				updateToolbar();
insilmaril@103
   153
			}
insilmaril@0
   154
		} else
insilmaril@0
   155
			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   156
	}	
insilmaril@0
   157
	calcBBoxSize();
insilmaril@0
   158
	positionBBox();	
insilmaril@0
   159
}
insilmaril@0
   160
insilmaril@0
   161
void FlagRowObj::activate (const QString &foname)
insilmaril@0
   162
{
insilmaril@311
   163
	// Note: "activate" is also called during loading of a map
insilmaril@311
   164
	// Here we do not check for exclusive flags!
insilmaril@0
   165
	FlagObj *fo=findFlag (foname);
insilmaril@0
   166
	if (parentRow)
insilmaril@0
   167
	{
insilmaril@0
   168
		if (!fo)
insilmaril@0
   169
		{
insilmaril@0
   170
			// FlagObj is not present in this row.
insilmaril@0
   171
			// Copy it from parentRow and activate there
insilmaril@0
   172
			fo=parentRow->findFlag (foname);
insilmaril@0
   173
			if (fo)
insilmaril@0
   174
			{
insilmaril@0
   175
				fo=addFlag (fo);
insilmaril@0
   176
				fo->activate();
insilmaril@256
   177
				if (showFlags) 
insilmaril@256
   178
					fo->setVisibility (visible);
insilmaril@256
   179
				else
insilmaril@256
   180
					fo->setVisibility (false);
insilmaril@0
   181
				calcBBoxSize();
insilmaril@0
   182
			} else
insilmaril@0
   183
				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   184
		}	
insilmaril@0
   185
	} else
insilmaril@0
   186
	{
insilmaril@0
   187
		// I am the parentRow, mark flag as used
insilmaril@0
   188
		if (fo)
insilmaril@0
   189
		{
insilmaril@0
   190
			fo->setUsed(true);
insilmaril@0
   191
			fo->activate();
insilmaril@0
   192
		}	
insilmaril@0
   193
		else
insilmaril@0
   194
			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
insilmaril@0
   195
	}
insilmaril@0
   196
}
insilmaril@0
   197
insilmaril@103
   198
insilmaril@0
   199
void FlagRowObj::deactivate (const QString &foname)
insilmaril@0
   200
{
insilmaril@0
   201
	FlagObj *fo=findFlag (foname);
insilmaril@408
   202
	if (fo) 
insilmaril@408
   203
	{
insilmaril@408
   204
		flag.remove(fo);
insilmaril@408
   205
		delete (fo);
insilmaril@408
   206
	}	
insilmaril@0
   207
	calcBBoxSize();
insilmaril@0
   208
	positionBBox();
insilmaril@0
   209
}
insilmaril@0
   210
insilmaril@0
   211
void FlagRowObj::deactivateAll ()
insilmaril@0
   212
{
insilmaril@0
   213
	if (!parentRow)
insilmaril@0
   214
	{
insilmaril@406
   215
		for (int i=0; i<flag.size(); ++i)
insilmaril@408
   216
			if (flag.at(i)->isActive()) flag.at(i)->deactivate();
insilmaril@0
   217
	} else
insilmaril@446
   218
	{
insilmaril@446
   219
		while (!flag.isEmpty())
insilmaril@446
   220
			delete flag.takeFirst();
insilmaril@446
   221
		calcBBoxSize();
insilmaril@446
   222
		positionBBox();
insilmaril@446
   223
	}
insilmaril@0
   224
}
insilmaril@0
   225
insilmaril@103
   226
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
insilmaril@103
   227
{
insilmaril@103
   228
	// deactivate all flags in keepof, but keep keepfo [sic!]
insilmaril@103
   229
	if (keepfo)
insilmaril@103
   230
	{
insilmaril@311
   231
		QString g=keepfo->getGroup();
insilmaril@311
   232
		if (g!="undefined")
insilmaril@311
   233
		{
insilmaril@406
   234
			for (int i=0; i<flag.size(); ++i)
insilmaril@406
   235
				if (g==flag.at(i)->getGroup() && keepfo!=flag.at(i)) 
insilmaril@408
   236
				{
insilmaril@411
   237
					FlagObj *fo=flag.at(i);
insilmaril@411
   238
					flag.remove (fo);
insilmaril@411
   239
					delete (fo);
insilmaril@408
   240
				}	
insilmaril@311
   241
		}		
insilmaril@103
   242
	}	
insilmaril@103
   243
}
insilmaril@103
   244
insilmaril@366
   245
void FlagRowObj::setToolBar(QToolBar *tb)
insilmaril@366
   246
{
insilmaril@366
   247
	toolbar=tb;
insilmaril@366
   248
}
insilmaril@366
   249
insilmaril@0
   250
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   251
{
insilmaril@366
   252
	if (toolbar)
insilmaril@0
   253
	{
insilmaril@366
   254
		toolbar->setEnabled (b);
insilmaril@0
   255
	}
insilmaril@0
   256
}
insilmaril@0
   257
insilmaril@256
   258
void FlagRowObj::setShowFlags (bool b)
insilmaril@256
   259
{
insilmaril@256
   260
	showFlags=b;
insilmaril@256
   261
}
insilmaril@256
   262
insilmaril@0
   263
void FlagRowObj::resetUsedCounter()
insilmaril@0
   264
{
insilmaril@406
   265
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
   266
		flag.at(i)->setUsed (false);
insilmaril@0
   267
}
insilmaril@0
   268
insilmaril@0
   269
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   270
{
insilmaril@0
   271
	// Build xml string
insilmaril@0
   272
	QString s;
insilmaril@0
   273
	if (parentRow)
insilmaril@406
   274
		for (int i=0; i<flag.size(); ++i)
insilmaril@0
   275
		{
insilmaril@0
   276
			// save flag to xml, if flag is set 
insilmaril@406
   277
			s+=valueElement("standardflag",flag.at(i)->getName() );
insilmaril@0
   278
insilmaril@0
   279
			// and tell parentRow, that this flag is used
insilmaril@406
   280
			parentRow->activate(flag.at(i)->getName() );
insilmaril@0
   281
		}	
insilmaril@0
   282
	else
insilmaril@0
   283
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   284
		// and I am a parentRow 
insilmaril@0
   285
		// and this flag is really used somewhere
insilmaril@0
   286
		if (writeflags)
insilmaril@406
   287
			for (int i=0; i<flag.size(); ++i)
insilmaril@406
   288
				if (flag.at(i)->isUsed()) flag.at(i)->saveToDir (tmpdir,prefix);
insilmaril@0
   289
	return s;		
insilmaril@0
   290
insilmaril@0
   291
}
insilmaril@0
   292
insilmaril@0
   293
void FlagRowObj::setName (const QString &n)
insilmaril@0
   294
{
insilmaril@0
   295
	name=n;
insilmaril@0
   296
}
insilmaril@0
   297
insilmaril@103
   298
void  FlagRowObj::updateToolbar()
insilmaril@0
   299
{
insilmaril@0
   300
	if (parentRow)
insilmaril@0
   301
	{
insilmaril@0
   302
		// We are just a branch, not the toolbar default
insilmaril@366
   303
		// but state has to be copied from ourselves to parentrow!
insilmaril@0
   304
		parentRow->deactivateAll();
insilmaril@0
   305
		// In parentRow activate all existing (==active) flags
insilmaril@406
   306
		for (int i=0; i<flag.size(); ++i)
insilmaril@406
   307
			parentRow->activate(flag.at(i)->getName());
insilmaril@103
   308
		parentRow->updateToolbar();	
insilmaril@0
   309
	} else
insilmaril@0
   310
	{
insilmaril@0
   311
		// We are the toolbar default
insilmaril@366
   312
		if (toolbar)
insilmaril@366
   313
		{
insilmaril@366
   314
			// Update state of actions in toolbar
insilmaril@406
   315
			for (int i=0; i<flag.size(); ++i)
insilmaril@406
   316
				flag.at(i)->updateAction();
insilmaril@366
   317
		}	
insilmaril@0
   318
	}
insilmaril@0
   319
}
insilmaril@0
   320
insilmaril@0
   321
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   322
{
insilmaril@406
   323
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
   324
		if (flag.at(i)->getName()==name) return flag.at(i);
insilmaril@0
   325
	return NULL;
insilmaril@0
   326
}
insilmaril@0
   327