kwave  18.07.70
SonagramDialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  SonagramDialog.cpp - dialog for setting up the sonagram window
3  -------------------
4  begin : Fri Jul 28 2000
5  copyright : (C) 2000 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include <QButtonGroup>
25 #include <QCheckBox>
26 #include <QLabel>
27 #include <QLayout>
28 #include <QPushButton>
29 #include <QRadioButton>
30 #include <QSlider>
31 #include <QString>
32 #include <QStringList>
33 
34 #include <KComboBox>
35 #include <KHelpClient>
36 #include <KLocalizedString>
37 
38 #include "libkwave/Plugin.h"
39 #include "libkwave/String.h"
40 #include "libkwave/Utils.h"
42 
43 #include "SonagramDialog.h"
44 
45 //***************************************************************************
47  :QDialog(p.parentWidget()), Ui::SonagramDlg(),
48  m_length(p.selection(Q_NULLPTR, Q_NULLPTR, Q_NULLPTR, true)),
49  m_rate(p.signalRate())
50 {
51  setupUi(this);
52  setModal(true);
53 
54  Q_ASSERT(pointbox);
55  Q_ASSERT(pointslider);
56  Q_ASSERT(windowtypebox);
57  if (!pointbox) return;
58  if (!pointslider) return;
59  if (!windowtypebox) return;
60 
61  pointslider->setMaximum(Kwave::toInt(m_length / 16));
62 
64  for (unsigned int i = 0; i < Kwave::WindowFunction::count(); i++) {
65  windowtypebox->addItem(Kwave::WindowFunction::description(wf, true));
66  ++wf;
67  }
68 
69  setPoints(1); // must set the minimum number of points to get
70  setBoxPoints(0); // the largest windowlabel
71 
72  // Set a size hint:
73  // try to make the image's aspect ratio (a) = sqrt(2)
74  //
75  // samples: s
76  // fft_points: np
77  // image_width: w = s / np
78  // image_height: h = np / 2
79  // a = w / h = 2*s / (np^2)
80  // => np = sqrt( 2 * s / a) )
81  const double aspect_ratio = sqrt(2);
82  double np = sqrt(2.0 * static_cast<double>(m_length) / aspect_ratio);
83 
84  // round down to an exponent of 2, this makes the image more
85  // wide than heigh and gives a fast calculation
86  int bits = Kwave::toInt(floor(log(np) / log(2)));
87  if (bits < 2) bits = 2;
88  if (bits > 16) bits = 16;
89  setPoints(1 << (bits-1));
90  setBoxPoints(0);
91 
92  connect(
93  buttonBox->button(QDialogButtonBox::Help), SIGNAL(clicked()),
94  this, SLOT(invokeHelp())
95  );
96  connect(pointslider, SIGNAL(valueChanged(int)), SLOT(setPoints(int)));
97  connect(pointbox, SIGNAL(activated(int)), SLOT(setBoxPoints(int)));
98 
99  // set the focus onto the "OK" button
100  buttonBox->button(QDialogButtonBox::Ok)->setFocus();
101 }
102 
103 //***************************************************************************
104 void Kwave::SonagramDialog::parameters(QStringList &list)
105 {
106  Q_ASSERT(pointbox);
107  Q_ASSERT(windowtypebox);
108  Q_ASSERT(rbColor);
109 
110  QString param;
111  list.clear();
112 
113  // parameter #0: number of fft points
114  param = pointbox ? pointbox->currentText() : QString();
115  list.append(param);
116 
117  // parameter #1: index of the window function
119  (windowtypebox) ? windowtypebox->currentIndex() : 0);
120  param = Kwave::WindowFunction::name(wf);
121  list.append(param);
122 
123  // parameter #2: flag: use color instead of greyscale
124  param.setNum(rbColor ? (rbColor->isChecked() ? 1 : 0) : 0);
125  list.append(param);
126 
127  // parameter #3: flag: track changes
128  param.setNum((cbTrackChanges && cbTrackChanges->isChecked())
129  ? 1 : 0);
130  list.append(param);
131 
132  // parameter #4: flag: follow selection
133  param.setNum((cbFollowSelection && cbFollowSelection->isChecked())
134  ? 1 : 0);
135  list.append(param);
136 
137 }
138 
139 //***************************************************************************
141 {
142  Q_ASSERT(points >= 0);
143  QString text;
144  points *= 2;
145 
146  text.setNum(points);
147  pointbox->setEditText(text);
148 
149  windowlabel->setText(i18n("(resulting window size: %1)",
150  Kwave::ms2string(points * 1.0E3 / m_rate)));
151 
152  bitmaplabel->setText(i18n("Size of bitmap: %1x%2",
153  (m_length / points) + 1,
154  points/2));
155 }
156 
157 //***************************************************************************
159 {
160  Q_ASSERT(windowtypebox);
161  if (!windowtypebox) return;
162  windowtypebox->setCurrentIndex(Kwave::WindowFunction::index(type));
163 }
164 
165 //***************************************************************************
167 {
168  Q_ASSERT(rbColor);
169  if (!rbColor) return;
170 
171  rbColor->setChecked(color);
172  rbGreyScale->setChecked(!color);
173 }
174 
175 //***************************************************************************
177 {
178  Q_ASSERT(cbTrackChanges);
179  if (!cbTrackChanges) return;
180  cbTrackChanges->setChecked(track_changes);
181 }
182 
183 //***************************************************************************
184 void Kwave::SonagramDialog::setFollowSelection(bool follow_selection)
185 {
186  Q_ASSERT(cbFollowSelection);
187  if (!cbFollowSelection) return;
188  cbFollowSelection->setChecked(follow_selection);
189 }
190 
191 //***************************************************************************
193 {
194  Q_ASSERT(num >= 0);
195  int points = pointbox->itemText(num).toInt();
196  pointslider->setValue(points / 2);
197 }
198 
199 //***************************************************************************
201 {
202 }
203 
204 //***************************************************************************
206 {
207  KHelpClient::invokeHelp(_("plugin_sect_sonagram"));
208 }
209 
210 //***************************************************************************
211 //***************************************************************************
static QString description(window_function_t type, bool localized)
static unsigned int index(window_function_t type)
window_function_t
void parameters(QStringList &list)
QString Q_DECL_EXPORT ms2string(double ms, int precision=6)
Definition: Utils.cpp:66
static window_function_t findFromIndex(unsigned int index)
SonagramDialog(Kwave::Plugin &p)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
void setColorMode(int color)
void setTrackChanges(bool track_changes)
void setBoxPoints(int num)
int toInt(T x)
Definition: Utils.h:127
void setFollowSelection(bool follow_selection)
sample_index_t m_length
static unsigned int count()
#define _(m)
Definition: memcpy.c:66
static const QString name(window_function_t type)
void setPoints(int points)
void setWindowFunction(Kwave::window_function_t type)