completed WObjects for Qt
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 22 Feb 2009 17:56:31 +0000 (17:56 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 22 Feb 2009 17:56:31 +0000 (17:56 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@275 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/wbase/WObject.h
wob/order.wolf
woc/qtout.cpp
woc/qtout.h

index 6e57f15..d10cbc6 100644 (file)
@@ -20,6 +20,7 @@
 #include "exception.h"
 
 class QDomElement;
+class QDomDocument;
 
 class WObject:public QObject
 {
index 8b2c486..fbcc7b7 100644 (file)
                <Property name="value" type="int"/>
                <Property name="price" type="int" optional="1"/>
                <Property name="status" type="VoucherState" optional="1"/>
+               
+               <ToXml name="Full">voucherid value price status</ToXml>
+               <ToXml name="inOrder">voucherid value price status</ToXml>
        </Class>
        
        <Class name="Order" abstract="yes">
index 21390de..914f350 100644 (file)
@@ -60,7 +60,8 @@ static const QByteArray SRCSTART=
        "//AUTOMATICALLY GENERATED FILE\n"
        "#include \"%.h\"\n"
        "#include <QDomElement>\n"
-       "#include <QDomNodeList>\n";
+       "#include <QDomNodeList>\n"
+       "#include <QDomDocument>\n";
 //end section of all source files
 static const QByteArray SRCEND="\n//END OF FILE\n";
 
@@ -102,7 +103,7 @@ void WocQtClientOut::newClass(const WocClass&cls)
        classProperties(cls,hdr,src);
        
        //serializer
-       classSerializers(cls,hdr,src);
+       classSerializers(cls,hdr,src,cna);
        
        //deserializer (constructor)
        classDeserializer(cls,hdr,src,cna);
@@ -298,9 +299,83 @@ void WocQtClientOut::classDeserializer(const WocClass&cls,QFile&hdr,QFile&src,QS
        src.write(scd.toAscii());
 }
 
-void WocQtClientOut::classSerializers(const WocClass&cls,QFile&hdr,QFile&src)
+void WocQtClientOut::classSerializers(const WocClass&cls,QFile&hdr,QFile&src,QString cn)
 {
-       //TODO: implement
+       QStringList k=cls.serializers();
+       if(k.size()==0)return;
+       QString hcd="  public:\n";
+       QString scd;
+       for(int i=0;i<k.size();i++){
+               hcd+="\tQString toString"+k[i]+"();\n";
+               scd+="QString "+cn+"::toString"+k[i]+"()\n{\n";
+               scd+="\tQDomDocument doc;\n\tdoc.appendChild(toXml"+k[i]+"(doc));\n";
+               scd+="\treturn doc.toString();\n}\n";
+               hcd+="\tQDomElement toXml"+k[i]+"(QDomDocument&,QString name=\""+cls.name()+"\");\n";
+               scd+="QDomElement "+cn+"::toXml"+k[i]+"(QDomDocument&doc,QString name)\n{\n";
+               scd+="\tQDomElement r=doc.createElement(name);\n";
+               QStringList p=cls.serializerProperties(k[i]);
+               for(int j=0;j<p.size();j++){
+                       QStringList pv=p[j].split("/",QString::SkipEmptyParts);
+                       if(pv.size()<1){
+                               qDebug("Error: encountered empty property while creating serializer %s for class %s.",k[i].toAscii().data(),cls.name().toAscii().data());
+                               emit errorFound();
+                               return;
+                       }
+                       QString prop=pv[0];
+                       QString var;
+                       if(pv.size()>1)var=pv[1];
+                       //is it a list
+                       if(cls.propertyIsList(prop)){
+                               scd+="\tfor(int i=0;i<mp_"+prop+".size();i++){\n";
+                               if(cls.propertyIsObject(prop))
+                                       scd+="\t\tr.appendChild(mp_"+prop+"[i].toXml"+var+"(doc,\""+prop+"\"));\n";
+                               else
+                               if(cls.propertyIsEnum(prop)){
+                                       scd+="\t\tQDomElement el=doc.createElement(\""+prop+"\");\n";
+                                       scd+="\t\tel.appendChild(doc.createTextNode("+cls.propertyPlainType(prop)+"2str(mp_"+prop+"[i])));\n";
+                                       scd+="\t\tr.appendChild(el);\n";
+                               }else
+                               if(cls.propertyIsString(prop)){
+                                       scd+="\t\tQDomElement el=doc.createElement(\""+prop+"\");\n";
+                                       scd+="\t\tel.appendChild(doc.createTextNode(mp_"+prop+"[i]));\n";
+                                       scd+="\t\tr.appendChild(el);\n";
+                               }else
+                               if(cls.propertyIsInt(prop)){
+                                       scd+="\t\tQDomElement el=doc.createElement(\""+prop+"\");\n";
+                                       scd+="\t\tel.appendChild(doc.createTextNode(QString::number(mp_"+prop+"[i])));\n";
+                                       scd+="\t\tr.appendChild(el);\n";
+                               }else{
+                                       qDebug("Error: cannot generate serializer for class %s property %s.",cls.name().toAscii().data(),prop.toAscii().data());
+                                       emit errorFound();
+                                       return;
+                               }
+                               scd+="\t}\n";
+                       }else{
+                               //non lists
+                               scd+="\tif(!mp_"+prop+".isNull()){\n";
+                               if(cls.propertyIsAttribute(prop)){
+                                       scd+="\t\tr.setAttribute(\""+prop+"\",mp_"+prop+".value());\n";
+                               }else{
+                                       if(cls.propertyIsObject(prop)){
+                                               scd+="\t\tr.appendChild(mp_"+prop+".toXml"+var+"(doc,\""+prop+"\"));\n";
+                                       }else
+                                       if(cls.propertyIsString(prop)){
+                                               scd+="\t\tQDomElement el=doc.createElement(\""+prop+"\");\n";
+                                               scd+="\t\tel.appendChild(doc.createTextNode(mp_"+prop+"));\n";
+                                               scd+="\t\tr.appendChild(el);\n";
+                                       }else{
+                                               qDebug("Error: cannot generate serializer for class %s property %s.",cls.name().toAscii().data(),prop.toAscii().data());
+                                               emit errorFound();
+                                               return;
+                                       }
+                               }
+                               scd+="\t}\n";
+                       }
+               }
+               scd+="\treturn r;\n}\n";
+       }
+       hdr.write(hcd.toAscii());
+       src.write(scd.toAscii());
 }
 
 void WocQtClientOut::newTransaction(const WocTransaction&trn)
index a487c37..01f0a51 100644 (file)
@@ -40,7 +40,7 @@ class WocQtClientOut:public WocOutput
                /**helper: generate constructors/deserializer/copiers*/
                void classDeserializer(const WocClass&,QFile&,QFile&,QString);
                /**helper: generate serializers*/
-               void classSerializers(const WocClass&,QFile&,QFile&);
+               void classSerializers(const WocClass&,QFile&,QFile&,QString);
                /**helper: generate a proper Qt type for a property*/
                QString qttype(const WocClass&,QString,bool dolist=true);
 };