libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GtkCRNImage.h
Go to the documentation of this file.
1 /* Copyright 2010-2016 CoReNum, INSA-Lyon, ZHAO Xiaojuan, ENS-Lyon
2  *
3  * This file is part of libgtkcrnmm.
4  *
5  * libgtkcrnmm is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libgtkcrnmm is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libgtkcrnmm. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: GtkCRNImage.h
19  * \author Yann LEYDIER, ZHAO Xiaojuan
20  */
21 
22 #ifndef GtkCRNImage_HEADER
23 #define GtkCRNImage_HEADER
24 
25 #include <libgtkcrnmm_config.h>
26 #include <gtkmm.h>
29 #include <CRNGeometry/CRNRect.h>
30 #include <CRNStringUTF8.h>
31 #include <vector>
32 #include <iostream>
33 
34 namespace GtkCRN
35 {
43  class Image:
44 #ifdef CRN_USING_GTKMM3
45  public Gtk::Grid
46 #else
47  public Gtk::Table
48 #endif
49  {
50  public:
52  Image();
54  virtual ~Image() override;
55 
57 #ifndef CRN_USING_GTKMM3
58  void set_rulers_visible(bool is_visible);
59 #endif /* !CRN_USING_GTKMM3 */
60 
62  const crn::Point2DInt& get_offset() { return pos; }
64  void set_pixbuf(Glib::RefPtr<Gdk::Pixbuf> pb);
66  double get_zoom() const { return zoom; }
68  void set_zoom(double z);
70  void zoom_in();
72  void zoom_out();
74  void zoom_100();
76  void zoom_fit();
77 
79  void focus_on(int x, int y);
80 
82  void force_redraw() { need_recompute = true; }
83 
85 #ifdef CRN_USING_GTKMM3
86  void set_user_cursor(const Gdk::CursorType &cur);
87 #else /* CRN_USING_GTKMM3 */
88  void set_user_cursor(const Gdk::Cursor &cur);
89 #endif /* CRN_USING_GTKMM3 */
90 
91 #ifdef CRN_USING_GTKMM3
92 
101  Glib::RefPtr<Gio::SimpleActionGroup> get_actions() { return image_actions; }
102 #else
103 
112  Glib::RefPtr<Gtk::ActionGroup> get_actions() { return image_actions; }
113 #endif
114 
118  sigc::signal<void, crn::String, crn::String, MouseMode> signal_overlay_changed() { return overlay_changed; }
120  sigc::signal<void, guint, guint32, std::vector<std::pair<crn::String, crn::String> >, int, int > signal_rmb_clicked() { return rmb_clicked; }
122  sigc::signal<void, int, int> signal_user_mouse() { return user_mouse; }
124  sigc::signal<void, Glib::RefPtr<Gdk::Pixbuf> > signal_drawing() { return drawing; }
126  sigc::signal<void> signal_zoom_changed() { return zoom_changed; }
128  sigc::signal<void, int, int> signal_scrolled_event() { return scrolled_event; }
129 
131 #ifdef CRN_USING_GTKMM3
132  Glib::RefPtr<Gtk::Adjustment> get_hadjustment() { return hscrollbar.get_adjustment(); }
133 #else /* CRN_USING_GTKMM3 */
134  Gtk::Adjustment& get_hadjustment() { return *hscrollbar.get_adjustment(); }
135 #endif /* CRN_USING_GTKMM3 */
136 
137 #ifdef CRN_USING_GTKMM3
138  Glib::RefPtr<Gtk::Adjustment> get_vadjustment() { return vscrollbar.get_adjustment(); }
139 #else /* CRN_USING_GTKMM3 */
140  Gtk::Adjustment& get_vadjustment() { return *vscrollbar.get_adjustment(); }
141 #endif /* CRN_USING_GTKMM3 */
142 
144  enum class Overlay { None = 0, Rectangle, Point, Line, Text, User, Polygon };
146  struct OverlayItem
147  {
148  OverlayItem(const crn::StringUTF8& lab = ""):label(lab) {}
149  OverlayItem(const OverlayItem&) = delete;
150  OverlayItem(OverlayItem&&) = default;
151  OverlayItem& operator=(const OverlayItem&) = delete;
152  OverlayItem& operator=(OverlayItem&&) = default;
153  virtual ~OverlayItem() {}
155  };
156  struct Line: public OverlayItem
157  {
158  Line(const crn::Point2DInt &p1_, const crn::Point2DInt &p2_, const crn::StringUTF8& lab = ""): OverlayItem(lab), p1(p1_), p2(p2_) { }
159  Line(const Line&) = delete;
160  Line(Line&&) = default;
161  Line& operator=(const Line&) = delete;
162  Line& operator=(Line&&) = default;
164  };
165  struct Rectangle: public OverlayItem
166  {
167  Rectangle(const crn::Rect & r, const crn::StringUTF8& lab = ""): OverlayItem(lab), rect(r) { }
168  Rectangle(const Rectangle&) = delete;
169  Rectangle(Rectangle&&) = default;
170  Rectangle& operator=(const Rectangle&) = delete;
171  Rectangle& operator=(Rectangle&&) = default;
173  };
174  struct Point: public OverlayItem
175  {
176  Point(const crn::Point2DInt &p, const crn::StringUTF8& lab = ""): OverlayItem(lab), point(p) { }
177  Point(const Point&) = delete;
178  Point(Point&&) = default;
179  Point& operator=(const Point&) = delete;
180  Point& operator=(Point&&) = default;
182  };
183  struct Polygon: public OverlayItem
184  {
185  Polygon(const std::vector<crn::Point2DInt>& po, const crn::StringUTF8& lab = ""):OverlayItem(lab),points(po) { }
186  Polygon(std::vector<crn::Point2DInt> &&po, const crn::StringUTF8& lab = ""):OverlayItem(lab),points(std::move(po)) { }
187  Polygon(const Polygon&) = delete;
188  Polygon(Polygon&&) = delete;
189  Polygon& operator=(const Polygon&) = delete;
190  Polygon& operator=(Polygon&&) = delete;
191  std::vector<crn::Point2DInt> points;
192  };
193  struct Text: public OverlayItem
194  {
195  Text(const crn::Point2DInt &p, const crn::StringUTF8& lab = ""): OverlayItem(lab), pos(p) { }
196  Text(const Text&) = delete;
197  Text(Text&&) = delete;
198  Text& operator=(const Text&) = delete;
199  Text& operator=(Text&&) = delete;
201  };
204  {
206  OverlayConfig();
207 #ifdef CRN_USING_GTKMM3
208  Gdk::RGBA color1;
209  Gdk::RGBA color2;
210  Gdk::RGBA text_color;
211 #else
212  Gdk::Color color1;
213  Gdk::Color color2;
214  Gdk::Color text_color;
215 #endif
216  bool show_labels;
217  unsigned int text_size;
218  bool editable;
219  bool moveable;
220  bool can_jut_out;
221  bool fill;
222  double fill_alpha;
223  unsigned int cross_size;
224  bool draw_arrows;
225  unsigned int arrow_size;
227  Glib::ustring font_family;
229  bool show;
230  };
232  OverlayConfig& get_overlay_config(const crn::String &id) { return overlays[id].config; }
234  void show_overlay(const crn::String &id) { set_overlay_visible(id, true); }
236  void hide_overlay(const crn::String &id) { set_overlay_visible(id, false); }
238  void set_overlay_visible(const crn::String &id, bool visible);
240  void clear_overlays();
242  void clear_overlay(const crn::String &id);
244  void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, const crn::Rect &r, const crn::StringUTF8 &label = "");
246  void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, const crn::Point2DInt &point, const crn::StringUTF8 &label = "");
248  void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, const crn::Point2DInt &p1, const crn::Point2DInt &p2, const crn::StringUTF8 &label = "");
250  void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, const std::vector<crn::Point2DInt>& p, const crn::StringUTF8 &label="");
252  void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, std::vector<crn::Point2DInt> &&p, const crn::StringUTF8 &label="");
254  void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, const crn::StringUTF8 &label, const crn::Point2DInt &position);
256  OverlayItem& get_overlay_item(const crn::String &overlay_id, const crn::String &item_id);
258  const OverlayItem& get_overlay_item(const crn::String &overlay_id, const crn::String &item_id) const;
260  void remove_overlay_item(const crn::String &overlay_id, const crn::String &item_id);
261 
263  void set_selection_type(Overlay typ);
265  Overlay get_selection_type() const { return selection_type; }
269  bool has_selection() const;
271  void clear_selection();
273  static const crn::String& selection_overlay();
275  const crn::Rect& get_selection_as_rect() const;
279  std::pair<crn::Point2DInt, crn::Point2DInt> get_selection_as_line() const;
281  void set_selection(const crn::Rect &r);
283  void set_selection(const crn::Point2DInt &p);
285  void set_selection(const crn::Point2DInt &p1, const crn::Point2DInt &p2);
286 
287  private:
289 #ifdef CRN_USING_GTKMM3
290  bool expose(const Cairo::RefPtr<Cairo::Context> &cc);
291 #else /* CRN_USING_GTKMM3 */
292  bool expose(GdkEventExpose *ev);
293 #endif /* CRN_USING_GTKMM3 */
294 
295  bool configure(GdkEventConfigure *ev);
297  bool mouse_motion(GdkEventMotion *ev);
299  bool button_clicked(GdkEventButton *ev);
301  bool mouse_wheel(GdkEventScroll *ev);
302  MouseMode mouse_mode;
303  crn::Point2DDouble click_ref;
305  void scrolled();
307  bool refresh();
308  sigc::connection refresher;
310  bool need_redraw;
311  bool need_recompute;
312  Glib::RefPtr<Gdk::Pixbuf> image;
313  crn::Rect image_bounds;
314  crn::Point2DInt pos;
315  int dispw, disph;
316  double zoom;
317  Glib::RefPtr<Gdk::Pixbuf> buffer;
318  static int selection_margin;
319 #ifdef CRN_USING_GTKMM3
320  Glib::RefPtr<Gio::SimpleActionGroup> image_actions;
321 #else
322  Glib::RefPtr<Gtk::ActionGroup> image_actions;
323 #endif
324  crn::Point2DInt *movePoint;
325 
328  struct Overlay_internal
329  {
330  OverlayConfig config;
331  std::map<crn::String, std::unique_ptr<OverlayItem> > items;
332  };
333 
334  std::map<crn::String, Overlay_internal> overlays;
336  Overlay selection_type;
338  MouseMode find_selection_at(double mouse_x, double mouse_y, crn::String &overlay_id, crn::String &overlay_item_id);
340  void set_cursor_from_mode(MouseMode m);
341  crn::String selected_overlay;
342  crn::String selected_overlay_item;
344 #ifndef CRN_USING_GTKMM3
345  Gtk::HRuler hruler;
346  Gtk::VRuler vruler;
347 #endif /* !CRN_USING_GTKMM3 */
348  Gtk::DrawingArea da;
349 #ifndef CRN_USING_GTKMM3
350  Glib::RefPtr<Gdk::GC> da_gc;
351 #endif /* !CRN_USING_GTKMM3 */
352 #ifdef CRN_USING_GTKMM3
353  Gtk::Scrollbar hscrollbar;
354  Gtk::Scrollbar vscrollbar;
355 #else
356  Gtk::HScrollbar hscrollbar;
357  Gtk::VScrollbar vscrollbar;
358 #endif
359 #ifdef CRN_USING_GTKMM3
360  Gdk::CursorType scroll_cursor;
361  Gdk::CursorType select_cursor;
362  Gdk::CursorType move_cursor;
363  Gdk::CursorType move_1_cursor;
364  Gdk::CursorType drag_left_cursor;
365  Gdk::CursorType drag_bottom_left_cursor;
366  Gdk::CursorType drag_bottom_cursor;
367  Gdk::CursorType drag_bottom_right_cursor;
368  Gdk::CursorType drag_right_cursor;
369  Gdk::CursorType drag_top_right_cursor;
370  Gdk::CursorType drag_top_cursor;
371  Gdk::CursorType drag_top_left_cursor;
372  Gdk::CursorType user_cursor;
373 #else
374  Gdk::Cursor scroll_cursor;
375  Gdk::Cursor select_cursor;
376  Gdk::Cursor move_cursor;
377  Gdk::Cursor move_1_cursor;
378  Gdk::Cursor drag_left_cursor;
379  Gdk::Cursor drag_bottom_left_cursor;
380  Gdk::Cursor drag_bottom_cursor;
381  Gdk::Cursor drag_bottom_right_cursor;
382  Gdk::Cursor drag_right_cursor;
383  Gdk::Cursor drag_top_right_cursor;
384  Gdk::Cursor drag_top_cursor;
385  Gdk::Cursor drag_top_left_cursor;
386  Gdk::Cursor user_cursor;
387 #endif /* CRN_USING_GTKMM3 */
388  sigc::signal<void, crn::String, crn::String, MouseMode> overlay_changed;
389  sigc::signal<void, guint, guint32, std::vector<std::pair<crn::String, crn::String> >, int, int > rmb_clicked;
390  sigc::signal<void, int, int> user_mouse;
391  sigc::signal<void, Glib::RefPtr<Gdk::Pixbuf> > drawing;
392  sigc::signal<void> zoom_changed;
393  sigc::signal<void, int, int> scrolled_event;
395  };
396 }
397 
398 #endif
399 
400 
void clear_overlays()
Clears all overlays.
OverlayConfig()
Default constructor.
void set_overlay_visible(const crn::String &id, bool visible)
Shows or hides an overlay.
A 2D point class.
Polygon(std::vector< crn::Point2DInt > &&po, const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:186
Rectangle & operator=(const Rectangle &)=delete
An overlay item.
Definition: GtkCRNImage.h:146
void set_user_cursor(const Gdk::Cursor &cur)
Sets the cursor in user mouse mode.
Polygon & operator=(const Polygon &)=delete
OverlayConfig & get_overlay_config(const crn::String &id)
Gets the configuration of an overlay.
Definition: GtkCRNImage.h:232
sigc::signal< void, Glib::RefPtr< Gdk::Pixbuf > > signal_drawing()
Returns a signal that indicates that the image will be redrawn. Binds to void on_redraw(Glib::RefPtr<...
Definition: GtkCRNImage.h:124
sigc::signal< void, guint, guint32, std::vector< std::pair< crn::String, crn::String > >, int, int > signal_rmb_clicked()
Returns a signal that can be used to generate pop-up menus. Binds to void on_rmb_clicked(guint mouse_...
Definition: GtkCRNImage.h:120
OverlayItem & operator=(const OverlayItem &)=delete
void zoom_in()
Increments the zoom level by 10%.
sigc::signal< void > signal_zoom_changed()
Returns a signal that indicates that the zoom was changed. Binds to void on_zoom_changed().
Definition: GtkCRNImage.h:126
const crn::Rect & get_selection_as_rect() const
Gets the mouse selection as a rectangle.
Glib::RefPtr< Gtk::ActionGroup > get_actions()
Creates a tool button connected to the zoom_fit method.
Definition: GtkCRNImage.h:112
sigc::signal< void, int, int > signal_scrolled_event()
Returns a signal emitted when the image is scrolled. Binds to void on_scroll(int x, int y).
Definition: GtkCRNImage.h:128
Image()
Constructor.
Definition: GtkCRNImage.cpp:39
Text(const crn::Point2DInt &p, const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:195
std::vector< crn::Point2DInt > points
Definition: GtkCRNImage.h:191
Line(const crn::Point2DInt &p1_, const crn::Point2DInt &p2_, const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:158
void zoom_out()
Decrements the zoom level by 10%.
A UTF32 character string class.
Definition: CRNString.h:61
Overlay get_selection_type() const
Gets the mouse selection type.
Definition: GtkCRNImage.h:265
const crn::Point2DInt & get_selection_as_point() const
Gets the mouse selection as a point.
void zoom_100()
Sets the zoom level to 100%.
void set_selection_type(Overlay typ)
Sets the mouse selection type.
void hide_overlay(const crn::String &id)
Hides an overlay.
Definition: GtkCRNImage.h:236
const crn::Point2DInt & get_offset()
Returns to offset of the image (at scale 1:1)
Definition: GtkCRNImage.h:62
OverlayItem(const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:148
void clear_overlay(const crn::String &id)
Clears an overlay.
std::pair< crn::Point2DInt, crn::Point2DInt > get_selection_as_line() const
Gets the mouse selection as a line.
Text & operator=(const Text &)=delete
crn::Point2DInt p2
Definition: GtkCRNImage.h:163
OverlayConfig & get_selection_config()
Gets the mouse selection configuration.
Definition: GtkCRNImage.h:267
bool has_selection() const
Is there a mouse selection?
double get_zoom() const
Gets the zoom level.
Definition: GtkCRNImage.h:66
crn::Point2DInt p1
Definition: GtkCRNImage.h:163
virtual ~Image() override
Destructor.
Point & operator=(const Point &)=delete
void focus_on(int x, int y)
Focus the image on a point.
void add_overlay_item(const crn::String &overlay_id, const crn::String &item_id, const crn::Rect &r, const crn::StringUTF8 &label="")
Adds a rectangle to an overlay.
void show_overlay(const crn::String &id)
Shows an hidden overlay.
Definition: GtkCRNImage.h:234
Polygon(const std::vector< crn::Point2DInt > &po, const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:185
crn::Point2DInt point
Definition: GtkCRNImage.h:181
A widget that holds an image.
Definition: GtkCRNImage.h:43
crn::Point2DInt pos
Definition: GtkCRNImage.h:200
void set_rulers_visible(bool is_visible)
Are the rulers visible?
sigc::signal< void, int, int > signal_user_mouse()
Returns a signal that indicates a user mouse click. Binds to void on_mouse_clicked(int x...
Definition: GtkCRNImage.h:122
Gtk::Adjustment & get_hadjustment()
Gets the horizontal adjustment of the scrollbars.
Definition: GtkCRNImage.h:134
static const crn::String & selection_overlay()
Returns the name of the mouse selection overlay.
Gtk::Adjustment & get_vadjustment()
Gets the vertical adjustment of the scrollbars.
Definition: GtkCRNImage.h:140
void set_zoom(double z)
Sets the zoom level.
void force_redraw()
Force the image to redraw.
Definition: GtkCRNImage.h:82
MouseMode
State of the mouse activity.
Definition: GtkCRNImage.h:116
void set_pixbuf(Glib::RefPtr< Gdk::Pixbuf > pb)
Sets the new image to display.
void clear_selection()
Clears the mouse selection.
An overlay configuration.
Definition: GtkCRNImage.h:203
A character string class.
Definition: CRNStringUTF8.h:49
Line & operator=(const Line &)=delete
Rectangle(const crn::Rect &r, const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:167
sigc::signal< void, crn::String, crn::String, MouseMode > signal_overlay_changed()
Returns the signal associated to changes in selections. Binds to void on_overlay_changed(crn::String ...
Definition: GtkCRNImage.h:118
void zoom_fit()
Sets the zoom level to fit the image's size.
OverlayItem & get_overlay_item(const crn::String &overlay_id, const crn::String &item_id)
Gets an overlay item.
A 2D point class.
Definition: CRNPoint2DInt.h:39
void remove_overlay_item(const crn::String &overlay_id, const crn::String &item_id)
Removes an overlay item.
Point(const crn::Point2DInt &p, const crn::StringUTF8 &lab="")
Definition: GtkCRNImage.h:176
Overlay
Overlay types and mouse selection modes.
Definition: GtkCRNImage.h:144
void set_selection(const crn::Rect &r)
Sets the mouse selection.
A rectangle class.
Definition: CRNRect.h:46