kwave  18.07.70
BitrateSpinBox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  BitrateSpinBox.cpp - spinbox for selecting a bitrate for MP3 or Ogg
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 <QtGlobal>
21 
22 #include "libkwave/Utils.h"
23 
24 #include "BitrateSpinBox.h"
25 
26 /***************************************************************************/
28  :QSpinBox(parent), m_rates()
29 {
30  m_rates.append(0); // don't let it stay empty, that makes life easier
31 
32  connect(this, SIGNAL(valueChanged(int)),
33  this, SLOT(snapIn(int)));
34 }
35 
36 /***************************************************************************/
38 {
39 }
40 
41 /***************************************************************************/
43 {
44  int index = nearestIndex(value);
45  int old_index = index;
46  int old_value = m_rates[index];
47 
48  if (value == old_value) return;
49 
50  if ((value > old_value) && (index < Kwave::toInt(m_rates.size()) - 1))
51  index++;
52 
53  if ((value < old_value) && (index > 0))
54  index--;
55 
56  if (index != old_index) {
57  int v = m_rates[index];
58  setValue(v);
59  emit snappedIn(v);
60  }
61 }
62 
63 /***************************************************************************/
64 void Kwave::BitrateSpinBox::allowRates(const QList<int> &list)
65 {
66  int old_value = value();
67 
68  m_rates = list;
69  if (m_rates.isEmpty()) m_rates.append(0);
70 
71  // set new ranges
72  setMinimum(m_rates.first());
73  setMaximum(m_rates.last());
74 
75  setValue(old_value);
76 }
77 
78 //***************************************************************************
80 {
81  // find the nearest value
82  int nearest = 0;
83  foreach(int i, m_rates)
84  if (qAbs(i - rate) < qAbs(nearest - rate)) nearest = i;
85 
86  // find the index
87  int index = m_rates.contains(nearest) ? m_rates.indexOf(nearest) : 0;
88 
89  // limit the index into a reasonable range
90  if (index < 0)
91  index = 0;
92  if (index >= Kwave::toInt(m_rates.size()))
93  index = m_rates.size()-1;
94 
95  return index;
96 }
97 
98 //***************************************************************************
99 //***************************************************************************
virtual void allowRates(const QList< int > &list)
void snappedIn(int bitrate)
virtual void snapIn(int value)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
BitrateSpinBox(QWidget *parent)
int nearestIndex(int rate)
int toInt(T x)
Definition: Utils.h:127