1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 2 3 #include "smzoomcontroller.hh" 4 #include "smutils.hh" 5 #include <math.h> 6 #include <stdio.h> 7 8 #include <glib.h> 9 10 #include <QSlider> 11 #include <QVBoxLayout> 12 13 using namespace SpectMorph; 14 15 using std::string; 16 17 ZoomController::ZoomController (QObject *parent, double hzoom_max, double vzoom_max) : 18 QObject (parent) 19 { 20 init(); 21 hzoom_slider->setRange (-1000, (log10 (hzoom_max) - 2) * 1000); 22 vzoom_slider->setRange (-1000, (log10 (vzoom_max) - 2) * 1000); 23 } 24 25 ZoomController::ZoomController (QObject *parent, double hzoom_min, double hzoom_max, double vzoom_min, double vzoom_max) : 26 QObject (parent) 27 { 28 init(); 29 hzoom_slider->setRange ((log10 (hzoom_min) - 2) * 1000, (log10 (hzoom_max) - 2) * 1000); 30 vzoom_slider->setRange ((log10 (vzoom_min) - 2) * 1000, (log10 (vzoom_max) - 2) * 1000); 31 } 32 33 void 34 ZoomController::init() 35 { 36 vscrollbar = NULL; 37 hscrollbar = NULL; 38 39 hzoom_text = new QLabel ("HZoom"); 40 hzoom_slider = new QSlider (Qt::Horizontal); 41 hzoom_label = new QLabel(); 42 43 vzoom_text = new QLabel ("VZoom"); 44 vzoom_slider = new QSlider (Qt::Horizontal); 45 vzoom_label = new QLabel(); 46 47 connect (hzoom_slider, SIGNAL (valueChanged (int)), this, SLOT (on_hzoom_changed())); 48 connect (vzoom_slider, SIGNAL (valueChanged (int)), this, SLOT (on_vzoom_changed())); 49 50 old_hzoom = 1; 51 old_vzoom = 1; 52 53 on_hzoom_changed(); 54 on_vzoom_changed(); 55 } 56 57 double 58 ZoomController::get_hzoom() 59 { 60 return pow (10, hzoom_slider->value() / 1000.0); 61 } 62 63 double 64 ZoomController::get_vzoom() 65 { 66 return pow (10, vzoom_slider->value() / 1000.0); 67 } 68 69 void 70 ZoomController::on_hzoom_changed() 71 { 72 double hzoom = get_hzoom(); 73 string s = string_locale_printf ("%3.2f%%", 100.0 * hzoom); 74 hzoom_label->setText (s.c_str()); 75 76 if (hscrollbar) 77 { 78 const double hfactor = hzoom / old_hzoom; 79 hscrollbar->setValue (hfactor * hscrollbar->value() + ((hfactor - 1) * hscrollbar->pageStep() / 2)); 80 } 81 old_hzoom = hzoom; 82 Q_EMIT zoom_changed(); 83 } 84 85 void 86 ZoomController::on_vzoom_changed() 87 { 88 double vzoom = get_vzoom(); 89 string s = string_locale_printf ("%3.2f%%", 100.0 * vzoom); 90 vzoom_label->setText (s.c_str()); 91 92 if (vscrollbar) 93 { 94 const double vfactor = vzoom / old_vzoom; 95 vscrollbar->setValue (vfactor * vscrollbar->value() + ((vfactor - 1) * vscrollbar->pageStep() / 2)); 96 } 97 old_vzoom = vzoom; 98 Q_EMIT zoom_changed(); 99 } 100 101 void 102 ZoomController::set_vscrollbar (QScrollBar *scrollbar) 103 { 104 vscrollbar = scrollbar; 105 } 106 107 void 108 ZoomController::set_hscrollbar (QScrollBar *scrollbar) 109 { 110 hscrollbar = scrollbar; 111 } 112 113 QWidget* 114 ZoomController::hwidget (int i) 115 { 116 switch (i) 117 { 118 case 0: return hzoom_text; 119 case 1: return hzoom_slider; 120 case 2: return hzoom_label; 121 default: g_assert_not_reached(); 122 } 123 } 124 125 QWidget* 126 ZoomController::vwidget (int i) 127 { 128 switch (i) 129 { 130 case 0: return vzoom_text; 131 case 1: return vzoom_slider; 132 case 2: return vzoom_label; 133 default: g_assert_not_reached(); 134 } 135 } 136