CanvasCV: CanvasCV-doxygen/src/canvascv/shapes/shape.h Source File
CanvasCV  1.0.0
shape.h
1 #ifndef SHAPE_H
2 #define SHAPE_H
3 
4 #include "canvascv/colors.h"
5 #include "canvascv/consts.h"
6 
7 #include <opencv2/core/mat.hpp>
8 #include <string>
9 #include <list>
10 #include <iostream>
11 #include <memory>
12 #include <functional>
13 
14 namespace canvascv
15 {
16 
17 class Handle;
18 class Canvas;
19 
32 class Shape
33 {
34 public:
35 
37  Shape();
38 
40  Shape(const Shape &other);
41 
43  virtual ~Shape();
44 
49  enum Event
50  {
54  };
55 
57  typedef std::function<void(Shape*, Event)> CBPerShape;
58 
64  void notifyOnEvent(CBPerShape cb);
65 
66 
73  virtual std::list<Handle*> getConnectionTargets() = 0;
74 
75 
77  cv::Scalar getOutlineColor() const;
78 
80  virtual void setOutlineColor(const cv::Scalar &value);
81 
83  cv::Scalar getFillColor() const;
84 
86  virtual void setFillColor(const cv::Scalar &value);
87 
89  bool getLocked() const;
90 
92  virtual void setLocked(bool value);
93 
95  bool getVisible() const;
96 
98  virtual void setVisible(bool value);
99 
105  virtual const char *getType() const = 0;
106 
108  int getThickness() const;
109 
111  virtual void setThickness(int value);
112 
114  int getLineType() const;
115 
117  virtual void setLineType(int value);
118 
120  virtual bool isAtPos(const cv::Point &pos) = 0;
121 
122  virtual void translate(const cv::Point &offset) = 0;
123 
124  bool isReady() const;
125 
127  int getId();
128 
129 protected:
130  virtual void writeInternals(cv::FileStorage& fs) const = 0;
131  virtual void readInternals(const cv::FileNode& node) = 0;
132 
140  virtual bool mousePressed(const cv::Point &pos, bool onCreate = false) = 0;
141 
151  virtual bool mouseMoved(const cv::Point &pos) = 0;
152 
160  virtual std::shared_ptr<Shape> getShape(int id);
161 
168  virtual bool keyPressed(int &key);
169 
174  virtual void lostFocus();
175 
182  virtual bool mouseReleased(const cv::Point &pos) = 0;
183 
184  bool isEditing();
185 
191  virtual void draw(cv::Mat &canvas) = 0;
192 
194  void drawHelper(cv::Mat &canvas, Shape *other);
195 
196  const std::string &getStatusMsg() const;
197  virtual const std::string &getCreateStatusMsg() const = 0;
198  virtual const std::string &getEditStatusMsg() const = 0;
199 
200  void setCanvas(Canvas &value);
201 
202  void setDeleted();
203 
204  bool isDeleted();
205 
206  virtual void setReady();
207 
208  int id;
209  cv::Scalar outlineColor;
210  cv::Scalar fillColor;
211  bool locked;
212  bool visible;
213  bool editing;
214  int thickness;
215  int lineType;
216  Canvas *canvas;
217 
218 private:
219  friend void read(const cv::FileNode& node, Canvas& x, const Canvas&);
220  friend class Canvas;
221  friend class CompoundShape;
222 
224  void broadcastEvent(Event event);
225 
226  int genId();
227 
228  friend void write(cv::FileStorage& fs, const std::string&, const Shape& x);
229  friend void read(const cv::FileNode& node, Shape*& x, const Shape* default_value);
230 
231  void write(cv::FileStorage& fs) const;
232  void read(const cv::FileNode& node);
233 
234  std::list<CBPerShape> cbs;
235  bool deleted;
236  bool ready;
237 };
238 
239 // These write and read functions must be defined for the serialization in cv::FileStorage to work
240 void write(cv::FileStorage& fs, const std::string&, const Shape& x);
241 void read(const cv::FileNode& node, Shape*& x, const Shape* default_value);
242 
243 // Will write the xml to memory and output it's std::string
244 std::ostream & operator<<(std::ostream& o, const Shape &shape);
245 
246 }
247 
252 #endif // SHAPE_H
shape unselected
Definition: shape.h:52
virtual void setLineType(int value)
set the line type (LINE_4, LINE_8, LINE_AA)
virtual void draw(cv::Mat &canvas)=0
draw shape on the canvas
shape selected
Definition: shape.h:51
shape removed
Definition: shape.h:53
virtual bool mouseMoved(const cv::Point &pos)=0
mouseMoved
int getThickness() const
get line thickness to use when drawing
virtual std::list< Handle * > getConnectionTargets()=0
getConnectionTargets
virtual void setOutlineColor(const cv::Scalar &value)
set the outline color
virtual bool mousePressed(const cv::Point &pos, bool onCreate=false)=0
mousePressed
virtual std::shared_ptr< Shape > getShape(int id)
getShape
The Shape class hierarchy is for geomertric user interaction.
Definition: shape.h:32
void notifyOnEvent(CBPerShape cb)
used to register for notifications on shape
int getId()
return a unique id for this shape
int getLineType() const
get the line type (LINE_4, LINE_8, LINE_AA)
The CompoundShape class.
Definition: compoundshape.h:20
virtual void lostFocus()
lostFocus is called by Canvas if we&#39;re in it and just became non-active
virtual bool mouseReleased(const cv::Point &pos)=0
mouseReleased
bool getLocked() const
is the shape locked (can&#39;t be moved/edited)
Shape()
constructor
cv::Scalar getOutlineColor() const
get the outline color
virtual void setLocked(bool value)
set the shape lock state (can/can&#39;t be moved/edited)
cv::Scalar getFillColor() const
get the fill color (fill color is not very useful for shapes right now)
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
std::function< void(Shape *, Event)> CBPerShape
signature of a callback which gets the Event
Definition: shape.h:57
virtual ~Shape()
virtual destructor
virtual const char * getType() const =0
getType is always implemented by derived to return the same static pointer per shape.
virtual bool isAtPos(const cv::Point &pos)=0
returns true if shape is at pos, false otherwise
virtual bool keyPressed(int &key)
keyPressed will be called by Canvas for active shapes
bool getVisible() const
is the shape visible
Event
The Event enum will let you know what just happended to the shape.
Definition: shape.h:49
virtual void setVisible(bool value)
set the shape visible state
void drawHelper(cv::Mat &canvas, Shape *other)
helper method for non compund shapes to draw their members
virtual void setFillColor(const cv::Scalar &value)
set the fill color (fill color is not very useful for shapes right now)
The Canvas class is the entry point into CanvasCV.
Definition: canvas.h:33
virtual void setThickness(int value)
set line thickness to use when drawing