File indexing completed on 2024-04-14 03:49:28

0001 /*
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 /** \file tool.h
0008  *  \brief Note class
0009  */
0010 
0011 // TODO: split this file
0012 
0013 #ifndef STEPCORE_TOOL_H
0014 #define STEPCORE_TOOL_H
0015 
0016 #include "world.h"
0017 #include "types.h"
0018 
0019 #include <QByteArray>
0020 #include <QHash>
0021 #include <QStringList>
0022 
0023 namespace StepCore
0024 {
0025 
0026 /** \ingroup tools
0027  *  \brief Image embedded in Note
0028  */
0029 class NoteImage: public Item
0030 {
0031     STEPCORE_OBJECT(NoteImage)
0032 
0033 public:
0034     /** Constructs NoteImage */
0035     explicit NoteImage(const QString& name = QString(),
0036                        const QByteArray& image = QByteArray())
0037                             : Item(name), _image(image) {}
0038 
0039     /** Get image data */
0040     const QByteArray& image() const { return _image; }
0041 
0042     /** Set image data */
0043     void setImage(const QByteArray& image) { _image = image; }
0044 
0045 protected:
0046     QByteArray _image;
0047 };
0048 
0049 /** \ingroup tools
0050  *  \brief LaTeX formula embedded in Note
0051  */
0052 class NoteFormula: public NoteImage
0053 {
0054     STEPCORE_OBJECT(NoteFormula)
0055 
0056 public:
0057     /** Constructs NoteFormula */
0058     explicit NoteFormula(const QString& name = QString(),
0059                          const QByteArray& image = QByteArray(),
0060                          const QString& code = QString())
0061                             : NoteImage(name, image), _code(code) {}
0062 
0063     /** Get formula code */
0064     const QString& code() const { return _code; }
0065     /** Set formula code */
0066     void setCode(const QString& code) { _code = code; }
0067 
0068 protected:
0069     QString    _code;
0070 };
0071 
0072 /** \ingroup tools
0073  *  \brief Textual note item
0074  *
0075  *  Actual displaying of the Note should be
0076  *  implemented by application
0077  */
0078 class Note: public ItemGroup, public Tool
0079 {
0080     STEPCORE_OBJECT(Note)
0081 
0082 public:
0083     /** Constructs Note */
0084     explicit Note(const Vector2d &position = Vector2d::Zero(),
0085             const Vector2d &size = Vector2d(250,100), const QString &text = QString());
0086 
0087     /** Get position of the note */
0088     const Vector2d& position() const { return _position; }
0089     /** Set position of the note */
0090     void setPosition(const Vector2d& position) { _position = position; }
0091 
0092     /** Get size of the note */
0093     const Vector2d& size() const { return _size; }
0094     /** Set size of the note */
0095     void setSize(const Vector2d& size) { _size = size.array().abs(); }
0096 
0097     /** Get note text */
0098     const QString& text() const { return _text; }
0099     /** Set note text */
0100     void setText(const QString& text) { _text = text; }
0101 
0102 protected:
0103     Vector2d _position;
0104     Vector2d _size;
0105     QString  _text;
0106 };
0107 
0108 /** \ingroup tools
0109  *  \brief Graph item
0110  *
0111  *  Actual updating and displaying of the Graph
0112  *  should be implemented by application
0113  */
0114 class Graph: public Item, public Tool
0115 {
0116     STEPCORE_OBJECT(Graph)
0117 
0118 public:
0119     /** Constructs Graph */
0120     explicit Graph(const Vector2d &position = Vector2d::Zero(), const Vector2d &size = Vector2d(400,300));
0121 
0122     /** Get position of the graph */
0123     const Vector2d& position() const { return _position; }
0124     /** Set position of the graph */
0125     void setPosition(const Vector2d& position) { _position = position; }
0126 
0127     /** Get size of the graph */
0128     const Vector2d& size() const { return _size; }
0129     /** Set size of the graph */
0130     void setSize(const Vector2d& size) { _size = size.array().abs(); }
0131 
0132     /** Get pointer to the objects for X axis */
0133     Object* objectX() const { return _objectX; }
0134     /** Set pointer to the objects for X axis */
0135     void setObjectX(Object* objectX) { _objectX = objectX; }
0136 
0137     /** Get name of the property for X axis */
0138     QString propertyX() const { return _propertyX; }
0139     /** Set name of the property for X axis */
0140     void setPropertyX(const QString& propertyX) { _propertyX = propertyX; }
0141 
0142     /** Get vector index for the X axis */
0143     int indexX() const { return _indexX; }
0144     /** Set vector index for the X axis */
0145     void setIndexX(int indexX) { _indexX = indexX; }
0146 
0147     /** Get pointer to the objects for Y axis */
0148     Object* objectY() const { return _objectY; }
0149     /** Set pointer to the objects for Y axis */
0150     void setObjectY(Object* objectY) { _objectY = objectY; }
0151 
0152     /** Get name of the property for Y axis */
0153     QString propertyY() const { return _propertyY; }
0154     /** Set name of the property for Y axis */
0155     void setPropertyY(const QString& propertyY) { _propertyY = propertyY; }
0156 
0157     /** Get vector index for the Y axis */
0158     int indexY() const { return _indexY; }
0159     /** Set vector index for the Y axis */
0160     void setIndexY(int indexY) { _indexY = indexY; }
0161 
0162     /** Get auto-limits for X axis */
0163     bool autoLimitsX() const { return _autoLimitsX; }
0164     /** Set auto-limits for X axis */
0165     void setAutoLimitsX(bool autoLimitsX) { _autoLimitsX = autoLimitsX; }
0166 
0167     /** Get auto-limits for Y axis */
0168     bool autoLimitsY() const { return _autoLimitsY; }
0169     /** Set auto-limits for Y axis */
0170     void setAutoLimitsY(bool autoLimitsY) { _autoLimitsY = autoLimitsY; }
0171 
0172     /** Get limits for X axis */
0173     const Vector2d& limitsX() const { return _limitsX; }
0174     /** Set limits for X axis */
0175     void setLimitsX(const Vector2d& limitsX) { _limitsX = limitsX; }
0176 
0177     /** Get limits for Y axis */
0178     const Vector2d& limitsY() const { return _limitsY; }
0179     /** Set limits for Y axis */
0180     void setLimitsY(const Vector2d& limitsY) { _limitsY = limitsY; }
0181 
0182     /** Get show-lines flag */
0183     bool showLines() const { return _showLines; }
0184     /** Set show-lines flag */
0185     void setShowLines(bool showLines) { _showLines = showLines; }
0186 
0187     /** Get show-points flag */
0188     bool showPoints() const { return _showPoints; }
0189     /** Set show-points flag */
0190     void setShowPoints(bool showPoints) { _showPoints = showPoints; }
0191 
0192     /** Get points list */
0193     const Vector2dList& points() const { return _points; }
0194     /** Set points list */
0195     void setPoints(const Vector2dList& points) { _points = points; }
0196 
0197     /** Get pointer to the property for X axis (or zero if not defined) */
0198     const MetaProperty* propertyXPtr() const {
0199         return _objectX ? _objectX->metaObject()->property(_propertyX) : nullptr;
0200     }
0201 
0202     /** Get pointer to the property for Y axis (or zero if not defined) */
0203     const MetaProperty* propertyYPtr() const {
0204         return _objectY ? _objectY->metaObject()->property(_propertyY) : nullptr;
0205     }
0206 
0207     /** Returns true if X-axis data source is valid */
0208     bool isValidX() const;
0209     /** Returns true if Y-axis data source is valid */
0210     bool isValidY() const;
0211     /** Returns true if X- and Y-axis data source is valid */
0212     bool isValid() const { return isValidX() && isValidY(); }
0213 
0214     /** Get current point value */
0215     Vector2d currentValue() const { return currentValue(nullptr); }
0216 
0217     /** Get current point value
0218      *  \param ok Will indicate success of operation (if not null) */
0219     Vector2d currentValue(bool* ok) const;
0220 
0221     /** Get current point value and add it to points list
0222      *  \param ok Will indicate success of operation (if not null) */
0223     Vector2d recordPoint(bool* ok = nullptr);
0224 
0225     /** Clear points list */
0226     void clearPoints() { _points.clear(); }
0227 
0228     /** Return units of propertyX */
0229     QString unitsX() const;
0230 
0231     /** Return units of propertyY */
0232     QString unitsY() const;
0233 
0234     //void worldItemRemoved(Item* item);
0235     //void setWorld(World* world);
0236 
0237 protected:
0238     Vector2d _position;
0239     Vector2d _size;
0240 
0241     Object* _objectX;
0242     QString       _propertyX;
0243     int           _indexX;
0244 
0245     Object* _objectY;
0246     QString       _propertyY;
0247     int           _indexY;
0248 
0249     bool        _autoLimitsX;
0250     bool        _autoLimitsY;
0251 
0252     Vector2d    _limitsX;
0253     Vector2d    _limitsY;
0254 
0255     bool _showLines;
0256     bool _showPoints;
0257 
0258     Vector2dList _points;
0259 };
0260 
0261 /** \ingroup tools
0262  *  \brief Meter to observe properties of other objects
0263  *
0264  *  Actual displaying of the Meter and its user interaction
0265  *  should be implemented by application
0266  */
0267 class Meter: public Item, public Tool
0268 {
0269     STEPCORE_OBJECT(Meter)
0270 
0271 public:
0272     /** Constructs Meter */
0273     explicit Meter(const Vector2d &position = Vector2d::Zero(), const Vector2d &size = Vector2d(70,24));
0274 
0275     /** Get position of the meter */
0276     const Vector2d& position() const { return _position; }
0277     /** Set position of the meter */
0278     void setPosition(const Vector2d& position) { _position = position; }
0279 
0280     /** Get size of the meter */
0281     const Vector2d& size() const { return _size; }
0282     /** Set size of the meter */
0283     void setSize(const Vector2d& size) { _size = size.array().abs(); }
0284 
0285     /** Get pointer to the observed object */
0286     Object* object() const { return _object; }
0287     /** Set pointer to the observed object */
0288     void setObject(Object* object) { _object = object; }
0289 
0290     /** Get name of the observed property */
0291     QString property() const { return _property; }
0292     /** Set name of the observed property */
0293     void setProperty(const QString& property) { _property = property; }
0294 
0295     /** Get vector index of the observed property */
0296     int index() const { return _index; }
0297     /** Set vector index of the observed property */
0298     void setIndex(int index) { _index = index; }
0299 
0300     /** Get display digits */
0301     int digits() const { return _digits; }
0302     /** Set display digits */
0303     void setDigits(int digits) { _digits = digits; }
0304 
0305     /** Returns true if observed property is valid */
0306     bool isValid() const;
0307 
0308     /** Get pointer to the observed property */
0309     const MetaProperty* propertyPtr() const {
0310         return _object ? _object->metaObject()->property(_property) : nullptr;
0311     }
0312 
0313     /** Get value of the observed property */
0314     double value() const { return value(nullptr); }
0315 
0316     /** Set value of the controlled property */
0317     virtual void setValue(double) {}
0318 
0319     /** Get value of the observed property
0320      *  \param ok Will indicate success of operation (if not null) */
0321     double value(bool* ok) const;
0322 
0323     /** Return units of measured property */
0324     QString units() const;
0325 
0326     //void worldItemRemoved(Item* item);
0327     //void setWorld(World* world);
0328 
0329 protected:
0330     Vector2d _position;
0331     Vector2d _size;
0332 
0333     Object*  _object;
0334     QString  _property;
0335     int      _index;
0336 
0337     int      _digits;
0338 };
0339 
0340 /** \ingroup tools
0341  *  \brief Controller item to control properties of other objects
0342  *
0343  *  Actual displaying of the Controller and its user interaction
0344  *  should be implemented by application
0345  */
0346 class Controller: public Item, public Tool
0347 {
0348     STEPCORE_OBJECT(Controller)
0349 
0350 public:
0351     /** Constructs Controller */
0352     explicit Controller(const Vector2d &position = Vector2d::Zero(), const Vector2d &size = Vector2d(200,60));
0353 
0354     /** Get position of the Controller */
0355     const Vector2d& position() const { return _position; }
0356     /** Set position of the Controller */
0357     void setPosition(const Vector2d& position) { _position = position; }
0358 
0359     /** Get size of the Controller */
0360     const Vector2d& size() const { return _size; }
0361     /** Set size of the Controller */
0362     void setSize(const Vector2d& size) { _size = size.array().abs(); }
0363 
0364     /** Get pointer to the controlled object */
0365     Object* object() const { return _object; }
0366     /** Set pointer to the controlled object */
0367     void setObject(Object* object) { _object = object; }
0368 
0369     /** Get name of the controlled property */
0370     QString property() const { return _property; }
0371     /** Set name of the controlled property */
0372     void setProperty(const QString& property) { _property = property; }
0373 
0374     /** Get vector index of the controlled property */
0375     int index() const { return _index; }
0376     /** Set vector index of the controlled property */
0377     void setIndex(int index) { _index = index; }
0378 
0379     /** Get GUI slider limits */
0380     const Vector2d& limits() const { return _limits; }
0381     /** Set GUI slider limits */
0382     void setLimits(const Vector2d& limits) { _limits = limits; }
0383 
0384     /** Get GUI increase shortcut */
0385     const QString& decreaseShortcut() const { return _decreaseShortcut; }
0386     /** Set GUI increase shortcut */
0387     void setDecreaseShortcut(const QString& decreaseShortcut) { _decreaseShortcut = decreaseShortcut; }
0388 
0389     /** Get GUI decrease shortcut */
0390     const QString& increaseShortcut() const { return _increaseShortcut; }
0391     /** Set GUI decrease shortcut */
0392     void setIncreaseShortcut(const QString& increaseShortcut) { _increaseShortcut = increaseShortcut; }
0393 
0394     /** Get increment step */
0395     double increment() const { return _increment; }
0396     /** Set increment step */
0397     void setIncrement(double increment) { _increment = increment; }
0398 
0399     /** Returns true if controlled property is valid */
0400     bool isValid() const;
0401 
0402     /** Get pointer to the controlled property */
0403     const MetaProperty* propertyPtr() const {
0404         return _object ? _object->metaObject()->property(_property) : nullptr;
0405     }
0406 
0407     /** Get value of the controlled property */
0408     double value() const { return value(nullptr); }
0409     /** Set value of the controlled property */
0410     void setValue(double value) { setValue(value, nullptr); }
0411 
0412     /** Get value of the controlled property
0413      *  \param ok Will indicate success of operation (if not null) */
0414     double value(bool* ok) const;
0415     /** Set value of the controlled property
0416      *  \param value New value for the property
0417      *  \param ok Will indicate success of operation (if not null) */
0418     void setValue(double value, bool* ok);
0419 
0420     /** Return units of measured property */
0421     QString units() const;
0422 
0423     //void worldItemRemoved(Item* item);
0424     //void setWorld(World* world);
0425 
0426 protected:
0427     Vector2d _position;
0428     Vector2d _size;
0429 
0430     Object*  _object;
0431     QString  _property;
0432     int      _index;
0433 
0434     Vector2d _limits;
0435     QString  _decreaseShortcut;
0436     QString  _increaseShortcut;
0437 
0438     double   _increment;
0439 };
0440 
0441 class Particle;
0442 class RigidBody;
0443 
0444 /** \ingroup tools
0445  *  \brief Traces position of the body
0446  *
0447  *  Actual displaying of the Traces and its user interaction
0448  *  should be implemented by application
0449  */
0450 class Tracer: public Item, public Tool
0451 {
0452     STEPCORE_OBJECT(Tracer)
0453 
0454 public:
0455     /** Constructs Spring */
0456     explicit Tracer(Object* body = nullptr, const Vector2d& localPosition = Vector2d::Zero());
0457 
0458     /** Get pointer to the first body */
0459     Object* body() const { return _body; }
0460     /** Set pointer to the first connected body */
0461     void setBody(Object* body);
0462 
0463     /** Local position of the tracer on the body
0464      *  or in the world (if the tracer is not connected) */
0465     Vector2d localPosition() const { return _localPosition; }
0466     /** Set local position of the tracer on the body
0467      *  or in the world (if the tracer is not connected) */
0468     void setLocalPosition(const Vector2d& localPosition) { _localPosition = localPosition; }
0469 
0470     /** Position of the tracer */
0471     Vector2d position() const;
0472 
0473     /** Get points list */
0474     const Vector2dList& points() const { return _points; }
0475     /** Set points list */
0476     void setPoints(const Vector2dList& points) { _points = points; }
0477 
0478     /** Get current position value and add it to points list */
0479     Vector2d recordPoint() { Vector2d p = position(); _points.push_back(p); return p; }
0480 
0481     /** Clear points list */
0482     void clearPoints() { _points.clear(); }
0483 
0484     //void worldItemRemoved(Item* item);
0485     //void setWorld(World* world);
0486 
0487 protected:
0488     Object* _body;
0489     Vector2d _localPosition;
0490     Vector2dList _points;
0491 
0492     Particle*  _p;
0493     RigidBody* _r;
0494 };
0495 
0496 } // namespace StepCore
0497 
0498 #endif