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