Debian Codegraph @

src/modules/GLSample.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 "GLSample.h"
20
21 #include <cassert>
22 #include <cmath>
23 #include <iostream>
24 using namespace std;
25 #include <qtimer.h>
26 #include <qtooltip.h>
27 #include <qimage.h>
28 #include <qboxlayout.h>
29 #include <qwidgetaction.h>
30 #include <QPainter>
31
32 GLSample::GLSample(QWidget* parent)
33 : QOpenGLWidget(parent)
34 , View(tr("Waveform"), this)
35 , m_font("Helvetica")
36 , m_max_value(1.0)
37 {
38 // settings
39 setting_show->setIcon(QIcon(":/fmit/ui/images/module_waveformperiod.svg"));
40 setting_show->setChecked(false);
41 hide();
42
43 setting_hasFading = new QAction(tr("Show fading"), this);
44 setting_hasFading->setCheckable(true);
45 connect(setting_hasFading, SIGNAL(toggled(bool)), this, SLOT(update()));
46 m_popup_menu.addAction(setting_hasFading);
47
48 QHBoxLayout* numFadingActionLayout = new QHBoxLayout(&m_popup_menu);
49
50 QLabel* numFadingActionTitle = new QLabel(tr("Number of fading"), &m_popup_menu);
51 numFadingActionLayout->addWidget(numFadingActionTitle);
52
53 setting_spinNumFading = new QSpinBox(&m_popup_menu);
54 setting_spinNumFading->setMinimum(1);
55 setting_spinNumFading->setMaximum(10000);
56 setting_spinNumFading->setSingleStep(1);
57 setting_spinNumFading->setValue(20);
58 setting_spinNumFading->setToolTip(tr("Number of fading"));
59 connect(setting_spinNumFading, SIGNAL(valueChanged(int)), this, SLOT(update()));
60 numFadingActionLayout->addWidget(setting_spinNumFading);
61
62 QWidget* numFadingActionWidget = new QWidget(&m_popup_menu);
63 numFadingActionWidget->setLayout(numFadingActionLayout);
64
65 QWidgetAction* numFadingAction = new QWidgetAction(&m_popup_menu);
66 numFadingAction->setDefaultWidget(numFadingActionWidget);
67 m_popup_menu.addAction(numFadingAction);
68 }
69
70 void GLSample::save()
71 {
72 s_settings->setValue("hasFading", setting_hasFading->isChecked());
73 s_settings->setValue("spinNumFading", setting_spinNumFading->value());
74 }
75 void GLSample::load()
76 {
77 setting_hasFading->setChecked(s_settings->value("hasFading", setting_hasFading->isChecked()).toBool());
78 setting_spinNumFading->setValue(s_settings->value("spinNumFading", setting_spinNumFading->value()).toInt());
79 }
80 void GLSample::clearSettings()
81 {
82 s_settings->remove("hasFading");
83 s_settings->remove("spinNumFading");
84 }
85
86 GLSample::Sample::Sample(double t, const deque<double>& d)
87 {
88 time = t;
89 data = d;
90 max_value = 0.0;
91 for(size_t i=0; i<data.size(); i++)
92 max_value = max(max_value, abs(data[i]));
93 }
94
95 void GLSample::add(double time, const deque<double>& data)
96 {
97 m_samples.push_front(Sample(time, data));
98
99 if(setting_hasFading->isChecked())
100 while(!m_samples.empty() && int(m_samples.size())>setting_spinNumFading->value())
101 m_samples.pop_back();
102 else
103 while(!m_samples.empty() && m_samples.size()>1)
104 m_samples.pop_back();
105 }
106
107 void GLSample::initializeGL()
108 {
109 // Set the clear color to black
110 glClearColor(1.0, 1.0, 1.0, 0.0);
111
112 // glShadeModel( GL_FLAT );
113 glShadeModel( GL_SMOOTH );
114
115 glLoadIdentity();
116 }
117
118 void GLSample::paintGL()
119 {
120 glClear(GL_COLOR_BUFFER_BIT);
121
122 // name
123 glColor3f(0.75,0.75,0.75);
124 QPainter painter(this);
125 m_font.setPixelSize(20);
126 painter.setFont(m_font);
127 painter.drawText(2, 20, tr("Waveform's period"));
128 painter.end();
129
130 // horiz lines
131 glBegin(GL_LINES);
132 float gray = 0.5;
133 glColor3f(gray, gray, gray);
134 glVertex2i(0, height()/2);
135 glVertex2i(width(), height()/2);
136 glEnd();
137
138 if(m_max_value==0.0) m_max_value = 1.0;
139
140 // sample
141 if(!m_samples.empty())
142 {
143 m_max_value = 0.0;
144 for(int j=int(m_samples.size())-1; j>=0; j--)
145 {
146 m_max_value = max(m_max_value, abs(m_samples[j].max_value));
147 if(!setting_hasFading->isChecked()) j=-1;
148 }
149
150 for(int j=int(m_samples.size())-1; j>=0; j--)
151 {
152 if(m_max_value!=0.0)
153 {
154 glBegin(GL_LINE_STRIP);
155 float grey = float(j)/m_samples.size();
156 glColor3f(grey, grey, grey);
157 int size = int(m_samples[j].data.size());
158 float step = float(width())/size;
159 for(int i=size-1; i>0; i--)
160 glVertex2i(int((size-i-1)*step), int((m_samples[j].data[i]/m_max_value)*height()/2 + height()/2));
161 glVertex2i(width(), int((m_samples[j].data[0]/m_max_value)*height()/2 + height()/2));
162 glEnd();
163 }
164
165 if(!setting_hasFading->isChecked()) j=-1;
166 }
167 }
168
169 glFlush();
170 }
171
172 void GLSample::resizeGL( int w, int h )
173 {
174 // Set the new viewport size
175 glViewport(0, 0, (GLint)w, (GLint)h);
176
177 // Choose the projection matrix to be the matrix
178 // manipulated by the following calls
179 glMatrixMode(GL_PROJECTION);
180
181 // Set the projection matrix to be the identity matrix
182 glLoadIdentity();
183
184 // Define the dimensions of the Orthographic Viewing Volume
185 glOrtho(0.0, w, 0.0, h, 0.0, 1.0);
186
187 // Choose the modelview matrix to be the matrix
188 // manipulated by further calls
189 glMatrixMode(GL_MODELVIEW);
190 }