CanvasCV: CanvasCV-doxygen/src/canvascv/canvas.h Source File
CanvasCV  1.0.0
canvas.h
1 #ifndef CANVAS_H
2 #define CANVAS_H
3 
4 #include "canvascv/colors.h"
5 #include "canvascv/consts.h"
6 
7 #include "shapes/shape.h"
8 #include "widgets/widget.h"
9 #include "shapes/shapefactory.h"
10 #include "widgets/widgetfactory.h"
11 #include "widgets/text.h"
12 #include "widgets/layoutbase.h"
13 
14 #include <list>
15 #include <memory>
16 #include <functional>
17 #include <sstream>
18 
20 namespace canvascv
21 {
22 
33 class Canvas : public LayoutBase
34 {
35 public:
37  typedef std::function<void(Shape*)> CBCanvasShape;
38 
47  Canvas(const std::string &winNameVal, cv::Size sizeVal = cv::Size());
48 
50  ~Canvas();
51 
60  void redrawOn(const cv::Mat &src, cv::Mat &dst);
61 
62 
71  void redrawOn(cv::Mat &dst);
72 
79  void setImage(const cv::Mat &img);
80 
82  bool onMousePress(const cv::Point &pos);
83 
85  void onMouseRelease(const cv::Point &pos);
86 
88  void onMouseMove(const cv::Point &pos);
89 
100  std::shared_ptr<Shape> createShape(std::string type, const cv::Point &pos = cv::Point(0,0));
101 
111  template <class T>
112  std::shared_ptr<T> createShape(const cv::Point &pos = cv::Point(0,0));
113 
119  std::string getShapeType() const
120  {
121  return shapeType;
122  }
123 
129  void setShapeType(std::string value)
130  {
131  shapeType = value;
132  }
133 
138  void deleteActive();
139 
144  void deleteShape(const std::shared_ptr<Shape> &shape);
145 
150  void deleteWidget(const std::shared_ptr<Widget> &widget);
151 
158  void notifyOnShapeCreate(CBCanvasShape cb);
159 
166  void notifyOnShapeModify(CBCanvasShape cb);
167 
174  void notifyOnShapeDelete(CBCanvasShape cb);
175 
180  void clearShapes();
181 
186  void clearWidgets();
187 
195  template <class T>
196  void getShapes(std::list<std::shared_ptr<T>> &result);
197 
204  std::shared_ptr<Shape> getShape(int id);
205 
212  void getShapes(const cv::Point &pos, std::list<std::shared_ptr<Shape>> &result);
213 
216  {
217  hasScreenText = false;
218  }
219 
222  {
223  hasStatusMsg = false;
224  }
225 
238  void enableScreenText(cv::Scalar color = Colors::Black,
239  cv::Scalar bgColor = Colors::LightGray,
240  double scale = Consts::DEFAULT_FONT_SCALE,
241  int thickness = Consts::DEFAULT_FONT_THICKNESS,
242  uchar alpha = 80,
243  int fontFace = Consts::DEFAULT_FONT);
244 
257  void enableStatusMsg(cv::Scalar color = Colors::Orange,
258  cv::Scalar bgColor = Colors::LightGray,
259  double scale = Consts::DEFAULT_FONT_SCALE,
260  int thickness = Consts::DEFAULT_FONT_THICKNESS,
261  uchar alpha = 80,
262  int fontFace = Consts::DEFAULT_FONT);
263 
265  void setDefaultStatusMsg(const std::string &msg);
266 
268  std::string getDefaultStatusMsg() const;
269 
271  void setScreenText(const std::string &msg);
272 
274  void setSize(const cv::Size &value);
275 
277  cv::Size getSize();
278 
279  virtual void addWidget(const std::shared_ptr<Widget> &widget);
280  virtual std::shared_ptr<Widget> rmvWidget(const std::shared_ptr<Widget> &widget);
281 
283  bool getOn() const;
284 
286  void setOn(bool value);
287 
289  void writeShapesToFile(const std::string &filepath) const;
290 
292  void readShapesFromFile(const std::string &filepath);
293 
295  void setMouseCallback();
296 
298  void imshow(InputArray mat);
299 
313  int waitKeyEx(int delay = 0);
314 
321  void applyTheme(bool applyToCanvasText = false);
322 
333  static void fatal(string errorMsg, int exitStatus);
334 
336  void setDirty();
337 
338 protected:
339  virtual void recalc() {}
340 
341  virtual std::shared_ptr<Widget> rmvWidget(Widget *widget);
342 
343 
344  virtual const cv::Rect getBoundaries() const;
345 
346 private:
347 
348  Canvas();
349 
350  class StatusMsgGrd
351  {
352  public:
353  StatusMsgGrd(Canvas &val) : c(val) {}
354  ~StatusMsgGrd()
355  {
356  if (c.activeWidget)
357  {
358  c.setStatusMsg(c.activeWidget->getStatusMsg());
359  }
360  else if (c.activeShape)
361  {
362  c.setStatusMsg(c.activeShape->getStatusMsg());
363  }
364  else
365  {
366  c.setStatusMsg(c.defaultStatusMsg);
367  }
368  }
369  private:
370  Canvas &c;
371  };
372  friend class StatusMsgGrd;
373 
380  void consumeKey(int &key);
381 
382  void setStatusMsg(const std::string &msg);
383 
384  virtual bool setDirtyLayout();
385 
386  void broadcastCreate(Shape *shape);
387  void broadcastModify(Shape *shape);
388  void broadcastDelete(Shape *shape);
389 
390  void processNewShape();
391 
392  bool on;
393  bool isDirty;
394  cv::Rect boundaries;
395  bool hasScreenText;
396  bool hasStatusMsg;
397  std::shared_ptr<Text> screenText;
398  std::shared_ptr<Text> statusMsg;
399  std::string defaultStatusMsg;
400  std::string shapeType;
401  cv::Point dragPos;
402  cv::Mat latestFrameSrc;
403  std::string winName;
404  std::list<std::shared_ptr<Shape>> shapes;
405  std::list<std::shared_ptr<Widget>> widgets;
406  std::shared_ptr<Shape> activeShape;
407  std::shared_ptr<Widget> activeWidget;
408  std::list<CBCanvasShape> createNotifs;
409  std::list<CBCanvasShape> modifyNotifs;
410  std::list<CBCanvasShape> deleteNotifs;
411 
412  friend void operator >> (const cv::FileNode& n, Canvas& value)
413  {
414  read( n, value, Canvas());
415  }
416 
417  friend void write(cv::FileStorage& fs, const std::string&, const Canvas& x);
418  friend void read(const cv::FileNode& node, Canvas& x, const Canvas&);
419 };
420 
421 template <class T>
422 std::shared_ptr<T> Canvas::createShape(const cv::Point &pos)
423 {
424  std::shared_ptr<T> shape(ShapeFactoryT<T>::newShape(pos));
425  shapes.push_back(shape);
426  processNewShape();
427  ((Shape*)shape.get())->setReady();
428  ((Shape*)shape.get())->setLocked(false); // (Maybe better is setReady does this for the needed shape)
429  ((Shape*)shape.get())->lostFocus();
430  activeShape.reset();
431  return shape;
432 }
433 
434 template <class T>
435 void Canvas::getShapes(std::list<std::shared_ptr<T>> &result)
436 {
437  for (auto &shape : shapes)
438  {
439  std::shared_ptr<T> derived = std::dynamic_pointer_cast<T>(shape);
440  if (derived.get())
441  {
442  result.push_back(derived);
443  }
444  }
445 }
446 
447 void writeShapes(cv::FileStorage& fs, const std::string&, const Canvas& x);
448 void readShapes(const cv::FileNode& node, Canvas& x, const Canvas&);
449 
450 }
451 
452 #define CCV_STR(X) ((std::stringstream&)(std::stringstream() << X)).str()
453 #define CCV_C_STR(X) CCV_STR(X).c_str()
454 
455 // doxygen markups below
456 
461 namespace std {}
463 
465 namespace cv {}
466 
467 #endif // CANVAS_H
static const cv::Scalar Black
&#160;&#160; RGB: (0,0,0) HEX: #000000
Definition: colors.h:152
void setSize(const cv::Size &value)
set the Canvas size to match the cv::Mat we&#39;ll use in redrawOn;
virtual void addWidget(const std::shared_ptr< Widget > &widget)
adds the widget to this Layout after removing it from it&#39;s previous layout
void setImage(const cv::Mat &img)
setImage
void deleteWidget(const std::shared_ptr< Widget > &widget)
delete specific widget
int waitKeyEx(int delay=0)
waitKeyEx
The LayoutBase class.
Definition: layoutbase.h:14
void disableStatusMsg()
disable the bottom left text area for auto status messages
Definition: canvas.h:221
void redrawOn(const cv::Mat &src, cv::Mat &dst)
redrawOn draws the shapes on dst
void setOn(bool value)
redrawOn will do nothing if value is &#39;false&#39;
The c++ std namespace.
Definition: canvas.h:462
The OpenCV namespace.
Definition: canvas.h:465
void clearWidgets()
clear all widgets from Canvas
void setDirty()
while waiting for events the Canvas is redrawn only if it is dirty
void setShapeType(std::string value)
set default shape type to draw (could be "")
Definition: canvas.h:129
The Shape class hierarchy is for geomertric user interaction.
Definition: shape.h:32
static const cv::Scalar Orange
&#160;&#160; RGB: (255,165,0) HEX: #FFA500
Definition: colors.h:36
static void fatal(string errorMsg, int exitStatus)
fatal
void notifyOnShapeDelete(CBCanvasShape cb)
used to register for notifications on shape deletion
The ShapeFactoryT class.
Definition: shapefactory.h:45
void deleteShape(const std::shared_ptr< Shape > &shape)
delete specific shape
std::shared_ptr< Shape > getShape(int id)
getShape
void notifyOnShapeModify(CBCanvasShape cb)
used to register for notifications on shape modification (actually when it is deselected) ...
std::string getDefaultStatusMsg() const
get the default status message (active widget and shapes will override it)
bool getOn() const
is redrawOn() on/off?
static const cv::Scalar LightGray
&#160;&#160; RGB: (211,211,211) HEX: #D3D3D3
Definition: colors.h:160
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
void enableStatusMsg(cv::Scalar color=Colors::Orange, cv::Scalar bgColor=Colors::LightGray, double scale=Consts::DEFAULT_FONT_SCALE, int thickness=Consts::DEFAULT_FONT_THICKNESS, uchar alpha=80, int fontFace=Consts::DEFAULT_FONT)
enableStatusMsg enables the bottom left text area for auto status messages
std::function< void(Shape *)> CBCanvasShape
notification on shapes create/modify/delete from this Canvas instance
Definition: canvas.h:37
std::shared_ptr< Shape > createShape(std::string type, const cv::Point &pos=cv::Point(0, 0))
createShape
virtual std::shared_ptr< Widget > rmvWidget(const std::shared_ptr< Widget > &widget)
rmvWidget
void notifyOnShapeCreate(CBCanvasShape cb)
used to register for notifications on shape creation
void clearShapes()
clear all shapes from Canvas
cv::Size getSize()
get the Canvas size
void setScreenText(const std::string &msg)
manually set the screen text message. It remains until disabled or changed.
void writeShapesToFile(const std::string &filepath) const
write all the shapes currently in the Canvas to a file
void deleteActive()
delete shape currenty selected
void onMouseMove(const cv::Point &pos)
You should delegate OpenCV mouse callback events to this method.
void setDefaultStatusMsg(const std::string &msg)
set the default status message (active widget and shapes will override it)
bool onMousePress(const cv::Point &pos)
You should delegate OpenCV mouse callback events to this method - returns true if did something at po...
void readShapesFromFile(const std::string &filepath)
load all the from a file into the canvas (removing all current shapes in the process) ...
void disableScreenText()
disable the top left text area for manual user messages
Definition: canvas.h:215
std::string getShapeType() const
get default shape type to draw
Definition: canvas.h:119
The Widget class.
Definition: widget.h:28
void getShapes(std::list< std::shared_ptr< T >> &result)
getShapes of a specific type
Definition: canvas.h:435
~Canvas()
release all shapes and widgets. Shape callbacks are invoked doe delete.
The Canvas class is the entry point into CanvasCV.
Definition: canvas.h:33
void applyTheme(bool applyToCanvasText=false)
applyTheme
void enableScreenText(cv::Scalar color=Colors::Black, cv::Scalar bgColor=Colors::LightGray, double scale=Consts::DEFAULT_FONT_SCALE, int thickness=Consts::DEFAULT_FONT_THICKNESS, uchar alpha=80, int fontFace=Consts::DEFAULT_FONT)
enableScreenText enables the top left text area for manual user messages
void onMouseRelease(const cv::Point &pos)
You should delegate OpenCV mouse callback events to this method.
void setMouseCallback()
utility method to handle mouse events on the associated window
void imshow(InputArray mat)
utility method which uses the winName encapsulated in Canvas