flagrowobj.cpp
author insilmaril
Mon May 08 13:25:48 2006 +0000 (2006-05-08)
changeset 319 b4b4ff36d160
parent 256 ba81afcb2b51
child 366 e95081c21da2
permissions -rw-r--r--
1.7.16 Small bugfixes
insilmaril@0
     1
#include "flagrowobj.h"
insilmaril@0
     2
insilmaril@0
     3
/////////////////////////////////////////////////////////////////
insilmaril@0
     4
// FlagRowObj
insilmaril@0
     5
/////////////////////////////////////////////////////////////////
insilmaril@0
     6
FlagRowObj::FlagRowObj()
insilmaril@0
     7
{
insilmaril@166
     8
//    cout << "Const FlagRowObj ()\n";
insilmaril@0
     9
    init ();
insilmaril@0
    10
}
insilmaril@0
    11
insilmaril@0
    12
FlagRowObj::FlagRowObj(QCanvas* c):MapObj(c) 
insilmaril@0
    13
{
insilmaril@0
    14
//    cout << "Const FlagRowObj\n";
insilmaril@0
    15
    init ();
insilmaril@0
    16
}
insilmaril@0
    17
insilmaril@0
    18
FlagRowObj::~FlagRowObj()
insilmaril@0
    19
{
insilmaril@0
    20
//    cout << "Destr FlagRowObj\n";
insilmaril@0
    21
	flag.clear();
insilmaril@0
    22
}
insilmaril@0
    23
insilmaril@0
    24
void FlagRowObj::init ()
insilmaril@0
    25
{
insilmaril@0
    26
    flag.setAutoDelete (true);
insilmaril@0
    27
	parentRow=NULL;
insilmaril@256
    28
	showFlags=true;
insilmaril@0
    29
}
insilmaril@0
    30
insilmaril@0
    31
void FlagRowObj::copy (FlagRowObj* other)
insilmaril@0
    32
{
insilmaril@0
    33
    MapObj::copy(other);
insilmaril@0
    34
	parentRow=other->parentRow;
insilmaril@0
    35
	flag.clear();
insilmaril@0
    36
	FlagObj *fo;
insilmaril@0
    37
    for (fo=other->flag.first(); fo; fo=other->flag.next() )
insilmaril@0
    38
		addFlag (fo);
insilmaril@0
    39
}
insilmaril@0
    40
insilmaril@0
    41
void FlagRowObj::clone (FlagRowObj* pr)
insilmaril@0
    42
{
insilmaril@0
    43
	// Difference to copy:
insilmaril@0
    44
	// We don't copy the flags here, they
insilmaril@0
    45
	// are created on the fly by toggle and activate
insilmaril@0
    46
	// This saves lots of canvas objects.
insilmaril@0
    47
	MapObj::copy(pr);
insilmaril@0
    48
	flag.clear();
insilmaril@0
    49
	parentRow=pr;
insilmaril@0
    50
}
insilmaril@0
    51
insilmaril@0
    52
