annotate widgets/ListInputDialog.cpp @ 854:c17719e488c9

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