attribute.cpp
author insilmaril
Mon, 07 Sep 2009 15:36:57 +0000
changeset 791 f1006de05c54
parent 671 367d875453ed
permissions -rw-r--r--
Fixed several Model errors using ModelTest
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@671
   215
	typeList
insilmaril@671
   216
		<< "Undefined"
insilmaril@671
   217
		<< "IntList"
insilmaril@671
   218
		<< "FreeInt"
insilmaril@671
   219
		<< "StringList"
insilmaril@671
   220
		<< "FreeString"
insilmaril@671
   221
		<< "UniqueString";
insilmaril@616
   222
}
insilmaril@616
   223
insilmaril@616
   224
AttributeTable::~AttributeTable()
insilmaril@616
   225
{
insilmaril@636
   226
	clear();
insilmaril@616
   227
}
insilmaril@616
   228
insilmaril@616
   229
void AttributeTable::clear ()
insilmaril@616
   230
{
insilmaril@636
   231
	attdefs.clear();
insilmaril@616
   232
}
insilmaril@616
   233
insilmaril@636
   234
AttributeDef* AttributeTable::addKey (const QString &k, const AttributeType &t)
insilmaril@616
   235
{
insilmaril@636
   236
	for (int i=0; i<attdefs.count();++i)
insilmaril@616
   237
	{
insilmaril@636
   238
		if (attdefs.at(i)->getKey()==k )
insilmaril@636
   239
		{
insilmaril@636
   240
			qWarning (QString ("AttributeTable::addKey (%1) already in table\n").arg(k).ascii());
insilmaril@636
   241
			return NULL;
insilmaril@636
   242
		}
insilmaril@616
   243
	}
insilmaril@636
   244
	AttributeDef *ad=new AttributeDef;
insilmaril@636
   245
	ad->setKey (k);
insilmaril@636
   246
	ad->setType (t);
insilmaril@636
   247
	attdefs.append (ad);
insilmaril@636
   248
	return ad;
insilmaril@616
   249
}
insilmaril@616
   250
insilmaril@616
   251
void AttributeTable::removeKey (const QString &k)
insilmaril@616
   252
{
insilmaril@636
   253
	for (int i=0; i<attdefs.count();++i)
insilmaril@616
   254
	{
insilmaril@636
   255
		if (attdefs.at(i)->getKey()==k )
insilmaril@636
   256
		{
insilmaril@636
   257
			
insilmaril@636
   258
			delete (attdefs.at(i));
insilmaril@636
   259
			attdefs.removeAt (i);
insilmaril@636
   260
			return ;
insilmaril@636
   261
		}
insilmaril@616
   262
	}
insilmaril@636
   263
	qWarning (QString ("AttributeTable::removeKey (%1) key not in table\n").arg(k).ascii());
insilmaril@636
   264
}
insilmaril@636
   265
insilmaril@671
   266
AttributeDef* AttributeTable::getDef(const QString &key)
insilmaril@636
   267
{
insilmaril@636
   268
	for (int i=0; i<attdefs.count();++i)
insilmaril@671
   269
		if (attdefs.at(i)->getKey()==key ) return attdefs.at(i);
insilmaril@671
   270
	qWarning (QString ("AttributeTable::getDef (%1) key not in table\n").arg(key).ascii());
insilmaril@636
   271
	return NULL;	
insilmaril@616
   272
}
insilmaril@616
   273
insilmaril@626
   274
int AttributeTable::countKeys()
insilmaril@626
   275
{
insilmaril@636
   276
	return attdefs.count();
insilmaril@616
   277
}
insilmaril@616
   278
insilmaril@616
   279
QStringList AttributeTable::getKeys ()
insilmaril@616
   280
{
insilmaril@636
   281
	QStringList kl;
insilmaril@636
   282
	for (int i=0; i<attdefs.count();i++)
insilmaril@636
   283
		kl.append (attdefs.at(i)->getKey());
insilmaril@636
   284
	return kl;
insilmaril@616
   285
}
insilmaril@616
   286
insilmaril@671
   287
QStringList AttributeTable::getTypes ()
insilmaril@671
   288
{
insilmaril@671
   289
	return typeList;
insilmaril@671
   290
}
insilmaril@671
   291
insilmaril@616
   292
QString AttributeTable::getDataXML()
insilmaril@616
   293
{
insilmaril@616
   294
	return valueElement ("attributeList","key","value");
insilmaril@616
   295
}