Debian Codegraph @

src/modules/GLVolumeHistory.cpp

raw
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "fmit"
4
5 // "fmit" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // "fmit" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 #include "GLVolumeHistory.h"
20
21 #include <iostream>
22 using namespace std;
23 #include <qtimer.h>
24 #include <qimage.h>
25 #include <QPainter>
26 #include <Music/Music.h>
27
28 GLVolumeHistory::Note::Note(int h)
29 : ht(h)
30 {
31 }
32 GLVolumeHistory::Note::Note(int h, int num, int den)
33 : ht(h)
34 {
35 factor = QString::number(num)+"/"+QString::number(den);
36 }
37 GLVolumeHistory::Note::Note(int h, float cents)
38 : ht(h)
39 {
40 factor = QString::number(cents);
41 }
42 QString GLVolumeHistory::Note::getName() const
43 {
44 return QString::fromStdString(Music::h2n(ht))+factor;
45 }
46
47 GLVolumeHistory::GLVolumeHistory(QWidget* parent)
48 : QOpenGLWidget(parent)
49 , View(tr("Volume history"), this)
50 , m_font("Helvetica")
51 {
52 setting_show->setCheckable(true);
53 setting_show->setIcon(QIcon(":/fmit/ui/images/module_volumehistory.svg"));
54 setting_show->setChecked(false);
55 hide();
56
57 m_volume_treshold = -100;
58
59 setting_keep = new QAction(tr("Keep previous notes"), this);
60 setting_keep->setCheckable(true);
61 setting_keep->setChecked(false);
62 connect(setting_keep, SIGNAL(toggled(bool)), this, SLOT(keepPreviousNotes(bool)));
63 m_popup_menu.addAction(setting_keep);
64 }
65
66 void GLVolumeHistory::save()
67 {
68 s_settings->setValue("keep", setting_keep->isChecked());
69 }
70 void GLVolumeHistory::load()
71 {
72 setting_keep->setChecked(s_settings->value("keep", setting_keep->isChecked()).toBool());
73 }
74 void GLVolumeHistory::clearSettings()
75 {
76 s_settings->remove("keep");
77 }
78
79 void GLVolumeHistory::addNote(GLVolumeHistory::Note note)
80 {
81 m_notes.push_back(note);
82
83 if(!setting_keep->isChecked())
84 while(m_notes.size()>1)
85 m_notes.pop_front();
86 }
87 void GLVolumeHistory::addVolume(float vol)
88 {
89 m_notes.back().volumes.push_back(vol);
90 }
91
92 void GLVolumeHistory::keepPreviousNotes(bool keep)
93 {
94 if(!keep)
95 while(m_notes.size()>1)
96 m_notes.pop_front();
97 }
98
99 void GLVolumeHistory::setVolumeTreshold(int treshold)
100 {
101 m_volume_treshold = treshold;
102 }
103
104 void GLVolumeHistory::initializeGL()
105 {
106 // Set the clear color to black
107 glClearColor(1.0, 1.0, 1.0, 0.0);
108
109 // glShadeModel( GL_FLAT );
110 glShadeModel( GL_SMOOTH );
111
112 glLoadIdentity();
113 }
114
115 void GLVolumeHistory::paintGL()
116 {
117 // cout << "GLVolumeHistory::paintGL " << m_notes.size() << endl;
118
119 glClear(GL_COLOR_BUFFER_BIT);
120
121 glLineWidth(1.0f);
122
123 // name
124 glColor3f(0.75,0.75,0.75);
125 QPainter painter(this);
126 m_font.setPixelSize(20);
127 painter.setFont(m_font);
128 painter.drawText(2, 20, tr("Volume"));
129 painter.end();
130
131 int s = 2+fontMetrics().horizontalAdvance("50%");
132
133 // horiz lines
134 if(2*height()/50>2)
135 {
136 glBegin(GL_LINES);
137 float gray = 0.875;
138 glColor3f(gray, gray, gray);
139 for(int h=2; h<50; h+=2)
140 {
141 glVertex2i(s, h*height()/50);
142 glVertex2i(width(), h*height()/50);
143 }
144 glEnd();
145 }
146 if(10*height()/50>2)
147 {
148 glBegin(GL_LINES);
149 float gray = 0.75;
150 glColor3f(gray, gray, gray);
151 for(int h=10; h<50; h+=10)
152 {
153 glVertex2i(s, h*height()/50);
154 glVertex2i(width(), h*height()/50);
155 }
156 glEnd();
157 }
158
159 glColor3f(0.5f,0.5f,0.5f);
160 painter.begin(this);
161 m_font.setPixelSize(14);
162 painter.setFont(m_font);
163 QFontMetrics fm(m_font);
164 int dy = -fm.xHeight()/2;
165 string sfraq = "[dB]";
166 painter.drawText(2, height()-2, QString(sfraq.c_str()));
167 sfraq = "-10";
168 painter.drawText(2, 10*height()/50-dy, QString(sfraq.c_str()));
169 sfraq = "-20";
170 painter.drawText(2, 20*height()/50-dy, QString(sfraq.c_str()));
171 sfraq = "-30";
172 painter.drawText(2, 30*height()/50-dy, QString(sfraq.c_str()));
173 sfraq = "-40";
174 painter.drawText(2, 40*height()/50-dy, QString(sfraq.c_str()));
175 painter.end();
176
177 glColor3f(1.0,0.5,0.5);
178 glLineWidth(2.0f);
179 glBegin(GL_LINES);
180 glVertex2f(s, height()+height()*m_volume_treshold/50);
181 glVertex2f(width(), height()+height()*m_volume_treshold/50);
182 glEnd();
183
184 // volumes
185 if(!m_notes.empty())
186 {
187 int total_size = 0;
188 for(size_t i=0; i<m_notes.size(); i++)
189 total_size += int(m_notes[i].volumes.size())-1;
190
191 float step = float(width()-s)/total_size;
192
193 // cout << "total_size=" << total_size << " step=" << step << endl;
194
195 int curr_total = 0;
196 for(size_t i=0; i<m_notes.size(); i++)
197 {
198 float x = s+step*curr_total;
199
200 // if it's not the first, add a separation
201 if(i>0)
202 {
203 glColor3f(0.75,0.75,0.75);
204 glLineWidth(1.0f);
205 glBegin(GL_LINES);
206 glVertex2f(x, 0); glVertex2f(x, height());
207 glEnd();
208 }
209
210 // the note name
211 string str = Music::h2n(m_notes[i].ht);
212 glColor3f(0.0,0.0,1.0);
213 painter.begin(this);
214 m_font.setPixelSize(14);
215 painter.setFont(m_font);
216 painter.drawText(x+2, height()-2, QString(str.c_str()));
217 painter.end();
218
219 // draw the volume graph
220 if(!m_notes[i].volumes.empty())
221 {
222 // glColor3f(0.33,0.33,1.0);
223 glColor3f(0.0f,0.0f,0.0f);
224 glLineWidth(2.0f);
225 glBegin(GL_LINE_STRIP);
226 for(int j=0; j<int(m_notes[i].volumes.size()); j++)
227 glVertex2f(x+j*step, height()+height()*m_notes[i].volumes[j]/50.0);
228 glEnd();
229 }
230
231 curr_total += int(m_notes[i].volumes.size())-1;
232 }
233 }
234
235 glFlush();
236 }
237
238 void GLVolumeHistory::resizeGL( int w, int h )
239 {
240 // Set the new viewport size
241 glViewport(0, 0, (GLint)w, (GLint)h);
242
243 // Choose the projection matrix to be the matrix
244 // manipulated by the following calls
245 glMatrixMode(GL_PROJECTION);
246
247 // Set the projection matrix to be the identity matrix
248 glLoadIdentity();
249
250 // Define the dimensions of the Orthographic Viewing Volume
251 glOrtho(0.0, w, 0.0, h, 0.0, 1.0);
252
253 // Choose the modelview matrix to be the matrix
254 // manipulated by further calls
255 glMatrixMode(GL_MODELVIEW);
256 }
257