[Kst] extragear/graphics/kst/kst/extensions/js
George Staikos
staikos at kde.org
Fri Dec 9 05:17:04 CET 2005
SVN commit 486875 by staikos:
add support for remove by index as well, though there is a bug I can't track
down. it works, but it throws a type error exception.
M +1 -0 TODO
M +7 -0 bind_collection.cpp
M +11 -1 bind_collection.h
M +41 -17 bind_curvecollection.cpp
M +24 -7 bind_viewobjectcollection.cpp
--- trunk/extragear/graphics/kst/kst/extensions/js/TODO #486874:486875
@@ -29,3 +29,4 @@
- Do we expose the "kst" object? If not, we need to add some additional things
like exit()
- Make .position and .size directly manipulated?
+- Collection.remove(int) throws a type error for some reason
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_collection.cpp #486874:486875
@@ -58,6 +58,7 @@
static CollectionProperties collectionProperties[] = {
+ { "readOnly", 0L, &KstBindCollection::readOnly },
{ "length", 0L, &KstBindCollection::length },
{ 0L, 0L, 0L }
};
@@ -180,6 +181,12 @@
}
+KJS::Value KstBindCollection::readOnly(KJS::ExecState *exec) const {
+ Q_UNUSED(exec)
+ return KJS::Boolean(_readOnly);
+}
+
+
KJS::Value KstBindCollection::length(KJS::ExecState *exec) const {
KJS::Object eobj = KJS::Error::create(exec, KJS::GeneralError);
exec->setException(eobj);
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_collection.h #486874:486875
@@ -26,7 +26,8 @@
/* @class Collection
@description This is the generic collection class used by Kst to store lists
of objects. It behaves mostly like an array in JavaScript. At
- this time it is read-only.
+ this time indices are read-only. In addition, if a collection
+ is read-only, mutator methods will throw exceptions.
Iteration looks something like this:
<code><pre>
@@ -62,6 +63,10 @@
// default throws an exception
virtual KJS::Value prepend(KJS::ExecState *exec, const KJS::List& args);
/* @method remove
+ @arg number n The index of the entry to remove.
+ @description Removes an entry from the collection.
+ */
+ /* @method remove
@arg Object obj The object to remove.
@description Removes an object from the collection.
*/
@@ -78,6 +83,11 @@
0..(length - 1) so it is easy to iterate.
*/
virtual KJS::Value length(KJS::ExecState *exec) const;
+ /* @property boolean readOnly
+ @readonly
+ @description True if this is a read-only collection.
+ */
+ virtual KJS::Value readOnly(KJS::ExecState *exec) const;
virtual QStringList collection(KJS::ExecState *exec) const;
virtual KJS::Value extract(KJS::ExecState *exec, const KJS::Identifier& item) const;
virtual KJS::Value extract(KJS::ExecState *exec, unsigned item) const;
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_curvecollection.cpp #486874:486875
@@ -186,35 +186,59 @@
}
if (_isPlot) {
- KstVCurvePtr c = extractVCurve(exec, args[0]);
- if (!c) {
- KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
- exec->setException(eobj);
- return KJS::Undefined();
- }
-
Kst2DPlotPtr p = *Kst2DPlot::globalPlotList().findTag(_plot);
if (!p) {
KJS::Object eobj = KJS::Error::create(exec, KJS::GeneralError);
exec->setException(eobj);
return KJS::Undefined();
}
- KstWriteLocker rl(p);
- if (p->Curves.contains(c.data())) {
- p->removeCurve(c.data());
- KstApp::inst()->paintAll(KstPainter::P_PAINT);
+
+ KstVCurvePtr c = extractVCurve(exec, args[0]);
+ if (c) {
+ KstWriteLocker rl(p);
+ if (p->Curves.contains(c.data())) {
+ p->removeCurve(c.data());
+ KstApp::inst()->paintAll(KstPainter::P_PAINT);
+ }
+ } else {
+ unsigned i = 0;
+ if (args[0].type() == KJS::NumberType && args[0].toUInt32(i)) {
+ if (i >= p->Curves.count()) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::RangeError);
+ exec->setException(eobj);
+ } else {
+ KstWriteLocker rl(p);
+ p->removeCurve(p->Curves[i].data());
+ KstApp::inst()->paintAll(KstPainter::P_PAINT);
+ }
+ } else {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+ exec->setException(eobj);
+ }
}
return KJS::Undefined();
} else if (_legend) {
KstVCurvePtr c = extractVCurve(exec, args[0]);
if (!c) {
- KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
- exec->setException(eobj);
- return KJS::Undefined();
+ unsigned i = 0;
+ if (args[0].type() == KJS::NumberType && args[0].toUInt32(i)) {
+ if (i >= _legend->curves().count()) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::RangeError);
+ exec->setException(eobj);
+ } else {
+ KstWriteLocker rl(_legend);
+ _legend->removeCurve(_legend->curves()[i]);
+ KstApp::inst()->paintAll(KstPainter::P_PAINT);
+ }
+ } else {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+ exec->setException(eobj);
+ }
+ } else {
+ KstWriteLocker rl(_legend);
+ _legend->removeCurve(c.data());
+ KstApp::inst()->paintAll(KstPainter::P_PAINT);
}
- KstWriteLocker rl(_legend);
- _legend->removeCurve(c.data());
- KstApp::inst()->paintAll(KstPainter::P_PAINT);
return KJS::Undefined();
}
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_viewobjectcollection.cpp #486874:486875
@@ -111,6 +111,8 @@
if (_parent) {
KstViewObjectPtr c = extractViewObject(exec, args[0]);
if (!c) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+ exec->setException(eobj);
return KJS::Undefined();
}
@@ -136,6 +138,8 @@
if (_parent) {
KstViewObjectPtr c = extractViewObject(exec, args[0]);
if (!c) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+ exec->setException(eobj);
return KJS::Undefined();
}
@@ -179,14 +183,27 @@
if (_parent) {
KstViewObjectPtr c = extractViewObject(exec, args[0]);
- if (!c) {
- return KJS::Undefined();
+ if (c) {
+ KstWriteLocker rl(_parent);
+ _parent->removeChild(c);
+ KstApp::inst()->paintAll(KstPainter::P_PAINT);
+ } else {
+ unsigned i = 0;
+ if (args[0].type() == KJS::NumberType && args[0].toUInt32(i)) {
+ if (i >= _parent->children().count()) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::RangeError);
+ exec->setException(eobj);
+ } else {
+ KstWriteLocker rl(_parent);
+ _parent->removeChild(_parent->children()[i]);
+ KstApp::inst()->paintAll(KstPainter::P_PAINT);
+ }
+ } else {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+ exec->setException(eobj);
+ return KJS::Undefined();
+ }
}
-
- KstWriteLocker rl(_parent);
- _parent->removeChild(c);
- _parent->setDirty();
- KstApp::inst()->paintAll(KstPainter::P_PAINT);
return KJS::Undefined();
}
More information about the Kst
mailing list