attribute.cpp
author insilmaril
Fri Feb 01 15:28:35 2008 +0000 (2008-02-01)
changeset 664 01c028006e6d
parent 626 96c8e6860e0c
child 671 367d875453ed
permissions -rw-r--r--
Added brasilian translation by Amadeu Júnior
     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 }
   216 
   217 AttributeTable::~AttributeTable()
   218 {
   219 	clear();
   220 }
   221 
   222 void AttributeTable::clear ()
   223 {
   224 	attdefs.clear();
   225 }
   226 
   227 AttributeDef* AttributeTable::addKey (const QString &k, const AttributeType &t)
   228 {
   229 	for (int i=0; i<attdefs.count();++i)
   230 	{
   231 		if (attdefs.at(i)->getKey()==k )
   232 		{
   233 			qWarning (QString ("AttributeTable::addKey (%1) already in table\n").arg(k).ascii());
   234 			return NULL;
   235 		}
   236 	}
   237 	AttributeDef *ad=new AttributeDef;
   238 	ad->setKey (k);
   239 	ad->setType (t);
   240 	attdefs.append (ad);
   241 	return ad;
   242 }
   243 
   244 void AttributeTable::removeKey (const QString &k)
   245 {
   246 	for (int i=0; i<attdefs.count();++i)
   247 	{
   248 		if (attdefs.at(i)->getKey()==k )
   249 		{
   250 			
   251 			delete (attdefs.at(i));
   252 			attdefs.removeAt (i);
   253 			return ;
   254 		}
   255 	}
   256 	qWarning (QString ("AttributeTable::removeKey (%1) key not in table\n").arg(k).ascii());
   257 }
   258 
   259 AttributeDef* AttributeTable::getDef(const QString &k)
   260 {
   261 	for (int i=0; i<attdefs.count();++i)
   262 		if (attdefs.at(i)->getKey()==k ) return attdefs.at(i);
   263 	qWarning (QString ("AttributeTable::getDef (%1) key not in table\n").arg(k).ascii());
   264 	return NULL;	
   265 }
   266 
   267 int AttributeTable::countKeys()
   268 {
   269 	return attdefs.count();
   270 }
   271 
   272 QStringList AttributeTable::getKeys ()
   273 {
   274 	QStringList kl;
   275 	for (int i=0; i<attdefs.count();i++)
   276 		kl.append (attdefs.at(i)->getKey());
   277 	return kl;
   278 }
   279 
   280 QString AttributeTable::getDataXML()
   281 {
   282 	return valueElement ("attributeList","key","value");
   283 }