Chris@188: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@188: 
Chris@188: /*
Chris@188:     Sonic Visualiser
Chris@188:     An audio file viewer and annotation editor.
Chris@188:     Centre for Digital Music, Queen Mary, University of London.
Chris@188:     This file copyright 2007 QMUL.
Chris@188:     
Chris@188:     This program is free software; you can redistribute it and/or
Chris@188:     modify it under the terms of the GNU General Public License as
Chris@188:     published by the Free Software Foundation; either version 2 of the
Chris@188:     License, or (at your option) any later version.  See the file
Chris@188:     COPYING included with this distribution for more information.
Chris@188: */
Chris@188: 
Chris@188: #include "RangeInputDialog.h"
Chris@188: 
Chris@188: #include <QDoubleSpinBox>
Chris@188: #include <QGridLayout>
Chris@188: #include <QLabel>
Chris@188: #include <QHBoxLayout>
Chris@282: #include <QDialogButtonBox>
Chris@188: #include <QPushButton>
Chris@188: 
Chris@188: RangeInputDialog::RangeInputDialog(QString title, QString message,
Chris@188:                                    QString unit, float min, float max, 
Chris@188:                                    QWidget *parent) :
Chris@188:     QDialog(parent)
Chris@188: {
Chris@188:     QGridLayout *grid = new QGridLayout;
Chris@188:     setLayout(grid);
Chris@188: 
Chris@188:     setWindowTitle(title);
Chris@188:     
Chris@188:     QLabel *messageLabel = new QLabel;
Chris@188:     messageLabel->setText(message);
Chris@188:     grid->addWidget(messageLabel, 0, 0, 1, 5);
Chris@188: 
Chris@188:     m_rangeStart = new QDoubleSpinBox;
Chris@188:     m_rangeStart->setDecimals(4);
Chris@188:     m_rangeStart->setMinimum(min);
Chris@188:     m_rangeStart->setMaximum(max);
Chris@188:     m_rangeStart->setSuffix(unit);
Chris@188:     grid->addWidget(m_rangeStart, 1, 1);
Chris@188:     connect(m_rangeStart, SIGNAL(valueChanged(double)),
Chris@188:             this, SLOT(rangeStartChanged(double)));
Chris@188:     
Chris@188:     grid->addWidget(new QLabel(tr(" to ")), 1, 2);
Chris@188: 
Chris@188:     m_rangeEnd = new QDoubleSpinBox;
Chris@188:     m_rangeEnd->setDecimals(4);
Chris@188:     m_rangeEnd->setMinimum(min);
Chris@188:     m_rangeEnd->setMaximum(max);
Chris@188:     m_rangeEnd->setSuffix(unit);
Chris@188:     grid->addWidget(m_rangeEnd, 1, 3);
Chris@188:     connect(m_rangeEnd, SIGNAL(valueChanged(double)),
Chris@188:             this, SLOT(rangeEndChanged(double)));
Chris@188: 
Chris@282:     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@282:                                                 QDialogButtonBox::Cancel);
Chris@282:     grid->addWidget(bb, 2, 0, 1, 5);
Chris@282:     connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@282:     connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@188: }
Chris@188: 
Chris@188: RangeInputDialog::~RangeInputDialog()
Chris@188: {
Chris@188: }
Chris@188: 
Chris@188: void
Chris@188: RangeInputDialog::getRange(float &min, float &max)
Chris@188: {
Chris@188:     min = float(m_rangeStart->value());
Chris@188:     max = float(m_rangeEnd->value());
Chris@188: 
Chris@188:     if (min > max) {
Chris@188:         float tmp = min;
Chris@188:         min = max;
Chris@188:         max = tmp;
Chris@188:     }
Chris@188: }
Chris@188: 
Chris@188: void
Chris@188: RangeInputDialog::setRange(float start, float end)
Chris@188: {
Chris@188:     if (start > end) {
Chris@188:         float tmp = start;
Chris@188:         start = end;
Chris@188:         end = tmp;
Chris@188:     }
Chris@188: 
Chris@188:     blockSignals(true);
Chris@188:     m_rangeStart->setValue(start);
Chris@188:     m_rangeEnd->setValue(end);
Chris@188:     blockSignals(false);
Chris@188: }
Chris@188: 
Chris@188: void
Chris@188: RangeInputDialog::rangeStartChanged(double min)
Chris@188: {
Chris@188:     double max = m_rangeEnd->value();
Chris@188:     if (min > max) {
Chris@188:         double tmp = min;
Chris@188:         min = max;
Chris@188:         max = tmp;
Chris@188:     }
Chris@188:     emit rangeChanged(float(min), float(max));
Chris@188: }
Chris@188: 
Chris@188: 
Chris@188: void
Chris@188: RangeInputDialog::rangeEndChanged(double max)
Chris@188: {
Chris@188:     double min = m_rangeStart->value();
Chris@188:     if (min > max) {
Chris@188:         double tmp = min;
Chris@188:         min = max;
Chris@188:         max = tmp;
Chris@188:     }
Chris@188:     emit rangeChanged(float(min), float(max));
Chris@188: }
Chris@188: