attribute.cpp
author insilmaril
Wed, 16 Jan 2008 15:45:19 +0000
changeset 659 b582bba9e312
parent 636 f83abc1f75b4
child 671 367d875453ed
permissions -rw-r--r--
Fixed missing MapCenter
insilmaril@636
     1
#include <iostream>
insilmaril@636
     2
insilmaril@616
     3
#include "attribute.h"
insilmaril@616
     4
insilmaril@636
     5
using namespace std;
insilmaril@636
     6
insilmaril@636
     7
extern bool debug;
insilmaril@636
     8
insilmaril@616
     9
Attribute::Attribute()
insilmaril@616
    10
{
insilmaril@636
    11
	table=NULL;
insilmaril@636
    12
	definition=NULL;
insilmaril@616
    13
}
insilmaril@616
    14
insilmaril@636
    15
void Attribute::setKey (const QString &k, const AttributeType &t)
insilmaril@616
    16
{
insilmaril@636
    17
	if (!table)
insilmaril@636
    18
	{
insilmaril@636
    19
		qWarning (QString("Attribute::setKey (%1)  No table defined!\n").arg(k).ascii());
insilmaril@636
    20
		return;	
insilmaril@636
    21
	}
insilmaril@636
    22
	
insilmaril@636
    23
	if (!definition)
insilmaril@636
    24
	{
insilmaril@636
    25
		definition=table->getDef(k);
insilmaril@636
    26
		if (!definition)
insilmaril@636
    27
		{
insilmaril@636
    28
			table->addKey (k,t);
insilmaril@636
    29
			return;	
insilmaril@636
    30
		}
insilmaril@636
    31
	}	
insilmaril@636
    32
	qWarning (QString("Attribute::setKey (%1)  attribute already defined!\n").arg(k).ascii());
insilmaril@616
    33
}
insilmaril@616
    34
insilmaril@616
    35
QString Attribute::getKey ()
insilmaril@616
    36
{
insilmaril@636
    37
	if (!table)
insilmaril@636
    38
	{
insilmaril@636
    39
		qWarning ("Attribute::getKey ()  No table defined!");
insilmaril@636
    40
		return QString();	
insilmaril@636
    41
	}
insilmaril@636
    42
	if (!definition)
insilmaril@636
    43
	{
insilmaril@636
    44
		qWarning ("Attribute::getKey ()  No attribute defined!");
insilmaril@636
    45
		return QString ();	
insilmaril@636
    46
	}	
insilmaril@636
    47
	return definition->getKey();
insilmaril@616
    48
}
insilmaril@616
    49
insilmaril@616
    50
void Attribute::setValue(const QString &v)
insilmaril@616
    51
{
insilmaril@636
    52
	if (!table)
insilmaril@636
    53
	{
insilmaril@636
    54
		qWarning (QString ("Attribute::setValue (%1)  No table defined!").arg(v));
insilmaril@636
    55
		return;	
insilmaril@636
    56
	}
insilmaril@636
    57
	if (!definition)
insilmaril@636
    58
	{
insilmaril@636
    59
		qWarning (QString ("Attribute::setValue (%1)  No attribute defined!").arg(v));
insilmaril@636
    60
		return;	
insilmaril@636
    61
	}	
insilmaril@636
    62
	definition->setValue (v);
insilmaril@616
    63
}
insilmaril@616
    64
insilmaril@636
    65
QVariant Attribute::getValue()
insilmaril@616
    66
{
insilmaril@636
    67
	if (!table)
insilmaril@636
    68
	{
insilmaril@636
    69
		qWarning ("Attribute::getValue  No table defined!");
insilmaril@636
    70
		return QString();	
insilmaril@636
    71
	}
insilmaril@636
    72
	if (!definition)
insilmaril@636
    73
	{
insilmaril@636
    74
		qWarning ("Attribute::getValue  No attribute defined!");
insilmaril@636
    75
		return QString();	
insilmaril@636
    76
	}	
insilmaril@636
    77
	QVariant v= definition->getValue();
insilmaril@636
    78
	return v;
insilmaril@636
    79
}
insilmaril@636
    80
insilmaril@636
    81
void Attribute::setType (const AttributeType &t)
insilmaril@636
    82
{
insilmaril@636
    83
	if (!table)
insilmaril@636
    84
	{
insilmaril@636
    85
		qWarning ("Attribute::setType  No table defined!");
insilmaril@636
    86
		return;
insilmaril@636
    87
	}
insilmaril@636
    88
	if (!definition)
insilmaril@636
    89
	{
insilmaril@636
    90
		qWarning ("Attribute::setType  No attribute defined!");
insilmaril@636
    91
		return; 
insilmaril@636
    92
	}	
insilmaril@636
    93
	definition->setType (t);
insilmaril@636
    94
}
insilmaril@636
    95
insilmaril@636
    96
AttributeType Attribute::getType()
insilmaril@636
    97
{
insilmaril@636
    98
	if (!table)
insilmaril@636
    99
	{
insilmaril@636
   100
		qWarning ("Attribute::getType  No table defined!");
insilmaril@636
   101
		return Undefined;	
insilmaril@636
   102
	}
insilmaril@636
   103
	if (!definition)
insilmaril@636
   104
	{
insilmaril@636
   105
		qWarning ("Attribute::getType  No attribute defined!");
insilmaril@636
   106
		return Undefined;	
insilmaril@636
   107
	}	
insilmaril@636
   108
	return definition->getType();
insilmaril@636
   109
}
insilmaril@636
   110
insilmaril@636
   111
QString Attribute::getTypeString()
insilmaril@636
   112
{
insilmaril@636
   113
	if (!table)
insilmaril@636
   114
	{
insilmaril@636
   115
		qWarning ("Attribute::getTypeString  No table defined!");
insilmaril@636
   116
		return "Undefined";	
insilmaril@636
   117
	}
insilmaril@636
   118
	if (!definition)
insilmaril@636
   119
	{
insilmaril@636
   120
		qWarning ("Attribute::getTypeString  No attribute defined!");
insilmaril@636
   121
		return "Undefined";	
insilmaril@636
   122
	}	
insilmaril@636
   123
	return definition->getTypeString();
insilmaril@616
   124
}
insilmaril@616
   125
insilmaril@626
   126
void Attribute::setTable (AttributeTable *at)
insilmaril@626
   127
{
insilmaril@636
   128
	if (at)
insilmaril@636
   129
		table=at;
insilmaril@636
   130
	 else
insilmaril@636
   131
		qWarning ("Attribute::setTable  table==NULL");
insilmaril@636
   132
	
insilmaril@626
   133
}
insilmaril@626
   134
insilmaril@626
   135
AttributeTable* Attribute::getTable()
insilmaril@626
   136
{
insilmaril@626
   137
	return table;
insilmaril@626
   138
}
insilmaril@626
   139
insilmaril@616
   140
QString Attribute::getDataXML()
insilmaril@616
   141
{
insilmaril@636
   142
	QString a=beginElement ("attribute");
insilmaril@636
   143
	a+=attribut ("key",getKey());
insilmaril@636
   144
	a+=attribut ("value",getValue().toString() );
insilmaril@636
   145
	a+=attribut ("type",getTypeString () );
insilmaril@636
   146
	return a;
insilmaril@616
   147
}
insilmaril@616
   148
insilmaril@616
   149
insilmaril@616
   150
///////////////////////////////////////////////////////////////
insilmaril@636
   151
AttributeDef::AttributeDef()
insilmaril@636
   152
{
insilmaril@636
   153
}
insilmaril@636
   154
insilmaril@636
   155
AttributeDef::~AttributeDef()
insilmaril@636
   156
{
insilmaril@636
   157
}
insilmaril@636
   158
insilmaril@636
   159
void AttributeDef::setType (const AttributeType &t)
insilmaril@636
   160
{
insilmaril@636
   161
	type=t;
insilmaril@636
   162
}
insilmaril@636
   163
insilmaril@636
   164
AttributeType AttributeDef::getType ()
insilmaril@636
   165
{
insilmaril@636
   166
	return type;
insilmaril@636
   167
}
insilmaril@636
   168
insilmaril@636
   169
QString AttributeDef::getTypeString ()
insilmaril@636
   170
{
insilmaril@636
   171
	if (type==StringList)
insilmaril@636
   172
		return "StringList";
insilmaril@636
   173
	else if (type==FreeString)
insilmaril@636
   174
		return "FreeString";
insilmaril@636
   175
	else if (type==UniqueString)
insilmaril@636
   176
		return "UniqueString";
insilmaril@636
   177
	return "Undefined";
insilmaril@636
   178
}
insilmaril@636
   179
insilmaril@636
   180
void AttributeDef::setKey (const QString &k)
insilmaril@636
   181
{
insilmaril@636
   182
	key=k;
insilmaril@636
   183
}
insilmaril@636
   184
insilmaril@636
   185
void AttributeDef::setValue (const QString &v)
insilmaril@636
   186
{
insilmaril@636
   187
}
insilmaril@636
   188
insilmaril@636
   189
