flagrowobj.cpp
author insilmaril
Fri Aug 19 07:42:31 2005 +0000 (2005-08-19)
changeset 154 fa05daf043a8
parent 0 7a96bd401351
child 166 325958acb69b
permissions -rw-r--r--
added license in new aboutwindow and debian subdir by Steffen Joeris
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@0
     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@0
    89
    bbox.setX (absPos.x() );
insilmaril@0
    90
    bbox.setY (absPos.y() );
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@0
   107
	bbox.setSize (QSize(boxsize.width(), boxsize.height() ));
insilmaril@0
   108
}
insilmaril@0
   109
insilmaril@0
   110
QString FlagRowObj::getFlagName (const QPoint &p)
insilmaril@0
   111
{
insilmaril@0
   112
	if (!inBBox (p)) return "";
insilmaril@0
   113
	FlagObj *fo;
insilmaril@0
   114
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   115
		if (fo->inBBox (p)) return fo->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@0
   124
	if (parentRow)
insilmaril@0
   125
	{
insilmaril@0
   126
		if (fo)
insilmaril@0
   127
			return fo->isActive();
insilmaril@0
   128
		else
insilmaril@0
   129
			qWarning ("FlagRowObj::isActive of "+name+" couldn't find "+foname);
insilmaril@0
   130
			
insilmaril@0
   131
	} else
insilmaril@0
   132
		if (fo) return true;
insilmaril@0
   133
	return false;
insilmaril@0
   134
}
insilmaril@0
   135
insilmaril@103
   136
void FlagRowObj::toggle (const QString &foname, bool exclusive)
insilmaril@0
   137
{
insilmaril@0
   138
	FlagObj *fo=findFlag (foname);
insilmaril@0
   139
	if (fo)
insilmaril@0
   140
	{
insilmaril@0
   141
		// FlagObj is here, it will be active, too.
insilmaril@0
   142
		// Deactivate it by removing it from this row.
insilmaril@0
   143
		flag.remove (fo);
insilmaril@0
   144
	} else
insilmaril@0
   145
	{
insilmaril@0
   146
		// FlagObj is not present in this row.
insilmaril@0
   147
		// Copy it from parentRow
insilmaril@0
   148
		fo=parentRow->findFlag (foname);
insilmaril@0
   149
		if (fo)
insilmaril@0
   150
		{
insilmaril@0
   151
			fo=addFlag (fo);
insilmaril@0
   152
			fo->activate();
insilmaril@103
   153
			if (exclusive) 
insilmaril@103
   154
			{
insilmaril@103
   155
				deactivateGroup (fo);
insilmaril@103
   156
				updateToolbar();
insilmaril@103
   157
			}
insilmaril@0
   158
		} else
insilmaril@0
   159
			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   160
	}	
insilmaril@0
   161
	calcBBoxSize();
insilmaril@0
   162
	positionBBox();	
insilmaril@0
   163
}
insilmaril@0
   164
insilmaril@0
   165
