flagrowobj.cpp
author insilmaril
Thu, 23 Nov 2006 16:18:26 +0000
changeset 407 5db8dfd30ea2
parent 406 1c8ff1928b97
child 408 c2a05fa925a1
permissions -rw-r--r--
Removed even more QT3 stuff. Drag & Drop not 100% functional at the moment
insilmaril@0
     1
#include "flagrowobj.h"
insilmaril@0
     2
insilmaril@366
     3
#include <QToolBar>
insilmaril@366
     4
insilmaril@0
     5
/////////////////////////////////////////////////////////////////
insilmaril@0
     6
// FlagRowObj
insilmaril@0
     7
/////////////////////////////////////////////////////////////////
insilmaril@0
     8
FlagRowObj::FlagRowObj()
insilmaril@0
     9
{
insilmaril@166
    10
//    cout << "Const FlagRowObj ()\n";
insilmaril@0
    11
    init ();
insilmaril@0
    12
}
insilmaril@0
    13
insilmaril@366
    14
FlagRowObj::FlagRowObj(Q3Canvas* c):MapObj(c) 
insilmaril@0
    15
{
insilmaril@0
    16
//    cout << "Const FlagRowObj\n";
insilmaril@0
    17
    init ();
insilmaril@0
    18
}
insilmaril@0
    19
insilmaril@0
    20
FlagRowObj::~FlagRowObj()
insilmaril@0
    21
{
insilmaril@406
    22
 //   cout << "Destr FlagRowObj\n";
insilmaril@406
    23
	while (!flag.isEmpty())
insilmaril@406
    24
		delete flag.takeFirst();
insilmaril@0
    25
}
insilmaril@0
    26
insilmaril@0
    27
void FlagRowObj::init ()
insilmaril@0
    28
{
insilmaril@0
    29
	parentRow=NULL;
insilmaril@256
    30
	showFlags=true;
insilmaril@0
    31
}
insilmaril@0
    32
insilmaril@0
    33
void FlagRowObj::copy (FlagRowObj* other)
insilmaril@0
    34
{
insilmaril@0
    35
    MapObj::copy(other);
insilmaril@0
    36
	parentRow=other->parentRow;
insilmaril@0
    37
	flag.clear();
insilmaril@406
    38
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
    39
		addFlag (flag.at(i));
insilmaril@0
    40
}
insilmaril@0
    41
insilmaril@0
    42
void FlagRowObj::clone (FlagRowObj* pr)
insilmaril@0
    43
{
insilmaril@0
    44
	// Difference to copy:
insilmaril@0
    45
	// We don't copy the flags here, they
insilmaril@0
    46
	// are created on the fly by toggle and activate
insilmaril@0
    47
	// This saves lots of canvas objects.
insilmaril@0
    48
	MapObj::copy(pr);
insilmaril@0
    49
	flag.clear();
insilmaril@0
    50
	parentRow=pr;
insilmaril@0
    51
}
insilmaril@0
    52
insilmaril@0
    53
