• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • script
  • classes
arrayscriptclass.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2011, 2012 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 
24 #include "arrayscriptclass.h"
25 #include "../../datatypes/array/arraydatainformation.h"
26 #include "../../parsers/parserutils.h"
27 #include "../../parsers/scriptvalueconverter.h"
28 #include "../scriptlogger.h"
29 
30 #include <KDebug>
31 
32 ArrayScriptClass::ArrayScriptClass(QScriptEngine* engine, ScriptHandlerInfo* handlerInfo)
33  : DefaultScriptClass(engine, handlerInfo)
34 {
35  s_length = engine->toStringHandle(ParserStrings::PROPERTY_LENGTH);
36  mIterableProperties.append(qMakePair(s_length, QScriptValue::PropertyFlags(QScriptValue::Undeletable)));
37  s_childType = engine->toStringHandle(QLatin1String("childType"));
38  //the preferred property (the same as childType)
39  s_type = engine->toStringHandle(ParserStrings::PROPERTY_TYPE);
40  mIterableProperties.append(qMakePair(s_type, QScriptValue::PropertyFlags(QScriptValue::Undeletable)));
41 
42  mArrayPrototype = engine->newObject();
43  mArrayPrototype.setProperty(QLatin1String("toString"), engine->newFunction(Array_proto_toString));
44 }
45 
46 ArrayScriptClass::~ArrayScriptClass()
47 {
48 }
49 
50 bool ArrayScriptClass::queryAdditionalProperty(const DataInformation* data, const QScriptString& name, QScriptClass::QueryFlags* flags, uint* id)
51 {
52  Q_UNUSED(data)
53  //no need to modify flags since both read and write are handled
54  if (name == s_length)
55  return true;
56  else if (name == s_type || name == s_childType)
57  return true;
58  else
59  {
60  bool isArrayIndex;
61  quint32 pos = name.toArrayIndex(&isArrayIndex);
62  if (isArrayIndex && pos <= data->childCount())
63  {
64  *id = pos + 1; //add 1 to distinguish from the default value of 0
65  *flags &= ~HandlesWriteAccess; //writing is not yet supported
66  return true;
67  }
68  }
69  return false; //not found
70 }
71 
72 bool ArrayScriptClass::additionalPropertyFlags(const DataInformation* data, const QScriptString& name, uint id, QScriptValue::PropertyFlags* flags)
73 {
74  Q_UNUSED(data)
75  Q_UNUSED(name)
76  if (name == s_childType)
77  return true; //undeleteable is on by default
78  if (id != 0)
79  {
80  *flags |= QScriptValue::ReadOnly;
81  return true;
82  }
83  return false;
84 }
85 
86 
87 QScriptValue ArrayScriptClass::additionalProperty(const DataInformation* data, const QScriptString& name, uint id)
88 {
89  const ArrayDataInformation* aData = data->asArray();
90  if (id != 0)
91  {
92  quint32 pos = id - 1;
93  if (pos >= data->childCount())
94  {
95  aData->logError() << "attempting to access out of bounds child: index was" << pos
96  << ", length is" << data->childCount();
97  return engine()->currentContext()->throwError(QScriptContext::RangeError,
98  QString(QLatin1String("Attempting to access array index %1, but length is %2")).arg(
99  QString::number(pos), QString::number(data->childCount())));
100  }
101  else
102  {
103  return aData->childToScriptValue(pos, engine(), mHandlerInfo);
104  }
105  }
106  else if (name == s_length)
107  return aData->length();
108  else if (name == s_type)
109  return aData->childType();
110  else if (name == s_childType)
111  {
112  aData->logWarn() << "Using property 'childType' is deprecated, use the new name 'type' instead";
113  return aData->childType();
114  }
115  return QScriptValue();
116 }
117 
118 bool ArrayScriptClass::setAdditionalProperty(DataInformation* data, const QScriptString& name, uint, const QScriptValue& value)
119 {
120  ArrayDataInformation* aData = data->asArray();
121  if (name == s_length)
122  {
123  if (value.isFunction())
124  {
125  aData->setLengthFunction(value);
126  }
127  else
128  {
129  ParsedNumber<uint> newLength = ParserUtils::uintFromScriptValue(value);
130  if (!newLength.isValid)
131  {
132  aData->logError() << "new length of array is invalid:" << newLength.string;
133  aData->setArrayLength(0);
134  }
135  else
136  {
137  aData->setArrayLength(newLength.value);
138  }
139  }
140  return true;
141  }
142  else if (name == s_type || name == s_childType)
143  {
144  if (name == s_childType)
145  aData->logWarn() << "Using property 'childType' is deprecated, use the new name 'type' instead";
146 
147  DataInformation* newChildType = ScriptValueConverter::convert(value,
148  aData->name(), aData->logger(), aData);
149 
150  if (!newChildType)
151  aData->logError() << "Failed to parse new child type:" << value.toString();
152  else
153  aData->setArrayType(newChildType);
154  return true;
155  }
156  return false;
157 }
158 
159 QScriptValue ArrayScriptClass::prototype() const
160 {
161  return mArrayPrototype;
162 }
163 
164 QScriptValue ArrayScriptClass::Array_proto_toString(QScriptContext* ctx, QScriptEngine* eng)
165 {
166  DataInformation* data = toDataInformation(ctx->thisObject());
167  if (!data)
168  {
169  kWarning() << "could not cast data";
170  return eng->undefinedValue();
171  }
172  return data->typeName();
173 }
174 
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
ArrayDataInformation::childType
QScriptValue childType() const
Definition: arraydatainformation.cpp:116
ArrayScriptClass::mArrayPrototype
QScriptValue mArrayPrototype
Definition: arrayscriptclass.h:48
DataInformation::name
QString name() const
Definition: datainformation.h:258
ArrayDataInformation::setArrayLength
bool setArrayLength(uint newLength)
Definition: arraydatainformation.cpp:68
ArrayScriptClass::Array_proto_toString
static QScriptValue Array_proto_toString(QScriptContext *ctx, QScriptEngine *eng)
Definition: arrayscriptclass.cpp:164
DataInformation::childCount
virtual unsigned int childCount() const =0
Definition: datainformation.h:278
ArrayScriptClass::ArrayScriptClass
ArrayScriptClass(QScriptEngine *engine, ScriptHandlerInfo *handlerInfo)
Definition: arrayscriptclass.cpp:32
ParsedNumber::value
T value
Definition: parserutils.h:88
DataInformation::typeName
QString typeName() const
Definition: datainformation.h:406
ParsedNumber::string
QString string
Definition: parserutils.h:87
ParsedNumber
Holds a number that was converted either from a QScriptValue or a QString.
Definition: parserutils.h:84
DataInformation::logger
ScriptLogger * logger() const
Definition: datainformation.cpp:134
DataInformationBase::asArray
ArrayDataInformation * asArray()
ArrayDataInformation::setLengthFunction
void setLengthFunction(QScriptValue newFunc)
Definition: arraydatainformation.h:178
ArrayDataInformation::length
uint length() const
Definition: arraydatainformation.h:98
ArrayDataInformation
Definition: arraydatainformation.h:36
ArrayDataInformation::setArrayType
void setArrayType(DataInformation *newChildtype)
Sets the new array type.
Definition: arraydatainformation.cpp:83
ParserUtils::uintFromScriptValue
ParsedNumber< uint > uintFromScriptValue(const QScriptValue &val)
Definition: parserutils.cpp:103
ParserStrings::PROPERTY_TYPE
const QString PROPERTY_TYPE
Definition: parserutils.h:130
ParserStrings::PROPERTY_LENGTH
const QString PROPERTY_LENGTH
Definition: parserutils.h:132
DefaultScriptClass::mHandlerInfo
ScriptHandlerInfo * mHandlerInfo
Definition: defaultscriptclass.h:84
DefaultScriptClass::toDataInformation
static DataInformation * toDataInformation(const QScriptValue &val)
Convert a QScriptValue to DataInformation than qscriptvalue_cast, since we know exactly what to expec...
Definition: defaultscriptclass.cpp:75
ArrayScriptClass::additionalProperty
virtual QScriptValue additionalProperty(const DataInformation *data, const QScriptString &name, uint id)
Definition: arrayscriptclass.cpp:87
ArrayScriptClass::setAdditionalProperty
virtual bool setAdditionalProperty(DataInformation *data, const QScriptString &name, uint id, const QScriptValue &value)
Definition: arrayscriptclass.cpp:118
DefaultScriptClass::mIterableProperties
PropertyInfoList mIterableProperties
Contains all properties of this class, classes inheriting should add their own properties to this lis...
Definition: defaultscriptclass.h:82
ArrayScriptClass::prototype
virtual QScriptValue prototype() const
Definition: arrayscriptclass.cpp:159
ArrayScriptClass::s_length
QScriptString s_length
Definition: arrayscriptclass.h:45
ArrayScriptClass::s_type
QScriptString s_type
Definition: arrayscriptclass.h:47
ScriptHandlerInfo
Definition: scripthandlerinfo.h:39
ScriptValueConverter::convert
DataInformation * convert(const QScriptValue &value, const QString &name, ScriptLogger *logger, DataInformation *parent)
If the value is one element.
Definition: scriptvalueconverter.cpp:36
ArrayScriptClass::queryAdditionalProperty
virtual bool queryAdditionalProperty(const DataInformation *data, const QScriptString &name, QScriptClass::QueryFlags *flags, uint *id)
Definition: arrayscriptclass.cpp:50
ArrayScriptClass::~ArrayScriptClass
virtual ~ArrayScriptClass()
Definition: arrayscriptclass.cpp:46
arrayscriptclass.h
DefaultScriptClass
Definition: defaultscriptclass.h:36
ArrayScriptClass::s_childType
QScriptString s_childType
Definition: arrayscriptclass.h:46
ParsedNumber::isValid
bool isValid
Definition: parserutils.h:89
ArrayScriptClass::additionalPropertyFlags
virtual bool additionalPropertyFlags(const DataInformation *data, const QScriptString &name, uint, QScriptValue::PropertyFlags *flags)
Definition: arrayscriptclass.cpp:72
ArrayDataInformation::childToScriptValue
QScriptValue childToScriptValue(uint index, QScriptEngine *engine, ScriptHandlerInfo *handlerInfo) const
Definition: arraydatainformation.cpp:198
DataInformation::logWarn
QDebug logWarn() const
just a shorthand for logger->warn(this)
Definition: datainformation.h:319
DataInformation::logError
QDebug logError() const
just a shorthand for logger->error(this)
Definition: datainformation.h:324
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal