Navit SVN

/work/compile/navit/src/navit/gui/qml/proxy.h

00001 #ifndef NAVIT_GUI_QML_PROXY_H
00002 #define NAVIT_GUI_QML_PROXY_H
00003 
00004 class NGQStandardItemModel  : public QStandardItemModel {
00005 public:
00006         NGQStandardItemModel(QObject* parent) : QStandardItemModel(parent) {
00007                 //Populate role list
00008                 roleNames.insert(NGQStandardItemModel::ItemId, "itemId");
00009                 roleNames.insert(NGQStandardItemModel::ItemName, "itemName");
00010                 roleNames.insert(NGQStandardItemModel::ItemIcon, "itemIcon");
00011                 roleNames.insert(NGQStandardItemModel::ItemPath, "itemPath");
00012                 roleNames.insert(NGQStandardItemModel::ItemValue, "itemValue");
00013                 this->setRoleNames(roleNames);
00014         }
00015 
00016         enum listRoles {ItemId=Qt::UserRole+1,ItemName=Qt::UserRole+2,ItemIcon=Qt::UserRole+3,ItemPath=Qt::UserRole+4,ItemValue=Qt::UserRole+5};
00017 private:
00018         QHash<int, QByteArray> roleNames;
00019 };
00020 
00021 class NGQProxy : public QObject {
00022         Q_OBJECT;
00023 
00024         Q_PROPERTY(int itemId READ itemId  NOTIFY itemIdSignal);
00025 public:
00026     NGQProxy(struct gui_priv* this_,QObject *parent) : QObject(parent) {
00027         this->object=this_;
00028     }
00029 
00030 signals:
00031         void itemIdSignal(int itemId);
00032 
00033 public slots:
00034         //Attribute read/write
00035         QString getAttr(const QString &attr_name) {
00036                 QString ret;
00037                 struct attr attr;
00038                 
00039                 getAttrFunc(attr_from_name(attr_name.toStdString().c_str()), &attr, NULL);
00040                 if (ATTR_IS_INT(attr.type)) {
00041                         ret.setNum(attr.u.num);
00042                 }
00043                 if (ATTR_IS_DOUBLE(attr.type)) {
00044                         ret.setNum(*attr.u.numd);
00045                 }
00046                 if (ATTR_IS_STRING(attr.type)) {
00047                         ret=attr.u.str;
00048                 }
00049                 if (attr.type==attr_layout) {
00050                         ret=attr.u.layout->name;
00051                 }
00052                 return ret;
00053         }
00054         void setAttr(const QString &attr_name, const QString &attr_string) {
00055                         struct attr attr_value;
00056                         double *helper;
00057 
00058                         dbg(1,"Setting %s to %s\n",attr_name.toStdString().c_str(),attr_string.toStdString().c_str());
00059                         getAttrFunc(attr_from_name(attr_name.toStdString().c_str()), &attr_value, NULL);
00060 
00061                         if (ATTR_IS_INT(attr_value.type)) {
00062                                 //Special handling for "true"/"false"
00063                                 if (attr_string=="true") {
00064                                         attr_value.u.num=1;
00065                                 } else if (attr_string=="false") {
00066                                         attr_value.u.num=0;
00067                                 } else {
00068                                         attr_value.u.num=attr_string.toInt();
00069                                 }
00070                         }
00071                         if (ATTR_IS_DOUBLE(attr_value.type)) {
00072                                 helper = g_new0(double,1);
00073                                 *helper=attr_string.toDouble();
00074                                 attr_value.u.numd=helper;
00075                         }
00076                         if (ATTR_IS_STRING(attr_value.type)) {
00077                                 attr_value.u.str=(char*)attr_string.toStdString().c_str();
00078                         }
00079 
00080                         setAttrFunc(&attr_value);
00081 
00082                         return;
00083         }
00084         
00085         int itemId() {
00086                 return _itemId;
00087         }
00088 protected:
00089     struct gui_priv* object;
00090 
00091         int _itemId;
00092 
00093         virtual int setAttrFunc(struct attr *attr)=0;
00094         virtual int getAttrFunc(enum attr_type type, struct attr *attr, struct attr_iter *iter)=0;
00095         virtual struct attr_iter* getIterFunc() { return NULL; };
00096         virtual void dropIterFunc(struct attr_iter*) { return; };
00097 
00098         QDomElement _fieldValueHelper(QDomDocument doc, QString field,QString value) {
00099                         QDomElement fieldTag=doc.createElement(field);
00100                         QDomText valueText=doc.createTextNode(value);
00101                         fieldTag.appendChild(valueText);
00102                         return fieldTag;
00103         }
00104 
00105 };
00106 
00107 #include "proxy.moc"
00108 
00109 #endif /* NAVIT_GUI_QML_PROXY_H */