void FlagRowObj::move(double x, double y)
insilmaril@0
    54
{
insilmaril@0
    55
    MapObj::move(x,y);
insilmaril@0
    56
	int dx=0;
insilmaril@406
    57
	for (int i=0; i<flag.size(); ++i)
insilmaril@0
    58
	{
insilmaril@406
    59
		flag.at(i)->move(x+dx,y);
insilmaril@406
    60
		dx+=QSize(flag.at(i)->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@406
    72
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
    73
		flag.at(i)->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@406
    97
	for (int i=0; i<flag.size(); ++i)
insilmaril@0
    98
	{
insilmaril@406
    99
		size=flag.at(i)->getSize();
insilmaril@0
   100
		// add widths
insilmaril@0
   101
		boxsize.setWidth(boxsize.width() + size.width() );
insilmaril@0
   102
		// maximize height
insilmaril@0
   103
		if (size.height() > boxsize.height() ) 
insilmaril@0
   104
			boxsize.setHeight(size.height() );
insilmaril@0
   105
	}
insilmaril@173
   106
	bbox.setSize (boxsize);
insilmaril@173
   107
	clickBox.setSize (boxsize);
insilmaril@0
   108
}
insilmaril@0
   109
insilmaril@0
   110
QString FlagRowObj::getFlagName (const QPoint &p)
insilmaril@0
   111
{
insilmaril@173
   112
	if (!inBox (p)) return "";
insilmaril@406
   113
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
   114
		if (flag.at(i)->inBox (p)) return flag.at(i)->getName();
insilmaril@0
   115
	return "";	
insilmaril@0
   116
insilmaril@0
   117
	
insilmaril@0
   118
}
insilmaril@0
   119
insilmaril@0
   120
bool FlagRowObj::isActive (const QString &foname)
insilmaril@0
   121
{
insilmaril@0
   122
	FlagObj *fo=findFlag (foname);
insilmaril@236
   123
	if (parentRow && fo)
insilmaril@236
   124
		return fo->isActive();
insilmaril@236
   125
	else
insilmaril@0
   126
		if (fo) return true;
insilmaril@0
   127
	return false;
insilmaril@0
   128
}
insilmaril@0
   129
insilmaril@103
   130
void FlagRowObj::toggle (const QString &foname, bool exclusive)
insilmaril@0
   131
{
insilmaril@0
   132
	FlagObj *fo=findFlag (foname);
insilmaril@0
   133
	if (fo)
insilmaril@0
   134
	{
insilmaril@0
   135
		// FlagObj is here, it will be active, too.
insilmaril@0
   136
		// Deactivate it by removing it from this row.
insilmaril@0
   137
		flag.remove (fo);
insilmaril@0
   138
	} else
insilmaril@0
   139
	{
insilmaril@0
   140
		// FlagObj is not present in this row.
insilmaril@0
   141
		// Copy it from parentRow
insilmaril@0
   142
		fo=parentRow->findFlag (foname);
insilmaril@0
   143
		if (fo)
insilmaril@0
   144
		{
insilmaril@0
   145
			fo=addFlag (fo);
insilmaril@0
   146
			fo->activate();
insilmaril@103
   147
			if (exclusive) 
insilmaril@103
   148
			{
insilmaril@103
   149
				deactivateGroup (fo);
insilmaril@103
   150
				updateToolbar();
insilmaril@103
   151
			}
insilmaril@0
   152
		} else
insilmaril@0
   153
			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   154
	}	
insilmaril@0
   155
	calcBBoxSize();
insilmaril@0
   156
	positionBBox();	
insilmaril@0
   157
}
insilmaril@0
   158
insilmaril@0
   159
void FlagRowObj::activate (const QString &foname)
insilmaril@0
   160
{
insilmaril@311
   161
	// Note: "activate" is also called during loading of a map
insilmaril@311
   162
	// Here we do not check for exclusive flags!
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@256
   175
				if (showFlags) 
insilmaril@256
   176
					fo->setVisibility (visible);
insilmaril@256
   177
				else
insilmaril@256
   178
					fo->setVisibility (false);
insilmaril@0
   179
				calcBBoxSize();
insilmaril@0
   180
			} else
insilmaril@0
   181
				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
insilmaril@0
   182
		}	
insilmaril@0
   183
	} else
insilmaril@0
   184
	{
insilmaril@0
   185
		// I am the parentRow, mark flag as used
insilmaril@0
   186
		if (fo)
insilmaril@0
   187
		{
insilmaril@0
   188
			fo->setUsed(true);
insilmaril@0
   189
			fo->activate();
insilmaril@0
   190
		}	
insilmaril@0
   191
		else
insilmaril@0
   192
			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
insilmaril@0
   193
	}
insilmaril@0
   194
}
insilmaril@0
   195
insilmaril@103
   196
insilmaril@0
   197
void FlagRowObj::deactivate (const QString &foname)
insilmaril@0
   198
{
insilmaril@0
   199
	FlagObj *fo=findFlag (foname);
insilmaril@0
   200
	if (fo) flag.remove(fo);
insilmaril@0
   201
	calcBBoxSize();
insilmaril@0
   202
	positionBBox();
insilmaril@0
   203
}
insilmaril@0
   204
insilmaril@0
   205
void FlagRowObj::deactivateAll ()
insilmaril@0
   206
{
insilmaril@0
   207
	if (!parentRow)
insilmaril@0
   208
	{
insilmaril@406
   209
		for (int i=0; i<flag.size(); ++i)
insilmaril@406
   210
			flag.at(i)->deactivate();
insilmaril@0
   211
	} else
insilmaril@0
   212
		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
insilmaril@0
   213
}
insilmaril@0
   214