void AttributeDef::setValue (const QVariant &v)
insilmaril@636
   190
{
insilmaril@636
   191
	if (type==Undefined)
insilmaril@636
   192
		qWarning ("AttributeDef::setValue  No type defined!");
insilmaril@636
   193
	else if (type==StringList)
insilmaril@636
   194
		value=v;
insilmaril@636
   195
	else if (type==UniqueString)
insilmaril@636
   196
		value=v;
insilmaril@636
   197
	else
insilmaril@636
   198
		qWarning ("AttributeDef::setValue Unknown type???");
insilmaril@636
   199
		
insilmaril@636
   200
}
insilmaril@636
   201
insilmaril@636
   202
QVariant AttributeDef::getValue ()
insilmaril@636
   203
{
insilmaril@636
   204
	return QVariant ();
insilmaril@636
   205
}
insilmaril@636
   206
insilmaril@636
   207
QString AttributeDef::getKey ()
insilmaril@636
   208
{
insilmaril@636
   209
	return key;
insilmaril@636
   210
}
insilmaril@636
   211
insilmaril@636
   212
///////////////////////////////////////////////////////////////
insilmaril@616
   213
AttributeTable::AttributeTable()
insilmaril@616
   214
{
insilmaril@616
   215
}
insilmaril@616
   216
insilmaril@616
   217
AttributeTable::~AttributeTable()
insilmaril@616
   218
{
insilmaril@636
   219
	clear();
insilmaril@616
   220
}
insilmaril@616
   221
insilmaril@616
   222
void AttributeTable::clear ()
insilmaril@616
   223
{
insilmaril@636
   224
	attdefs.clear();
insilmaril@616
   225
}
insilmaril@616
   226
insilmaril@636
   227
AttributeDef* AttributeTable::addKey (const QString &k, const AttributeType &t)
insilmaril@616
   228
{
insilmaril@636
   229
	for (int i=0; i<attdefs.count();++i)
insilmaril@616
   230
	{
insilmaril@636
   231
		if (attdefs.at(i)->getKey()==k )
insilmaril@636
   232
		{
insilmaril@636
   233
			qWarning (QString ("AttributeTable::addKey (%1) already in table\n").arg(k).ascii());
insilmaril@636
   234
			return NULL;
insilmaril@636
   235
		}
insilmaril@616
   236
	}
insilmaril@636
   237
	AttributeDef *ad=new AttributeDef;
insilmaril@636
   238
	ad->setKey (k);
insilmaril@636
   239
	ad->setType (t);
insilmaril@636
   240
	attdefs.append (ad);
insilmaril@636
   241
	return ad;
insilmaril@616
   242
}
insilmaril@616
   243
insilmaril@616
   244
void AttributeTable::removeKey (const QString &k)
insilmaril@616
   245
{
insilmaril@636
   246
	for (int i=0; i<attdefs.count();++i)
insilmaril@616
   247
	{
insilmaril@636
   248
		if (attdefs.at(i)->getKey()==k )
insilmaril@636
   249
		{
insilmaril@636
   250
			
insilmaril@636
   251
			delete (attdefs.at(i));
insilmaril@636
   252
			attdefs.removeAt (i);
insilmaril@636
   253
			return ;
insilmaril@636
   254
		}
insilmaril@616
   255
	}
insilmaril@636
   256
	qWarning (QString ("AttributeTable::removeKey (%1) key not in table\n").arg(k).ascii());
insilmaril@636
   257
}
insilmaril@636
   258
insilmaril@636
   259
AttributeDef* AttributeTable::getDef(const QString &k)
insilmaril@636
   260
{
insilmaril@636
   261
	for (int i=0; i<attdefs.count();++i)
insilmaril@636
   262
		if (attdefs.at(i)->getKey()==k ) return attdefs.at(i);
insilmaril@636
   263
	qWarning (QString ("AttributeTable::getDef (%1) key not in table\n").arg(k).ascii());
insilmaril@636
   264
	return NULL;	
insilmaril@616
   265
}
insilmaril@616
   266
insilmaril@626
   267
int AttributeTable::countKeys()
insilmaril@626
   268
{
insilmaril@636
   269
	return attdefs.count();
insilmaril@616
   270
}
insilmaril@616
   271
insilmaril@616
   272
QStringList AttributeTable::getKeys ()
insilmaril@616
   273
{
insilmaril@636
   274
	QStringList kl;
insilmaril@636
   275
	for (int i=0; i<attdefs.count();i++)
insilmaril@636
   276
		kl.append (attdefs.at(i)->getKey());
insilmaril@636
   277
	return kl;
insilmaril@616
   278
}
insilmaril@616
   279
insilmaril@616
   280
QString AttributeTable::getDataXML()
insilmaril@616
   281
{
insilmaril@616
   282
	return valueElement ("attributeList","key","value");
insilmaril@616
   283
}