void FlagRowObj::activate (const QString &foname)
insilmaril@0
   166
{
insilmaril@0
   167
	FlagObj *fo=findFlag (foname);
insilmaril@0
   168
	if (parentRow)
insilmaril@0
   169
	{
insilmaril@0
   170
		if (!fo)
insilmaril@0
   171
		{
insilmaril@0
   172
			// FlagObj is not present in this row.
insilmaril@0
   173
			// Copy it from parentRow and activate there
insilmaril@0
   174
			fo=parentRow->findFlag (foname);
insilmaril@0
   175
			if (fo)
insilmaril@0
   176
			{
insilmaril@0
   177
				fo=addFlag (fo);
insilmaril@0
   178
				fo->activate();
insilmaril@0
   179
				fo->setVisibility (visible);
insilmaril@0
   180
				calcBBoxSize();
insilmaril@0
   181
				positionBBox();
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@0
   202
	if (fo) flag.remove(fo);
insilmaril@0
   203
	calcBBoxSize();
insilmaril@0
   204
	positionBBox();
insilmaril@0
   205
}
insilmaril@0
   206
insilmaril@0
   207
void FlagRowObj::deactivateAll ()
insilmaril@0
   208
{
insilmaril@0
   209
	if (!parentRow)
insilmaril@0
   210
	{
insilmaril@0
   211
		FlagObj *fo;
insilmaril@0
   212
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   213
			fo->deactivate();
insilmaril@0
   214
	} else
insilmaril@0
   215
		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
insilmaril@0
   216
}
insilmaril@0
   217
insilmaril@103
   218
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
insilmaril@103
   219
{
insilmaril@103
   220
	// deactivate all flags in keepof, but keep keepfo [sic!]
insilmaril@103
   221
	if (keepfo)
insilmaril@103
   222
	{
insilmaril@103
   223
		FlagObj *fo;
insilmaril@103
   224
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@103
   225
			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
insilmaril@103
   226
				flag.remove(fo);
insilmaril@103
   227
	}	
insilmaril@103
   228
}
insilmaril@103
   229
insilmaril@0
   230
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   231
{
insilmaril@0
   232
	// If we have no parent, we are the default FlagRowObj
insilmaril@0
   233
	// and have QToolbarButtons
insilmaril@0
   234
	if (!parentRow)
insilmaril@0
   235
	{
insilmaril@0
   236
		FlagObj *fo;
insilmaril@0
   237
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   238
			fo->setEnabled (b);
insilmaril@0
   239
	}
insilmaril@0
   240
}
insilmaril@0
   241
insilmaril@0
   242
void FlagRowObj::resetUsedCounter()
insilmaril@0
   243
{
insilmaril@0
   244
	FlagObj *fo;
insilmaril@0
   245
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   246
		fo->setUsed (false);
insilmaril@0
   247
}
insilmaril@0
   248
insilmaril@0
   249
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   250
{
insilmaril@0
   251
	// Build xml string
insilmaril@0
   252
	QString s;
insilmaril@0
   253
	FlagObj *fo;
insilmaril@0
   254
	if (parentRow)
insilmaril@0
   255
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   256
		{
insilmaril@0
   257
			// save flag to xml, if flag is set 
insilmaril@0
   258
			s+=valueElement("standardflag",fo->getName() );
insilmaril@0
   259
insilmaril@0
   260
			// and tell parentRow, that this flag is used
insilmaril@0
   261
			parentRow->activate(fo->getName() );
insilmaril@0
   262
		}	
insilmaril@0
   263
	else
insilmaril@0
   264
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   265
		// and I am a parentRow 
insilmaril@0
   266
		// and this flag is really used somewhere
insilmaril@0
   267
		if (writeflags)
insilmaril@0
   268
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   269
				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
insilmaril@0
   270
	return s;		
insilmaril@0
   271
insilmaril@0
   272
}
insilmaril@0
   273
insilmaril@0
   274
void FlagRowObj::setName (const QString &n)
insilmaril@0
   275
{
insilmaril@0
   276
	name=n;
insilmaril@0
   277
}
insilmaril@0
   278
insilmaril@0
   279
void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
insilmaril@0
   280
{
insilmaril@103
   281
	//Only make toolbar for the parentrow, not each row in branches
insilmaril@0
   282
	if (!parentRow)
insilmaril@0
   283
	{
insilmaril@0
   284
		// create bar and buttons
insilmaril@0
   285
		QToolBar* tb = new QToolBar( w);
insilmaril@0
   286
		tb->setLabel (n);
insilmaril@0
   287
		QAction *a;
insilmaril@0
   288
		FlagObj *fo;
insilmaril@0
   289
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   290
		{
insilmaril@0
   291
			a=new QAction (
insilmaril@0
   292
				fo->getToolTip(),
insilmaril@0
   293
				fo->getPixmap(),
insilmaril@0
   294
				fo->getName(),
insilmaril@0
   295
				0,
insilmaril@0
   296
				w,
insilmaril@0
   297
				fo->getName()
insilmaril@0
   298
			);
insilmaril@0
   299
			a->setToggleAction(true);
insilmaril@0
   300
			// FIXME should not be enabled by default, later in updateToolbar
insilmaril@0
   301
			a->setEnabled(true);
insilmaril@0
   302
			a->addTo (tb);
insilmaril@0
   303
			fo->setButton (a);
insilmaril@0
   304
			connect(a, SIGNAL( activated() ), 
insilmaril@0
   305
					w, SLOT( standardFlagChanged() ) );
insilmaril@0
   306
		}
insilmaril@0
   307
	} else
insilmaril@0
   308
		qWarning ("FlagRowObj::makeToolbar mustn't be called for ordinary rows");
insilmaril@0
   309
}
insilmaril@0
   310
insilmaril@103
   311
void  FlagRowObj::updateToolbar()
insilmaril@0
   312
{
insilmaril@0
   313
	FlagObj *fo;
insilmaril@0
   314
	if (parentRow)
insilmaril@0
   315
	{
insilmaril@0
   316
		// We are just a branch, not the toolbar default
insilmaril@0
   317
		parentRow->deactivateAll();
insilmaril@0
   318
		// In parentRow activate all existing (==active) flags
insilmaril@0
   319
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   320
			parentRow->activate(fo->getName());
insilmaril@103
   321
		parentRow->updateToolbar();	
insilmaril@0
   322
	} else
insilmaril@0
   323
	{
insilmaril@0
   324
		// We are the toolbar default
insilmaril@0
   325
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   326
			fo->updateButton();
insilmaril@0
   327
	}
insilmaril@0
   328
}
insilmaril@0
   329
insilmaril@0
   330
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   331
{
insilmaril@0
   332
	FlagObj *fo;
insilmaril@0
   333
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   334
	{
insilmaril@0
   335
		if (fo->getName()==name) return fo;
insilmaril@0
   336
	}
insilmaril@0
   337
	return NULL;
insilmaril@0
   338
}
insilmaril@0
   339