Tidy up a bit.
[dcpomatic.git] / src / wx / film_viewer.cc
index c42ccf6fe2bd08135fbbbaa4db23136bf3f9cd4a..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;
@@ -38,35 +39,71 @@ using namespace boost;
 class ThumbPanel : public wxPanel
 {
 public:
-       ThumbPanel (wxPanel* parent)
+       ThumbPanel (wxPanel* parent, Film* film)
                : wxPanel (parent)
+               , _film (film)
                , _image (0)
                , _bitmap (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 ();
                }
 
-               int x, y;
-               GetSize (&x, &y);
-               cout << "Render " << x << " " << y << "\n";
-               
                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;
+               }
+
+               setup ();
+               Refresh ();
+       }
+
+       void set (string f)
+       {
+               _pending_image = f;
+               Refresh ();
+       }
+
+       void set_crop (Crop c)
+       {
+               _pending_crop = c;
+               Refresh ();
        }
 
-       void load (string f)
+       void set_film (Film* f)
        {
-               cout << "loading " << f << "\n";
-               clear ();
-               _image = new wxImage (wxString (f.c_str(), wxConvUTF8));
-               _bitmap = new wxBitmap (_image->Scale (512, 512));
+               _film = f;
+               if (!_film) {
+                       clear ();
+                       Refresh ();
+               } else {
+                       setup ();
+                       Refresh ();
+               }
        }
 
+       /** Clear our thumbnail image */
        void clear ()
        {
                delete _bitmap;
@@ -75,183 +112,147 @@ 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;
+       Crop _current_crop;
+       Crop _pending_crop;
 };
 
 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
 EVT_PAINT (ThumbPanel::paint_event)
+EVT_SIZE (ThumbPanel::size_event)
 END_EVENT_TABLE ()
 
 FilmViewer::FilmViewer (Film* f, wxWindow* p)
        : wxPanel (p)
-       , _film (f)
+       , _film (0)
 {
        _sizer = new wxBoxSizer (wxVERTICAL);
        SetSizer (_sizer);
        
-       _thumb_panel = new ThumbPanel (this);
-       _thumb_panel->Show (true);
+       _thumb_panel = new ThumbPanel (this, f);
        _sizer->Add (_thumb_panel, 1, wxEXPAND);
 
-#if 0  
-       _scroller.add (_image);
-       
-       Gtk::HBox* controls = manage (new Gtk::HBox);
-       controls->set_spacing (6);
-       controls->pack_start (_position_slider);
-       
-       _vbox.pack_start (_scroller, true, true);
-       _vbox.pack_start (*controls, false, false);
-       _vbox.set_border_width (12);
-
-       _position_slider.set_digits (0);
-       _position_slider.signal_format_value().connect (sigc::mem_fun (*this, &FilmViewer::format_position_slider_value));
-       _position_slider.signal_value_changed().connect (sigc::mem_fun (*this, &FilmViewer::position_slider_changed));
+       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);
+       set_thumbnail (0);
 
-       _scroller.signal_size_allocate().connect (sigc::mem_fun (*this, &FilmViewer::scroller_size_allocate));
-#endif 
+       _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
 
        set_film (_film);
-
-       load_thumbnail (42);//XXX
 }
 
 void
-FilmViewer::load_thumbnail (int n)
+FilmViewer::set_thumbnail (int n)
 {
        if (_film == 0 || _film->num_thumbs() <= n) {
                return;
        }
 
-       int const left = _film->left_crop ();
-       int const right = _film->right_crop ();
-       int const top = _film->top_crop ();
-       int const bottom = _film->bottom_crop ();
-
-       _thumb_panel->load (_film->thumb_file(n));
-
-//     _pixbuf = Gdk::Pixbuf::create_from_file (_film->thumb_file (n));
-
-//     int const cw = _film->size().width - left - right;
-//     int const ch = _film->size().height - top - bottom;
-//     _cropped_pixbuf = Gdk::Pixbuf::create_subpixbuf (_pixbuf, left, top, cw, ch);
-//     update_scaled_pixbuf ();
-//     _image.set (_scaled_pixbuf);
+       _thumb_panel->set (_film->thumb_file(n));
 }
 
 void
