flagrowobj.cpp
author insilmaril
Tue, 14 Mar 2006 14:27:16 +0000
changeset 245 27b71695d690
parent 241 f5243654fe86
child 256 ba81afcb2b51
permissions -rw-r--r--
hideLinkInExport for Branches (Floats still missing). Floats are now OrnamentedObj.
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@0
    28
}
insilmaril@0
    29
insilmaril@0
    30
void FlagRowObj::copy (FlagRowObj* other)
insilmaril@0
    31
{
insilmaril@0
    32
    MapObj::copy(other);
insilmaril@0
    33
	parentRow=other->parentRow;
insilmaril@0
    34
	flag.clear();
insilmaril@0
    35
	FlagObj *fo;
insilmaril@0
    36
    for (fo=other->flag.first(); fo; fo=other->flag.next() )
insilmaril@0
    37
		addFlag (fo);
insilmaril@0
    38
}
insilmaril@0
    39
insilmaril@0
    40
void FlagRowObj::clone (FlagRowObj* pr)
insilmaril@0
    41
{
insilmaril@0
    42
	// Difference to copy:
insilmaril@0
    43
	// We don't copy the flags here, they
insilmaril@0
    44
	// are created on the fly by toggle and activate
insilmaril@0
    45
	// This saves lots of canvas objects.
insilmaril@0
    46
	MapObj::copy(pr);
insilmaril@0
    47
	flag.clear();
insilmaril@0
    48
	parentRow=pr;
insilmaril@0
    49
}
insilmaril@0
    50
insilmaril@0
    51
void FlagRowObj::move(double x, double y)
insilmaril@0
    52
{
insilmaril@0
    53
    MapObj::move(x,y);
insilmaril@0
    54
	int dx=0;
insilmaril@0
    55
	FlagObj *fo;
insilmaril@0
    56
    for (fo=flag.first(); fo; fo=flag.next() )
insilmaril@0
    57
	{
insilmaril@0
    58
		fo->move(x+dx,y);
insilmaril@0
    59
		dx+=QSize(fo->getSize() ).width();
insilmaril@0
    60
	}
insilmaril@0
    61
}
insilmaril@0
    62
insilmaril@0
    63
void FlagRowObj::moveBy(double x, double y)
insilmaril@0
    64
{
insilmaril@0
    65
    move (x+absPos.x(),y+absPos.y() );
insilmaril@0
    66
}
insilmaril@0
    67
insilmaril@0
    68
void FlagRowObj::setVisibility (bool v)
insilmaril@0
    69
{
insilmaril@0
    70
	MapObj::setVisibility(v);
insilmaril@0
    71
	FlagObj *fo;
insilmaril@0
    72
	for (fo=flag.first(); fo; fo=flag.next() )
insilmaril@0
    73
		fo->setVisibility (v);
insilmaril@0
    74
}
insilmaril@0
    75
insilmaril@0
    76
