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