svgui  1.9
ListInputDialog.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "ListInputDialog.h"
00017 
00018 #include <QVBoxLayout>
00019 #include <QHBoxLayout>
00020 #include <QLabel>
00021 #include <QStringList>
00022 #include <QRadioButton>
00023 #include <QPushButton>
00024 #include <QDialogButtonBox>
00025 
00026 ListInputDialog::ListInputDialog(QWidget *parent, const QString &title,
00027                                  const QString &labelText, const QStringList &list,
00028                                  int current) :
00029     QDialog(parent),
00030     m_strings(list)
00031 {
00032     setWindowTitle(title);
00033 
00034     QVBoxLayout *vbox = new QVBoxLayout(this);
00035     
00036     QLabel *label = new QLabel(labelText, this);
00037     vbox->addWidget(label);
00038     vbox->addStretch(1);
00039 
00040     int count = 0;
00041     for (QStringList::const_iterator i = list.begin(); i != list.end(); ++i) {
00042         QRadioButton *radio = new QRadioButton(*i);
00043         if (current == count++) radio->setChecked(true);
00044         m_radioButtons.push_back(radio);
00045         vbox->addWidget(radio);
00046     }
00047 
00048     vbox->addStretch(1);
00049 
00050     m_footnote = new QLabel;
00051     vbox->addWidget(m_footnote);
00052     m_footnote->hide();
00053     
00054     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
00055                                                 QDialogButtonBox::Cancel);
00056     vbox->addWidget(bb);
00057     connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
00058     connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
00059 }
00060 
00061 ListInputDialog::~ListInputDialog()
00062 {
00063 }
00064 
00065 QString
00066 ListInputDialog::getCurrentString() const
00067 {
00068     for (size_t i = 0; i < m_radioButtons.size(); ++i) {
00069         if (m_radioButtons[i]->isChecked()) {
00070             return m_strings[i];
00071         }
00072     }
00073     return "";
00074 }
00075 
00076 void
00077 ListInputDialog::setItemAvailability(int item, bool available)
00078 {
00079     m_radioButtons[item]->setEnabled(available);
00080 }
00081 
00082 void
00083 ListInputDialog::setFootnote(QString footnote)
00084 {
00085     m_footnote->setText(footnote);
00086     m_footnote->show();
00087 }
00088 
00089 QString
00090 ListInputDialog::getItem(QWidget *parent, const QString &title,
00091                          const QString &label, const QStringList &list,
00092                          int current, bool *ok)
00093 {
00094     ListInputDialog dialog(parent, title, label, list, current);
00095     
00096     bool accepted = (dialog.exec() == QDialog::Accepted);
00097     if (ok) *ok = accepted;
00098 
00099     return dialog.getCurrentString();
00100 }
00101