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 = "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);
125 
127  void SetUserColourPolygonNegative(wxColour newColour) { m_colour_polygon_negative=newColour; };
128  void SetUserColourPolygonPositive(wxColour newColour) { m_colour_polygon_positive=newColour; };
129  void SetUserColourPointSelected(wxColour newColour) { m_colour_point_selected=newColour; };
130  void SetUserColourPointUnselected(wxColour newColour) { m_colour_point_unselected=newColour; };
131 
133  void OnPaint(wxPaintEvent& e);
134 protected:
136  void OnSize(wxSizeEvent & e);
137 
139  double getScaleFactor() const;
141  double calcAutoScaleFactor(wxSize size);
143  void rescaleImage();
144 
145  private:
146 
147  //scaled bitmap
148  wxBitmap m_bitmap;
150  //filename of current editing file
151  std::string m_imageFilename;
152  // stores rotation of image
154  // size of displayed (probably scaled) image
155  wxSize m_imageSize;
156  // size of real image
157  wxSize m_realSize;
158  // variables for crop
160  vigra::Rect2D m_cropRect;
164  // draw active masks
166  // mask or crop mode
168 
170  int scale(int x) const
171  { return (int) (x * getScaleFactor() + 0.5); }
172 
173  double scale(double x) const
174  { return x * getScaleFactor(); }
175 
177  int transform(int x) const
178  { return (int) ((x+HuginBase::maskOffset) * getScaleFactor() + 0.5); }
179 
180  double transform(double x) const
181  { return (x+HuginBase::maskOffset) * getScaleFactor(); }
182 
183  wxPoint transform(const hugin_utils::FDiff2D & p) const
184  {
185  wxPoint r;
186  r.x = transform(p.x);
187  r.y = transform(p.y);
188  return r;
189  };
190 
192  int invtransform(int x) const
193  { return (int) (x/getScaleFactor()-HuginBase::maskOffset + 0.5); };
194 
195  double invtransform(double x) const
196  { return (x/getScaleFactor()-HuginBase::maskOffset); };
197 
198  hugin_utils::FDiff2D invtransform(const wxPoint & p) const
199  {
201  r.x = invtransform(p.x);
202  r.y = invtransform(p.y);
203  return r;
204  };
205 
206  // rotate coordinate to fit possibly rotated image display
207  // useful for drawing something on the rotated display
208  template <class T>
209  T applyRot(const T & p) const
210  {
211  switch (m_imgRotation) {
212  case ROT0:
213  return p;
214  break;
215  case ROT90:
216  return T(m_realSize.GetHeight()-1 - p.y, p.x);
217  break;
218  case ROT180:
219  return T(m_realSize.GetWidth()-1 - p.x, m_realSize.GetHeight()-1 - p.y);
220  break;
221  case ROT270:
222  return T(p.y, m_realSize.GetWidth()-1 - p.x);
223  break;
224  default:
225  return p;
226  break;
227  }
228  }
229 
230  // rotate coordinate to fit possibly rotated image display
231  // useful for converting rotated display coordinates to image coordinates
232  template <class T>
233  T applyRotInv(const T & p) const
234  {
235  switch (m_imgRotation) {
236  case ROT90:
237  return T(p.y, m_realSize.GetHeight()-1 - p.x);
238  break;
239  case ROT180:
240  return T(m_realSize.GetWidth()-1 - p.x, m_realSize.GetHeight()-1 - p.y);
241  break;
242  case ROT270:
243  return T(m_realSize.GetWidth()-1 - p.y, p.x);
244  break;
245  case ROT0:
246  default:
247  return p;
248  break;
249  }
250  }
251 
252  //draw the given polygon
253  void DrawPolygon(wxDC &dc, HuginBase::MaskPolygon poly, bool isSelected, bool drawMarker);
254  //draw a selection rectange, when called the second time the rectangle is deleted
255  void DrawSelectionRectangle();
256  // draws the crop rectangle and/or circle
257  void DrawCrop(wxDC & dc);
258  // find the polygon for which the point p is inside the polygon
260  // returns a set of points which are in the selection rectangle
261  bool SelectPointsInsideMouseRect(HuginBase::UIntSet &points,const bool considerSelectedOnly);
262  // updates the crop
263  void UpdateCrop(hugin_utils::FDiff2D delta);
264 
265  // where the cursor is
266  enum ClickPos
267  {
269  };
270  // test, where the curos is
271  ClickPos GetClickPos(vigra::Point2D pos);
272 
275  {
276  NO_IMAGE=0, // no image selected
277  NO_MASK, // image loaded, but none active mask
278  NO_SELECTION, // active polygon, but no point selected
279  POINTS_SELECTED, // points selected
280  POINTS_MOVING, // dragging points
281  POINTS_DELETING, // remove points inside rect
282  POINTS_ADDING, // adding new points add mouse position
283  POLYGON_SELECTING, // selecting an region to select another polygon
284  REGION_SELECTING, // currently selecting an region
285  NEW_POLYGON_STARTED, // modus is new polygon, but no point setted yet
286  NEW_POLYGON_CREATING, // currently creating new polygon
287  CROP_SHOWING, // only showing the crop
288  CROP_MOVING, // dragging crop
289  CROP_CIRCLE_SCALING, // circular crop changing
290  CROP_LEFT_MOVING, // dragging left border of crop
291  CROP_RIGHT_MOVING, // dragging right border of crop
292  CROP_TOP_MOVING, // dragging top border of crop
293  CROP_BOTTOM_MOVING // dragging bottom border of crop
294  };
296 
300 
304  unsigned int m_activeMask;
305  // the active masks, the one which is currently editing
307  // all selected points
309 
310  ImageCache::EntryPtr m_img;
311 
312  // positions of mouse drag
313  wxPoint m_dragStartPos;
314  wxPoint m_currentPos;
315  // variable for saving scrolling state
317  wxPoint m_scrollPos;
318 
319  // colours for different parts
325  wxOverlay m_overlay;
327 
328  DECLARE_DYNAMIC_CLASS(MaskImageCtrl)
329 };
330 
332 class MaskImageCtrlXmlHandler : public wxXmlResourceHandler
333 {
334  DECLARE_DYNAMIC_CLASS(MaskImageCtrlXmlHandler)
335 
336 public:
338  virtual wxObject *DoCreateResource();
339  virtual bool CanHandle(wxXmlNode *node);
340 };
341 
342 
343 #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 OnPaint(wxPaintEvent &e)
drawing routine
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
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
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name="panel")
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
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
void DrawCrop(wxDC &dc)
HuginBase::MaskPolygon m_editingMask