Debian Codegraph @

Qt/Qt_SliderPainter.cpp

raw symbols SCIP
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 #include <math.h>
19
20 #include <QWidget>
21 #include <QGraphicsItem>
22 #include <QTimer>
23 #include <QSlider>
24 #include <QTimerEvent>
25
26 #include "EditorWidget.h"
27
28 #include "Qt_SliderPainter_proc.h"
29
30 #include "Qt_SliderPainterPainter.h"
31
32
33 const int k_timer_interval = 50;
34
35
36 static int scale_int(int x, int x1, int x2, int y1, int y2){
37 return (int)scale((float)x,(float)x1,(float)x2,(float)y1,(float)y2);
38 }
39
40 static float gain2db(float val){
41 if(val<=0.0f)
42 return -100.0f;
43
44 return 20*log10(val);
45 }
46
47 static float db2linear(float db){
48 if(db<-70)
49 return 0.0f;
50 else if(db>40)
51 return 1.0f;
52 else
53 return scale(db,-70.0f,40.0f,0.0f,1.0f);
54 }
55
56 #if 0
57 // Function iec_scale picked from meterbridge by Steve Harris.
58 // db is a value between 0 and 1.
59
60 static float iec_scale(float db) {
61
62 db = 20.0f * log10f(db);
63
64
65 float def = 0.0f; /* Meter deflection %age */
66
67 if (db < -70.0f) {
68 def = 0.0f;
69 } else if (db < -60.0f) {
70 def = (db + 70.0f) * 0.25f;
71 } else if (db < -50.0f) {
72 def = (db + 60.0f) * 0.5f + 5.0f;
73 } else if (db < -40.0f) {
74 def = (db + 50.0f) * 0.75f + 7.5;
75 } else if (db < -30.0f) {
76 def = (db + 40.0f) * 1.5f + 15.0f;
77 } else if (db < -20.0f) {
78 def = (db + 30.0f) * 2.0f + 30.0f;
79 } else if (db < 0.0f) {
80 def = (db + 20.0f) * 2.5f + 50.0f;
81 } else {
82 def = 100.0f;
83 }
84
85 return def * 2.0f / 200.0f;
86 }
87 #endif
88
89
90
91
92 #define MAX_CHANNELS 8
93
94 //namespace{ // stupid c++ with all this useless syntax necessary to make a program appear to work. C++ is only great if you like a challenge and don't want things done.
95
96 namespace{
97 struct AutomationOrPeakData{
98 float *value;
99 int requested_pos;
100 int last_drawn_pos;
101
102 int *color;
103
104 int ch;
105 int num_ch;
106
107 bool is_automation;
108 };
109 }
110
111 static int DATA_get_y1(AutomationOrPeakData *data, int height){
112 return scale_int(data->ch,0,data->num_ch,1,height-1);
113 }
114
115 static int DATA_get_y2(AutomationOrPeakData *data, int height){
116 return scale_int(data->ch+1,0,data->num_ch,1,height-1);
117 }
118
119 struct SliderPainter{
120
121 struct Timer : public QTimer{
122 SliderPainter *_painter;
123
124 void timerEvent(QTimerEvent * e){
125 //if(_painter->isVisible())
126 // printf("oh yeah. value: %f. last_drawn: %d, visible? %d\n",*data->value, data->last_drawn_pos,_painter->isVisible());
127
128 if(_painter->isVisible()==false)
129 return;
130
131 for(unsigned int i=0;i<_painter->_data.size();i++){
132 AutomationOrPeakData *data = _painter->_data.at(i);
133
134 float gain;
135
136 if(data->is_automation)
137 gain = *data->value;
138 else{
139 float db = gain2db(*data->value);
140 if(db>4.0f)
141 _painter->_peak_color = 14;
142 else if(db>0.0f)
143 _painter->_peak_color = 6;
144 else
145 _painter->_peak_color = 13;
146
147 gain = db2linear(db);
148 }
149
150 data->requested_pos = scale(gain,0.0f,1.0f,
151 0.0f,(float)_painter->width())
152 - 1;
153
154 if(data->last_drawn_pos != data->requested_pos){
155
156 //printf("Painting. Last drawn: %d. requested: %d\n",data->last_drawn_pos,data->requested_pos);
157
158
159 int y1 = DATA_get_y1(data,_painter->height());
160 int y2 = DATA_get_y2(data,_painter->height());
161 int height = y2-y1;
162
163 //printf("y1: %d, y2: %d, height: %d. req: %d, last: %d\n",y1,y2,height,data->requested_pos,data->last_drawn_pos);
164 _painter->update(data->requested_pos,
165 y1,4,height+1);
166 _painter->update(data->last_drawn_pos,
167 y1,4,height+1);
168
169 //printf("%d - %d\n",data->requested_pos,data->last_drawn_pos);
170 }
171 }
172
173 //e->accept();
174 }
175 }; // struct Timer
176
177 std::vector<AutomationOrPeakData*>_data;
178
179 QSlider *_qslider;
180 QGraphicsItem *_graphics_item; // Either widget or _graphics_item must be set.
181
182 int _x1;
183 int _y1;
184 int _x2;
185 int _y2;
186
187 float *_peak_values;
188 bool _local_peak_values;
189 float _automation_value;
190 int _automation_color;
191 int _peak_color;
192
193 bool _alternative_color;
194
195 int _value;
196
197 int _num_automations;
198 Timer _timer;
199
200 QString _display_string;
201
202 int _num_channels;
203
204
205 int value(){
206 if(_qslider!=NULL)
207 return _qslider->value();
208 else
209 return _value;
210 }
211
212 int minimum(){
213 if(_qslider!=NULL)
214 return _qslider->minimum();
215 else
216 return 0;
217 }
218
219 int maximum(){
220 if(_qslider!=NULL)
221 return _qslider->maximum();
222 else
223 return 10000;
224 }
225
226 int orientation(){
227 if(_qslider!=NULL)
228 return _qslider->orientation();
229 else
230 return Qt::Horizontal;
231 }
232
233 bool isEnabled(){
234 if(_qslider!=NULL)
235 return _qslider->isEnabled();
236 else
237 return _num_channels>0;
238 }
239
240 bool isVisible(){
241 if(_qslider!=NULL)
242 return _qslider->isVisible() && !_qslider->isHidden();
243 else
244 return _graphics_item->isVisible();
245 }
246
247 void update(int x, int y, int width, int height){
248 //fprintf(stderr,"x: %d. _qslider: %p\n",x,_qslider);
249 if(x<0)
250 x=0;
251 if(y<0)
252 y=0;
253
254 //fprintf(stderr,"y: %d. _qslider: %p\n",y,_qslider);
255 if(_qslider!=NULL){
256 return _qslider->update(x,y,width,height);
257 }else
258 return _graphics_item->update(_x1+x, _y1+y, width, height);
259 }
260
261 int width(){
262 if(_qslider!=NULL)
263 return _qslider->width();
264 else
265 return _x2-_x1;
266 }
267
268 int height(){
269 if(_qslider!=NULL)
270 return _qslider->height();
271 else
272 return _y2-_y1;
273 }
274
275 void init(){
276 _value = 0;
277 _num_automations = 0;
278 _num_channels = 0;
279 _peak_values = NULL;
280 _local_peak_values=false;
281 _automation_color = 13;
282 _peak_color = 13;
283 _alternative_color = false;
284 }
285
286 SliderPainter(QSlider *widget)
287 : _qslider(widget)
288 , _graphics_item(NULL)
289 , _automation_value(0.0f)
290 {
291 init();
292 }
293
294 SliderPainter(QGraphicsItem *graphics_item)
295 : _qslider(NULL)
296 , _graphics_item(graphics_item)
297 {
298 init();
299 }
300
301 ~SliderPainter(){
302 if(_local_peak_values==true)
303 free(_peak_values);
304
305 for(unsigned int i=0;i<_data.size();i++) // Don't like iterators. Rather free memory manually than using them.
306 delete _data.at(i);
307 }
308
309 AutomationOrPeakData *create_automation_data(){
310 AutomationOrPeakData *data = new AutomationOrPeakData;
311
312 data->value = &_automation_value;
313 data->requested_pos = 0;
314 data->last_drawn_pos = 0;
315
316 data->color = &_automation_color;
317
318 data->ch = 0;
319 data->num_ch = 1;
320
321 data->is_automation = true;
322
323 _data.push_back(data);
324 if(_num_automations==0){
325 _timer._painter = this;
326 _timer.setInterval(k_timer_interval);
327 _timer.start();
328 }
329 _num_automations++;
330
331 return data;
332 }
333
334 float *obtain_automation_value_pointer(){
335 create_automation_data();
336 return &_automation_value;
337 }
338
339 int *obtain_automation_color_pointer(){
340 return &_automation_color;
341 }
342
343 float *obtain_peak_value_pointers(int num_channels, float *peak_values){
344 bool is_replacing = _peak_values!=NULL;
345
346 if(peak_values==NULL) {
347 _peak_values = (float*)calloc(sizeof(float*),num_channels);
348
349 _local_peak_values = true;
350
351 } else {
352
353 _peak_values = peak_values;
354
355 }
356
357 if(is_replacing){
358 while(_num_automations>0)
359 release_automation_pointers(); // Warning! This works only since _is_replacing==true just for the bottom bar volume slider (which doesn't have automations).
360 _data.clear();
361 }
362
363 for(int ch=0;ch<num_channels;ch++){
364 AutomationOrPeakData *data = create_automation_data();
365 data->ch = ch;
366 data->num_ch = num_channels;
367 data->is_automation = false;
368 data->value = &_peak_values[ch];
369 data->color = &_peak_color;
370 }
371
372 return _peak_values;
373 }
374
375 void set_peak_value_pointers(int num_channels, float *pointers){
376 obtain_peak_value_pointers(num_channels, pointers);
377 }
378
379 void release_automation_pointers(){
380 _num_automations--;
381 if(_num_automations==0)
382 _timer.stop();
383 }
384
385 void paint(QPainter *p){
386 #ifdef COMPILING_RADIUM
387 QColor *colors = static_cast<EditorWidget*>(root->song->tracker_windows->os_visual.widget)->colors;
388 #else
389 QColor *colors = g_colors;
390 #endif
391
392 SLIDERPAINTERPAINTER_paint(p,0,0,width(),height(),
393 isEnabled(),
394 scale(value(),minimum(),maximum(),0.0f,1.0f),
395 _display_string,
396 _alternative_color);
397
398 for(unsigned int i=0;i<_data.size();i++){
399 AutomationOrPeakData *data = _data.at(i);
400 int y1 = DATA_get_y1(data,height());
401 int y2 = DATA_get_y2(data,height());
402 int height = y2-y1;
403
404 p->fillRect(data->requested_pos+1 ,y1+1,
405 2, height-1,
406 colors[*data->color]);
407
408 p->setPen(QPen(colors[11].light(120),1));
409 p->drawRect(data->requested_pos, y1,
410 3, height);
411
412 data->last_drawn_pos = data->requested_pos;
413 }
414 }
415
416 };
417 //}
418
419 SliderPainter *SLIDERPAINTER_create(QSlider *qslider){
420 return new SliderPainter(qslider);
421 }
422
423 SliderPainter *SLIDERPAINTER_create(QGraphicsItem *graphics_item, int x1, int y1, int x2, int y2){
424 SliderPainter *painter = new SliderPainter(graphics_item);
425 painter->_x1=x1;
426 painter->_y1=y1;
427 painter->_x2=x2;
428 painter->_y2=y2;
429
430 return painter;
431 }
432
433
434 void SLIDERPAINTER_delete(SliderPainter *painter){
435 delete painter;
436 }
437
438 void SLIDERPAINTER_setValue(SliderPainter *painter, int value){
439 painter->_value = value;
440 }
441
442 void SLIDERPAINTER_set_num_channels(SliderPainter *painter, int num_channels){
443 painter->_num_channels = num_channels;
444 }
445
446 void SLIDERPAINTER_paint(SliderPainter *painter, QPainter *p){
447 painter->paint(p);
448 }
449
450 float *SLIDERPAINTER_obtain_peak_value_pointers(SliderPainter *painter, int num_channels){
451 return painter->obtain_peak_value_pointers(num_channels,NULL);
452 }
453
454 void SLIDERPAINTER_set_peak_value_pointers(SliderPainter *painter, int num_channels, float *pointers){
455 painter->set_peak_value_pointers(num_channels, pointers);
456 }
457
458 float *SLIDERPAINTER_obtain_automation_value_pointer(SliderPainter *painter){
459 return painter->obtain_automation_value_pointer();
460 }
461 int *SLIDERPAINTER_obtain_automation_color_pointer(SliderPainter *painter){
462 return painter->obtain_automation_color_pointer();
463 }
464 void SLIDERPAINTER_release_automation_pointers(SliderPainter *painter){
465 painter->release_automation_pointers();
466 }
467
468 // Used for chips where the slider controls input volume instead of output volume.
469 void SLIDERPAINTER_set_alternative_color(SliderPainter *painter){
470 painter->_alternative_color = true;
471 }
472
473 void SLIDERPAINTER_set_string(SliderPainter *painter,QString string){
474 painter->_display_string = string;
475 }
476
477 QString SLIDERPAINTER_get_string(SliderPainter *painter){
478 return painter->_display_string;
479 }