1.1 --- a/attribute.cpp Tue Dec 04 12:32:56 2007 +0000
1.2 +++ b/attribute.cpp Mon Feb 11 12:44:00 2008 +0000
1.3 @@ -1,34 +1,135 @@
1.4 +#include <iostream>
1.5 +
1.6 #include "attribute.h"
1.7
1.8 +using namespace std;
1.9 +
1.10 +extern bool debug;
1.11 +
1.12 Attribute::Attribute()
1.13 {
1.14 - key="";
1.15 - value="";
1.16 + table=NULL;
1.17 + definition=NULL;
1.18 }
1.19
1.20 -void Attribute::setKey (const QString &k)
1.21 +void Attribute::setKey (const QString &k, const AttributeType &t)
1.22 {
1.23 - key=k;
1.24 + if (!table)
1.25 + {
1.26 + qWarning (QString("Attribute::setKey (%1) No table defined!\n").arg(k).ascii());
1.27 + return;
1.28 + }
1.29 +
1.30 + if (!definition)
1.31 + {
1.32 + definition=table->getDef(k);
1.33 + if (!definition)
1.34 + {
1.35 + table->addKey (k,t);
1.36 + return;
1.37 + }
1.38 + }
1.39 + qWarning (QString("Attribute::setKey (%1) attribute already defined!\n").arg(k).ascii());
1.40 }
1.41
1.42 QString Attribute::getKey ()
1.43 {
1.44 - return key;
1.45 + if (!table)
1.46 + {
1.47 + qWarning ("Attribute::getKey () No table defined!");
1.48 + return QString();
1.49 + }
1.50 + if (!definition)
1.51 + {
1.52 + qWarning ("Attribute::getKey () No attribute defined!");
1.53 + return QString ();
1.54 + }
1.55 + return definition->getKey();
1.56 }
1.57
1.58 void Attribute::setValue(const QString &v)
1.59 {
1.60 - value=v;
1.61 + if (!table)
1.62 + {
1.63 + qWarning (QString ("Attribute::setValue (%1) No table defined!").arg(v));
1.64 + return;
1.65 + }
1.66 + if (!definition)
1.67 + {
1.68 + qWarning (QString ("Attribute::setValue (%1) No attribute defined!").arg(v));
1.69 + return;
1.70 + }
1.71 + definition->setValue (v);
1.72 }
1.73
1.74 -QString Attribute::getValue()
1.75 +QVariant Attribute::getValue()
1.76 {
1.77 - return value;
1.78 + if (!table)
1.79 + {
1.80 + qWarning ("Attribute::getValue No table defined!");
1.81 + return QString();
1.82 + }
1.83 + if (!definition)
1.84 + {
1.85 + qWarning ("Attribute::getValue No attribute defined!");
1.86 + return QString();
1.87 + }
1.88 + QVariant v= definition->getValue();
1.89 + return v;
1.90 +}
1.91 +
1.92 +void Attribute::setType (const AttributeType &t)
1.93 +{
1.94 + if (!table)
1.95 + {
1.96 + qWarning ("Attribute::setType No table defined!");
1.97 + return;
1.98 + }
1.99 + if (!definition)
1.100 + {
1.101 + qWarning ("Attribute::setType No attribute defined!");
1.102 + return;
1.103 + }
1.104 + definition->setType (t);
1.105 +}
1.106 +
1.107 +AttributeType Attribute::getType()
1.108 +{
1.109 + if (!table)
1.110 + {
1.111 + qWarning ("Attribute::getType No table defined!");
1.112 + return Undefined;
1.113 + }
1.114 + if (!definition)
1.115 + {
1.116 + qWarning ("Attribute::getType No attribute defined!");
1.117 + return Undefined;
1.118 + }
1.119 + return definition->getType();
1.120 +}
1.121 +
1.122 +QString Attribute::getTypeString()
1.123 +{
1.124 + if (!table)
1.125 + {
1.126 + qWarning ("Attribute::getTypeString No table defined!");
1.127 + return "Undefined";
1.128 + }
1.129 + if (!definition)
1.130 + {
1.131 + qWarning ("Attribute::getTypeString No attribute defined!");
1.132 + return "Undefined";
1.133 + }
1.134 + return definition->getTypeString();
1.135 }
1.136
1.137 void Attribute::setTable (AttributeTable *at)
1.138 {
1.139 - table=at;
1.140 + if (at)
1.141 + table=at;
1.142 + else
1.143 + qWarning ("Attribute::setTable table==NULL");
1.144 +
1.145 }
1.146
1.147 AttributeTable* Attribute::getTable()
1.148 @@ -38,76 +139,142 @@
1.149
1.150 QString Attribute::getDataXML()
1.151 {
1.152 - return valueElement ("attribute",key,value);
1.153 + QString a=beginElement ("attribute");
1.154 + a+=attribut ("key",getKey());
1.155 + a+=attribut ("value",getValue().toString() );
1.156 + a+=attribut ("type",getTypeString () );
1.157 + return a;
1.158 }
1.159
1.160
1.161 ///////////////////////////////////////////////////////////////
1.162 +AttributeDef::AttributeDef()
1.163 +{
1.164 +}
1.165 +
1.166 +AttributeDef::~AttributeDef()
1.167 +{
1.168 +}
1.169 +
1.170 +void AttributeDef::setType (const AttributeType &t)
1.171 +{
1.172 + type=t;
1.173 +}
1.174 +
1.175 +AttributeType AttributeDef::getType ()
1.176 +{
1.177 + return type;
1.178 +}
1.179 +
1.180 +QString AttributeDef::getTypeString ()
1.181 +{
1.182 + if (type==StringList)
1.183 + return "StringList";
1.184 + else if (type==FreeString)
1.185 + return "FreeString";
1.186 + else if (type==UniqueString)
1.187 + return "UniqueString";
1.188 + return "Undefined";
1.189 +}
1.190 +
1.191 +void AttributeDef::setKey (const QString &k)
1.192 +{
1.193 + key=k;
1.194 +}
1.195 +
1.196 +void AttributeDef::setValue (const QString &v)
1.197 +{
1.198 +}
1.199 +
1.200 +void AttributeDef::setValue (const QVariant &v)
1.201 +{
1.202 + if (type==Undefined)
1.203 + qWarning ("AttributeDef::setValue No type defined!");
1.204 + else if (type==StringList)
1.205 + value=v;
1.206 + else if (type==UniqueString)
1.207 + value=v;
1.208 + else
1.209 + qWarning ("AttributeDef::setValue Unknown type???");
1.210 +
1.211 +}
1.212 +
1.213 +QVariant AttributeDef::getValue ()
1.214 +{
1.215 + return QVariant ();
1.216 +}
1.217 +
1.218 +QString AttributeDef::getKey ()
1.219 +{
1.220 + return key;
1.221 +}
1.222 +
1.223 +///////////////////////////////////////////////////////////////
1.224 AttributeTable::AttributeTable()
1.225 {
1.226 - clear();
1.227 }
1.228
1.229 AttributeTable::~AttributeTable()
1.230 {
1.231 + clear();
1.232 }
1.233
1.234 void AttributeTable::clear ()
1.235 {
1.236 - keys.clear();
1.237 - values.clear();
1.238 + attdefs.clear();
1.239 }
1.240
1.241 -void AttributeTable::addKey (const QString &k)
1.242 +AttributeDef* AttributeTable::addKey (const QString &k, const AttributeType &t)
1.243 {
1.244 - if (!keys.contains (k) )
1.245 + for (int i=0; i<attdefs.count();++i)
1.246 {
1.247 - keys.append (k);
1.248 - values.append (QStringList() );
1.249 + if (attdefs.at(i)->getKey()==k )
1.250 + {
1.251 + qWarning (QString ("AttributeTable::addKey (%1) already in table\n").arg(k).ascii());
1.252 + return NULL;
1.253 + }
1.254 }
1.255 + AttributeDef *ad=new AttributeDef;
1.256 + ad->setKey (k);
1.257 + ad->setType (t);
1.258 + attdefs.append (ad);
1.259 + return ad;
1.260 }
1.261
1.262 void AttributeTable::removeKey (const QString &k)
1.263 {
1.264 - int i=keys.indexOf (k);
1.265 - if (i>=0)
1.266 + for (int i=0; i<attdefs.count();++i)
1.267 {
1.268 - keys.removeAt(i);
1.269 - values.removeAt(i);
1.270 + if (attdefs.at(i)->getKey()==k )
1.271 + {
1.272 +
1.273 + delete (attdefs.at(i));
1.274 + attdefs.removeAt (i);
1.275 + return ;
1.276 + }
1.277 }
1.278 + qWarning (QString ("AttributeTable::removeKey (%1) key not in table\n").arg(k).ascii());
1.279 +}
1.280 +
1.281 +AttributeDef* AttributeTable::getDef(const QString &k)
1.282 +{
1.283 + for (int i=0; i<attdefs.count();++i)
1.284 + if (attdefs.at(i)->getKey()==k ) return attdefs.at(i);
1.285 + qWarning (QString ("AttributeTable::getDef (%1) key not in table\n").arg(k).ascii());
1.286 + return NULL;
1.287 }
1.288
1.289 int AttributeTable::countKeys()
1.290 {
1.291 - return keys.count();
1.292 -}
1.293 -
1.294 -void AttributeTable::addValue (const QString &k, const QString &v)
1.295 -{
1.296 - int i=keys.indexOf (k);
1.297 - if (i<0)
1.298 - {
1.299 - keys.append (k);
1.300 - values.append (QStringList (v));
1.301 - } else
1.302 - {
1.303 - int j=values.at(i).indexOf(k);
1.304 - if (j<0) values[i].append (QString(v));
1.305 - }
1.306 + return attdefs.count();
1.307 }
1.308
1.309 QStringList AttributeTable::getKeys ()
1.310 {
1.311 - return keys;
1.312 -}
1.313 -
1.314 -QStringList AttributeTable::getValues(const QString &k)
1.315 -{
1.316 - int i=keys.indexOf (k);
1.317 - if (i>=0)
1.318 - return values.at(i);
1.319 - else
1.320 - return QStringList();
1.321 + QStringList kl;
1.322 + for (int i=0; i<attdefs.count();i++)
1.323 + kl.append (attdefs.at(i)->getKey());
1.324 + return kl;
1.325 }
1.326
1.327 QString AttributeTable::getDataXML()