911124
#pragma once
911124
911124
#ifndef ASSISTANT_INCLUDED
911124
#define ASSISTANT_INCLUDED
911124
911124
// TnzTools includes
911124
#include <tools track.h=""></tools>
911124
911124
// TnzCore includes
911124
#include <tsmartpointer.h></tsmartpointer.h>
911124
#include <tgeometry.h></tgeometry.h>
ae9154
#include <tmetaimage.h></tmetaimage.h>
3bac66
#include <tproperty.h></tproperty.h>
911124
4da757
// Qt includes
4da757
#include <qcoreapplication></qcoreapplication>
4da757
911124
// std includes
911124
#include <vector></vector>
911124
#include <string></string>
911124
#include <map></map>
911124
911124
911124
#undef DVAPI
911124
#undef DVVAR
911124
#ifdef TNZTOOLS_EXPORTS
911124
#define DVAPI DV_EXPORT_API
911124
#define DVVAR DV_EXPORT_VAR
911124
#else
911124
#define DVAPI DV_IMPORT_API
911124
#define DVVAR DV_IMPORT_VAR
911124
#endif
911124
911124
911124
//==============================================================
911124
911124
//  Forward declarations
911124
3bac66
class TProperty;
3bac66
class TPropertyGroup;
3bac66
fa009d
class TTool;
911124
class TToolViewer;
911124
class TAssistant;
da847a
class TAssistantBase;
9cf8be
class TAssistantPoint;
911124
class TGuideline;
911124
911124
typedef TSmartPointerT<tguideline> TGuidelineP;</tguideline>
911124
typedef std::vector<tguidelinep> TGuidelineList;</tguidelinep>
c5e805
typedef std::map<tstringid, tassistantpoint=""> TAssistantPointMap;</tstringid,>
7900ea
typedef std::vector<const tassistantpoint*=""> TAssistantPointOrder;</const>
911124
911124
//===================================================================
911124
4da757
911124
//*****************************************************************************************
911124
//    TGuideline definition
911124
//*****************************************************************************************
911124
4df9cd
class DVAPI TGuideline : public TSmartObject {
911124
public:
8074a5
  const bool enabled;
3d32e2
  const double magnetism;
8074a5
8074a5
  TGuideline(bool enabled, double magnetism):
8074a5
    enabled(enabled), magnetism(magnetism) { }
3d32e2
911124
  virtual TTrackPoint transformPoint(const TTrackPoint &point) const
911124
    { return point; }
f278a5
  
f278a5
  // this function uses in calcTrackWeight to select best guideline,
f278a5
  // does not use for drawing
f278a5
  virtual TPointD nearestPoint(const TPointD &point) const
f278a5
    { return transformPoint(TTrackPoint(point)).position; }
f278a5
  
8074a5
  virtual void draw(bool active, bool enabled) const
911124
    { }
f278a5
  
8074a5
  void draw(bool active = false) const
8074a5
    { draw(active, true); }
8074a5
8074a5
  TTrackPoint smoothTransformPoint(const TTrackPoint &point, double magnetism = 1.0) const {
8074a5
    return enabled
8074a5
         ? TTrack::interpolationLinear(point, transformPoint(point), magnetism*this->magnetism)
8074a5
         : point;
8074a5
  }
911124
8074a5
  void drawSegment(
8074a5
    const TPointD &p0,
8074a5
    const TPointD &p1,
8074a5
    double pixelSize,
8074a5
    bool active,
8074a5
    bool enabled = true) const;
4df9cd
9cf8be
  double calcTrackWeight(const TTrack &track, const TAffine &toScreen, bool &outLongEnough) const;
9cf8be
  static TGuidelineP findBest(const TGuidelineList &guidelines, const TTrack &track, const TAffine &toScreen, bool &outLongEnough);
911124
};
911124
911124
911124
//*****************************************************************************************
9cf8be
//    TAssistantPoint definition
9cf8be
//*****************************************************************************************
9cf8be
9cf8be
class DVAPI TAssistantPoint {
9cf8be
public:
9cf8be
  enum Type {
9cf8be
    Circle,
9cf8be
    CircleFill,
c5e805
    CircleCross,
3a19fe
    CircleDiagonalCross,
c7854d
    CircleDots,
c7854d
    CircleDoubleDots,
9cf8be
  };
9cf8be
c5e805
  const TStringId name;
7900ea
  const TPointD defPosition;
7900ea
9cf8be
  Type type;
9cf8be
  TPointD position;
249386
  double radius;
c5e805
  bool visible;
c5e805
c5e805
  mutable bool selected;
9cf8be
7900ea
  explicit TAssistantPoint(const TStringId &name, const TPointD &defPosition = TPointD());
9cf8be
};
9cf8be
4da757
4da757
//*****************************************************************************************
4da757
//    TAssistantType definition
4da757
//*****************************************************************************************
4da757
4da757
class DVAPI TAssistantType: public TMetaObjectType {
4da757
public:
4da757
  TAssistantType(const TStringId &name):
4da757
    TMetaObjectType(name) { }
4da757
  TMetaObjectHandler* createHandler(TMetaObject &obj) const override;
da847a
  virtual TAssistantBase* createAssistant(TMetaObject &obj) const
4da757
    { return 0; }
4da757
};
4da757
4da757
4da757
//*****************************************************************************************
4da757
//    TAssistantTypeT definition
4da757
//*****************************************************************************************
4da757
4da757
template<typename t=""></typename>
4da757
class TAssistantTypeT: public TAssistantType {
4da757
public:
4da757
  typedef T Type;
4da757
4da757
  explicit TAssistantTypeT(
4da757
    const TStringId &name,
4da757
    const TStringId &alias1 = TStringId(),
4da757
    const TStringId &alias2 = TStringId(),
4da757
    const TStringId &alias3 = TStringId(),
4da757
    const TStringId &alias4 = TStringId(),
4da757
    const TStringId &alias5 = TStringId()
4da757
  ):
4da757
    TAssistantType(TStringId(name))
4da757
  {
4da757
    if (alias1) registerAlias(alias1);
4da757
    if (alias2) registerAlias(alias2);
4da757
    if (alias3) registerAlias(alias3);
4da757
    if (alias4) registerAlias(alias4);
4da757
    if (alias5) registerAlias(alias5);
4da757
  }
4da757
4da757
  explicit TAssistantTypeT(
4da757
    const std::string &name,
4da757
    const std::string &alias1 = std::string(),
4da757
    const std::string &alias2 = std::string(),
4da757
    const std::string &alias3 = std::string(),
4da757
    const std::string &alias4 = std::string(),
4da757
    const std::string &alias5 = std::string()
4da757
  ):
4da757
    TAssistantType(TStringId(name))
4da757
  {
4da757
    if (!alias1.empty()) registerAlias(TStringId(alias1));
4da757
    if (!alias2.empty()) registerAlias(TStringId(alias2));
4da757
    if (!alias3.empty()) registerAlias(TStringId(alias3));
4da757
    if (!alias4.empty()) registerAlias(TStringId(alias4));
4da757
    if (!alias5.empty()) registerAlias(TStringId(alias5));
4da757
  }
4da757
da847a
  TAssistantBase* createAssistant(TMetaObject &obj) const override
4da757
    { return new Type(obj); }
4da757
  QString getLocalName() const override {
4da757
    QString localName = Type::getLocalName();
4da757
    return localName.isEmpty() ? QString::fromStdString(name.str()) : localName;
4da757
  }
4da757
};
4da757
4da757
9cf8be
//*****************************************************************************************
da847a
//    TAssistantBase definition
911124
//*****************************************************************************************
911124
da847a
class DVAPI TAssistantBase : public TMetaObjectHandler {
da847a
  Q_DECLARE_TR_FUNCTIONS(TAssistantBase)
da847a
public:
da847a
  enum {
da847a
    DRAW_ERROR = 1,
da847a
  };
da847a
  
da847a
  static unsigned int drawFlags;
da847a
  static const double lineWidthScale;
da847a
  
9cf8be
protected:
3bac66
  const TStringId m_idEnabled;
9cf8be
  const TStringId m_idPoints;
9cf8be
  const TStringId m_idX;
9cf8be
  const TStringId m_idY;
3a19fe
  const TStringId m_idZ;
3a19fe
  const TStringId m_idW;
3bac66
  const TStringId m_idMagnetism;
9cf8be
c5e805
  TAssistantPointMap m_points;
7900ea
  TAssistantPointOrder m_pointsOrder;
c5e805
  TAssistantPoint* m_basePoint;
9cf8be
3bac66
  mutable TPropertyGroup m_properties;
3bac66
911124
public:
da847a
  TAssistantBase(TMetaObject &object);
9cf8be
4da757
  static QString getLocalName()
4da757
    { return QString(); }
4da757
c5e805
  inline const TAssistantPointMap& points() const
9cf8be
    { return m_points; }
7900ea
  inline const TAssistantPointOrder& pointsOrder() const
7900ea
    { return m_pointsOrder; }
c5e805
c5e805
  inline const TAssistantPoint* findPoint(const TStringId &name) const {
c5e805
    TAssistantPointMap::const_iterator i = points().find(name);
c5e805
    return i == points().end() ? 0 : &i->second;
c5e805
  }
9cf8be
249386
  void fixPoints();
b15438
  bool move(const TPointD &position);
b15438
  bool movePoint(const TStringId &name, const TPointD &position);
c5e805
  void setPointSelection(const TStringId &name, bool selected) const;
c5e805
  void setAllPointsSelection(bool selected) const;
9cf8be
3bac66
  bool getEnabled() const
3bac66
    { return data()[m_idEnabled].getBool(); }
3bac66
  void setEnabled(bool x)
3bac66
    { if (getEnabled() != x) data()[m_idEnabled].setBool(x); }
3bac66
c5e805
  inline void selectPoint(const TStringId &name) const
c5e805
    { setPointSelection(name, true); }
c5e805
  inline void deselectPoint(const TStringId &name) const
c5e805
    { setPointSelection(name, false); }
249386
  inline void selectAll() const
c5e805
    { setAllPointsSelection(true); }
249386
  inline void deselectAll() const
da847a
    { setAllPointsSelection(false); }
9cf8be
3bac66
  TPropertyGroup& getProperties() const
3bac66
    { return m_properties; }
3bac66
  void propertyChanged(const TStringId &name)
c7854d
    { onPropertyChanged(name); }
3bac66
c5e805
  const TAssistantPoint& getBasePoint() const;
c5e805
9cf8be
protected:
c5e805
  TAssistantPoint& addPoint(
c5e805
    const TStringId &name,
c5e805
    TAssistantPoint::Type type,
7900ea
    const TPointD &defPosition,
c5e805
    bool visible,
c5e805
    double radius );
c5e805
c5e805
  TAssistantPoint& addPoint(
c5e805
    const TStringId &name,
c5e805
    TAssistantPoint::Type type = TAssistantPoint::Circle,
7900ea
    const TPointD &defPosition = TPointD(),
c5e805
    bool visible               = true );
c5e805
c5e805
  inline TAssistantPoint& addPoint(
c5e805
    const std::string &name,
c5e805
    TAssistantPoint::Type type,
7900ea
    const TPointD &defPosition,
c5e805
    bool visible,
c5e805
    double radius )
7900ea
      { return addPoint(TStringId(name), type, defPosition, visible, radius); }
c5e805
c5e805
  inline TAssistantPoint& addPoint(
c5e805
    const std::string &name,
c5e805
    TAssistantPoint::Type type = TAssistantPoint::Circle,
7900ea
    const TPointD &defPosition = TPointD(),
c5e805
    bool visible               = true )
7900ea
      { return addPoint(TStringId(name), type, defPosition, visible); }
c5e805
3bac66
  //! usually called when meta-object created
3bac66
  void onSetDefaults() override;
9cf8be
  //! called when part of variant data changed
9cf8be
  void onDataChanged(const TVariant &value) override;
7900ea
  //! called when field of root struct of variant data changed
7900ea
  virtual void onDataFieldChanged(const TStringId &name, const TVariant &value);
9cf8be
  //! load object data from variant
9cf8be
  virtual void onAllDataChanged();
249386
  //! fix positions of all points (as like as all points moved)
9cf8be
  virtual void onFixPoints();
9cf8be
  //! try to move point
c5e805
  virtual void onMovePoint(TAssistantPoint &point, const TPointD &position);
9cf8be
  //! save object data to variant
da847a
  void onFixData() override;
3bac66
  //! load all properties from variant
3bac66
  virtual void updateProperties();
3bac66
  //! load single property from variant
3bac66
  virtual void updateProperty(const TStringId &name, const TVariant &value);
3bac66
  //! put value from property to variant
3bac66
  virtual void onPropertyChanged(const TStringId &name);
9cf8be
c7854d
  double getDrawingAlpha(bool enabled = true) const;
c7854d
  double getDrawingGridAlpha() const;
c7854d
13f20b
  static void drawSegment(const TPointD &p0, const TPointD &p1, double pixelSize, double alpha0, double alpha1);
3a19fe
  static void drawMark(const TPointD &p, const TPointD &normal, double pixelSize, double alpha);
3a19fe
  static void drawDot(const TPointD &p, double alpha);
3a19fe
  static void drawPoint(const TAssistantPoint &point, double pixelSize);
3a19fe
  static void drawIndex(const TPointD &p, int index, bool selected, double pixelSize);
ae9154
13f20b
  static inline void drawSegment(const TPointD &p0, const TPointD &p1, double pixelSize, double alpha)
13f20b
    { drawSegment(p0, p1, pixelSize, alpha, alpha); }
c7854d
  inline void drawSegment(const TPointD &p0, const TPointD &p1, double pixelSize) const
c7854d
    { drawSegment(p0, p1, pixelSize, getDrawingAlpha()); }
c7854d
  inline void drawDot(const TPointD &p) const
c7854d
    { drawDot(p, getDrawingAlpha()); }
c7854d
da847a
  TIntProperty* createSpinProperty(const TStringId &id, int def, int min, int max, bool hasMax = true);
da847a
  inline TIntProperty* createSpinProperty(const TStringId &id, int def, int min)
da847a
    { return createSpinProperty(id, def, min, 0, false); }
da847a
  
4da757
  void addProperty(TProperty *p);
4da757
  void setTranslation(const TStringId &name, const QString &localName) const;
3bac66
9cf8be
public:
4da757
  virtual void updateTranslation() const;
8074a5
  virtual void draw(TToolViewer *viewer, bool enabled) const;
8074a5
  void draw(TToolViewer *viewer) const { draw(viewer, true); }
9cf8be
  virtual void drawEdit(TToolViewer *viewer) const;
da847a
  virtual void drawEdit(TToolViewer *viewer, int index) const;
da847a
};
da847a
da847a
da847a
//*****************************************************************************************
da847a
//    TAssistant definition
da847a
//*****************************************************************************************
da847a
da847a
class DVAPI TAssistant : public TAssistantBase {
da847a
  Q_DECLARE_TR_FUNCTIONS(TAssistant)
da847a
protected:
da847a
  const TStringId m_idMagnetism;
da847a
da847a
  void onSetDefaults() override;
da847a
  void onFixData() override;
da847a
da847a
public:
da847a
  TAssistant(TMetaObject &object);
da847a
da847a
  double getMagnetism() const
da847a
    { return data()[m_idMagnetism].getDouble(); }
da847a
  void setMagnetism(double x)
da847a
    { if (getMagnetism() != x) data()[m_idMagnetism].setDouble(x); }
da847a
da847a
public:
da847a
  void updateTranslation() const override;
da847a
  virtual void getGuidelines(const TPointD &position, const TAffine &toTool, TGuidelineList &outGuidelines) const;
9a49d4
13f20b
  // calc W-coefficient and i-bounds for formula: x0 + 1/(i*W + 1)
9a49d4
  static bool calcPerspectiveStep(
9a49d4
    double minStep,
9a49d4
    double minX,
9a49d4
    double maxX,
9a49d4
    double x0,
9a49d4
    double x1,
9a49d4
    double x2,
13f20b
    double &outW,
13f20b
    double &outMinI,
13f20b
    double &outMaxI );
fa009d
  
fa009d
  static bool scanAssistants(
fa009d
    TTool *tool,
fa009d
    const TPointD *positions,
fa009d
    int positionsCount,
fa009d
    TGuidelineList *outGuidelines,
fa009d
    bool draw,
fa009d
    bool enabledOnly,
fa009d
    bool markEnabled,
fa009d
    bool drawGuidelines,
fa009d
    TImage *skipImage );
911124
};
911124
16421e
16421e
//*****************************************************************************************
16421e
//    export template implementations for win32
16421e
//*****************************************************************************************
16421e
16421e
#ifdef _WIN32
16421e
template class DVAPI TSmartPointerT<tguideline>;</tguideline>
16421e
#endif
16421e
16421e
911124
#endif