comparison widgets/ListInputDialog.cpp @ 126:0e95c127bb53

* New dialog for selection from a list of items through radio buttons rather than a combobox * Move system-specific init stuff out into another file (to avoid Window naming conflict (X11 vs base/Window.h))
author Chris Cannam
date Thu, 27 Jul 2006 16:08:04 +0000
parents
children 387f2f6fc333
comparison
equal deleted inserted replaced
125:999ae0f7d10c 126:0e95c127bb53
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
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #include "ListInputDialog.h"
16
17 #include <QVBoxLayout>
18 #include <QHBoxLayout>
19 #include <QLabel>
20 #include <QStringList>
21 #include <QRadioButton>
22 #include <QPushButton>
23
24 ListInputDialog::ListInputDialog(QWidget *parent, const QString &title,
25 const QString &labelText, const QStringList &list,
26 int current, Qt::WFlags f) :
27 QDialog(parent, f),
28 m_strings(list)
29 {
30 setWindowTitle(title);
31
32 QVBoxLayout *vbox = new QVBoxLayout(this);
33
34 QLabel *label = new QLabel(labelText, this);
35 vbox->addWidget(label);
36 vbox->addStretch(1);
37
38 int count = 0;
39 for (QStringList::const_iterator i = list.begin(); i != list.end(); ++i) {
40 QRadioButton *radio = new QRadioButton(*i);
41 if (current == count++) radio->setChecked(true);
42 m_radioButtons.push_back(radio);
43 vbox->addWidget(radio);
44 }
45
46 vbox->addStretch(1);
47
48 QHBoxLayout *hbox = new QHBoxLayout;
49 vbox->addLayout(hbox, Qt::AlignRight);
50
51 QPushButton *ok = new QPushButton(tr("OK"), this);
52 ok->setDefault(true);
53
54 QPushButton *cancel = new QPushButton(tr("Cancel"), this);
55
56 QSize bs = ok->sizeHint().expandedTo(cancel->sizeHint());
57 ok->setFixedSize(bs);
58 cancel->setFixedSize(bs);
59
60 hbox->addStretch();
61 hbox->addWidget(ok);
62 hbox->addWidget(cancel);
63
64 QObject::connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
65 QObject::connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
66 }
67
68 ListInputDialog::~ListInputDialog()
69 {
70 }
71
72 QString
73 ListInputDialog::getCurrentString() const
74 {
75 for (int i = 0; i < m_radioButtons.size(); ++i) {
76 if (m_radioButtons[i]->isChecked()) {
77 return m_strings[i];
78 }
79 }
80 return "";
81 }
82
83 QString
84 ListInputDialog::getItem(QWidget *parent, const QString &title,
85 const QString &label, const QStringList &list,
86 int current, bool *ok, Qt::WFlags f)
87 {
88 ListInputDialog dialog(parent, title, label, list, current, f);
89
90 bool accepted = (dialog.exec() == QDialog::Accepted);
91 if (ok) *ok = accepted;
92
93 return dialog.getCurrentString();
94 }
95