Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MaskImageCtrl.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
10 /* This is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This software is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this software. If not, see
22  * <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #ifndef _MaskImageCtrl_H
27 #define _MaskImageCtrl_H
28 
29 #include <base_wx/wxImageCache.h>
30 #include <wx/overlay.h>
31 
32 class MaskEditorPanel;
33 
38 class MaskImageCtrl : public wxScrolledWindow
39 {
40 public:
45  { }
46 
47  bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTAB_TRAVERSAL, const wxString& name = wxT("panel"));
48 
49  void Init(MaskEditorPanel * parent);
50 
56 
58  void setPreviewOnly() { m_previewOnly=true; };
60  void setImage (const std::string & filename, HuginBase::MaskPolygonVector newMask, HuginBase::MaskPolygonVector masksToDraw, ImageRotation rot);
64  void setCrop(HuginBase::SrcPanoImage::CropMode newCropMode,vigra::Rect2D newCropRect, bool isCentered, hugin_utils::FDiff2D center, bool isCircleCrop);
66  vigra::Rect2D getCrop() { return m_cropRect; };
68  void setActiveMask(unsigned int newMask, bool doUpdate=true);
72  void selectAllMarkers();
73 
75  void OnMouseMove(wxMouseEvent& mouse);
77  void OnLeftMouseDown(wxMouseEvent& mouse);
79  void OnLeftMouseUp(wxMouseEvent& mouse);
81  void OnLeftMouseDblClick(wxMouseEvent& mouse);
83  void OnRightMouseDown(wxMouseEvent& mouse);
85  void OnRightMouseUp(wxMouseEvent& mouse);
87  void OnMiddleMouseDown(wxMouseEvent& mouse);
89  void OnMiddleMouseUp(wxMouseEvent& mouse);
91  void OnKeyUp(wxKeyEvent &e);
93  void OnChar(wxKeyEvent& e);
96  void OnCaptureLost(wxMouseCaptureLostEvent &e);
98  void OnKillFocus(wxFocusEvent &e);
100  void OnScroll(wxScrollWinEvent &e);
101 
103  void startNewPolygon();
105  wxSize DoGetBestSize() const;
106 
108  void SetMaskMode(bool newMaskMode);
109 
114  void setScale(double factor);
115 
117  double getScale()
118  { return m_fitToWindow ? 0 : m_scaleFactor; }
119 
122 
124  void setDrawingActiveMasks(bool newDrawActiveMasks);
126  void update();
127 
129  void SetUserColourPolygonNegative(wxColour newColour) { m_colour_polygon_negative=newColour; };
130  void SetUserColourPolygonPositive(wxColour newColour) { m_colour_polygon_positive=newColour; };
131  void SetUserColourPointSelected(wxColour newColour) { m_colour_point_selected=newColour; };
132  void SetUserColourPointUnselected(wxColour newColour) { m_colour_point_unselected=newColour; };
133 
135  virtual void OnDraw(wxDC& dc);
136 protected:
138  void OnSize(wxSizeEvent & e);
139 
141  double getScaleFactor() const;
143  double calcAutoScaleFactor(wxSize size);
145  void rescaleImage();
146 
147  private:
148 
149  //scaled bitmap
150  wxBitmap m_bitmap;
152  //filename of current editing file
153  std::string m_imageFilename;
154  // stores rotation of image
156  // size of displayed (probably scaled) image
157  wxSize m_imageSize;
158  // size of real image
159  wxSize m_realSize;
160  // variables for crop
162  vigra::Rect2D m_cropRect;
166  // draw active masks
168  // mask or crop mode
170 
172  int scale(int x) const
173  { return (int) (x * getScaleFactor() + 0.5); }
174 
175  double scale(double x) const
176  { return x * getScaleFactor(); }
177 
179  int transform(int x) const
180  { return (int) ((x+HuginBase::maskOffset) * getScaleFactor() + 0.5); }
181 
182  double transform(double x) const
183  { return (x+HuginBase::maskOffset) * getScaleFactor(); }
184 
185  wxPoint transform(const hugin_utils::FDiff2D & p) const
186  {
187  wxPoint r;
188  r.x = transform(p.x);
189  r.y = transform(p.y);
190  return r;
191  };
192 
194  int invtransform(int x) const
195  { return (int) (x/getScaleFactor()-HuginBase::maskOffset + 0.5); };
196 
197  double invtransform(double x) const
198  { return (x/getScaleFactor()-HuginBase::maskOffset); };
199 
200  hugin_utils::FDiff2D invtransform(const wxPoint & p) const
201  {
203  r.x = invtransform(p.x);
204  r.y = invtransform(p.y);
205  return r;
206  };
207 
208  // rotate coordinate to fit possibly rotated image display
209  // useful for drawing something on the rotated display
210  template <class T>
211  T applyRot(const T & p) const
212  {
213  switch (m_imgRotation) {
214  case ROT0:
215  return p;
216  break;
217  case ROT90:
218  return T(m_realSize.GetHeight()-1 - p.y, p.x);
219  break;
220  case ROT180:
221  return T(m_realSize.GetWidth()-1 - p.x, m_realSize.GetHeight()-1 - p.y);
222  break;
223  case ROT270:
224  return T(p.y, m_realSize.GetWidth()-1 - p.x);
225  break;
226  default:
227  return p;
228  break;
229  }
230  }
231 
232  // rotate coordinate to fit possibly rotated image display
233  // useful for converting rotated display coordinates to image coordinates
234  template <class T>
235  T applyRotInv(const T & p) const
236  {
237  switch (m_imgRotation) {
238  case ROT90:
239  return T(p.y, m_realSize.GetHeight()-1 - p.x);
240  break;
241  case ROT180:
242  return T(m_realSize.GetWidth()-1 - p.x, m_realSize.GetHeight()-1 - p.y);
243  break;
244  case ROT270:
245  return T(m_realSize.GetWidth()-1 - p.y, p.x);
246  break;
247  case ROT0:
248  default:
249  return p;
250  break;
251  }
252  }
253 
254  //draw the given polygon
255  void DrawPolygon(wxDC &dc, HuginBase::MaskPolygon poly, bool isSelected, bool drawMarker);
256  //draw a selection rectange, when called the second time the rectangle is deleted
257  void DrawSelectionRectangle();
258  // draws the crop rectangle and/or circle
259  void DrawCrop(wxDC & dc);
260  void DrawCrop();
261  // find the polygon for which the point p is inside the polygon
263  // returns a set of points which are in the selection rectangle
264  bool SelectPointsInsideMouseRect(HuginBase::UIntSet &points,const bool considerSelectedOnly);
265  // updates the crop
266  void UpdateCrop(hugin_utils::FDiff2D delta);
267  // clears the overlay
268  void ClearOverlay();
269 
270  // where the cursor is
271  enum ClickPos
272  {
274  };
275  // test, where the curos is
276  ClickPos GetClickPos(vigra::Point2D pos);
277 
280  {
281  NO_IMAGE=0, // no image selected
282  NO_MASK, // image loaded, but none active mask
283  NO_SELECTION, // active polygon, but no point selected
284  POINTS_SELECTED, // points selected
285  POINTS_MOVING, // dragging points
286  POINTS_DELETING, // remove points inside rect
287  POINTS_ADDING, // adding new points add mouse position
288  POLYGON_SELECTING, // selecting an region to select another polygon
289  REGION_SELECTING, // currently selecting an region
290  NEW_POLYGON_STARTED, // modus is new polygon, but no point setted yet
291  NEW_POLYGON_CREATING, // currently creating new polygon
292  CROP_SHOWING, // only showing the crop
293  CROP_MOVING, // dragging crop
294  CROP_CIRCLE_SCALING, // circular crop changing
295  CROP_LEFT_MOVING, // dragging left border of crop
296  CROP_RIGHT_MOVING, // dragging right border of crop
297  CROP_TOP_MOVING, // dragging top border of crop
298  CROP_BOTTOM_MOVING // dragging bottom border of crop
299  };
301 
305 
309  unsigned int m_activeMask;
310  // the active masks, the one which is currently editing
312  // all selected points
314 
315  ImageCache::EntryPtr m_img;
316 
317  // positions of mouse drag
318  wxPoint m_dragStartPos;
319  wxPoint m_currentPos;
320  // variable for saving scrolling state
322  wxPoint m_scrollPos;
323 
324  // colours for different parts
330  wxOverlay m_overlay;
332 
334  DECLARE_DYNAMIC_CLASS(MaskImageCtrl)
335 };
336 
338 class MaskImageCtrlXmlHandler : public wxXmlResourceHandler
339 {
340  DECLARE_DYNAMIC_CLASS(MaskImageCtrlXmlHandler)
341 
342 public:
344  virtual wxObject *DoCreateResource();
345  virtual bool CanHandle(wxXmlNode *node);
346 };
347 
348 
349 #endif // _MaskImageCtrl_H
bool m_showActiveMasks
ImageRotation
image rotation.
Definition: MaskImageCtrl.h:55
T applyRotInv(const T &p) const
wxPoint m_currentPos
wxColour m_color_selection
void OnKeyUp(wxKeyEvent &e)
event handler for keyboard
wxBitmap m_bitmap
wxOverlay m_overlay
HuginBase::SrcPanoImage::CropMode m_cropMode
const int maskOffset
polygon can exceed the image maximal maskOffset pixels in each direction bigger polygons will be clip...
Definition: Mask.h:44
void setActiveMask(unsigned int newMask, bool doUpdate=true)
mark mask with image as beeing editing
bool SelectPointsInsideMouseRect(HuginBase::UIntSet &points, const bool considerSelectedOnly)
void SetUserColourPointUnselected(wxColour newColour)
MaskImageCtrl()
ctor.
Definition: MaskImageCtrl.h:43
MaskEditorPanel * m_editPanel
std::string m_imageFilename
virtual wxObject * DoCreateResource()
hugin_utils::FDiff2D m_cropCenter
mask editor panel.
vigra::Rect2D getCrop()
returns the current crop rect
Definition: MaskImageCtrl.h:66
void rescaleImage()
rescale the image
void OnLeftMouseDblClick(wxMouseEvent &mouse)
event handler for left double click
wxColor m_colour_point_selected
void setPreviewOnly()
if called, the mouse handlers are deactivated
Definition: MaskImageCtrl.h:58
HuginBase::MaskPolygonVector m_imageMask
wxPoint m_scrollPos
void SetUserColourPolygonNegative(wxColour newColour)
sets the colour for different parts
hugin_utils::FDiff2D invtransform(const wxPoint &p) const
void SetUserColourPointSelected(wxColour newColour)
mask editor
Definition: MaskImageCtrl.h:38
void OnMouseMove(wxMouseEvent &mouse)
event handler when mouse is moving
std::set< unsigned int > UIntSet
Definition: PanoramaData.h:51
void OnRightMouseUp(wxMouseEvent &mouse)
event handler when right mouse button is released
double transform(double x) const
xrc handler for mask editor
void setImage(const std::string &filename, HuginBase::MaskPolygonVector newMask, HuginBase::MaskPolygonVector masksToDraw, ImageRotation rot)
set the current image and mask list, this loads also the image from cache
int invtransform(int x) const
translate screen coordinates to image coordinates, considers additional added border ...
double scale(double x) const
HuginBase::MaskPolygonVector getNewMask() const
returns the vector of all mask (including new created mask)
Definition: MaskImageCtrl.h:70
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name=wxT("panel"))
void update()
initiate redraw
void OnRightMouseDown(wxMouseEvent &mouse)
event handler when right mouse button is pressed
double calcAutoScaleFactor(wxSize size)
calculate new scale factor for this image
ImageRotation getCurrentRotation()
returns the current rotation of displayed image
double getScale()
return scale factor, 0 for autoscale
void OnSize(wxSizeEvent &e)
handler called when size of control was changed
wxPoint transform(const hugin_utils::FDiff2D &p) const
int scale(int x) const
scale of width/height
void OnCaptureLost(wxMouseCaptureLostEvent &e)
event handler, when mouse capture is lost, e.g.
double getScaleFactor() const
get scale factor (calculates factor when fit to window is active)
wxSize m_imageSize
std::vector< MaskPolygon > MaskPolygonVector
Definition: Mask.h:147
void OnKillFocus(wxFocusEvent &e)
event handler, when editor lost focus, mainly cancels creating new polygon
void DrawSelectionRectangle()
void selectAllMarkers()
select all points of active mask
void setNewMasks(HuginBase::MaskPolygonVector newMasks, HuginBase::MaskPolygonVector masksToDraw)
updates masks for currently selected image
double invtransform(double x) const
void OnMiddleMouseDown(wxMouseEvent &mouse)
event handler for middle mouse button, start scrolling
ImageRotation m_imgRotation
void OnLeftMouseUp(wxMouseEvent &mouse)
event handler when right mouse button is released
bool m_middleMouseScroll
void setScale(double factor)
set the scaling factor for mask editing display.
MaskEditorState m_maskEditState
double m_scaleFactor
ClickPos GetClickPos(vigra::Point2D pos)
void Init(MaskEditorPanel *parent)
void SetMaskMode(bool newMaskMode)
sets the control to mask (newMaskMode=true) or crop (newMaskMode=false) mode
void DrawPolygon(wxDC &dc, HuginBase::MaskPolygon poly, bool isSelected, bool drawMarker)
HuginBase::UIntSet m_selectedPoints
ImageCache::EntryPtr m_img
wxColor m_colour_polygon_positive
void SetUserColourPolygonPositive(wxColour newColour)
void OnScroll(wxScrollWinEvent &e)
event handler for remember scroll position
T applyRot(const T &p) const
void startNewPolygon()
starts creating a new polygon
wxBitmap m_disabledBitmap
unsigned int m_activeMask
wxColor m_colour_polygon_negative
wxColor m_colour_point_unselected
wxSize DoGetBestSize() const
returns size of currently scaled image
void FindPolygon(hugin_utils::FDiff2D p)
void setCrop(HuginBase::SrcPanoImage::CropMode newCropMode, vigra::Rect2D newCropRect, bool isCentered, hugin_utils::FDiff2D center, bool isCircleCrop)
updates the crop mode and crop rect
int transform(int x) const
convert image coordinate to screen coordinates, considers additional added border ...
HuginBase::MaskPolygonVector m_masksToDraw
vigra::Rect2D m_cropRect
wxPoint m_dragStartPos
MaskEditorState
different states of the editor
void UpdateCrop(hugin_utils::FDiff2D delta)
virtual bool CanHandle(wxXmlNode *node)
void OnMiddleMouseUp(wxMouseEvent &mouse)
event handler for middle mouse button, end scrolling
virtual void OnDraw(wxDC &dc)
drawing routine
void OnLeftMouseDown(wxMouseEvent &mouse)
event handler when left mouse button is pressed
base class, which stores one mask polygon
Definition: Mask.h:52
void OnChar(wxKeyEvent &e)
event handler for scrolling with keyboard
void setDrawingActiveMasks(bool newDrawActiveMasks)
set if active masks should be drawn
HuginBase::MaskPolygon m_editingMask