flagrowobj.cpp
author insilmaril
Thu Jul 17 09:27:20 2008 +0000 (2008-07-17)
branchrelease-1-12-maintained
changeset 39 e37153bea487
child 2 608f976aa7bb
child 103 c810a11d11d9
permissions -rw-r--r--
removed pixel garbage on right side of exported images
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@0
   136
void FlagRowObj::toggle (const QString &foname)
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@0
   153
		} else
insilmaril@0
   154
			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   155
	}	
insilmaril@0
   156
	calcBBoxSize();
insilmaril@0
   157
	positionBBox();	
insilmaril@0
   158
}
insilmaril@0
   159
insilmaril@0
   160
void FlagRowObj::activate (const QString &foname)
insilmaril@0
   161
{
insilmaril@0
   162
	FlagObj *fo=findFlag (foname);
insilmaril@0
   163
	if (parentRow)
insilmaril@0
   164
	{
insilmaril@0
   165
		if (!fo)
insilmaril@0
   166
		{
insilmaril@0
   167
			// FlagObj is not present in this row.
insilmaril@0
   168
			// Copy it from parentRow and activate there
insilmaril@0
   169
			fo=parentRow->findFlag (foname);
insilmaril@0
   170
			if (fo)
insilmaril@0
   171
			{
insilmaril@0
   172
				fo=addFlag (fo);
insilmaril@0
   173
				fo->activate();
insilmaril@0
   174
				fo->setVisibility (visible);
insilmaril@0
   175
				calcBBoxSize();
insilmaril@0
   176
				positionBBox();
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@0
   193
void FlagRowObj::deactivate (const QString &foname)
insilmaril@0
   194
{
insilmaril@0
   195
	FlagObj *fo=findFlag (foname);
insilmaril@0
   196
	if (fo) flag.remove(fo);
insilmaril@0
   197
	calcBBoxSize();
insilmaril@0
   198
	positionBBox();
insilmaril@0
   199
}
insilmaril@0
   200
insilmaril@0
   201
void FlagRowObj::deactivateAll ()
insilmaril@0
   202
{
insilmaril@0
   203
	if (!parentRow)
insilmaril@0
   204
	{
insilmaril@0
   205
		FlagObj *fo;
insilmaril@0
   206
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   207
		{
insilmaril@0
   208
			fo->deactivate();
insilmaril@0
   209
		}
insilmaril@0
   210
	} else
insilmaril@0
   211
		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
insilmaril@0
   212
}
insilmaril@0
   213
insilmaril@0
   214
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   215
{
insilmaril@0
   216
	// If we have no parent, we are the default FlagRowObj
insilmaril@0
   217
	// and have QToolbarButtons
insilmaril@0
   218
	if (!parentRow)
insilmaril@0
   219
	{
insilmaril@0
   220
		FlagObj *fo;
insilmaril@0
   221
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   222
			fo->setEnabled (b);
insilmaril@0
   223
	}
insilmaril@0
   224
}
insilmaril@0
   225
insilmaril@0
   226
void FlagRowObj::resetUsedCounter()
insilmaril@0
   227
{
insilmaril@0
   228
	FlagObj *fo;
insilmaril@0
   229
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   230
		fo->setUsed (false);
insilmaril@0
   231
}
insilmaril@0
   232
insilmaril@0
   233
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   234
{
insilmaril@0
   235
	// Build xml string
insilmaril@0
   236
	QString s;
insilmaril@0
   237
	FlagObj *fo;
insilmaril@0
   238
	if (parentRow)
insilmaril@0
   239
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   240
		{
insilmaril@0
   241
			// save flag to xml, if flag is set 
insilmaril@0
   242
			s+=valueElement("standardflag",fo->getName() );
insilmaril@0
   243
insilmaril@0
   244
			// and tell parentRow, that this flag is used
insilmaril@0
   245
			parentRow->activate(fo->getName() );
insilmaril@0
   246
		}	
insilmaril@0
   247
	else
insilmaril@0
   248
		// Save icons to dir, if verbose is set (xml export)
insilmaril@0
   249
		// and I am a parentRow 
insilmaril@0
   250
		// and this flag is really used somewhere
insilmaril@0
   251
		if (writeflags)
insilmaril@0
   252
			for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   253
				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
insilmaril@0
   254
	return s;		
insilmaril@0
   255
insilmaril@0
   256
}
insilmaril@0
   257
insilmaril@0
   258
void FlagRowObj::setName (const QString &n)
insilmaril@0
   259
{
insilmaril@0
   260
	name=n;
insilmaril@0
   261
}
insilmaril@0
   262
insilmaril@0
   263
void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
insilmaril@0
   264
{
insilmaril@0
   265
	if (!parentRow)
insilmaril@0
   266
	{
insilmaril@0
   267
		// create bar and buttons
insilmaril@0
   268
		QToolBar* tb = new QToolBar( w);
insilmaril@0
   269
		tb->setLabel (n);
insilmaril@0
   270
		QAction *a;
insilmaril@0
   271
		FlagObj *fo;
insilmaril@0
   272
		for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   273
		{
insilmaril@0
   274
			a=new QAction (
insilmaril@0
   275
				fo->getToolTip(),
insilmaril@0
   276
				fo->getPixmap(),
insilmaril@0
   277
				fo->getName(),
insilmaril@0
   278
				0,
insilmaril@0
   279
				w,
insilmaril@0
   280
				fo->getName()
insilmaril@0
   281
			);
insilmaril@0
   282
			a->setToggleAction(true);
insilmaril@0
   283
			// FIXME should not be enabled by default, later in updateToolbar
insilmaril@0
   284
			a->setEnabled(true);
insilmaril@0
   285
			a->addTo (tb);
insilmaril@0
   286
			fo->setButton (a);
insilmaril@0
   287
			connect(a, SIGNAL( activated() ), 
insilmaril@0
   288
					w, SLOT( standardFlagChanged() ) );
insilmaril@0
   289
		}
insilmaril@0
   290
	} else
insilmaril@0
   291
		qWarning ("FlagRowObj::makeToolbar mustn't be called for ordinary rows");
insilmaril@0
   292
}
insilmaril@0
   293
insilmaril@0
   294
void  FlagRowObj::updateToolBar()
insilmaril@0
   295
{
insilmaril@0
   296
	FlagObj *fo;
insilmaril@0
   297
	if (parentRow)
insilmaril@0
   298
	{
insilmaril@0
   299
		// We are just a branch, not the toolbar default
insilmaril@0
   300
		parentRow->deactivateAll();
insilmaril@0
   301
		// In parentRow activate all existing (==active) flags
insilmaril@0
   302
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   303
			parentRow->activate(fo->getName());
insilmaril@0
   304
		parentRow->updateToolBar();	
insilmaril@0
   305
	} else
insilmaril@0
   306
	{
insilmaril@0
   307
		// We are the toolbar default
insilmaril@0
   308
		for (fo=flag.first();fo; fo=flag.next() ) 
insilmaril@0
   309
			fo->updateButton();
insilmaril@0
   310
	}
insilmaril@0
   311
}
insilmaril@0
   312
insilmaril@0
   313
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   314
{
insilmaril@0
   315
	FlagObj *fo;
insilmaril@0
   316
	for (fo=flag.first();fo; fo=flag.next() )
insilmaril@0
   317
	{
insilmaril@0
   318
		if (fo->getName()==name) return fo;
insilmaril@0
   319
	}
insilmaril@0
   320
	return NULL;
insilmaril@0
   321
}
insilmaril@0
   322