void FlagRowObj::move(double x, double y)
insilmaril@0
    53
{
insilmaril@0
    54
    MapObj::move(x,y);
insilmaril@0
    55
	int dx=0;
insilmaril@0
    56
	FlagObj *fo;
insilmaril@0
    57
    for (fo=flag.first(); fo; fo=flag.next() )
insilmaril@0
    58
	{
insilmaril@0
    59
		fo->move(x+dx,y);
insilmaril@0
    60
		dx+=QSize(fo->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@0
    72
	FlagObj *fo;
insilmaril@0
    73
	for (fo=flag.first(); fo; fo=flag.next() )
insilmaril@0
    74
		fo->setVisibility (v);
insilmaril@0
    75
}
insilmaril@0
    76
insilmaril@0
    77
FlagObj* FlagRowObj::addFlag (FlagObj *fo)
insilmaril@0
    78
{
insilmaril@0
    79
	FlagObj *newfo=new FlagObj (canvas);
insilmaril@0
    80
	newfo->move (absPos.x() + bbox.width(), absPos.y() );
insilmaril@0
    81
	newfo->copy (fo);	// create a deep copy of fo
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@0
    96
	QSize size(0,0);
insilmaril@0
    97
	QSize boxsize(0,0);
insilmaril@0
    98
	FlagObj *fo;
insilmaril@0
    99
    for (fo=flag.first(); fo; fo=flag.next() )
insilmaril@0
   100
	{
insilmaril@0
   101
		size=fo->getSize();
insilmaril@0
   102
		// add widths
insilmaril@0
   103
		boxsize.setWidth(boxsize.width() + size.width() );
insilmaril@0
   104
		// maximize height
insilmaril@0
   105
		if (size.height() > boxsize.height() ) 
insilmaril@0
   106
			boxsize.setHeight(size.height() );
insilmaril@0
   107
	}
insilmaril@173
   108
	bbox.setSize (boxsize);
insilmaril@173
   109
	clickBox.setSize (boxsize);
insilmaril@0
   110
}
insilmaril@0
   111
insilmaril@0
   112
QString FlagRowObj::getFlagName (const QPoint &p)
insilmaril@0
   113
{
insilmaril@173
   114
	if (!inBox (p)) return "";
insilmaril@0
   115
	FlagObj *fo;
insilmaril@0
   116
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@173
   117
		if (fo->inBox (p)) return fo->getName();
insilmaril@0
   118
	return "";	
insilmaril@0
   119
insilmaril@0
   120
	
insilmaril@0
   121
}
insilmaril@0
   122
insilmaril@0
   123
bool FlagRowObj::isActive (const QString &foname)
insilmaril@0
   124
{
insilmaril@0
   125
	FlagObj *fo=findFlag (foname);
insilmaril@236
   126
	if (parentRow && fo)
insilmaril@236
   127
		return fo->isActive();
insilmaril@236
   128
	else
insilmaril@0
   129
		if (fo) return true;
insilmaril@0
   130
	return false;
insilmaril@0
   131
}
insilmaril@0
   132
insilmaril@103
   133
void FlagRowObj::toggle (const QString &foname, bool exclusive)
insilmaril@0
   134
{
insilmaril@0
   135
	FlagObj *fo=findFlag (foname);
insilmaril@0
   136
	if (fo)
insilmaril@0
   137
	{
insilmaril@0
   138
		// FlagObj is here, it will be active, too.
insilmaril@0
   139
		// Deactivate it by removing it from this row.
insilmaril@0
   140
		flag.remove (fo);
insilmaril@0
   141
	} else
insilmaril@0
   142
	{
insilmaril@0
   143
		// FlagObj is not present in this row.
insilmaril@0
   144
		// Copy it from parentRow
insilmaril@0
   145
		fo=parentRow->findFlag (foname);
insilmaril@0
   146
		if (fo)
insilmaril@0
   147
		{
insilmaril@0
   148
			fo=addFlag (fo);
insilmaril@0
   149
			fo->activate();
insilmaril@103
   150
			if (exclusive) 
insilmaril@103
   151
			{
insilmaril@103
   152
				deactivateGroup (fo);
insilmaril@103
   153
				updateToolbar();
insilmaril@103
   154
			}
insilmaril@0
   155
		} else
insilmaril@0
   156
			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   157
	}	
insilmaril@0
   158
	calcBBoxSize();
insilmaril@0
   159
	positionBBox();	
insilmaril@0
   160
}
insilmaril@0
   161
insilmaril@0
   162
void FlagRowObj::activate (const QString &foname)
insilmaril@0
   163
{
insilmaril@311
   164
	// Note: "activate" is also called during loading of a map
insilmaril@311
   165
	// Here we do not check for exclusive flags!
insilmaril@0
   166
	FlagObj *fo=findFlag (foname);
insilmaril@0
   167
	if (parentRow)
insilmaril@0
   168
	{
insilmaril@0
   169
		if (!fo)
insilmaril@0
   170
		{
insilmaril@0
   171
			// FlagObj is not present in this row.
insilmaril@0
   172
			// Copy it from parentRow and activate there
insilmaril@0
   173
			fo=parentRow->findFlag (foname);
insilmaril@0
   174
			if (fo)
insilmaril@0
   175
			{
insilmaril@0
   176
				fo=addFlag (fo);
insilmaril@0
   177
				fo->activate();
insilmaril@256
   178
				if (showFlags) 
insilmaril@256
   179
					fo->setVisibility (visible);
insilmaril@256
   180
				else
insilmaril@256
   181
					fo->setVisibility (false);
insilmaril@0
   182
				calcBBoxSize();
insilmaril@0
   183
			} else
insilmaril@0
   184
				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   185
		}	
insilmaril@0
   186
	} else
insilmaril@0
   187
	{
insilmaril@0
   188
		// I am the parentRow, mark flag as used
insilmaril@0
   189
		if (fo)
insilmaril@0
   190
		{
insilmaril@0
   191
			fo->setUsed(true);
insilmaril@0
   192
			fo->activate();
insilmaril@0
   193
		}	
insilmaril@0
   194
		else
insilmaril@0
   195
			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
insilmaril@0
   196
	}
insilmaril@0
   197
}
insilmaril@0
   198
insilmaril@103
   199
insilmaril@0
   200
void FlagRowObj::deactivate (const QString &foname)
insilmaril@0
   201
{
insilmaril@0
   202
	FlagObj *fo=findFlag (foname);
insilmaril@0
   203
	if (fo) flag.remove(fo);
insilmaril@0
   204
	calcBBoxSize();
insilmaril@0
   205
	positionBBox();
insilmaril@0
   206
}
insilmaril@0
   207
insilmaril@0
   208
void FlagRowObj::deactivateAll ()
insilmaril@0
   209
{
insilmaril@0
   210
	if (!parentRow)
insilmaril@0
   211
	{
insilmaril@0
   212
		FlagObj *fo;
insilmaril@0
   213
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   214
			fo->deactivate();
insilmaril@0
   215
	} else
insilmaril@0
   216
		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
insilmaril@0
   217
}
insilmaril@0
   218
insilmaril@103
   219
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
insilmaril@103
   220
{
insilmaril@103
   221
	// deactivate all flags in keepof, but keep keepfo [sic!]
insilmaril@103
   222
	if (keepfo)
insilmaril@103
   223
	{
insilmaril@311
   224
		QString g=keepfo->getGroup();
insilmaril@311
   225
		if (g!="undefined")
insilmaril@311
   226
		{
insilmaril@311
   227
			FlagObj *fo;
insilmaril@311
   228
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@311
   229
				if (g==fo->getGroup() && keepfo!=fo) 
insilmaril@311
   230
					flag.remove(fo);
insilmaril@311
   231
		}		
insilmaril@103
   232
	}	
insilmaril@103
   233
}
insilmaril@103
   234
insilmaril@0
   235
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   236
{
insilmaril@0
   237
	// If we have no parent, we are the default FlagRowObj
insilmaril@0
   238
	// and have QToolbarButtons
insilmaril@0
   239
	if (!parentRow)
insilmaril@0
   240
	{
insilmaril@0
   241
		FlagObj *fo;
insilmaril@0
   242
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   243
			fo->setEnabled (b);
insilmaril@0
   244
	}
insilmaril@0
   245
}
insilmaril@0
   246
insilmaril@256
   247
void FlagRowObj::setShowFlags (bool b)
insilmaril@256
   248
{
insilmaril@256
   249
	showFlags=b;
insilmaril@256
   250
}
insilmaril@256
   251
insilmaril@0
   252
void FlagRowObj::resetUsedCounter()
insilmaril@0
   253
{
insilmaril@0
   254
	FlagObj *fo;
insilmaril@0
   255
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   256
		fo->setUsed (false);
insilmaril@0
   257
}
insilmaril@0
   258
insilmaril@0
   259
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   260
{
insilmaril@0
   261
	// Build xml string
insilmaril@0
   262
	QString s;
insilmaril@0
   263
	FlagObj *fo;
insilmaril@0
   264
	if (parentRow)
insilmaril@0
   265
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   266
		{
insilmaril@0
   267
			// save flag to xml, if flag is set 
insilmaril@0
   268
			s+=valueElement("standardflag",fo->getName() );
insilmaril@0
   269
insilmaril@0
   270
			// and tell parentRow, that this flag is used
insilmaril@0
   271
			parentRow->activate(fo->getName() );
insilmaril@0
   272
		}	
insilmaril@0
   273
	else
insilmaril@0
   274
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   275
		// and I am a parentRow 
insilmaril@0
   276
		// and this flag is really used somewhere
insilmaril@0
   277
		if (writeflags)
insilmaril@0
   278
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   279
				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
insilmaril@0
   280
	return s;		
insilmaril@0
   281
insilmaril@0
   282
}
insilmaril@0
   283
insilmaril@0
   284
void FlagRowObj::setName (const QString &n)
insilmaril@0
   285
{
insilmaril@0
   286
	name=n;
insilmaril@0
   287
}
insilmaril@0
   288
insilmaril@0
   289
void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
insilmaril@0
   290
{
insilmaril@103
   291
	//Only make toolbar for the parentrow, not each row in branches
insilmaril@0
   292
	if (!parentRow)
insilmaril@0
   293
	{
insilmaril@0
   294
		// create bar and buttons
insilmaril@0
   295
		QToolBar* tb = new QToolBar( w);
insilmaril@0
   296
		tb->setLabel (n);
insilmaril@0
   297
		QAction *a;
insilmaril@0
   298
		FlagObj *fo;
insilmaril@0
   299
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   300
		{
insilmaril@0
   301
			a=new QAction (
insilmaril@0
   302
				fo->getToolTip(),
insilmaril@0
   303
				fo->getPixmap(),
insilmaril@0
   304
				fo->getName(),
insilmaril@0
   305
				0,
insilmaril@0
   306
				w,
insilmaril@0
   307
				fo->getName()
insilmaril@0
   308
			);
insilmaril@0
   309
			a->setToggleAction(true);
insilmaril@0
   310
			// FIXME should not be enabled by default, later in updateToolbar
insilmaril@0
   311
			a->setEnabled(true);
insilmaril@0
   312
			a->addTo (tb);
insilmaril@0
   313
			fo->setButton (a);
insilmaril@0
   314
			connect(a, SIGNAL( activated() ), 
insilmaril@0
   315
					w, SLOT( standardFlagChanged() ) );
insilmaril@0
   316
		}
insilmaril@0
   317
	} else
insilmaril@166
   318
		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
insilmaril@0
   319
}
insilmaril@0
   320
insilmaril@103
   321
void  FlagRowObj::updateToolbar()
insilmaril@0
   322
{
insilmaril@0
   323
	FlagObj *fo;
insilmaril@0
   324
	if (parentRow)
insilmaril@0
   325
	{
insilmaril@0
   326
		// We are just a branch, not the toolbar default
insilmaril@0
   327
		parentRow->deactivateAll();
insilmaril@0
   328
		// In parentRow activate all existing (==active) flags
insilmaril@0
   329
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   330
			parentRow->activate(fo->getName());
insilmaril@103
   331
		parentRow->updateToolbar();	
insilmaril@0
   332
	} else
insilmaril@0
   333
	{
insilmaril@0
   334
		// We are the toolbar default
insilmaril@0
   335
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   336
			fo->updateButton();
insilmaril@0
   337
	}
insilmaril@0
   338
}
insilmaril@0
   339
insilmaril@0
   340
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   341
{
insilmaril@0
   342
	FlagObj *fo;
insilmaril@0
   343
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   344
	{
insilmaril@0
   345
		if (fo->getName()==name) return fo;
insilmaril@0
   346
	}
insilmaril@0
   347
	return NULL;
insilmaril@0
   348
}
insilmaril@0
   349