ListInputDialog.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "ListInputDialog.h"
17 
18 #include <QVBoxLayout>
19 #include <QHBoxLayout>
20 #include <QLabel>
21 #include <QStringList>
22 #include <QRadioButton>
23 #include <QPushButton>
24 #include <QDialogButtonBox>
25 
26 ListInputDialog::ListInputDialog(QWidget *parent, const QString &title,
27  const QString &labelText, const QStringList &list,
28  int current) :
29  QDialog(parent),
30  m_strings(list)
31 {
32  setWindowTitle(title);
33 
34  QVBoxLayout *vbox = new QVBoxLayout(this);
35 
36  QLabel *label = new QLabel(labelText, this);
37  vbox->addWidget(label);
38  vbox->addStretch(1);
39 
40  int count = 0;
41  for (QStringList::const_iterator i = list.begin(); i != list.end(); ++i) {
42  QRadioButton *radio = new QRadioButton(*i);
43  if (current == count++) radio->setChecked(true);
44  m_radioButtons.push_back(radio);
45  vbox->addWidget(radio);
46  }
47 
48  vbox->addStretch(1);
49 
50  m_footnote = new QLabel;
51  vbox->addWidget(m_footnote);
52  m_footnote->hide();
53 
54  QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
55  QDialogButtonBox::Cancel);
56  vbox->addWidget(bb);
57  connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
58  connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
59 }
60 
62 {
63 }
64 
65 QString
67 {
68  for (size_t i = 0; i < m_radioButtons.size(); ++i) {
69  if (m_radioButtons[i]->isChecked()) {
70  return m_strings[int(i)];
71  }
72  }
73  return "";
74 }
75 
76 void
77 ListInputDialog::setItemAvailability(int item, bool available)
78 {
79  m_radioButtons[item]->setEnabled(available);
80 }
81 
82 void
84 {
85  m_footnote->setText(footnote);
86  m_footnote->show();
87 }
88 
89 QString
90 ListInputDialog::getItem(QWidget *parent, const QString &title,
91  const QString &label, const QStringList &list,
92  int current, bool *ok)
93 {
94  ListInputDialog dialog(parent, title, label, list, current);
95 
96  bool accepted = (dialog.exec() == QDialog::Accepted);
97  if (ok) *ok = accepted;
98 
99  return dialog.getCurrentString();
100 }
101 
std::vector< QRadioButton * > m_radioButtons
QStringList m_strings
ListInputDialog(QWidget *parent, const QString &title, const QString &label, const QStringList &list, int current=0)
Like QInputDialog::getItem(), except that it offers the items as a set of radio buttons instead of in...
virtual ~ListInputDialog()
QString getCurrentString() const
void setFootnote(QString footnote)
void setItemAvailability(int item, bool available)
static QString getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &list, int current=0, bool *ok=0)