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