Tidy up a bit.
[dcpomatic.git] / src / wx / film_viewer.cc
index 6f56252932938306cbd44232e386c1e3874ebd3e..3c7d76bce03edaf9dad1ab53ebd12e27606f2533 100644 (file)
@@ -31,6 +31,7 @@
 #include "lib/film_state.h"
 #include "lib/options.h"
 #include "film_viewer.h"
+#include "wx_util.h"
 
 using namespace std;
 using namespace boost;
@@ -43,73 +44,66 @@ public:
                , _film (film)
                , _image (0)
                , _bitmap (0)
-               , _left_crop (0)
-               , _right_crop (0)
-               , _top_crop (0)
-               , _bottom_crop (0)
        {
        }
 
+       /** Handle a paint event */
        void paint_event (wxPaintEvent& ev)
        {
-               if (!_bitmap) {
-                       return;
+               if (_current_image != _pending_image) {
+                       delete _image;
+                       _image = new wxImage (std_to_wx (_pending_image));
+                       _current_image = _pending_image;
+                       setup ();
+               }
+
+               if (_current_crop != _pending_crop) {
+                       _current_crop = _pending_crop;
+                       setup ();
                }
 
                wxPaintDC dc (this);
-               dc.DrawBitmap (*_bitmap, 0, 0, false);
+               if (_bitmap) {
+                       dc.DrawBitmap (*_bitmap, 0, 0, false);
+               }
        }
 
+       /** Handle a size event */
        void size_event (wxSizeEvent &)
        {
                if (!_image) {
                        return;
                }
 
-               resize ();
+               setup ();
+               Refresh ();
        }
 
-       void resize ()
+       void set (string f)
        {
-               int vw, vh;
-               GetSize (&vw, &vh);
-
-               float const target = _film->format()->ratio_as_float ();
-
-               _cropped_image = _image->GetSubImage (
-                       wxRect (_left_crop, _top_crop, _image->GetWidth() - (_left_crop + _right_crop), _image->GetHeight() - (_top_crop + _bottom_crop))
-                       );
-
-               if ((float (vw) / vh) > target) {
-                       /* view is longer (horizontally) than the ratio; fit height */
-                       _cropped_image.Rescale (vh * target, vh);
-               } else {
-                       /* view is shorter (horizontally) than the ratio; fit width */
-                       _cropped_image.Rescale (vw, vw / target);
-               }
-
-               delete _bitmap;
-               _bitmap = new wxBitmap (_cropped_image);
-
+               _pending_image = f;
                Refresh ();
        }
 
-       void load (string f)
+       void set_crop (Crop c)
        {
-               delete _image;
-               _image = new wxImage (wxString (f.c_str(), wxConvUTF8));
-               resize ();
+               _pending_crop = c;
+               Refresh ();
        }
 
-       void set_crop (int l, int r, int t, int b)
+       void set_film (Film* f)
        {
-               _left_crop = l;
-               _right_crop = r;
-               _top_crop = t;
-               _bottom_crop = b;
-               resize ();
+               _film = f;
+               if (!_film) {
+                       clear ();
+                       Refresh ();
+               } else {
+                       setup ();
+                       Refresh ();
+               }
        }
 
+       /** Clear our thumbnail image */
        void clear ()
        {
                delete _bitmap;
@@ -118,17 +112,56 @@ public:
                _image = 0;
        }
 
+       void refresh ()
+       {
+               setup ();
+               Refresh ();
+       }
+
        DECLARE_EVENT_TABLE ();
 
 private:
+
+       void setup ()
+       {
+               if (!_film || !_image) {
+                       return;
+               }
+               
+               int vw, vh;
+               GetSize (&vw, &vh);
+
+               float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
+
+               _cropped_image = _image->GetSubImage (
+                       wxRect (
+                               _current_crop.left,
+                               _current_crop.top,
+                               _image->GetWidth() - (_current_crop.left + _current_crop.right),
+                               _image->GetHeight() - (_current_crop.top + _current_crop.bottom)
+                               )
+                       );
+
+               if ((float (vw) / vh) > target) {
+                       /* view is longer (horizontally) than the ratio; fit height */
+                       _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
+               } else {
+                       /* view is shorter (horizontally) than the ratio; fit width */
+                       _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
+               }
+
+               delete _bitmap;
+               _bitmap = new wxBitmap (_cropped_image);
+       }
+
        Film* _film;
        wxImage* _image;
+       std::string _current_image;
+       std::string _pending_image;
        wxImage _cropped_image;
        wxBitmap* _bitmap;
-       int _left_crop;
-       int _right_crop;
-       int _top_crop;
-       int _bottom_crop;
+       Crop _current_crop;
+       Crop _pending_crop;
 };
 
 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
@@ -138,7 +171,7 @@ END_EVENT_TABLE ()
 
 FilmViewer::FilmViewer (Film* f, wxWindow* p)
        : wxPanel (p)
-       , _film (f)
+       , _film (0)
 {
        _sizer = new wxBoxSizer (wxVERTICAL);
        SetSizer (_sizer);
@@ -149,7 +182,7 @@ FilmViewer::FilmViewer (Film* f, wxWindow* p)
        int const max = f ? f->num_thumbs() - 1 : 0;
        _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
        _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
-       load_thumbnail (0);
+       set_thumbnail (0);
 
        _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
 
@@ -157,32 +190,26 @@ FilmViewer::FilmViewer (Film* f, wxWindow* p)
 }
 
 void
-FilmViewer::load_thumbnail (int n)
+FilmViewer::set_thumbnail (int n)
 {
        if (_film == 0 || _film->num_thumbs() <= n) {
                return;
        }
 
-       _thumb_panel->load (_film->thumb_file(n));
-}
-
-void
-FilmViewer::reload_current_thumbnail ()
-{
-       load_thumbnail (_slider->GetValue ());
+       _thumb_panel->set (_film->thumb_file(n));
 }
 
 void
 FilmViewer::slider_changed (wxCommandEvent &)
 {
-       reload_current_thumbnail ();
+       set_thumbnail (_slider->GetValue ());
 }
 
 void
 FilmViewer::film_changed (Film::Property p)
 {
-       if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
-               _thumb_panel->set_crop (_film->left_crop(), _film->right_crop(), _film->top_crop(), _film->bottom_crop ());
+       if (p == Film::CROP) {
+               _thumb_panel->set_crop (_film->crop ());
        } else if (p == Film::THUMBS) {
                if (_film && _film->num_thumbs() > 1) {
                        _slider->SetRange (0, _film->num_thumbs () - 1);
@@ -192,9 +219,9 @@ FilmViewer::film_changed (Film::Property p)
                }
                
                _slider->SetValue (0);
-               reload_current_thumbnail ();
+               set_thumbnail (0);
        } else if (p == Film::FORMAT) {
-               reload_current_thumbnail ();
+               _thumb_panel->refresh ();
        } else if (p == Film::CONTENT) {
                setup_visibility ();
                _film->examine_content ();
@@ -205,16 +232,22 @@ FilmViewer::film_changed (Film::Property p)
 void
 FilmViewer::set_film (Film* f)
 {
+       if (_film == f) {
+               return;
+       }
+       
        _film = f;
+       _thumb_panel->set_film (_film);
 
        if (!_film) {
-               _thumb_panel->clear ();
                return;
        }
 
        _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
-
+       film_changed (Film::CROP);
        film_changed (Film::THUMBS);
+       _thumb_panel->refresh ();
+       setup_visibility ();
 }
 
 void