kwave  18.07.70
BitrateWidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  BitrateWidget.cpp - widget selecting a bitrate for MP3 or Ogg/Vorbis
3  -------------------
4  begin : Thu Oct 24 2002
5  copyright : (C) 2002 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <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 <QSlider>
21 #include <QSpinBox>
22 #include <QtGlobal>
23 
24 #include "libkwave/Utils.h"
25 
26 #include "BitrateSpinBox.h"
27 #include "BitrateWidget.h"
28 
29 /***************************************************************************/
31  :QWidget(parent),
32  Ui::BitrateWidgetBase(), m_rates()
33 {
34  setupUi(this);
35  m_rates.append(0); // don't let it stay empty, that makes life easier
36 
37  connect(slider, SIGNAL(valueChanged(int)),
38  this, SLOT(sliderChanged(int)));
39  connect(spinbox, SIGNAL(valueChanged(int)),
40  this, SLOT(spinboxChanged(int)));
41  connect(slider, SIGNAL(sliderReleased()),
42  this, SLOT(snapInSlider()));
43  connect(spinbox, SIGNAL(snappedIn(int)),
44  slider, SLOT(setValue(int)));
45 }
46 
47 /***************************************************************************/
49 {
50 }
51 
52 /***************************************************************************/
54 {
55  slider->setValue(bitrate);
56  spinbox->setValue(bitrate);
57 }
58 
59 /***************************************************************************/
61 {
62  int value = slider->value();
63  int index = nearestIndex(value);
64  return m_rates[index];
65 }
66 
67 /***************************************************************************/
69 {
70  spinbox->setSpecialValueText(text);
71 }
72 
73 /***************************************************************************/
74 void Kwave::BitrateWidget::allowRates(const QList<int> &list)
75 {
76  int old_value = value();
77 
78  m_rates = list;
79  if (m_rates.isEmpty()) m_rates.append(0);
80 
81  // set new ranges
82  spinbox->allowRates(m_rates);
83  slider->setMinimum(m_rates.first());
84  slider->setMaximum(m_rates.last());
85 
86  setValue(old_value);
87 }
88 
89 //***************************************************************************
91 {
92  // find the nearest value
93  int nearest = 0;
94  foreach(int i, m_rates)
95  if (qAbs(i - rate) < qAbs(nearest - rate)) nearest = i;
96 
97  // find the index
98  int index = m_rates.contains(nearest) ? m_rates.indexOf(nearest) : 0;
99 
100  // limit the index into a reasonable range
101  if (index < 0)
102  index = 0;
103  if (index >= Kwave::toInt(m_rates.size()))
104  index = m_rates.size()-1;
105 
106  return index;
107 }
108 
109 //***************************************************************************
111 {
112  int index = nearestIndex(value);
113  spinbox->setValue(m_rates[index]);
114 
115  emit valueChanged(value);
116 }
117 
118 //***************************************************************************
120 {
121  emit valueChanged(value);
122 }
123 
124 //***************************************************************************
126 {
127  int value = slider->value();
128  int index = nearestIndex(value);
129  slider->setValue(m_rates[index]); // snap in
130 }
131 
132 //***************************************************************************
133 //***************************************************************************
void valueChanged(int value)
int nearestIndex(int rate)
void spinboxChanged(int value)
QList< int > m_rates
Definition: BitrateWidget.h:78
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
virtual void setValue(int bitrate)
int toInt(T x)
Definition: Utils.h:127
virtual void setSpecialValueText(const QString &text)
virtual void allowRates(const QList< int > &list)
BitrateWidget(QWidget *parent)
void sliderChanged(int value)