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 OnMouseWheel(wxMouseEvent& mouse);
93  void OnKeyUp(wxKeyEvent &e);
95  void OnChar(wxKeyEvent& e);
98  void OnCaptureLost(wxMouseCaptureLostEvent &e);
100  void OnKillFocus(wxFocusEvent &e);
102  void OnScroll(wxScrollWinEvent &e);
103 
105  void startNewPolygon();
107  wxSize DoGetBestSize() const override;
108 
110  void SetMaskMode(bool newMaskMode);
111 
116  void setScale(double factor);
117 
119  double getScale()
120  { return m_fitToWindow ? 0 : m_scaleFactor; }
121 
124 
126  void setDrawingActiveMasks(bool newDrawActiveMasks);
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  void OnPaint(wxPaintEvent& e);
136 protected:
138  void OnSize(wxSizeEvent & e);
140  void OnSizeFinished(wxTimerEvent& e);
141 
143  double getScaleFactor() const;
145  double calcAutoScaleFactor(wxSize size);
147  void rescaleImage();
148 
149  private:
150 
151  //scaled bitmap
152  wxBitmap m_bitmap;
154  //filename of current editing file
155  std::string m_imageFilename;
156  // stores rotation of image
158  // size of displayed (probably scaled) image
159  wxSize m_imageSize;
160  // size of real image
161  wxSize m_realSize;
162  // variables for crop
164  vigra::Rect2D m_cropRect;
168  // draw active masks
170  // mask or crop mode
172  // timer to resize image only once at end of resize
173  wxTimer m_resizeTimer;
174 
176  int scale(int x) const
177  { return (int) (x * getScaleFactor() + 0.5); }
178 
179  double scale(double x) const
180  { return x * getScaleFactor(); }
181 
183  int transform(int x) const
184  { return (int) ((x+HuginBase::maskOffset) * getScaleFactor() + 0.5); }
185 
186  double transform(double x) const
187  { return (x+HuginBase::maskOffset) * getScaleFactor(); }
188 
189  wxPoint transform(const hugin_utils::FDiff2D & p) const
190  {
191  wxPoint r;
192  r.x = transform(p.x);
193  r.y = transform(p.y);
194  return r;
195  };
196 
198  int invtransform(int x) const
199  { return (int) (x/getScaleFactor()-HuginBase::maskOffset + 0.5); };
200 
201  double invtransform(double x) const
202  { return (x/getScaleFactor()-HuginBase::maskOffset); };
203 
204  hugin_utils::FDiff2D invtransform(const wxPoint & p) const
205  {
207  r.x = invtransform(p.x);
208  r.y = invtransform(p.y);
209  return r;
210  };
211 
212  // rotate coordinate to fit possibly rotated image display
213  // useful for drawing something on the rotated display
214  template <class T>
215  T applyRot(const T & p) const
216  {
217  switch (m_imgRotation) {
218  case ROT0:
219  return p;
220  break;
221  case ROT90:
222  return T(m_realSize.GetHeight()-1 - p.y, p.x);
223  break;
224  case ROT180:
225  return T(m_realSize.GetWidth()-1 - p.x, m_realSize.GetHeight()-1 - p.y);
226  break;
227  case ROT270:
228  return T(p.y, m_realSize.GetWidth()-1 - p.x);
229  break;
230  default:
231  return p;
232  break;
233  }
234  }
235 
236  // rotate coordinate to fit possibly rotated image display
237  // useful for converting rotated display coordinates to image coordinates
238  template <class T>
239  T applyRotInv(const T & p) const
240  {
241  switch (m_imgRotation) {
242  case ROT90:
243  return T(p.y, m_realSize.GetHeight()-1 - p.x);
244  break;
245  case ROT180:
246  return T(m_realSize.GetWidth()-1 - p.x, m_realSize.GetHeight()-1 - p.y);
247  break;
248  case ROT270:
249  return T(m_realSize.GetWidth()-1 - p.y, p.x);
250  break;
251  case ROT0:
252  default:
253  return p;
254  break;
255  }
256  }
257 
258  //draw the given polygon
259  void DrawPolygon(wxDC &dc, HuginBase::MaskPolygon poly, bool isSelected, bool drawMarker);
260  //draw a selection rectange, when called the second time the rectangle is deleted
261  void DrawSelectionRectangle();
262  // draws the crop rectangle and/or circle
263  void DrawCrop(wxDC & dc);
264  // find the polygon for which the point p is inside the polygon
266  // returns a set of points which are in the selection rectangle
267  bool SelectPointsInsideMouseRect(HuginBase::UIntSet &points,const bool considerSelectedOnly);
268  // updates the crop
269  void UpdateCrop(hugin_utils::FDiff2D delta);
270 
271  // where the cursor is
272  enum ClickPos
273  {
275  };
276  // test, where the curos is
277  ClickPos GetClickPos(vigra::Point2D pos);
278 
281  {
282  NO_IMAGE=0, // no image selected
283  NO_MASK, // image loaded, but none active mask
284  NO_SELECTION, // active polygon, but no point selected
285  POINTS_SELECTED, // points selected
286  POINTS_MOVING, // dragging points
287  POINTS_DELETING, // remove points inside rect
288  POINTS_ADDING, // adding new points add mouse position
289  POLYGON_SELECTING, // selecting an region to select another polygon
290  REGION_SELECTING, // currently selecting an region
291  NEW_POLYGON_STARTED, // modus is new polygon, but no point setted yet
292  NEW_POLYGON_CREATING, // currently creating new polygon
293  CROP_SHOWING, // only showing the crop
294  CROP_MOVING, // dragging crop
295  CROP_CIRCLE_SCALING, // circular crop changing
296  CROP_LEFT_MOVING, // dragging left border of crop
297  CROP_RIGHT_MOVING, // dragging right border of crop
298  CROP_TOP_MOVING, // dragging top border of crop
299  CROP_BOTTOM_MOVING // dragging bottom border of crop
300  };
302 
306 
310  unsigned int m_activeMask;
311  // the active masks, the one which is currently editing
313  // all selected points
315 
316  ImageCache::EntryPtr m_img;
317 
318  // positions of mouse drag
319  wxPoint m_dragStartPos;
320  wxPoint m_currentPos;
321  // variable for saving scrolling state
323  wxPoint m_scrollPos;
324 
325  // colours for different parts
331  wxOverlay m_overlay;
333 
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 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
void OnMouseWheel(wxMouseEvent &mouse)
event handler for mouse wheel
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 DoGetBestSize() const override
returns size of currently scaled image
wxSize m_imageSize
std::vector< MaskPolygon > MaskPolygonVector
Definition: Mask.h:150
void OnKillFocus(wxFocusEvent &e)
event handler, when editor lost focus, mainly cancels creating new polygon
void OnSizeFinished(wxTimerEvent &e)
handler called at end of all resize
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
wxTimer m_resizeTimer
void startNewPolygon()
starts creating a new polygon
wxBitmap m_disabledBitmap
unsigned int m_activeMask
wxColor m_colour_polygon_negative
wxColor m_colour_point_unselected
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