FlagObj* FlagRowObj::addFlag (FlagObj *fo)
insilmaril@0
    77
{
insilmaril@0
    78
	FlagObj *newfo=new FlagObj (canvas);
insilmaril@0
    79
	newfo->move (absPos.x() + bbox.width(), absPos.y() );
insilmaril@0
    80
	newfo->copy (fo);	// create a deep copy of fo
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@0
    95
	QSize size(0,0);
insilmaril@0
    96
	QSize boxsize(0,0);
insilmaril@0
    97
	FlagObj *fo;
insilmaril@0
    98
    for (fo=flag.first(); fo; fo=flag.next() )
insilmaril@0
    99
	{
insilmaril@0
   100
		size=fo->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@0
   111
QString FlagRowObj::getFlagName (const QPoint &p)
insilmaril@0
   112
{
insilmaril@173
   113
	if (!inBox (p)) return "";
insilmaril@0
   114
	FlagObj *fo;
insilmaril@0
   115
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@173
   116
		if (fo->inBox (p)) return fo->getName();
insilmaril@0
   117
	return "";	
insilmaril@0
   118
insilmaril@0
   119
	
insilmaril@0
   120
}
insilmaril@0
   121
insilmaril@0
   122
bool FlagRowObj::isActive (const QString &foname)
insilmaril@0
   123
{
insilmaril@0
   124
	FlagObj *fo=findFlag (foname);
insilmaril@236
   125
	if (parentRow && fo)
insilmaril@236
   126
		return fo->isActive();
insilmaril@236
   127
	else
insilmaril@0
   128
		if (fo) return true;
insilmaril@0
   129
	return false;
insilmaril@0
   130
}
insilmaril@0
   131
insilmaril@103
   132
void FlagRowObj::toggle (const QString &foname, bool exclusive)
insilmaril@0
   133
{
insilmaril@0
   134
	FlagObj *fo=findFlag (foname);
insilmaril@0
   135
	if (fo)
insilmaril@0
   136
	{
insilmaril@0
   137
		// FlagObj is here, it will be active, too.
insilmaril@0
   138
		// Deactivate it by removing it from this row.
insilmaril@0
   139
		flag.remove (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@0
   163
	FlagObj *fo=findFlag (foname);
insilmaril@0
   164
	if (parentRow)
insilmaril@0
   165
	{
insilmaril@0
   166
		if (!fo)
insilmaril@0
   167
		{
insilmaril@0
   168
			// FlagObj is not present in this row.
insilmaril@0
   169
			// Copy it from parentRow and activate there
insilmaril@0
   170
			fo=parentRow->findFlag (foname);
insilmaril@0
   171
			if (fo)
insilmaril@0
   172
			{
insilmaril@0
   173
				fo=addFlag (fo);
insilmaril@0
   174
				fo->activate();
insilmaril@0
   175
				fo->setVisibility (visible);
insilmaril@0
   176
				calcBBoxSize();
insilmaril@0
   177
			} else
insilmaril@0
   178
				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   179
		}	
insilmaril@0
   180
	} else
insilmaril@0
   181
	{
insilmaril@0
   182
		// I am the parentRow, mark flag as used
insilmaril@0
   183
		if (fo)
insilmaril@0
   184
		{
insilmaril@0
   185
			fo->setUsed(true);
insilmaril@0
   186
			fo->activate();
insilmaril@0
   187
		}	
insilmaril@0
   188
		else
insilmaril@0
   189
			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
insilmaril@0
   190
	}
insilmaril@0
   191
}
insilmaril@0
   192
insilmaril@103
   193
insilmaril@0
   194
void FlagRowObj::deactivate (const QString &foname)
insilmaril@0
   195
{
insilmaril@0
   196
	FlagObj *fo=findFlag (foname);
insilmaril@0
   197
	if (fo) flag.remove(fo);
insilmaril@0
   198
	calcBBoxSize();
insilmaril@0
   199
	positionBBox();
insilmaril@0
   200
}
insilmaril@0
   201
insilmaril@0
   202
void FlagRowObj::deactivateAll ()
insilmaril@0
   203
{
insilmaril@0
   204
	if (!parentRow)
insilmaril@0
   205
	{
insilmaril@0
   206
		FlagObj *fo;
insilmaril@0
   207
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   208
			fo->deactivate();
insilmaril@0
   209
	} else
insilmaril@0
   210
		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
insilmaril@0
   211
}
insilmaril@0
   212
insilmaril@103
   213
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
insilmaril@103
   214
{
insilmaril@103
   215
	// deactivate all flags in keepof, but keep keepfo [sic!]
insilmaril@103
   216
	if (keepfo)
insilmaril@103
   217
	{
insilmaril@103
   218
		FlagObj *fo;
insilmaril@103
   219
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@103
   220
			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
insilmaril@103
   221
				flag.remove(fo);
insilmaril@103
   222
	}	
insilmaril@103
   223
}
insilmaril@103
   224
insilmaril@0
   225
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   226
{
insilmaril@0
   227
	// If we have no parent, we are the default FlagRowObj
insilmaril@0
   228
	// and have QToolbarButtons
insilmaril@0
   229
	if (!parentRow)
insilmaril@0
   230
	{
insilmaril@0
   231
		FlagObj *fo;
insilmaril@0
   232
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   233
			fo->setEnabled (b);
insilmaril@0
   234
	}
insilmaril@0
   235
}
insilmaril@0
   236
insilmaril@0
   237
void FlagRowObj::resetUsedCounter()
insilmaril@0
   238
{
insilmaril@0
   239
	FlagObj *fo;
insilmaril@0
   240
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   241
		fo->setUsed (false);
insilmaril@0
   242
}
insilmaril@0
   243
insilmaril@0
   244
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   245
{
insilmaril@0
   246
	// Build xml string
insilmaril@0
   247
	QString s;
insilmaril@0
   248
	FlagObj *fo;
insilmaril@0
   249
	if (parentRow)
insilmaril@0
   250
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   251
		{
insilmaril@0
   252
			// save flag to xml, if flag is set 
insilmaril@0
   253
			s+=valueElement("standardflag",fo->getName() );
insilmaril@0
   254
insilmaril@0
   255
			// and tell parentRow, that this flag is used
insilmaril@0
   256
			parentRow->activate(fo->getName() );
insilmaril@0
   257
		}	
insilmaril@0
   258
	else
insilmaril@0
   259
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   260
		// and I am a parentRow 
insilmaril@0
   261
		// and this flag is really used somewhere
insilmaril@0
   262
		if (writeflags)
insilmaril@0
   263
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   264
				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
insilmaril@0
   265
	return s;		
insilmaril@0
   266
insilmaril@0
   267
}
insilmaril@0
   268
insilmaril@0
   269
void FlagRowObj::setName (const QString &n)
insilmaril@0
   270
{
insilmaril@0
   271
	name=n;
insilmaril@0
   272
}
insilmaril@0
   273
insilmaril@0
   274
void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
insilmaril@0
   275
{
insilmaril@103
   276
	//Only make toolbar for the parentrow, not each row in branches
insilmaril@0
   277
	if (!parentRow)
insilmaril@0
   278
	{
insilmaril@0
   279
		// create bar and buttons
insilmaril@0
   280
		QToolBar* tb = new QToolBar( w);
insilmaril@0
   281
		tb->setLabel (n);
insilmaril@0
   282
		QAction *a;
insilmaril@0
   283
		FlagObj *fo;
insilmaril@0
   284
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   285
		{
insilmaril@0
   286
			a=new QAction (
insilmaril@0
   287
				fo->getToolTip(),
insilmaril@0
   288
				fo->getPixmap(),
insilmaril@0
   289
				fo->getName(),
insilmaril@0
   290
				0,
insilmaril@0
   291
				w,
insilmaril@0
   292
				fo->getName()
insilmaril@0
   293
			);
insilmaril@0
   294
			a->setToggleAction(true);
insilmaril@0
   295
			// FIXME should not be enabled by default, later in updateToolbar
insilmaril@0
   296
			a->setEnabled(true);
insilmaril@0
   297
			a->addTo (tb);
insilmaril@0
   298
			fo->setButton (a);
insilmaril@0
   299
			connect(a, SIGNAL( activated() ), 
insilmaril@0
   300
					w, SLOT( standardFlagChanged() ) );
insilmaril@0
   301
		}
insilmaril@0
   302
	} else
insilmaril@166
   303
		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
insilmaril@0
   304
}
insilmaril@0
   305
insilmaril@103
   306
void  FlagRowObj::updateToolbar()
insilmaril@0
   307
{
insilmaril@0
   308
	FlagObj *fo;
insilmaril@0
   309
	if (parentRow)
insilmaril@0
   310
	{
insilmaril@0
   311
		// We are just a branch, not the toolbar default
insilmaril@0
   312
		parentRow->deactivateAll();
insilmaril@0
   313
		// In parentRow activate all existing (==active) flags
insilmaril@0
   314
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   315
			parentRow->activate(fo->getName());
insilmaril@103
   316
		parentRow->updateToolbar();	
insilmaril@0
   317
	} else
insilmaril@0
   318
	{
insilmaril@0
   319
		// We are the toolbar default
insilmaril@0
   320
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   321
			fo->updateButton();
insilmaril@0
   322
	}
insilmaril@0
   323
}
insilmaril@0
   324
insilmaril@0
   325
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   326
{
insilmaril@0
   327
	FlagObj *fo;
insilmaril@0
   328
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   329
	{
insilmaril@0
   330
		if (fo->getName()==name) return fo;
insilmaril@0
   331
	}
insilmaril@0
   332
	return NULL;
insilmaril@0
   333
}
insilmaril@0
   334