WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZxItem.cpp
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2013, 2014 Bob Dinitto
4  *
5  * ZxItem.cpp
6  *
7  * Created on: 29-Sep, 2013
8  * Author: Bob Dinitto bob@ninzo.com
9  *
10  * Zuble is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 #include <QXmlNodeModelIndex>
27 #include <QtQml>
28 #include "ZxItem.h"
29 #include "ZxQuery.h"
30 #include "ZblThreadErr.h"
31 
32 namespace Zbl
33 {
34 
35 ZxItem::ZxItem(QObject *parent) :
36  QObject(parent), m_zQuery(NULL)
37 {
38 }
39 
41 {
42  qmlRegisterType<ZxItem>("org.zuble.qml", 1, 0, "ZxItem");
43 }
44 
45 void ZxItem::setValue(const QXmlItem& item, ZxQuery* zQuery)
46 {
47  m_item = item;
48 
49  m_zQuery = zQuery;
50 
51  emit stringValueChanged();
52 }
53 
54 void ZxItem::setAtomicValue(QVariant value)
55 {
56  m_item = QXmlItem(value);
57 
58  //m_zQuery = zQuery;
59 
60  emit stringValueChanged();
61 }
62 
64 {
65  return m_item.isAtomicValue();
66 }
67 
68 bool ZxItem::isNode() const
69 {
70  return m_item.isNode();
71 }
72 
73 bool ZxItem::isNull() const
74 {
75  return m_item.isNull();
76 }
77 
78 QVariant ZxItem::toAtomicValue() const
79 {
80  return m_item.toAtomicValue();
81 }
82 
83 const QString ZxItem::getLocalName()
84 {
85  QString result;
86 
87  if(!ensureIntegrity("ZxItem::getLocalName", "Item Not Initialized"))
88  return result;
89 
90  QXmlNodeModelIndex mi(m_item.toNodeModelIndex());
91 
92  if(!mi.isNull())
93  {
94  QXmlName name = mi.model()->name(mi);
95 
96  result = name.localName(m_zQuery->getNamePool());
97  }
98 
99  return result;
100 }
101 
102 const QString ZxItem::getStringValue()
103 {
104  QString result;
105 
106  if(!ensureIntegrity("ZxItem::elementById", "Item Not Initialized"))
107  return result;
108 
109  QXmlNodeModelIndex mi(m_item.toNodeModelIndex());
110 
111  if(!mi.isNull())
112  result = mi.model()->stringValue(mi);
113 
114  return result;
115 
116 }
117 
118 
120 {
121  if(!ensureIntegrity("ZxItem::getRootNode", "Item Not Initialized"))
122  return NULL;
123 
124  QXmlNodeModelIndex miThis(m_item.toNodeModelIndex());
125 
126  if(miThis.isNull())
127  return NULL;
128 
129  QXmlNodeModelIndex miRoot = miThis.model()->root(miThis);
130 
131  ZxItem* result = new ZxItem();
132 
133  if(!miRoot.isNull())
134  result->setValue(QXmlItem(miRoot), m_zQuery);
135  else
136  result->setValue(QXmlItem(), m_zQuery);
137 
138  return result;
139 
140 }
141 
142 QObject* ZxItem::elementById(const QString& id)
143 {
144  if(!ensureIntegrity("ZxItem::elementById", "Item Not Initialized"))
145  return NULL;
146 
147  QXmlNodeModelIndex miThis(m_item.toNodeModelIndex());
148 
149  if(miThis.isNull())
150  return NULL;
151 
152  ZxItem* result = new ZxItem();
153 
154  QXmlNamePool np(m_zQuery->getNamePool());
155 
156  QXmlName idName(np, id);
157 
158  /*
159 
160  QXmlNodeModelIndex miRoot = miThis.model()->root(miThis);
161 
162  if(miRoot.isNull())
163  {
164  qDebug("ZxItem::elementById - no root node returned");
165 
166  result->setValue(QXmlItem(), m_zQuery);
167 
168  return result;
169  }
170  */
171 
172  QXmlNodeModelIndex miId = miThis.model()->elementById(idName);
173 
174 
175  if(!miId.isNull())
176  result->setValue(QXmlItem(miId), m_zQuery);
177  else
178  result->setValue(QXmlItem(), m_zQuery);
179 
180  return result;
181 }
182 
183 bool ZxItem::ensureIntegrity(const QString& code, const QString& message)
184 {
185  if(m_zQuery)
186  return true;
187 
188  zThreadErr.raiseError("ZxItem", code, message);
189 
190  return false;
191 
192 }
193 
194 
195 
196 } // Zbl
This class allows Javascript programs to access QXmlItem objects.
Definition: ZxItem.h:43
Q_INVOKABLE void setAtomicValue(QVariant value)
set the item to an atomic value
Definition: ZxItem.cpp:54
ZxItem(QObject *parent=0)
Definition: ZxItem.cpp:35
static void registerType()
Registers ZxItem as a QML type.
Definition: ZxItem.cpp:40
Q_INVOKABLE bool isNull() const
determine if this item is null
Definition: ZxItem.cpp:73
Q_INVOKABLE bool isAtomicValue() const
determine if this item is an atomic value. Atomic values are represented by QVariant objects...
Definition: ZxItem.cpp:63
Q_INVOKABLE QVariant toAtomicValue() const
convert this item to it&#39;s atomic value equivalent.
Definition: ZxItem.cpp:78
Q_INVOKABLE QObject * elementById(const QString &id)
Obtain the element node from this node model that has the specified id attribute. ...
Definition: ZxItem.cpp:142
bool ensureIntegrity(const QString &code, const QString &message)
Definition: ZxItem.cpp:183
ZxQuery * m_zQuery
Pointer to the ZxQuery object associated with this item.
Definition: ZxItem.h:172
void stringValueChanged()
Definition: ZAndGate.cpp:6
QXmlNamePool getNamePool() const
Obtains the XML name pool for the encapsulated QXmlQuery object.
Definition: ZxQuery.cpp:343
QXmlItem m_item
Encapsulated QmlItem object.
Definition: ZxItem.h:163
#define zThreadErr
where does this show up?
Definition: ZblThreadErr.h:39
Q_INVOKABLE bool isNode() const
determine if this item is a node value.
Definition: ZxItem.cpp:68
const QString getLocalName()
returns the local name of this item
Definition: ZxItem.cpp:83
const QString getStringValue()
returns the string value of this item
Definition: ZxItem.cpp:102
This class provides access to the QXMLQuery class from Javascript.
Definition: ZxQuery.h:48
QObject * getRootNode()
Obtain the root node from the node model that contains this node.
Definition: ZxItem.cpp:119
void setValue(const QXmlItem &item, ZxQuery *zQuery)
Definition: ZxItem.cpp:45