| 1 | /* Copyright 2012 Kjetil S. Matheussen |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License |
| 5 | as published by the Free Software Foundation; either version 2 |
| 6 | of the License, or (at your option) any later version. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 16 | |
| 17 | |
| 18 | #ifndef QT_MYQSLIDER_H |
| 19 | #define QT_MYQSLIDER_H |
| 20 | |
| 21 | #include <QSlider> |
| 22 | #include <QMouseEvent> |
| 23 | #include <QPainter> |
| 24 | #include <QTimer> |
| 25 | #include <QVector> |
| 26 | #include <QApplication> |
| 27 | #include <QFont> |
| 28 | |
| 29 | #include "EditorWidget.h" |
| 30 | |
| 31 | #include "../common/instruments_proc.h" |
| 32 | #include "../common/vector_proc.h" |
| 33 | #include "../common/settings_proc.h" |
| 34 | |
| 35 | #include "../audio/undo_audio_effect_proc.h" |
| 36 | #include "../audio/SoundPlugin.h" |
| 37 | #include "../audio/SoundPlugin_proc.h" |
| 38 | |
| 39 | #include "Qt_instruments_proc.h" |
| 40 | |
| 41 | #include "Qt_SliderPainter_proc.h" |
| 42 | |
| 43 | static int scale_int(int x, int x1, int x2, int y1, int y2){ |
| 44 | return (int)scale((float)x,(float)x1,(float)x2,(float)y1,(float)y2); |
| 45 | } |
| 46 | |
| 47 | struct MyQSlider; |
| 48 | |
| 49 | #ifdef COMPILING_RADIUM |
| 50 | extern QVector<MyQSlider*> g_all_myqsliders; |
| 51 | extern struct Root *root; |
| 52 | #else |
| 53 | QVector<MyQSlider*> g_all_myqsliders; |
| 54 | #endif |
| 55 | |
| 56 | static int g_minimum_height = 0; |
| 57 | |
| 58 | struct MyQSlider : public QSlider{ |
| 59 | |
| 60 | public: |
| 61 | struct Patch *_patch; |
| 62 | int _effect_num; |
| 63 | |
| 64 | SliderPainter *_painter; |
| 65 | |
| 66 | bool _minimum_size_set; |
| 67 | |
| 68 | void init(){ |
| 69 | _has_mouse=false; |
| 70 | |
| 71 | _patch = NULL; |
| 72 | _effect_num = 0; |
| 73 | |
| 74 | if(g_minimum_height==0){ |
| 75 | QFontMetrics fm(QApplication::font()); |
| 76 | QRect r =fm.boundingRect("In Out 234234 dB"); |
| 77 | g_minimum_height = r.height()+4; |
| 78 | printf("Minimum height: %d, family: %s, font pixelsize: %d, font pointsize: %d\n",g_minimum_height,QApplication::font().family().toUtf8().constData(),QApplication::font().pixelSize(),QApplication::font().pointSize()); |
| 79 | } |
| 80 | |
| 81 | _minimum_size_set = false; // minimumSize must be set later. I think ui generated code overwrites it when set here. |
| 82 | |
| 83 | _painter = SLIDERPAINTER_create(this); |
| 84 | |
| 85 | g_all_myqsliders.push_back(this); |
| 86 | } |
| 87 | |
| 88 | MyQSlider ( QWidget * parent = 0 ) : QSlider(parent) {init();} |
| 89 | MyQSlider ( Qt::Orientation orientation, QWidget * parent = 0 ) : QSlider(orientation,parent) { init();} |
| 90 | |
| 91 | ~MyQSlider(){ |
| 92 | g_all_myqsliders.remove(g_all_myqsliders.indexOf(this)); |
| 93 | SLIDERPAINTER_delete(_painter); |
| 94 | } |
| 95 | |
| 96 | #if 0 |
| 97 | void handle_system_delay(bool down){ |
| 98 | SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata; |
| 99 | const SoundPluginType *type = plugin->type; |
| 100 | |
| 101 | if(_effect_num==type->num_effects+EFFNUM_DELAY_TIME){ |
| 102 | if(down==true) |
| 103 | plugin->delay.is_on = false; |
| 104 | else |
| 105 | plugin->delay.is_on = true; |
| 106 | } |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | bool _has_mouse; |
| 111 | |
| 112 | void handle_mouse_event ( QMouseEvent * event ){ |
| 113 | //printf("Got mouse press event %d / %d\n",(int)event->x(),(int)event->y()); |
| 114 | if (orientation() == Qt::Vertical) |
| 115 | setValue(scale_int(event->y(),height(),0,minimum(),maximum())); |
| 116 | else |
| 117 | setValue(scale_int(event->x(),0,width(),minimum(),maximum())); |
| 118 | |
| 119 | event->accept(); |
| 120 | } |
| 121 | |
| 122 | // mousePressEvent |
| 123 | void mousePressEvent ( QMouseEvent * event ) |
| 124 | { |
| 125 | //printf("Got mouse pres event %d / %d\n",(int)event->x(),(int)event->y()); |
| 126 | if (event->button() == Qt::LeftButton){ |
| 127 | |
| 128 | #ifdef COMPILING_RADIUM |
| 129 | if(_patch!=NULL && _patch->instrument==get_audio_instrument()){ |
| 130 | Undo_AudioEffect_CurrPos(_patch, _effect_num); |
| 131 | //handle_system_delay(true); |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | handle_mouse_event(event); |
| 136 | _has_mouse = true; |
| 137 | |
| 138 | }else{ |
| 139 | |
| 140 | #ifdef COMPILING_RADIUM |
| 141 | vector_t options = {0}; |
| 142 | VECTOR_push_back(&options, "Reset"); |
| 143 | //VECTOR_push_back(&options, "Set Value"); |
| 144 | |
| 145 | int command = GFX_Menu(root->song->tracker_windows, NULL, "", &options); |
| 146 | |
| 147 | if(command==0 && _patch!=NULL && _patch->instrument==get_audio_instrument()){ |
| 148 | SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata; |
| 149 | PLUGIN_reset_one_effect(plugin,_effect_num); |
| 150 | GFX_update_instrument_widget(_patch); |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | #if 0 |
| 155 | else if(command==1 && _patch!=NULL && _patch->instrument==get_audio_instrument()){ |
| 156 | SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata; |
| 157 | char *s = GFX_GetString(root->song->tracker_windows,NULL, (char*)"new value"); |
| 158 | if(s!=NULL){ |
| 159 | float value = OS_get_double_from_string(s); |
| 160 | Undo_AudioEffect_CurrPos(_patch, _effect_num); |
| 161 | PLUGIN_set_effect_value(plugin,-1,_effect_num,value,PLUGIN_STORED_TYPE,PLUGIN_STORE_VALUE); |
| 162 | GFX_update_instrument_widget(_patch); |
| 163 | } |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | event->accept(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void mouseMoveEvent ( QMouseEvent * event ) |
| 172 | { |
| 173 | if (_has_mouse){ |
| 174 | handle_mouse_event(event); |
| 175 | }else |
| 176 | QSlider::mouseMoveEvent(event); |
| 177 | } |
| 178 | |
| 179 | void mouseReleaseEvent ( QMouseEvent * event ) |
| 180 | { |
| 181 | //printf("Got mouse release event %d / %d\n",(int)event->x(),(int)event->y()); |
| 182 | if (_has_mouse){ |
| 183 | #if 0 |
| 184 | if(_patch!=NULL && _patch->instrument==get_audio_instrument()){ |
| 185 | handle_system_delay(false); |
| 186 | } |
| 187 | #endif |
| 188 | handle_mouse_event(event); |
| 189 | _has_mouse=false; |
| 190 | }else |
| 191 | QSlider::mouseReleaseEvent(event); |
| 192 | } |
| 193 | |
| 194 | void paintEvent ( QPaintEvent * ev ){ |
| 195 | if(_minimum_size_set==false){ |
| 196 | _minimum_size_set=true; |
| 197 | setMinimumHeight(g_minimum_height); |
| 198 | } |
| 199 | |
| 200 | #if 0 |
| 201 | { |
| 202 | QFontMetrics fm(QApplication::font()); |
| 203 | //QRect r =fm.boundingRect(SLIDERPAINTER_get_string(_painter)); |
| 204 | int width = fm.width(SLIDERPAINTER_get_string(_painter)) + 20; |
| 205 | if(minimumWidth() < width) |
| 206 | setMinimumWidth(width); |
| 207 | } |
| 208 | |
| 209 | #endif |
| 210 | |
| 211 | QPainter p(this); |
| 212 | SLIDERPAINTER_paint(_painter,&p); |
| 213 | } |
| 214 | |
| 215 | }; |
| 216 | |
| 217 | #endif // QT_MYQSLIDER_H |