CanvasCV: CanvasCV-doxygen/src/canvascv/shapes/handle.h Source File
CanvasCV  1.0.0
handle.h
1 #ifndef HANDLE_H
2 #define HANDLE_H
3 
4 #include "shape.h"
5 #include "shapefactory.h"
6 
7 #include <opencv2/opencv.hpp>
8 #include <functional>
9 #include <list>
10 
11 namespace canvascv
12 {
13 
19 class Handle : public Shape
20 {
21 public:
22  typedef std::function<void(const cv::Point &)> PosChangedCB;
23  typedef std::list<PosChangedCB>::iterator CBID;
24 
26  const cv::Point &operator()() const {
27  return pt;
28  }
29 
37  void setPos(const cv::Point &pos, bool notify = true);
38 
39  virtual void translate(const cv::Point &offset);
40 
42  int getRadius() const;
43 
45  CBID addPosChangedCB(PosChangedCB cb);
46 
48  void delPosChangedCB(const CBID &id);
49 
51  void connect(Handle &other);
52 
54  void disconnect(Handle &other);
55 
56  virtual bool isAtPos(const cv::Point &pos)
57  {
58  return isPoint(pos);
59  }
60 
61  virtual std::list<Handle *> getConnectionTargets();
62 
63  virtual const char *getType() const;
64 
65  static const char * type;
66 
67  virtual ~Handle();
68 
69 protected:
70  friend class ShapeFactory;
71  template <class T> friend class ShapeFactoryT;
72 
73  Handle(const cv::Point &pos);
74 
75  virtual void writeInternals(cv::FileStorage &fs) const;
76  virtual void readInternals(const cv::FileNode &node);
77 
78  virtual void draw(cv::Mat &canvas);
79  virtual bool mousePressed(const cv::Point &pos, bool = false);
80  virtual bool mouseMoved(const cv::Point &pos);
81  virtual bool mouseReleased(const cv::Point &pos);
82  virtual void lostFocus();
83 
84  virtual const string &getCreateStatusMsg() const;
85  virtual const string &getEditStatusMsg() const;
86 
87 private:
88 
89  void connectedFrom(Handle &other);
90  void disconnectFrom(Handle &other);
91 
92  bool isPoint(const cv::Point &pos, int threshold=3)
93  {
94  threshold += thickness/2;
95  return abs(pos.x - pt.x) <= threshold &&
96  abs(pos.y - pt.y) <= threshold;
97  }
98 
99  void broadcastPosChanged(const cv::Point& pos);
100 
101  std::list<PosChangedCB> posChangedCBs;
102  std::map<Handle*, CBID> connectedHandles;
103  cv::Point pt;
104  bool allowSetPos;
105 };
106 
107 }
108 
109 #endif // HANDLE_H
110 
111 
The ShapeFactory class.
Definition: shapefactory.h:24
CBID addPosChangedCB(PosChangedCB cb)
register to be notified when this handle changes
virtual bool mouseReleased(const cv::Point &pos)
mouseReleased
virtual void draw(cv::Mat &canvas)
draw shape on the canvas
void disconnect(Handle &other)
disconnect from a handle that needed to change with us
virtual bool mousePressed(const cv::Point &pos, bool=false)
mousePressed
virtual bool isAtPos(const cv::Point &pos)
returns true if shape is at pos, false otherwise
Definition: handle.h:56
void connect(Handle &other)
connect to a handle to change when we change
The Shape class hierarchy is for geomertric user interaction.
Definition: shape.h:32
virtual const char * getType() const
getType is always implemented by derived to return the same static pointer per shape.
const cv::Point & operator()() const
return the Point location of this Handle
Definition: handle.h:26
The ShapeFactoryT class.
Definition: shapefactory.h:45
int getRadius() const
return the radius of this circled handle
virtual bool mouseMoved(const cv::Point &pos)
mouseMoved
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
void delPosChangedCB(const CBID &id)
unregister to be notified when this handle changes
void setPos(const cv::Point &pos, bool notify=true)
setPos
virtual void lostFocus()
lostFocus is called by Canvas if we&#39;re in it and just became non-active
virtual std::list< Handle * > getConnectionTargets()
getConnectionTargets
The Handle class.
Definition: handle.h:19