File indexing completed on 2024-04-28 07:39:41

0001 /*.
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003     SPDX-FileCopyrightText: 2014 Inge Wallin <inge@lysator.liu.se>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef STEP_TOOLGRAPHICS_H
0009 #define STEP_TOOLGRAPHICS_H
0010 
0011 #include "worldgraphics.h"
0012 #include "stepgraphicsitem.h"
0013 
0014 #include <QGraphicsTextItem>
0015 #include <QPointer>
0016 #include <QWidget>
0017 
0018 #include <KComboBox>
0019 #include <KTextEdit>
0020 
0021 #include <limits.h>
0022 
0023 #include <stepcore/tool.h>
0024 
0025 class KPlotWidget;
0026 class KPlotObject;
0027 class KToggleAction;
0028 class QAction;
0029 class QDialog;
0030 class QDialogButtonBox;
0031 class QAbstractButton;
0032 class QSlider;
0033 class QLabel;
0034 
0035 namespace Ui {
0036     class WidgetConfigureGraph;
0037     class WidgetConfigureMeter;
0038     class WidgetConfigureController;
0039 }
0040 
0041 namespace StepCore {
0042     class Note;
0043     class Meter;
0044     class Graph;
0045     class Controller;
0046     class Tracer;
0047 }
0048 
0049 /////////////////////////////////////////////////////////////////////////////////////////
0050 
0051 class WidgetVertexHandlerGraphicsItem: public OnHoverHandlerGraphicsItem
0052 {
0053     Q_OBJECT
0054 
0055 public:
0056     WidgetVertexHandlerGraphicsItem(StepCore::Item* item, WorldModel* worldModel,
0057                                         QGraphicsItem* parent, int vertexNum)
0058         : OnHoverHandlerGraphicsItem(item, worldModel, parent, nullptr, nullptr, vertexNum) {}
0059 
0060 
0061 protected:
0062     void setValue(const StepCore::Vector2d& value) override;
0063     StepCore::Vector2d value() override;
0064 };
0065 
0066 class WidgetGraphicsItem: public QObject, public StepGraphicsItem
0067 {
0068     Q_OBJECT
0069 
0070 public:
0071     WidgetGraphicsItem(StepCore::Item* item, WorldModel* worldModel);
0072     ~WidgetGraphicsItem();
0073 
0074     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
0075 
0076     void stateChanged() override;
0077     void viewScaleChanged() override;
0078     void worldDataChanged(bool dynamicOnly) override;
0079 
0080 protected:
0081     void setCenteralWidget(QWidget* widget);
0082     QWidget* centeralWidget() const { return _centralWidget; }
0083 
0084     const QBrush& backgroundBrush() const { return _backgroundBrush; }
0085     void setBackgroundBrush(const QBrush& brush) { _backgroundBrush = brush; }
0086 
0087     void mouseSetPos(const QPointF& pos, const QPointF&, MovingState movingState) override;
0088     OnHoverHandlerGraphicsItem* createOnHoverHandler(const QPointF& pos) override;
0089 
0090     QPointer<QWidget> _centralWidget;
0091     QBrush   _backgroundBrush;
0092 };
0093 
0094 /////////////////////////////////////////////////////////////////////////////////////////
0095 
0096 class NoteGraphicsItem;
0097 class NoteTextEdit: public KTextEdit
0098 {
0099     Q_OBJECT
0100 
0101 public:
0102     explicit NoteTextEdit(NoteGraphicsItem* noteItem, QWidget* parent = nullptr)
0103         : KTextEdit(parent), _noteItem(noteItem), _mousePressPoint(-1,-1) {}
0104     QString emptyNotice() const;
0105 
0106 protected:
0107     StepCore::NoteFormula* formulaAt(const QPoint& pos);
0108     void mouseMoveEvent(QMouseEvent *e) override;
0109     void mousePressEvent(QMouseEvent *e) override;
0110     void mouseReleaseEvent(QMouseEvent *e) override;
0111 
0112     NoteGraphicsItem* _noteItem;
0113     QPoint _mousePressPoint;
0114 };
0115 
0116 class KToolBar;
0117 class KSelectAction;
0118 class KFontAction;
0119 class KFontSizeAction;
0120 class NoteGraphicsItem: public WidgetGraphicsItem
0121 {
0122     Q_OBJECT
0123 
0124 public:
0125     NoteGraphicsItem(StepCore::Item* item, WorldModel* worldModel);
0126     void worldDataChanged(bool dynamicOnly) override;
0127 
0128 protected slots:
0129     void formatColor();
0130     void formatBold(bool checked);
0131     void formatAlign(QAction* action);
0132     void formatFontFamily(const QString& family);
0133     void formatFontSize(int size);
0134     void currentCharFormatChanged(const QTextCharFormat& f);
0135     void cursorPositionChanged();
0136     void insertImage();
0137     void insertFormula();
0138 
0139 protected:
0140     bool checkLatex();
0141     bool editFormula(StepCore::NoteFormula* formula);
0142     bool eventFilter(QObject* obj, QEvent* event) override;
0143 
0144     StepCore::Note* note() const;
0145     bool            _hasFocus;
0146 
0147     QList<StepCore::Item*> _newItems;
0148 
0149     QWidget*        _widget;
0150     NoteTextEdit*   _textEdit;
0151     KToolBar*       _toolBar;
0152 
0153     QAction *       _actionColor;
0154     KToggleAction* _actionBold;
0155     KToggleAction* _actionItalic;
0156     KToggleAction* _actionUnderline;
0157 
0158     KSelectAction* _actionAlign;
0159     KToggleAction* _actionAlignLeft;
0160     KToggleAction* _actionAlignCenter;
0161     KToggleAction* _actionAlignRight;
0162     KToggleAction* _actionAlignJustify;
0163 
0164     KFontAction*        _actionFont;
0165     KFontSizeAction*    _actionFontSize;
0166 
0167     QAction * _actionInsertImage;
0168     QAction * _actionInsertFormula;
0169 
0170     friend class NoteTextEdit;
0171 };
0172 
0173 /////////////////////////////////////////////////////////////////////////////////////////
0174 
0175 class DataSourceWidget: public QWidget
0176 {
0177     Q_OBJECT
0178 
0179 public:
0180     explicit DataSourceWidget(QWidget* parent = nullptr);
0181 
0182     void setSkipReadOnly(bool skipReadOnly) { _skipReadOnly = skipReadOnly; }
0183     void setDataSource(WorldModel* worldModel, StepCore::Object* object = nullptr,
0184                             const QString& property = QString(), int index = 0);
0185 
0186     StepCore::Object* dataObject() const;
0187     QString dataProperty() const { return _property->itemData(_property->currentIndex()).toString(); }
0188     int dataIndex() const { return _index->currentIndex(); }
0189 
0190 signals:
0191     void dataSourceChanged();
0192 
0193 protected slots:
0194     void objectSelected(int index);
0195     void propertySelected(int index);
0196 
0197 protected:
0198     void addObjects(const QModelIndex& parent, const QString& indent);
0199 
0200     WorldModel* _worldModel;
0201 
0202     KComboBox*  _object;
0203     KComboBox*  _property;
0204     KComboBox*  _index;
0205 
0206     bool _skipReadOnly;
0207 };
0208 
0209 /////////////////////////////////////////////////////////////////////////////////////////
0210 
0211 class GraphGraphicsItem: public WidgetGraphicsItem
0212 {
0213 public:
0214     GraphGraphicsItem(StepCore::Item* item, WorldModel* worldModel);
0215     void worldDataChanged(bool) override;
0216 
0217 protected:
0218     StepCore::Graph* graph() const;
0219     void adjustLimits();
0220 
0221     double _lastPointTime;
0222     QRgb   _lastColor;
0223 
0224     KPlotWidget* _plotWidget;
0225     KPlotObject* _plotObject;
0226     KPlotObject* _plotObject1;
0227 };
0228 
0229 class GraphMenuHandler: public ItemMenuHandler
0230 {
0231     Q_OBJECT
0232 
0233 public:
0234     GraphMenuHandler(StepCore::Object* object, WorldModel* worldModel, QObject* parent)
0235         : ItemMenuHandler(object, worldModel, parent) {}
0236 
0237     void populateMenu(QMenu* menu, KActionCollection* actions) override;
0238 
0239 protected slots:
0240     void clearGraph();
0241     void configureGraph();
0242     void confApply(QAbstractButton *button);
0243     void confChanged();
0244 
0245 protected:
0246     StepCore::Graph* graph() const;
0247     Ui::WidgetConfigureGraph* _confUi;
0248     QDialog*                  _confDialog;
0249     QDialogButtonBox         *_buttonBox;
0250     bool                      _confChanged;
0251 };
0252 
0253 /////////////////////////////////////////////////////////////////////////////////////////
0254 
0255 class QLCDNumber;
0256 class QFrame;
0257 class MeterGraphicsItem: public WidgetGraphicsItem
0258 {
0259 public:
0260     MeterGraphicsItem(StepCore::Item* item, WorldModel* worldModel);
0261     void worldDataChanged(bool) override;
0262 
0263 protected:
0264     StepCore::Meter* meter() const;
0265 
0266     QFrame*     _widget;
0267     QLCDNumber* _lcdNumber;
0268     QLabel*     _labelUnits;
0269 
0270     /*
0271     QLabel*  _labelMin;
0272     QLabel*  _labelMax;
0273     QLabel*  _labelSource;
0274     */
0275 };
0276 
0277 class MeterMenuHandler: public ItemMenuHandler
0278 {
0279     Q_OBJECT
0280 
0281 public:
0282     MeterMenuHandler(StepCore::Object* object, WorldModel* worldModel, QObject* parent)
0283         : ItemMenuHandler(object, worldModel, parent) {}
0284 
0285     void populateMenu(QMenu* menu, KActionCollection* actions) override;
0286 
0287 protected slots:
0288     void configureMeter();
0289     void confApply(QAbstractButton *button);
0290     void confChanged();
0291 
0292 protected:
0293     StepCore::Meter* meter() const;
0294     QAction * _configureAction;
0295     Ui::WidgetConfigureMeter *_confUi;
0296     QDialog                  *_confDialog;
0297     QDialogButtonBox         *_buttonBox;
0298     bool                      _confChanged;
0299 };
0300 
0301 /////////////////////////////////////////////////////////////////////////////////////////
0302 
0303 class ControllerGraphicsItem: public WidgetGraphicsItem
0304 {
0305     Q_OBJECT
0306 
0307 public:
0308     ControllerGraphicsItem(StepCore::Item* item, WorldModel* worldModel);
0309     void worldDataChanged(bool) override;
0310 
0311 protected slots:
0312     void incTriggered();
0313     void decTriggered();
0314     void sliderChanged(int value);
0315     void sliderReleased();
0316 
0317 protected:
0318     StepCore::Controller* controller() const;
0319 
0320     double _lastValue;
0321     bool _changed;
0322 
0323     QWidget* _widget;
0324     QSlider* _slider;
0325     QLabel*  _labelMin;
0326     QLabel*  _labelMax;
0327     QLabel*  _labelSource;
0328 
0329     QAction * _incAction;
0330     QAction * _decAction;
0331     QString  _incShortcut;
0332     QString  _decShortcut;
0333 
0334     static const int SLIDER_MIN = 0;
0335     static const int SLIDER_MAX = INT_MAX-100;
0336 };
0337 
0338 class ControllerMenuHandler: public ItemMenuHandler
0339 {
0340     Q_OBJECT
0341 
0342 public:
0343     ControllerMenuHandler(StepCore::Object* object, WorldModel* worldModel, QObject* parent)
0344         : ItemMenuHandler(object, worldModel, parent) {}
0345 
0346     void populateMenu(QMenu* menu, KActionCollection* actions) override;
0347 
0348 protected slots:
0349     void incTriggered();
0350     void decTriggered();
0351     void configureController();
0352     void confApply(QAbstractButton *button);
0353     void confChanged();
0354 
0355 protected:
0356     StepCore::Controller* controller() const;
0357     QAction * _configureAction;
0358     Ui::WidgetConfigureController *_confUi;
0359     QDialog                       *_confDialog;
0360     QDialogButtonBox              *_buttonBox;
0361     bool                           _confChanged;
0362 };
0363 
0364 /////////////////////////////////////////////////////////////////////////////////////////
0365 
0366 class TracerCreator: public AttachableItemCreator
0367 {
0368 public:
0369     TracerCreator(const QString& className, WorldModel* worldModel, WorldScene* worldScene)
0370         : AttachableItemCreator(className, worldModel, worldScene,
0371                             WorldScene::SnapRigidBody | WorldScene::SnapParticle |
0372                             WorldScene::SnapSetLocalPosition, nullptr) {}
0373 };
0374 
0375 class TracerGraphicsItem: public StepGraphicsItem
0376 {
0377 public:
0378     TracerGraphicsItem(StepCore::Item* item, WorldModel* worldModel);
0379 
0380     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
0381     QPainterPath shape() const override;
0382 
0383     void viewScaleChanged() override;
0384     void worldDataChanged(bool dynamicOnly) override;
0385 
0386 protected:
0387     void mouseSetPos(const QPointF& pos, const QPointF&, MovingState movingState) override;
0388     StepCore::Tracer* tracer() const;
0389     QPolygonF _points;
0390     QPointF   _lastPos;
0391     double    _lastPointTime;
0392     bool      _moving;
0393     QPointF   _movingDelta;
0394 };
0395 
0396 class TracerMenuHandler: public ItemMenuHandler
0397 {
0398     Q_OBJECT
0399 
0400 public:
0401     TracerMenuHandler(StepCore::Object* object, WorldModel* worldModel, QObject* parent)
0402         : ItemMenuHandler(object, worldModel, parent) {}
0403 
0404     void populateMenu(QMenu* menu, KActionCollection* actions) override;
0405 
0406 protected slots:
0407     void clearTracer();
0408 };
0409 
0410 
0411 #endif
0412