Debian Codegraph @

src/modules/GLFreqStruct.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 "GLFreqStruct.h"
20
21 #include <iostream>
22 using namespace std;
23 #include <qtooltip.h>
24 #include <qimage.h>
25 #include <qboxlayout.h>
26 #include <qwidgetaction.h>
27 #include <QPainter>
28 #include <Music/Music.h>
29 using namespace Music;
30
31 GLFreqStruct::GLFreqStruct(QWidget* parent)
32 : QOpenGLWidget(parent)
33 , View(tr("Harmonics"), this)
34 , m_font("Helvetica")
35 , m_components_max(1.0)
36 {
37 // settings
38 setting_show->setIcon(QIcon(":/fmit/ui/images/module_harmonics.svg"));
39 setting_show->setChecked(false);
40 hide();
41
42 QHBoxLayout* numComponentsActionLayout = new QHBoxLayout(&m_popup_menu);
43
44 QLabel* numComponentsActionTitle = new QLabel(tr("Number of harmonics"), &m_popup_menu);
45 numComponentsActionLayout->addWidget(numComponentsActionTitle);
46
47 setting_spinNumComponents = new QSpinBox(&m_popup_menu);
48 setting_spinNumComponents->setMinimum(1);
49 setting_spinNumComponents->setMaximum(128);
50 setting_spinNumComponents->setSingleStep(1);
51 setting_spinNumComponents->setValue(16);
52 m_components.resize(setting_spinNumComponents->value());
53 setting_spinNumComponents->setToolTip(tr("Number of harmonics"));
54 connect(setting_spinNumComponents, SIGNAL(valueChanged(int)), this, SLOT(spinNumComponentsChanged(int)));
55 numComponentsActionLayout->addWidget(setting_spinNumComponents);
56
57 QWidget* numComponentsActionWidget = new QWidget(&m_popup_menu);
58 numComponentsActionWidget->setLayout(numComponentsActionLayout);
59
60 QWidgetAction* numComponentsAction = new QWidgetAction(&m_popup_menu);
61 numComponentsAction->setDefaultWidget(numComponentsActionWidget);
62 m_popup_menu.addAction(numComponentsAction);
63
64 for(size_t i=0; i<m_components.size(); i++)
65 m_components[i] = 0.0;
66 }
67
68 void GLFreqStruct::save()
69 {
70 s_settings->setValue("spinNumComponents", setting_spinNumComponents->value());
71 }
72 void GLFreqStruct::load()
73 {
74 setting_spinNumComponents->setValue(s_settings->value("spinNumComponents", setting_spinNumComponents->value()).toInt());
75 }
76 void GLFreqStruct::clearSettings()
77 {
78 s_settings->remove("spinNumComponents");
79 }
80
81 void GLFreqStruct::spinNumComponentsChanged(int num)
82 {
83 m_components.resize(num);
84 }
85
86 void GLFreqStruct::initializeGL()
87 {
88 // Set the clear color to black
89 glClearColor(1.0, 1.0, 1.0, 0.0);
90
91 glShadeModel( GL_FLAT );
92 // glShadeModel( GL_SMOOTH );
93
94 glLoadIdentity();
95 }
96
97 void GLFreqStruct::paintGL()
98 {
99 if(m_components_max==0.0) return;
100
101 glClear(GL_COLOR_BUFFER_BIT);
102
103 int scale_height = 12;
104 int s = 2+fontMetrics().horizontalAdvance("-40dB");
105
106 // double scale_factor = 1.0;
107 // if(m_components_max>0.0)
108 // scale_factor = 1.0/m_components_max;
109
110 // bars
111 glBegin(GL_QUADS);
112 float step = float(width()-s)/m_components.size();
113 int space = (step>2)?1:0;
114 for(size_t i=0; i<m_components.size(); i++)
115 {
116 glColor3f(0.4, 0.4, 0.5);
117 // int y = int( (scale_factor*m_components[i]) * (height()-scale_height)) + scale_height;
118 int y = height() + int( (m_components[i]-m_components_max) * (height()-scale_height)/50);
119
120 // std::cout << i << ":" << m_components_max << " " << m_components[i] << " y=" << y << std::endl;
121
122 if(y>0)
123 {
124 glVertex2i(int(i*step)+s, scale_height);
125 glVertex2i(int(i*step)+s, y);
126 glVertex2i(int((i+1)*step)+s-space, y);
127 glVertex2i(int((i+1)*step)+s-space, scale_height);
128 }
129 }
130 glEnd();
131
132 // horiz lines
133 if(2*height()/50>2)
134 {
135 glBegin(GL_LINES);
136 float gray = 0.875;
137 glColor3f(gray, gray, gray);
138 for(int h=0; h<50; h+=2)
139 {
140 glVertex2i(s, height()-h*(height()-scale_height)/50);
141 glVertex2i(width(), height()-h*(height()-scale_height)/50);
142 }
143 glEnd();
144 }
145 if(10*height()/50>2)
146 {
147 glBegin(GL_LINES);
148 float gray = 0.75;
149 glColor3f(gray, gray, gray);
150 for(int h=0; h<50; h+=10)
151 {
152 glVertex2i(s, height()-h*(height()-scale_height)/50);
153 glVertex2i(width(), height()-h*(height()-scale_height)/50);
154 }
155 glEnd();
156 }
157
158 glColor3f(0.5f,0.5f,0.5f);
159 QPainter painter(this);
160 m_font.setPixelSize(14);
161 painter.setFont(m_font);
162 QFontMetrics fm(m_font);
163 int dy = -fm.xHeight()/2;
164 string sfraq = "-10dB";
165 painter.drawText(2, 10*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
166 sfraq = "-20dB";
167 painter.drawText(2, 20*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
168 sfraq = "-30dB";
169 painter.drawText(2, 30*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
170 sfraq = "-40dB";
171 painter.drawText(2, 40*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
172
173 // scale
174 m_font.setPixelSize(10);
175 glColor3f(0,0,0);
176 for(size_t i=0; i<m_components.size(); i++)
177 painter.drawText(int((i+0.5)*step)+s-3, height()-2, QString::number(i+1));
178
179 // name
180 glColor3f(0.75,0.75,0.75);
181 m_font.setPixelSize(20);
182 painter.drawText(2, 20, tr("Harmonics' amplitude"));
183 painter.end();
184
185 glFlush();
186 }
187
188 void GLFreqStruct::resizeGL( int w, int h )
189 {
190 // Set the new viewport size
191 glViewport(0, 0, (GLint)w, (GLint)h);
192
193 // Choose the projection matrix to be the matrix
194 // manipulated by the following calls
195 glMatrixMode(GL_PROJECTION);
196
197 // Set the projection matrix to be the identity matrix
198 glLoadIdentity();
199
200 // Define the dimensions of the Orthographic Viewing Volume
201 glOrtho(0.0, w, 0.0, h, 0.0, 1.0);
202
203 // Choose the modelview matrix to be the matrix
204 // manipulated by further calls
205 glMatrixMode(GL_MODELVIEW);
206 }
207