-FilmViewer::reload_current_thumbnail ()
+FilmViewer::slider_changed (wxCommandEvent &)
 {
-       load_thumbnail (42);//_position_slider.get_value ());
-}
-
-void
-FilmViewer::position_slider_changed ()
-{
-       reload_current_thumbnail ();
-}
-
-string
-FilmViewer::format_position_slider_value (double v) const
-{
-#if 0  
-       stringstream s;
-
-       if (_film && int (v) < _film->num_thumbs ()) {
-               int const f = _film->thumb_frame (int (v));
-               s << f << " " << seconds_to_hms (f / _film->frames_per_second ());
-       } else {
-               s << "-";
-       }
-       
-       return s.str ();
-#endif 
+       set_thumbnail (_slider->GetValue ());
 }
 
 void
 FilmViewer::film_changed (Film::Property p)
 {
-#if 0  
-       if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
-               reload_current_thumbnail ();
+       if (p == Film::CROP) {
+               _thumb_panel->set_crop (_film->crop ());
        } else if (p == Film::THUMBS) {
                if (_film && _film->num_thumbs() > 1) {
-                       _position_slider.set_range (0, _film->num_thumbs () - 1);
+                       _slider->SetRange (0, _film->num_thumbs () - 1);
                } else {
-                       _image.clear ();
-                       _position_slider.set_range (0, 1);
+                       _thumb_panel->clear ();
+                       _slider->SetRange (0, 1);
                }
                
-               _position_slider.set_value (0);
-               reload_current_thumbnail ();
+               _slider->SetValue (0);
+               set_thumbnail (0);
        } else if (p == Film::FORMAT) {
-               reload_current_thumbnail ();
+               _thumb_panel->refresh ();
        } else if (p == Film::CONTENT) {
                setup_visibility ();
                _film->examine_content ();
                update_thumbs ();
        }
-#endif 
 }
 
 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.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
+       film_changed (Film::CROP);
        film_changed (Film::THUMBS);
-}
-
-pair<int, int>
-FilmViewer::scaled_pixbuf_size () const
-{
-#if 0  
-       if (_film == 0) {
-               return make_pair (0, 0);
-       }
-       
-       int const cw = _film->size().width - _film->left_crop() - _film->right_crop(); 
-       int const ch = _film->size().height - _film->top_crop() - _film->bottom_crop();
-
-       float ratio = 1;
-       if (_film->format()) {
-               ratio = _film->format()->ratio_as_float() * ch / cw;
-       }
-
-       Gtk::Allocation const a = _scroller.get_allocation ();
-       float const zoom = min (float (a.get_width()) / (cw * ratio), float (a.get_height()) / cw);
-       return make_pair (cw * zoom * ratio, ch * zoom);
-#endif 
-}
-       
-void
-FilmViewer::update_scaled_pixbuf ()
-{
-#if 0  
-       pair<int, int> const s = scaled_pixbuf_size ();
-
-       if (s.first > 0 && s.second > 0 && _cropped_pixbuf) {
-               _scaled_pixbuf = _cropped_pixbuf->scale_simple (s.first, s.second, Gdk::INTERP_HYPER);
-               _image.set (_scaled_pixbuf);
-       }
-#endif 
+       _thumb_panel->refresh ();
+       setup_visibility ();
 }
 
 void
 FilmViewer::update_thumbs ()
 {
-#if 0  
        if (!_film) {
                return;
        }
@@ -268,18 +269,15 @@ FilmViewer::update_thumbs ()
        shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
        j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
        JobManager::instance()->add (j);
-#endif 
 }
 
 void
 FilmViewer::setup_visibility ()
 {
-#if 0  
        if (!_film) {
                return;
        }
 
        ContentType const c = _film->content_type ();
-       _position_slider.property_visible() = (c == VIDEO);
-#endif 
+       _slider->Show (c == VIDEO);
 }