insilmaril@103
   215
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
insilmaril@103
   216
{
insilmaril@103
   217
	// deactivate all flags in keepof, but keep keepfo [sic!]
insilmaril@103
   218
	if (keepfo)
insilmaril@103
   219
	{
insilmaril@311
   220
		QString g=keepfo->getGroup();
insilmaril@311
   221
		if (g!="undefined")
insilmaril@311
   222
		{
insilmaril@406
   223
			for (int i=0; i<flag.size(); ++i)
insilmaril@406
   224
				if (g==flag.at(i)->getGroup() && keepfo!=flag.at(i)) 
insilmaril@406
   225
					flag.remove(flag.at(i));
insilmaril@311
   226
		}		
insilmaril@103
   227
	}	
insilmaril@103
   228
}
insilmaril@103
   229
insilmaril@366
   230
void FlagRowObj::setToolBar(QToolBar *tb)
insilmaril@366
   231
{
insilmaril@366
   232
	toolbar=tb;
insilmaril@366
   233
}
insilmaril@366
   234
insilmaril@0
   235
void FlagRowObj::setEnabled (bool b)
insilmaril@0
   236
{
insilmaril@366
   237
	if (toolbar)
insilmaril@0
   238
	{
insilmaril@366
   239
		toolbar->setEnabled (b);
insilmaril@0
   240
	}
insilmaril@0
   241
}
insilmaril@0
   242
insilmaril@256
   243
void FlagRowObj::setShowFlags (bool b)
insilmaril@256
   244
{
insilmaril@256
   245
	showFlags=b;
insilmaril@256
   246
}
insilmaril@256
   247
insilmaril@0
   248
void FlagRowObj::resetUsedCounter()
insilmaril@0
   249
{
insilmaril@406
   250
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
   251
		flag.at(i)->setUsed (false);
insilmaril@0
   252
}
insilmaril@0
   253
insilmaril@0
   254
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
insilmaril@0
   255
{
insilmaril@0
   256
	// Build xml string
insilmaril@0
   257
	QString s;
insilmaril@0
   258
	if (parentRow)
insilmaril@406
   259
		for (int i=0; i<flag.size(); ++i)
insilmaril@0
   260
		{
insilmaril@0
   261
			// save flag to xml, if flag is set 
insilmaril@406
   262
			s+=valueElement("standardflag",flag.at(i)->getName() );
insilmaril@0
   263
insilmaril@0
   264
			// and tell parentRow, that this flag is used
insilmaril@406
   265
			parentRow->activate(flag.at(i)->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@406
   272
			for (int i=0; i<flag.size(); ++i)
insilmaril@406
   273
				if (flag.at(i)->isUsed()) flag.at(i)->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@103
   283
void  FlagRowObj::updateToolbar()
insilmaril@0
   284
{
insilmaril@0
   285
	if (parentRow)
insilmaril@0
   286
	{
insilmaril@0
   287
		// We are just a branch, not the toolbar default
insilmaril@366
   288
		// but state has to be copied from ourselves to parentrow!
insilmaril@0
   289
		parentRow->deactivateAll();
insilmaril@0
   290
		// In parentRow activate all existing (==active) flags
insilmaril@406
   291
		for (int i=0; i<flag.size(); ++i)
insilmaril@406
   292
			parentRow->activate(flag.at(i)->getName());
insilmaril@103
   293
		parentRow->updateToolbar();	
insilmaril@0
   294
	} else
insilmaril@0
   295
	{
insilmaril@0
   296
		// We are the toolbar default
insilmaril@366
   297
		if (toolbar)
insilmaril@366
   298
		{
insilmaril@366
   299
			// Update state of actions in toolbar
insilmaril@406
   300
			for (int i=0; i<flag.size(); ++i)
insilmaril@406
   301
				flag.at(i)->updateAction();
insilmaril@366
   302
		}	
insilmaril@0
   303
	}
insilmaril@0
   304
}
insilmaril@0
   305
insilmaril@0
   306
FlagObj* FlagRowObj::findFlag (const QString &name)
insilmaril@0
   307
{
insilmaril@406
   308
	for (int i=0; i<flag.size(); ++i)
insilmaril@406
   309
		if (flag.at(i)->getName()==name) return flag.at(i);
insilmaril@0
   310
	return NULL;
insilmaril@0
   311
}
insilmaril@0
   312