CanvasCV: CanvasCV-doxygen/src/canvascv/widgets/widget.h Source File
CanvasCV  1.0.0
widget.h
1 #ifndef WIDGET_H
2 #define WIDGET_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 Layout;
18 
28 class Widget
29 {
30 public:
31 
33  enum State
34  {
39  };
40 
42  typedef std::function<void(Widget*)> CBWidget;
43 
45  typedef std::function<void(Widget*, State)> CBWidgetState;
46 
48  typedef std::function<void(Widget*, int)> CBUserSelection;
49 
51  Widget(const cv::Point &pos = cv::Point(0,0));
52 
54  virtual ~Widget();
55 
61  virtual const char *getType() const = 0;
62 
68  void notifyOnChange(CBWidgetState cb);
69 
71  cv::Scalar getOutlineColor() const;
72 
74  virtual void setOutlineColor(const cv::Scalar &value);
75 
77  cv::Scalar getFillColor() const;
78 
80  virtual void setFillColor(const cv::Scalar &value);
81 
83  bool getVisible() const
84  {
85  return visible;
86  }
87 
89  virtual void setVisible(bool value);
90 
92  int getThickness() const;
93 
95  virtual void setThickness(int value);
96 
98  int getLineType() const;
99 
101  virtual void setLineType(int value);
102 
104  uchar getAlpha() const;
105 
107  void setAlpha(uchar value);
108 
115  enum Anchor
116  {
117  TOP = 0b00000001,
118  BOTTOM = 0b00000010,
119  LEFT = 0b00000100,
120  RIGHT = 0b00001000,
121  CENTER = 0b00010000,
130  };
131 
139  {
140  return layoutAnchor;
141  }
142 
149  void setLayoutAnchor(const Anchor &value);
150 
158  {
159  return flowAnchor;
160  }
161 
168  void setFlowAnchor(const Anchor &value);
169 
171  int getId()
172  {
173  return id;
174  }
175 
182  virtual const std::string &getStatusMsg() const;
183 
190  void setStatusMsg(const std::string &value);
191 
193  const cv::Point &getLocation() const;
194 
196  virtual void setLocation(const cv::Point &value);
197 
199  virtual void translate(const cv::Point &translation);
200 
202  bool getStretchX() const;
203 
205  void setStretchX(bool value);
206 
208  bool getStretchY() const;
209 
211  void setStretchY(bool value);
212 
214  cv::Scalar getSelectColor() const;
215 
217  virtual void setSelectColor(const cv::Scalar &value);
218 
220  std::shared_ptr<Widget> rmvFromLayout();
221 
223  enum Relief
224  {
225  FLAT,
226  RAISED,
227  SUNKEN,
228  SELECTED
229  };
230 
232  bool getIsSelectable() const;
233 
235  void setIsSelectable(bool value);
236 
238  int getForcedWidth() const;
239 
241  void setForcedWidth(int value);
242 
244  int getForcedHeight() const;
245 
247  void setForcedHeight(int value);
248 
250  void update();
251 
253  bool isRemoved() const;
254 
256  virtual const cv::Rect &getRect() = 0;
257 
259  bool getStretchXToParent() const;
260 
262  void setStretchXToParent(bool value);
263 
265  bool getStretchYToParent() const;
266 
268  void setStretchYToParent(bool value);
269 
270 protected:
271 
272  virtual bool isCompoundWidget() const;
273 
274  void mergeMats(cv::Mat &roiSrc, cv::Mat &roiDst);
275 
276  void paintRelief();
277 
278  Relief getRelief() const;
279  void setRelief(const Relief &value);
280 
281  cv::Rect getLayoutBoundaries() const;
282 
284  void setStateChangesBG();
285 
287  void allocateBG(const cv::Size &size);
288 
290  virtual void recalc() = 0;
291 
293  void flatWidget();
294 
296  void raisedWidget();
297 
299  void sunkenWidget();
300 
302  void selectedWidget();
303 
309  virtual void renderOn(cv::Mat &dst);
310 
312  virtual void drawFG(cv::Mat &dst) = 0;
313 
315  void callDrawFG(bool preAllocateMat=true);
316 
318  virtual const cv::Rect &getMinimalRect() = 0;
319 
320 
321  /* TODO - write/read widgets to file for a designer app
322  virtual void writeInternals(cv::FileStorage& fs) const = 0;
323  virtual void readInternals(const cv::FileNode& node) = 0;
324  */
325 
332  bool setDirty();
333 
334  bool getIsDirty() const;
335 
342  virtual bool isAtPos(const cv::Point &pos);
343 
344  State getState() const;
345 
346  Layout* getLayout();
347 
348  int id;
349  cv::Point location;
350  cv::Scalar selectColor;
351  Relief relief;
352  bool visible;
353  int thickness;
354  int lineType;
355  int forcedWidth;
356  int forcedHeight;
357  Anchor layoutAnchor;
358  Anchor flowAnchor;
359  bool stretchX;
360  bool stretchY;
361  bool stretchXToParent;
362  bool stretchYToParent;
363  bool isSelectable;
364  std::string statusMsg;
365  Layout *layout;
366 
367 private:
368  friend class Canvas;
369  friend class CompoundWidget;
370 
371  // All the layout implementations are friends
372  friend class LayoutBase;
373  friend class AutoLayout;
374  friend class VerticalLayout;
375  friend class HorizontalLayout;
376 
382  virtual void mousePressed();
383 
389  virtual void mouseReleased();
390 
396  virtual void mouseEnter();
397 
403  virtual void mouseLeave();
404 
405  int genId();
406 
407  void setLayout(Layout &value);
408 
409  void layoutResized(const cv::Rect &);
410 
412  virtual void broadcastChange(State status);
413 
414  /* TODO - write/read widgets to file for a designer app
415  friend void write(cv::FileStorage& fs, const std::string&, const Widget& x);
416  friend void read(const cv::FileNode& node, Widget*& x, const Widget* default_value);
417 
418  void write(cv::FileStorage& fs) const;
419  void read(const cv::FileNode& node);
420  */
421 
422  cv::Scalar outlineColor;
423  cv::Scalar fillColor;
424  cv::Mat bg;
425  cv::Mat fg;
426  State state;
427  bool isDirty;
428  int updateCalls;
429  std::list<CBWidgetState> changeNotifs;
430 };
431 
432 /* TODO - write/read widgets to file for a designer app
433 // These write and read functions must be defined for the serialization in cv::FileStorage to work
434 void write(cv::FileStorage& fs, const std::string&, const Widget& x);
435 void read(const cv::FileNode& node, Widget*& x, const Widget* default_value);
436 
437 // Will write the xml to memory and output it's std::string
438 std::ostream & operator<<(std::ostream& o, const Widget &widget);
439 */
440 
441 }
442 
443 #endif // WIDGET_H
Anchor getLayoutAnchor() const
getLayoutAnchor returns the anchor for using in the Layout we&#39;re in
Definition: widget.h:138
void setStateChangesBG()
widgets like buttons change bg on mouse events
virtual const std::string & getStatusMsg() const
getStatusMsg
uchar getAlpha() const
get the alpha value used for the widge background [0,255] => [transparent,opaque] ...
void setStatusMsg(const std::string &value)
setStatusMsg
void setForcedHeight(int value)
set the forced height for this widget (and calls setStretchY/setStretchYToParent(false)) ...
void setIsSelectable(bool value)
set if the widget should appear as selected when the mouse is over it
The VerticalLayout class.
Definition: verticallayout.h:42
void setForcedWidth(int value)
set the forced width for this widget (and calls setStretchX/setStretchXToParent(false)) ...
virtual void setSelectColor(const cv::Scalar &value)
set the color to use when a widget is selected
bool getVisible() const
is the widget visible
Definition: widget.h:83
bool getIsSelectable() const
get if the widget should appear as selected when the mouse is over it
int getId()
Widgets have a unique id per instance.
Definition: widget.h:171
bool isRemoved() const
returns true if the widget has no layout and false if it does
cv::Scalar getSelectColor() const
get the color to use when a widget is selected
int getForcedWidth() const
get the forced width for this widget
The LayoutBase class.
Definition: layoutbase.h:14
virtual void recalc()=0
update self so next call to &#39;draw&#39; will display correctly
cv::Scalar getOutlineColor() const
get the outline color
void selectedWidget()
delegate to Theme
std::shared_ptr< Widget > rmvFromLayout()
get rid of the widget
int getForcedHeight() const
get the forced height for this widget
top-right
Definition: widget.h:123
virtual bool isAtPos(const cv::Point &pos)
isAtPos
virtual void setLineType(int value)
set the line type (LINE_4, LINE_8, LINE_AA)
std::function< void(Widget *, State)> CBWidgetState
signature of a callback which gets the State
Definition: widget.h:45
The CompoundWidget class.
Definition: compoundwidget.h:18
bottom-left
Definition: widget.h:124
top-left
Definition: widget.h:122
Anchor
The Anchor enum.
Definition: widget.h:115
mouse left
Definition: widget.h:38
cv::Scalar getFillColor() const
get the bg color
int getThickness() const
get line thickness to use when drawing
virtual void setVisible(bool value)
set the widget visible state
Widget(const cv::Point &pos=cv::Point(0, 0))
constructor
int getLineType() const
get the line type (LINE_4, LINE_8, LINE_AA)
virtual const cv::Rect & getRect()=0
Actual size the widget is occupying due to Layout manager.
center-left
Definition: widget.h:128
void setFlowAnchor(const Anchor &value)
setFlowAnchor set internal widget alignment and direction of growth
virtual void drawFG(cv::Mat &dst)=0
dst is the roi of the widget size and not the full image
void setLayoutAnchor(const Anchor &value)
setLayoutAnchor sets the anchor for using in the Layout we&#39;re in
virtual void setThickness(int value)
set line thickness to use when drawing
Anchor getFlowAnchor() const
getFlowAnchor get internal widget alignment and direction of growth
Definition: widget.h:157
bool getStretchYToParent() const
get if this widget streches to parent layout height
void update()
removes &#39;dirty&#39; state and invokes the derived &#39;recalc/recalcCompound&#39;
void setStretchX(bool value)
set if the Layout we&#39;re in should stretch us in the X direction (and calls setStretchXToParent(false)...
bottom-right
Definition: widget.h:125
virtual const char * getType() const =0
getType is always implemented by derived to return the same static pointer per widget.
bool getStretchY() const
get if the Layout we&#39;re in should stretch us in the Y direction
void sunkenWidget()
delegate to Theme
bottom
Definition: widget.h:118
std::function< void(Widget *, int)> CBUserSelection
signature of a callback which gets a user selection
Definition: widget.h:48
virtual void translate(const cv::Point &translation)
move the widget
void setStretchXToParent(bool value)
set if this widget streches to parent layout width (and calls setStretchX(false)) ...
void setStretchY(bool value)
set if the Layout we&#39;re in should stretch us in the Y direction (and calls setStretchYToParent(false)...
got gocus
Definition: widget.h:35
top
Definition: widget.h:117
State
These are the widget states.
Definition: widget.h:33
The AutoLayout class.
Definition: autolayout.h:15
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
void callDrawFG(bool preAllocateMat=true)
helper method which delgates to drawFG for derived
virtual ~Widget()
virtual destructor
The HorizontalLayout class.
Definition: horizontallayout.h:37
bool getStretchX() const
get if the Layout we&#39;re in should stretch us in the X direction
const cv::Point & getLocation() const
get widget position in Canvas
center-top
Definition: widget.h:126
virtual void setFillColor(const cv::Scalar &value)
set the bg color
virtual void renderOn(cv::Mat &dst)
render the widget to dst
void setStretchYToParent(bool value)
set if this widget streches to parent layout height (and calls setStretchY(false)) ...
The Layout class.
Definition: layout.h:17
right
Definition: widget.h:120
Relief
Control the widget BG relieft style.
Definition: widget.h:223
void raisedWidget()
delegate to Theme
void flatWidget()
delegate to Theme
center-bottom
Definition: widget.h:127
center
Definition: widget.h:121
virtual void setLocation(const cv::Point &value)
set widget position in Canvas
void notifyOnChange(CBWidgetState cb)
used to register for notifications on a widget
void allocateBG(const cv::Size &size)
invokes Theme::allocateBG()
virtual const cv::Rect & getMinimalRect()=0
Minimal size the widget coould have occupy.
bool setDirty()
setDirty
virtual void setOutlineColor(const cv::Scalar &value)
set the outline color
The Widget class.
Definition: widget.h:28
left
Definition: widget.h:119
The Canvas class is the entry point into CanvasCV.
Definition: canvas.h:33
center-right
Definition: widget.h:129
std::function< void(Widget *)> CBWidget
signature of a callback which only gets the widget
Definition: widget.h:42
bool getStretchXToParent() const
get if this widget streches to parent layout width
lost focus
Definition: widget.h:36
void setAlpha(uchar value)
set the alpha value used for the widge background [0,255] => [transparent,opaque] ...
mouse pressed
Definition: widget.h:37