flagrowobj.cpp
author insilmaril
Mon, 10 Apr 2006 11:23:33 +0000
changeset 292 40a1403c736d
parent 256 ba81afcb2b51
child 311 6a7db028088e
permissions -rw-r--r--
more KDE icons
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@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@0
   201
	if (fo) flag.remove(fo);
insilmaril@0
   202
	calcBBoxSize();
insilmaril@0
   203
	positionBBox();
insilmaril@0
   204
}
insilmaril@0
   205
insilmaril@0
   206
void FlagRowObj::deactivateAll ()
insilmaril@0
   207
{
insilmaril@0
   208
	if (!parentRow)
insilmaril@0
   209
	{
insilmaril@0
   210
		FlagObj *fo;
insilmaril@0
   211
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   212
			fo->deactivate();
insilmaril@0
   213
	} else
insilmaril@0
   214
		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
insilmaril@0
   215
}
insilmaril@0
   216
insilmaril@103
   217
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
insilmaril@103
   218
{
insilmaril@103
   219
	// deactivate all flags in keepof, but keep keepfo [sic!]
insilmaril@103
   220
	if (keepfo)
insilmaril@103
   221
	{
insilmaril@103
   222
		FlagObj *fo;
insilmaril@103
   223
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@103
   224
			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
insilmaril@103
   225
				flag.remove(fo);
insilmaril@103
   226
	}	
insilmaril@103
   227
}
insilmaril@103
   228
insilmaril@0
   229
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   230
{
insilmaril@0
   231
	// If we have no parent, we are the default FlagRowObj
insilmaril@0
   232
	// and have QToolbarButtons
insilmaril@0
   233
	if (!parentRow)
insilmaril@0
   234
	{
insilmaril@0
   235
		FlagObj *fo;
insilmaril@0
   236
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   237
			fo->setEnabled (b);
insilmaril@0
   238
	}
insilmaril@0
   239
}
insilmaril@0
   240
insilmaril@256
   241
void FlagRowObj::setShowFlags (bool b)
insilmaril@256
   242
{
insilmaril@256
   243
	showFlags=b;
insilmaril@256
   244
}
insilmaril@256
   245
insilmaril@0
   246
void FlagRowObj::resetUsedCounter()
insilmaril@0
   247
{
insilmaril@0
   248
	FlagObj *fo;
insilmaril@0
   249
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   250
		fo->setUsed (false);
insilmaril@0
   251
}
insilmaril@0
   252
insilmaril@0
   253
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   254
{
insilmaril@0
   255
	// Build xml string
insilmaril@0
   256
	QString s;
insilmaril@0
   257
	FlagObj *fo;
insilmaril@0
   258
	if (parentRow)
insilmaril@0
   259
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   260
		{
insilmaril@0
   261
			// save flag to xml, if flag is set 
insilmaril@0
   262
			s+=valueElement("standardflag",fo->getName() );
insilmaril@0
   263
insilmaril@0
   264
			// and tell parentRow, that this flag is used
insilmaril@0
   265
			parentRow->activate(fo->getName() );
insilmaril@0
   266
		}	
insilmaril@0
   267
	else
insilmaril@0
   268
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   269
		// and I am a parentRow 
insilmaril@0
   270
		// and this flag is really used somewhere
insilmaril@0
   271
		if (writeflags)
insilmaril@0
   272
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   273
				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
insilmaril@0
   274
	return s;		
insilmaril@0
   275
insilmaril@0
   276
}
insilmaril@0
   277
insilmaril@0
   278
void FlagRowObj::setName (const QString &n)
insilmaril@0
   279
{
insilmaril@0
   280
	name=n;
insilmaril@0
   281
}
insilmaril@0
   282
insilmaril@0
   283
void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
insilmaril@0
   284
{
insilmaril@103
   285
	//Only make toolbar for the parentrow, not each row in branches
insilmaril@0
   286
	if (!parentRow)
insilmaril@0
   287
	{
insilmaril@0
   288
		// create bar and buttons
insilmaril@0
   289
		QToolBar* tb = new QToolBar( w);
insilmaril@0
   290
		tb->setLabel (n);
insilmaril@0
   291
		QAction *a;
insilmaril@0
   292
		FlagObj *fo;
insilmaril@0
   293
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   294
		{
insilmaril@0
   295
			a=new QAction (
insilmaril@0
   296
				fo->getToolTip(),
insilmaril@0
   297
				fo->getPixmap(),
insilmaril@0
   298
				fo->getName(),
insilmaril@0
   299
				0,
insilmaril@0
   300
				w,
insilmaril@0
   301
				fo->getName()
insilmaril@0
   302
			);
insilmaril@0
   303
			a->setToggleAction(true);
insilmaril@0
   304
			// FIXME should not be enabled by default, later in updateToolbar
insilmaril@0
   305
			a->setEnabled(true);
insilmaril@0
   306
			a->addTo (tb);
insilmaril@0
   307
			fo->setButton (a);
insilmaril@0
   308
			connect(a, SIGNAL( activated() ), 
insilmaril@0
   309
					w, SLOT( standardFlagChanged() ) );
insilmaril@0
   310
		}
insilmaril@0
   311
	} else
insilmaril@166
   312
		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
insilmaril@0
   313
}
insilmaril@0
   314
insilmaril@103
   315
void  FlagRowObj::updateToolbar()
insilmaril@0
   316
{
insilmaril@0
   317
	FlagObj *fo;
insilmaril@0
   318
	if (parentRow)
insilmaril@0
   319
	{
insilmaril@0
   320
		// We are just a branch, not the toolbar default
insilmaril@0
   321
		parentRow->deactivateAll();
insilmaril@0
   322
		// In parentRow activate all existing (==active) flags
insilmaril@0
   323
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   324
			parentRow->activate(fo->getName());
insilmaril@103
   325
		parentRow->updateToolbar();	
insilmaril@0
   326
	} else
insilmaril@0
   327
	{
insilmaril@0
   328
		// We are the toolbar default
insilmaril@0
   329
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   330
			fo->updateButton();
insilmaril@0
   331
	}
insilmaril@0
   332
}
insilmaril@0
   333
insilmaril@0
   334
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   335
{
insilmaril@0
   336
	FlagObj *fo;
insilmaril@0
   337
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   338
	{
insilmaril@0
   339
		if (fo->getName()==name) return fo;
insilmaril@0
   340
	}
insilmaril@0
   341
	return NULL;
insilmaril@0
   342
}
insilmaril@0
   343