• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

step/stepcore

tool.cc

Go to the documentation of this file.
00001 /* This file is part of StepCore library.
00002    Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
00003 
00004    StepCore library is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2 of the License, or
00007    (at your option) any later version.
00008 
00009    StepCore library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with StepCore; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017 */
00018 
00019 #include "tool.h"
00020 #include "particle.h"
00021 #include "rigidbody.h"
00022 
00023 namespace StepCore {
00024 
00025 STEPCORE_META_OBJECT(NoteImage, "NoteImage: an image embedded in Note", 0,
00026     STEPCORE_SUPER_CLASS(Item),
00027     STEPCORE_PROPERTY_RW(QByteArray, image, STEPCORE_UNITS_NULL, "Image data", image, setImage)
00028     )
00029 
00030 STEPCORE_META_OBJECT(NoteFormula, "NoteFormula: a formula embedded in Note", 0,
00031     STEPCORE_SUPER_CLASS(NoteImage),
00032     STEPCORE_PROPERTY_RW(QString, code, STEPCORE_UNITS_NULL, "Formula code", code, setCode)
00033     )
00034 
00035 STEPCORE_META_OBJECT(Note, "Note: displays a textual note on the scene", 0,
00036     STEPCORE_SUPER_CLASS(ItemGroup) STEPCORE_SUPER_CLASS(Tool),
00037     STEPCORE_PROPERTY_RW(StepCore::Vector2d, position, "m", "Note position on the scene", position, setPosition)
00038     STEPCORE_PROPERTY_RW(StepCore::Vector2d, size, "m", "Note size on the scene", size, setSize)
00039     STEPCORE_PROPERTY_RW(QString, text, STEPCORE_UNITS_NULL, "Text", text, setText)
00040     )
00041 
00042 STEPCORE_META_OBJECT(Graph, "Graph: plots a graph of any properties", 0,
00043     STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Tool),
00044     STEPCORE_PROPERTY_RW(StepCore::Vector2d, position, "m", "Graph position on the scene", position, setPosition)
00045     STEPCORE_PROPERTY_RW(StepCore::Vector2d, size, "m", "Graph size on the scene", size, setSize)
00046     STEPCORE_PROPERTY_RW(Object*, objectX, STEPCORE_UNITS_NULL, "X axis: object", objectX, setObjectX)
00047     STEPCORE_PROPERTY_RW(QString, propertyX, STEPCORE_UNITS_NULL, "X axis: object property", propertyX, setPropertyX)
00048     STEPCORE_PROPERTY_RW(int, indexX, STEPCORE_UNITS_NULL, "X axis: vector index", indexX, setIndexX)
00049     STEPCORE_PROPERTY_RW(Object*, objectY, STEPCORE_UNITS_NULL, "Y axis: object", objectY, setObjectY)
00050     STEPCORE_PROPERTY_RW(QString, propertyY, STEPCORE_UNITS_NULL, "Y axis: property", propertyY, setPropertyY)
00051     STEPCORE_PROPERTY_RW(int, indexY, STEPCORE_UNITS_NULL, "Y axis: vector index", indexY, setIndexY)
00052     STEPCORE_PROPERTY_RW(bool, autoLimitsX, STEPCORE_UNITS_NULL, "Auto-limits along X axis", autoLimitsX, setAutoLimitsX)
00053     STEPCORE_PROPERTY_RW(bool, autoLimitsY, STEPCORE_UNITS_NULL, "Auto-limits along Y axis", autoLimitsY, setAutoLimitsY)
00054     STEPCORE_PROPERTY_RW(StepCore::Vector2d, limitsX, STEPCORE_UNITS_NULL, "Limits along X axis", limitsX, setLimitsX)
00055     STEPCORE_PROPERTY_RW(StepCore::Vector2d, limitsY, STEPCORE_UNITS_NULL, "Limits along Y axis", limitsY, setLimitsY)
00056     STEPCORE_PROPERTY_RW(bool, showPoints, STEPCORE_UNITS_NULL, "Show points on the graph", showPoints, setShowPoints)
00057     STEPCORE_PROPERTY_RW(bool, showLines, STEPCORE_UNITS_NULL, "Show lines on the graph", showLines, setShowLines)
00058     STEPCORE_PROPERTY_R (StepCore::Vector2d, currentValue, STEPCORE_UNITS_NULL, "Current value", currentValue)
00059     STEPCORE_PROPERTY_RW_D(StepCore::Vector2dList, points, STEPCORE_UNITS_NULL, "points", points, setPoints)
00060     STEPCORE_PROPERTY_R (QString, unitsX, STEPCORE_UNITS_NULL, "Units along X axis", unitsX)
00061     STEPCORE_PROPERTY_R (QString, unitsY, STEPCORE_UNITS_NULL, "Units along Y axis", unitsY)
00062     )
00063 
00064 STEPCORE_META_OBJECT(Meter, "Meter: displays any property on the scene", 0,
00065     STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Tool),
00066     STEPCORE_PROPERTY_RW(StepCore::Vector2d, position, "m", "Meter position on the scene", position, setPosition)
00067     STEPCORE_PROPERTY_RW(StepCore::Vector2d, size, "m", "Meter size on the scene", size, setSize)
00068     STEPCORE_PROPERTY_RW(Object*, object, STEPCORE_UNITS_NULL, "Observed object", object, setObject)
00069     STEPCORE_PROPERTY_RW(QString, property, STEPCORE_UNITS_NULL, "Observed property", property, setProperty)
00070     STEPCORE_PROPERTY_RW(int, index, STEPCORE_UNITS_NULL, "Vector index", index, setIndex)
00071     STEPCORE_PROPERTY_RW(int, digits, STEPCORE_UNITS_NULL, "Display digits", digits, setDigits)
00072     STEPCORE_PROPERTY_R (double, value, STEPCORE_UNITS_NULL, "Value", value)
00073     STEPCORE_PROPERTY_R (QString, units, STEPCORE_UNITS_NULL, "Units of measured property", units)
00074     )
00075 
00076 STEPCORE_META_OBJECT(Controller, "Controller: allows to easily control any property", 0,
00077     STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Tool),
00078     STEPCORE_PROPERTY_RW(StepCore::Vector2d, position, "m", "Controller position on the scene", position, setPosition)
00079     STEPCORE_PROPERTY_RW(StepCore::Vector2d, size, "m", "Controller size on the scene", size, setSize)
00080     STEPCORE_PROPERTY_RW(Object*, object, STEPCORE_UNITS_NULL, "Controlled object", object, setObject)
00081     STEPCORE_PROPERTY_RW(QString, property, STEPCORE_UNITS_NULL, "Controlled property property", property, setProperty)
00082     STEPCORE_PROPERTY_RW(int, index, STEPCORE_UNITS_NULL, "Vector index", index, setIndex)
00083     STEPCORE_PROPERTY_RW(StepCore::Vector2d, limits, STEPCORE_UNITS_NULL, "Limits", limits, setLimits)
00084     STEPCORE_PROPERTY_RW(QString, increaseShortcut, STEPCORE_UNITS_NULL,
00085                             "Shortcut to increase the value", increaseShortcut, setIncreaseShortcut)
00086     STEPCORE_PROPERTY_RW(QString, decreaseShortcut, STEPCORE_UNITS_NULL,
00087                             "Shortcut to decrease the value", decreaseShortcut, setDecreaseShortcut)
00088     STEPCORE_PROPERTY_RW(double, increment, STEPCORE_UNITS_NULL, "Increment value", increment, setIncrement)
00089     STEPCORE_PROPERTY_RWF(double, value, STEPCORE_UNITS_NULL, "Value",
00090                             MetaProperty::DYNAMIC | MetaProperty::SIDEEFFECTS, value, setValue)
00091     STEPCORE_PROPERTY_R (QString, units, STEPCORE_UNITS_NULL, "Units of controlled property", units)
00092     )
00093 
00094 STEPCORE_META_OBJECT(Tracer, "Tracer: traces trajectory of a point on a body", 0,
00095     STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Tool),
00096     STEPCORE_PROPERTY_RW(Object*, body, STEPCORE_UNITS_NULL, "Traced body", body, setBody)
00097     STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition, "m",
00098                     "Local position", localPosition, setLocalPosition)
00099     STEPCORE_PROPERTY_R_D(StepCore::Vector2d, position, "m", "Position", position)
00100     STEPCORE_PROPERTY_RW_D(StepCore::Vector2dList, points, "m", "points", points, setPoints)
00101     )
00102 
00103 namespace {
00104 
00105 static double variantToDouble(const QVariant& v, int index, bool *ok)
00106 {
00107     if(ok) *ok = true;
00108 
00109     if(v.userType() == qMetaTypeId<Vector2d>()) {
00110         if(index >= 0 && index < 2) return v.value<Vector2d>()[index];
00111     } else {
00112         if(index == -1) return v.toDouble(ok);
00113     }
00114 
00115     if(ok) *ok = false;
00116     return 0.0;
00117 }
00118 
00119 static QVariant doubleToVariant(const QVariant& v, int index, double newV, bool *ok)
00120 {
00121     if(ok) *ok = true;
00122 
00123     if(v.userType() == qMetaTypeId<Vector2d>()) {
00124         if(index >= 0 && index < 2) {
00125             Vector2d vector = v.value<Vector2d>();
00126             vector[index] = newV;
00127             return QVariant::fromValue(vector);
00128         }
00129     } else {
00130         if(index == -1) {
00131             bool ok1; v.toDouble(&ok1);
00132             if(ok1) return newV;
00133         }
00134     }
00135 
00136     if(ok) *ok = false;
00137     return 0.0;
00138 }
00139 
00140 }
00141 
00142 Note::Note(Vector2d position, Vector2d size, QString text)
00143     : _position(position), _size(size), _text(text)
00144 {
00145 }
00146 
00147 Graph::Graph(Vector2d position, Vector2d size)
00148     : _position(position), _size(size),
00149       _objectX(0), _propertyX(), _indexX(-1),
00150       _objectY(0), _propertyY(), _indexY(-1),
00151       _autoLimitsX(true), _autoLimitsY(true),
00152       _limitsX(0,1), _limitsY(0,1),
00153       _showLines(true), _showPoints(false)
00154 {
00155 }
00156 
00157 bool Graph::isValidX() const
00158 {
00159     bool ok;
00160     const MetaProperty* prX = propertyXPtr(); if(!prX) return false;
00161     variantToDouble(prX->readVariant(_objectX), _indexX, &ok);
00162     return ok;
00163 }
00164 
00165 bool Graph::isValidY() const
00166 {
00167     bool ok;
00168     const MetaProperty* prY = propertyYPtr(); if(!prY) return false;
00169     variantToDouble(prY->readVariant(_objectY), _indexY, &ok);
00170     return ok;
00171 }
00172 
00173 Vector2d Graph::currentValue(bool* ok) const
00174 {
00175     const MetaProperty* prX = propertyXPtr();
00176     const MetaProperty* prY = propertyYPtr();
00177 
00178     if(prX && prY) {
00179         bool ok1, ok2;
00180         Vector2d point(variantToDouble(prX->readVariant(_objectX), _indexX, &ok1),
00181                        variantToDouble(prY->readVariant(_objectY), _indexY, &ok2));
00182         if(ok1 && ok2) {
00183             if(ok) *ok = true;
00184             return point;
00185         }
00186     }
00187 
00188     if(ok) *ok = false;
00189     return Vector2d(0);
00190 }
00191 
00192 Vector2d Graph::recordPoint(bool* ok)
00193 {
00194     bool ok1;
00195     Vector2d point(currentValue(&ok1));
00196     if(ok1) {
00197         if(ok) *ok = true;
00198         _points.push_back(point);
00199         return point;
00200     }
00201 
00202     if(ok) *ok = false;
00203     return Vector2d(0);
00204 }
00205 
00206 /*
00207 void Graph::worldItemRemoved(Item* item)
00208 {
00209     if(item == 0) return;
00210     if(item == _objectX) setObjectX(0);
00211     if(item == _objectY) setObjectY(0);
00212 }
00213 
00214 void Graph::setWorld(World* world)
00215 {
00216     if(world == NULL) {
00217         setObjectX(0);
00218         setObjectY(0);
00219     } else if(this->world() != NULL) { 
00220         if(_objectX != NULL) _objectX = world->object(_objectX->name());
00221         if(_objectY != NULL) _objectY = world->object(_objectY->name());
00222     }
00223     Item::setWorld(world);
00224 }
00225 */
00226 
00227 QString Graph::unitsX() const
00228 {
00229     const MetaProperty* pr = propertyXPtr();
00230     if(!pr && !isValidX()) return QString();
00231 
00232     return pr->units();
00233 }
00234 
00235 QString Graph::unitsY() const
00236 {
00237     const MetaProperty* pr = propertyYPtr();
00238     if(!pr && !isValidY()) return QString();
00239 
00240     return pr->units();
00241 }
00242 
00243 Meter::Meter(Vector2d position, Vector2d size)
00244     : _position(position), _size(size),
00245       _object(0), _property(), _index(-1),
00246       _digits(7)
00247 {
00248 }
00249 
00250 bool Meter::isValid() const
00251 {
00252     bool ok;
00253     const MetaProperty* pr = propertyPtr(); if(!pr) return false;
00254     variantToDouble(pr->readVariant(_object), _index, &ok);
00255     return ok;
00256 }
00257 
00258 double Meter::value(bool* ok) const
00259 {
00260     const MetaProperty* pr = propertyPtr();
00261 
00262     if(pr) {
00263         bool ok1;
00264         double v = variantToDouble(pr->readVariant(_object), _index, &ok1);
00265         if(ok1) {
00266             if(ok) *ok = true;
00267             return v;
00268         }
00269     }
00270 
00271     if(ok) *ok = false;
00272     return 0;
00273 }
00274 
00275 QString Meter::units() const
00276 {
00277     const MetaProperty* pr = propertyPtr();
00278     if(!pr && !isValid()) return QString();
00279 
00280     return pr->units();
00281 }
00282 
00283 /*
00284 void Meter::worldItemRemoved(Item* item)
00285 {
00286     if(item == 0) return;
00287     if(item == _object) setObject(0);
00288 }
00289 
00290 void Meter::setWorld(World* world)
00291 {
00292     if(world == NULL) {
00293         setObject(0);
00294     } else if(this->world() != NULL) { 
00295         if(_object != NULL) _object = world->object(_object->name());
00296     }
00297     Item::setWorld(world);
00298 }
00299 */
00300 
00301 Controller::Controller(Vector2d position, Vector2d size)
00302     : _position(position), _size(size),
00303       _object(0), _property(), _index(-1),
00304       _limits(-1,1), _increment(0.1)
00305 {
00306 }
00307 
00308 bool Controller::isValid() const
00309 {
00310     bool ok;
00311     const MetaProperty* pr = propertyPtr(); if(!pr) return false;
00312     variantToDouble(pr->readVariant(_object), _index, &ok);
00313     return ok && pr->isWritable();
00314 }
00315 
00316 double Controller::value(bool* ok) const
00317 {
00318     const MetaProperty* pr = propertyPtr();
00319 
00320     if(pr && pr->isWritable()) {
00321         bool ok1;
00322         double v = variantToDouble(pr->readVariant(_object), _index, &ok1);
00323         if(ok1) {
00324             if(ok) *ok = true;
00325             return v;
00326         }
00327     }
00328 
00329     if(ok) *ok = false;
00330     return 0;
00331 }
00332 
00333 void Controller::setValue(double value, bool* ok = 0)
00334 {
00335     const MetaProperty* pr = propertyPtr();
00336 
00337     if(pr && pr->isWritable()) {
00338         bool ok1;
00339         QVariant v = doubleToVariant(pr->readVariant(_object), _index, value, &ok1);
00340         if(ok1) {
00341             if(ok) *ok = true;
00342             pr->writeVariant(_object, v);
00343         }
00344     }
00345 
00346     if(ok) *ok = false;
00347 }
00348 
00349 QString Controller::units() const
00350 {
00351     const MetaProperty* pr = propertyPtr();
00352     if(!pr && !isValid()) return QString();
00353 
00354     return pr->units();
00355 }
00356 
00357 /*
00358 void Controller::worldItemRemoved(Item* item)
00359 {
00360     if(item == 0) return;
00361     if(item == _object) setObject(0);
00362 }
00363 
00364 void Controller::setWorld(World* world)
00365 {
00366     if(world == NULL) {
00367         setObject(0);
00368     } else if(this->world() != NULL) { 
00369         if(_object != NULL) _object = world->object(_object->name());
00370     }
00371     Item::setWorld(world);
00372 }
00373 */
00374 
00375 Tracer::Tracer(Object* body, const Vector2d& localPosition)
00376     : _localPosition(localPosition)
00377 {
00378     setColor(0xff0000ff);
00379     setBody(body);
00380 }
00381 
00382 void Tracer::setBody(Object* body)
00383 {
00384     if(body) {
00385         if(body->metaObject()->inherits<Particle>()) {
00386             _body = body;
00387             _p = static_cast<Particle*>(body);
00388             _r = NULL;
00389             return;
00390         } else if(body->metaObject()->inherits<RigidBody>()) {
00391             _body = body;
00392             _p = NULL;
00393             _r = static_cast<RigidBody*>(body);
00394             return;
00395         }
00396     }
00397     _body = NULL;
00398     _p = NULL;
00399     _r = NULL;
00400 }
00401 
00402 Vector2d Tracer::position() const
00403 {
00404     if(_p) return _p->position() + _localPosition;
00405     else if(_r) return _r->pointLocalToWorld(_localPosition);
00406     return _localPosition;
00407 }
00408 
00409 /*
00410 void Tracer::worldItemRemoved(Item* item)
00411 {
00412     if(item == _body) setBody(NULL);
00413 }
00414 
00415 void Tracer::setWorld(World* world)
00416 {
00417     if(world == NULL) {
00418         setBody(NULL);
00419     } else if(this->world() != NULL) { 
00420         if(_body != NULL) setBody(world->item(body()->name()));
00421     }
00422     Item::setWorld(world);
00423 }
00424 */
00425 
00426 } // namespace StepCore
00427 

step/stepcore

Skip menu "step/stepcore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal