flagrowobj.cpp
author insilmaril
Tue, 03 Jan 2006 09:44:41 +0000
changeset 173 309609406650
parent 166 325958acb69b
child 236 22a885118d50
permissions -rw-r--r--
1.7.6 New features for floatimages and fixes
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
	{	
insilmaril@173
   117
		cout << " "<<fo->getName()<<endl;
insilmaril@173
   118
		if (fo->inBox (p)) return fo->getName();
insilmaril@173
   119
	}	
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@0
   128
	if (parentRow)
insilmaril@0
   129
	{
insilmaril@0
   130
		if (fo)
insilmaril@0
   131
			return fo->isActive();
insilmaril@0
   132
		else
insilmaril@0
   133
			qWarning ("FlagRowObj::isActive of "+name+" couldn't find "+foname);
insilmaril@0
   134
			
insilmaril@0
   135
	} else
insilmaril@0
   136
		if (fo) return true;
insilmaril@0
   137
	return false;
insilmaril@0
   138
}
insilmaril@0
   139
insilmaril@103
   140
void FlagRowObj::toggle (const QString &foname, bool exclusive)
insilmaril@0
   141
{
insilmaril@0
   142
	FlagObj *fo=findFlag (foname);
insilmaril@0
   143
	if (fo)
insilmaril@0
   144
	{
insilmaril@0
   145
		// FlagObj is here, it will be active, too.
insilmaril@0
   146
		// Deactivate it by removing it from this row.
insilmaril@0
   147
		flag.remove (fo);
insilmaril@0
   148
	} else
insilmaril@0
   149
	{
insilmaril@0
   150
		// FlagObj is not present in this row.
insilmaril@0
   151
		// Copy it from parentRow
insilmaril@0
   152
		fo=parentRow->findFlag (foname);
insilmaril@0
   153
		if (fo)
insilmaril@0
   154
		{
insilmaril@0
   155
			fo=addFlag (fo);
insilmaril@0
   156
			fo->activate();
insilmaril@103
   157
			if (exclusive) 
insilmaril@103
   158
			{
insilmaril@103
   159
				deactivateGroup (fo);
insilmaril@103
   160
				updateToolbar();
insilmaril@103
   161
			}
insilmaril@0
   162
		} else
insilmaril@0
   163
			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   164
	}	
insilmaril@0
   165
	calcBBoxSize();
insilmaril@0
   166
	positionBBox();	
insilmaril@0
   167
}
insilmaril@0
   168
insilmaril@0
   169
void FlagRowObj::activate (const QString &foname)
insilmaril@0
   170
{
insilmaril@0
   171
	FlagObj *fo=findFlag (foname);
insilmaril@0
   172
	if (parentRow)
insilmaril@0
   173
	{
insilmaril@0
   174
		if (!fo)
insilmaril@0
   175
		{
insilmaril@0
   176
			// FlagObj is not present in this row.
insilmaril@0
   177
			// Copy it from parentRow and activate there
insilmaril@0
   178
			fo=parentRow->findFlag (foname);
insilmaril@0
   179
			if (fo)
insilmaril@0
   180
			{
insilmaril@0
   181
				fo=addFlag (fo);
insilmaril@0
   182
				fo->activate();
insilmaril@0
   183
				fo->setVisibility (visible);
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@103
   226
		FlagObj *fo;
insilmaril@103
   227
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@103
   228
			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
insilmaril@103
   229
				flag.remove(fo);
insilmaril@103
   230
	}	
insilmaril@103
   231
}
insilmaril@103
   232
insilmaril@0
   233
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   234
{
insilmaril@0
   235
	// If we have no parent, we are the default FlagRowObj
insilmaril@0
   236
	// and have QToolbarButtons
insilmaril@0
   237
	if (!parentRow)
insilmaril@0
   238
	{
insilmaril@0
   239
		FlagObj *fo;
insilmaril@0
   240
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   241
			fo->setEnabled (b);
insilmaril@0
   242
	}
insilmaril@0
   243
}
insilmaril@0
   244
insilmaril@0
   245
void FlagRowObj::resetUsedCounter()
insilmaril@0
   246
{
insilmaril@0
   247
	FlagObj *fo;
insilmaril@0
   248
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   249
		fo->setUsed (false);
insilmaril@0
   250
}
insilmaril@0
   251
insilmaril@0
   252
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   253
{
insilmaril@0
   254
	// Build xml string
insilmaril@0
   255
	QString s;
insilmaril@0
   256
	FlagObj *fo;
insilmaril@0
   257
	if (parentRow)
insilmaril@0
   258
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   259
		{
insilmaril@0
   260
			// save flag to xml, if flag is set 
insilmaril@0
   261
			s+=valueElement("standardflag",fo->getName() );
insilmaril@0
   262
insilmaril@0
   263
			// and tell parentRow, that this flag is used
insilmaril@0
   264
			parentRow->activate(fo->getName() );
insilmaril@0
   265
		}	
insilmaril@0
   266
	else
insilmaril@0
   267
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   268
		// and I am a parentRow 
insilmaril@0
   269
		// and this flag is really used somewhere
insilmaril@0
   270
		if (writeflags)
insilmaril@0
   271
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   272
				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
insilmaril@0
   273
	return s;		
insilmaril@0
   274
insilmaril@0
   275
}
insilmaril@0
   276
insilmaril@0
   277
void FlagRowObj::setName (const QString &n)
insilmaril@0
   278
{
insilmaril@0
   279
	name=n;
insilmaril@0
   280
}
insilmaril@0
   281
insilmaril@0
   282
void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
insilmaril@0
   283
{
insilmaril@103
   284
	//Only make toolbar for the parentrow, not each row in branches
insilmaril@0
   285
	if (!parentRow)
insilmaril@0
   286
	{
insilmaril@0
   287
		// create bar and buttons
insilmaril@0
   288
		QToolBar* tb = new QToolBar( w);
insilmaril@0
   289
		tb->setLabel (n);
insilmaril@0
   290
		QAction *a;
insilmaril@0
   291
		FlagObj *fo;
insilmaril@0
   292
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   293
		{
insilmaril@0
   294
			a=new QAction (
insilmaril@0
   295
				fo->getToolTip(),
insilmaril@0
   296
				fo->getPixmap(),
insilmaril@0
   297
				fo->getName(),
insilmaril@0
   298
				0,
insilmaril@0
   299
				w,
insilmaril@0
   300
				fo->getName()
insilmaril@0
   301
			);
insilmaril@0
   302
			a->setToggleAction(true);
insilmaril@0
   303
			// FIXME should not be enabled by default, later in updateToolbar
insilmaril@0
   304
			a->setEnabled(true);
insilmaril@0
   305
			a->addTo (tb);
insilmaril@0
   306
			fo->setButton (a);
insilmaril@0
   307
			connect(a, SIGNAL( activated() ), 
insilmaril@0
   308
					w, SLOT( standardFlagChanged() ) );
insilmaril@0
   309
		}
insilmaril@0
   310
	} else
insilmaril@166
   311
		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
insilmaril@0
   312
}
insilmaril@0
   313
insilmaril@103
   314
void  FlagRowObj::updateToolbar()
insilmaril@0
   315
{
insilmaril@0
   316
	FlagObj *fo;
insilmaril@0
   317
	if (parentRow)
insilmaril@0
   318
	{
insilmaril@0
   319
		// We are just a branch, not the toolbar default
insilmaril@0
   320
		parentRow->deactivateAll();
insilmaril@0
   321
		// In parentRow activate all existing (==active) flags
insilmaril@0
   322
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   323
			parentRow->activate(fo->getName());
insilmaril@103
   324
		parentRow->updateToolbar();	
insilmaril@0
   325
	} else
insilmaril@0
   326
	{
insilmaril@0
   327
		// We are the toolbar default
insilmaril@0
   328
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   329
			fo->updateButton();
insilmaril@0
   330
	}
insilmaril@0
   331
}
insilmaril@0
   332
insilmaril@0
   333
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   334
{
insilmaril@0
   335
	FlagObj *fo;
insilmaril@0
   336
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   337
	{
insilmaril@0
   338
		if (fo->getName()==name) return fo;
insilmaril@0
   339
	}
insilmaril@0
   340
	return NULL;
insilmaril@0
   341
}
insilmaril@0
   342