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