PreviewCropTool.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00023 #include "panoinc_WX.h"
00024 #include "panoinc.h"
00025 #include "PreviewCropTool.h"
00026 #include <config.h>
00027 #include "base_wx/platform.h"
00028 #include "hugin/config_defaults.h"
00029 #include "CommandHistory.h"
00030 #include "wxPanoCommand.h"
00031 
00032 #include <wx/platform.h>
00033 #ifdef __WXMAC__
00034 #include <OpenGL/gl.h>
00035 #else
00036 #include <GL/gl.h>
00037 #endif
00038 
00039 PreviewCropTool::PreviewCropTool(PreviewToolHelper *helper)
00040     : PreviewTool(helper)
00041 {
00042     
00043 }
00044 
00045 void PreviewCropTool::Activate()
00046 {
00047     helper->NotifyMe(PreviewToolHelper::MOUSE_MOVE, this);
00048     helper->NotifyMe(PreviewToolHelper::MOUSE_PRESS, this);
00049     helper->NotifyMe(PreviewToolHelper::REALLY_DRAW_OVER_IMAGES, this);
00050     mouse_down = false;
00051     moving_left = false;
00052     moving_right = false;
00053     moving_top = false;
00054     moving_bottom = false;
00055     helper->SetStatusMessage(_("Drag the inside of the cropping rectangle to adjust the crop."));
00056 }
00057 
00058 void PreviewCropTool::ReallyAfterDrawImagesEvent()
00059 {
00060     // draw lines for the border to crop:
00061     // We use 1/4 of the smallest dimension as the size of an internal margin
00062     // inside the cropping region.
00063     // dragging a point in that margin moves the edges for the side we are on
00064     // (so the corners move both edges)
00065     // dragging a point in the middle moves the whole frame.
00066     
00067     // find the cropped region
00068     HuginBase::PanoramaOptions *opts = helper->GetVisualizationStatePtr()->getViewState()->GetOptions();
00069     vigra::Rect2D roi = opts->getROI();
00070     double width = (double) roi.width(),
00071           height = (double) roi.height(),
00072           margin = (width > height ? height : width) / 4.0;
00073     top = (double) roi.top() + margin;
00074     bottom = (double) roi.bottom() - margin;
00075     left = (double) roi.left() + margin;
00076     right = (double) roi.right() - margin;
00077    
00078     // now draw boxes to indicate what dragging would do.
00079     if (!mouse_down)
00080     {
00081         glEnable(GL_BLEND);
00082         helper->GetVisualizationStatePtr()->getViewState()->GetTextureManager()->DisableTexture();
00083         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00084         glColor4f(1.0, 1.0, 1.0, 0.38197);
00085         glBegin(GL_QUADS);
00086             if (moving_left)
00087             {
00088                 glVertex2d(roi.left(), top); glVertex2d(left, top);
00089                 glVertex2d(left, bottom); glVertex2d(roi.left(), bottom);
00090             }
00091             if (moving_right)
00092             {
00093                 glVertex2d(right, top); glVertex2d(roi.right(), top);
00094                 glVertex2d(roi.right(), bottom); glVertex2d(right, bottom);
00095             }
00096             if (moving_top)
00097             {
00098                 glVertex2d(right, top); glVertex2d(right, roi.top());
00099                 glVertex2d(left, roi.top()); glVertex2d(left, top);
00100             }
00101             if (moving_bottom)
00102             {
00103                 glVertex2d(right, bottom); glVertex2d(right, roi.bottom());
00104                 glVertex2d(left, roi.bottom()); glVertex2d(left, bottom);
00105             }
00106         glEnd();
00107         glEnable(GL_TEXTURE_2D);
00108         glDisable(GL_BLEND);
00109     } else {
00110         // while dragging, reset the displayed ROI to ours incase something else
00111         // tries to redraw the preview with the panorama's real ROI.
00112         opts->setROI(new_roi);
00113         helper->GetViewStatePtr()->SetOptions(opts);
00114     }
00115 }
00116 
00117 void PreviewCropTool::MouseMoveEvent(double x, double y, wxMouseEvent & e)
00118 {
00119 
00120 //    std::cout << "outlines tool " << x << " " << y << std::endl;
00121 //    double xp, yp;
00122 //    HuginBase::PTools::Transform transform;
00123 //    HuginBase::SrcPanoImage image;
00124 //    image.setSize(vigra::Size2D(360,180));
00125 //    image.setHFOV(360);
00126 //    image.setProjection(HuginBase::BaseSrcPanoImage::EQUIRECTANGULAR);
00127 //    if (helper->GetPanoramaPtr()->getNrOfImages() > 0) {
00129 //        transform.createTransform(image, *(helper->GetVisualizationStatePtr()->GetOptions()));
00130 //        transform.transformImgCoord(xp,yp,x,y);
00131 //    std::cout << "outlines tool " << xp << " " << yp << std::endl;
00132 //    }
00133 
00134     if (mouse_down)
00135     {
00136         vigra::Rect2D roi = start_drag_options.getROI();
00137         if (moving_left)
00138         {
00139             if (roi.left()<roi.right())
00140             {
00141                 // apply the movement only if it does not bring about a negative crop
00142                 unsigned int left_d = (int) (start_drag_x - x);
00143                 roi.setUpperLeft(vigra::Point2D(roi.left() - left_d, roi.top()));
00144             }
00145         }
00146         if (moving_top)
00147         {
00148             if (roi.top()<roi.bottom())
00149             {
00150                 // apply the movement only if it does not bring about a negative crop
00151                 unsigned int top_d = (int) (start_drag_y - y);
00152                 roi.setUpperLeft(vigra::Point2D(roi.left(), roi.top() - top_d));
00153             }
00154         }
00155         if (moving_right)
00156         {
00157             if (roi.left()<roi.right())
00158             {
00159                 // apply the movement only if it does not bring about a negative crop
00160                 unsigned int right_d = (int) (start_drag_x - x);
00161                 roi.setLowerRight(vigra::Point2D(roi.right() - right_d, 
00162                                                  roi.bottom()));
00163             }
00164         }
00165         if (moving_bottom)
00166         {
00167             // apply the movement only if it does not bring about a negative crop
00168             if (roi.top()<roi.bottom())
00169             {
00170                 unsigned int bottom_d = (int) (start_drag_y - y);
00171                 roi.setLowerRight(vigra::Point2D(roi.right(),
00172                                                  roi.bottom() - bottom_d));
00173             }
00174         }
00175         // apply the movement only if it does not bring about a negative crop
00176                 if((roi.top()<roi.bottom())&&(roi.left()<roi.right()))
00177         {
00178             opts.setROI(roi);
00179             new_roi = roi;
00180             helper->GetVisualizationStatePtr()->getViewState()->SetOptions(&opts);
00181             helper->GetVisualizationStatePtr()->Redraw();
00182         }
00183     } else {
00184         start_drag_x = x;
00185         start_drag_y = y;
00186         // check if the pointer moved to a region where dragging does something
00187         // different. If so, update the display to reflect it.
00188         // Note that since we normally only allow redraws to rerender the scene
00189         // if the view of the panorama has changed, we have to force a redraw.
00190         // (our moving boxes aren't known by the view_state!)
00191         int changes = 0;
00192         if ((x < left) != moving_left)
00193         {
00194             moving_left = !moving_left;
00195             changes++;
00196         }
00197         if ((x > right) != moving_right)
00198         {
00199             moving_right = !moving_right;
00200             changes++;
00201         }
00202         if ((y < top) != moving_top)
00203         {
00204             moving_top = !moving_top;
00205             changes++;
00206         }
00207         if ((y > bottom) != moving_bottom)
00208         {
00209             moving_bottom = !moving_bottom;
00210             changes++;
00211         }
00212         if (!(moving_left || moving_right || moving_top || moving_bottom))
00213         {
00214             // in the middle moves the whole region
00215             moving_left = true; moving_right = true;
00216             moving_top = true; moving_bottom = true;
00217             changes++;
00218         }
00219         // the middle section is an exception to the other 4 rules:
00220         if (changes == 5) changes = 0;
00221         // Draw if the boxes we show are different to what is already drawn.
00222         if (changes)
00223         {
00224             // since we didn't change the panorama, view_state doesn't think we
00225             // should redraw. Persuade it otherwise:
00226             helper->GetVisualizationStatePtr()->ForceRequireRedraw();
00227             helper->GetVisualizationStatePtr()->Redraw(); // now redraw.
00228         }
00229     }
00230 }     
00231 
00232 
00233 void PreviewCropTool::MouseButtonEvent(wxMouseEvent &e)
00234 {
00235     if (e.GetButton() == wxMOUSE_BTN_LEFT)
00236     {
00237         if (e.ButtonDown())
00238         {
00239             start_drag_options = *helper->GetVisualizationStatePtr()->getViewState()->GetOptions();
00240             opts = start_drag_options;
00241             new_roi = opts.getROI();
00242             mouse_down = true;
00243             moving_left = false;
00244             moving_right = false;
00245             moving_top = false;
00246             moving_bottom = false;
00247             if (start_drag_x < left) moving_left = true;
00248             if (start_drag_x > right) moving_right = true;
00249             if (start_drag_y < top) moving_top = true;
00250             if (start_drag_y > bottom) moving_bottom = true;
00251             // in the middle, move everything.
00252             if (!(moving_left || moving_right || moving_top || moving_bottom))
00253             {
00254                 moving_left = true;
00255                 moving_right = true;
00256                 moving_top = true;
00257                 moving_bottom = true;
00258             }
00259         } else {
00260             if (mouse_down)
00261             {
00262                 mouse_down = false;
00263                 moving_left = false;
00264                 moving_right = false;
00265                 moving_top = false;
00266                 moving_bottom = false;
00267                 // set the new cropping region permanently.
00268                 GlobalCmdHist::getInstance().addCommand(
00269                     new PT::SetPanoOptionsCmd(*(helper->GetPanoramaPtr()),
00270                                               opts));
00271             }
00272         }
00273     }
00274 }
00275 

Generated on Sat Jul 26 01:25:36 2014 for Hugintrunk by  doxygen 1.3.9.1