1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/***************************************************************************
BitrateWidget.cpp - widget selecting a bitrate for MP3 or Ogg/Vorbis
-------------------
begin : Thu Oct 24 2002
copyright : (C) 2002 by Thomas Eschenbacher
email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QSlider>
#include <QSpinBox>
#include <QtGlobal>
#include "libkwave/Utils.h"
#include "BitrateSpinBox.h"
#include "BitrateWidget.h"
/***************************************************************************/
Kwave::BitrateWidget::BitrateWidget(QWidget *parent)
:QWidget(parent),
Ui::BitrateWidgetBase(), m_rates()
{
setupUi(this);
m_rates.append(0); // don't let it stay empty, that makes life easier
connect(slider, SIGNAL(valueChanged(int)),
this, SLOT(sliderChanged(int)));
connect(spinbox, SIGNAL(valueChanged(int)),
this, SLOT(spinboxChanged(int)));
connect(slider, SIGNAL(sliderReleased()),
this, SLOT(snapInSlider()));
connect(spinbox, SIGNAL(snappedIn(int)),
slider, SLOT(setValue(int)));
}
/***************************************************************************/
Kwave::BitrateWidget::~BitrateWidget()
{
}
/***************************************************************************/
void Kwave::BitrateWidget::setValue(int bitrate)
{
slider->setValue(bitrate);
spinbox->setValue(bitrate);
}
/***************************************************************************/
int Kwave::BitrateWidget::value()
{
int value = slider->value();
int index = nearestIndex(value);
return m_rates[index];
}
/***************************************************************************/
void Kwave::BitrateWidget::setSpecialValueText(const QString &text)
{
spinbox->setSpecialValueText(text);
}
/***************************************************************************/
void Kwave::BitrateWidget::allowRates(const QList<int> &list)
{
int old_value = value();
m_rates = list;
if (m_rates.isEmpty()) m_rates.append(0);
// set new ranges
spinbox->allowRates(m_rates);
slider->setMinimum(m_rates.first());
slider->setMaximum(m_rates.last());
setValue(old_value);
}
//***************************************************************************
int Kwave::BitrateWidget::nearestIndex(int rate)
{
// find the nearest value
int nearest = 0;
foreach(int i, m_rates)
if (qAbs(i - rate) < qAbs(nearest - rate)) nearest = i;
// find the index
qsizetype index = m_rates.contains(nearest) ?
m_rates.indexOf(nearest) : 0;
// limit the index into a reasonable range
index = qBound(0, index, m_rates.size() - 1);
return static_cast<int>(index);
}
//***************************************************************************
void Kwave::BitrateWidget::sliderChanged(int value)
{
int index = nearestIndex(value);
spinbox->setValue(m_rates[index]);
emit valueChanged(value);
}
//***************************************************************************
void Kwave::BitrateWidget::spinboxChanged(int value)
{
emit valueChanged(value);
}
//***************************************************************************
void Kwave::BitrateWidget::snapInSlider()
{
int slider_value = slider->value();
int index = nearestIndex(slider_value);
slider->setValue(m_rates[index]); // snap in
}
//***************************************************************************
//***************************************************************************
#include "moc_BitrateWidget.cpp"
|