annotate widgets/CSVAudioFormatDialog.cpp @ 1319:fbda05431ce0 import-audio-data

Refactor; the two use-cases of CSVFormatDialog were turning out to be different enough to justify two separate classes
author Chris Cannam
date Thu, 06 Sep 2018 13:58:09 +0100
parents widgets/CSVFormatDialog.cpp@b149b53df365
children 6440ba1ffc86
rev   line source
Chris@378 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@378 2
Chris@378 3 /*
Chris@378 4 Sonic Visualiser
Chris@378 5 An audio file viewer and annotation editor.
Chris@378 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1319 7 This file copyright 2006-2018 Chris Cannam and QMUL.
Chris@378 8
Chris@378 9 This program is free software; you can redistribute it and/or
Chris@378 10 modify it under the terms of the GNU General Public License as
Chris@378 11 published by the Free Software Foundation; either version 2 of the
Chris@378 12 License, or (at your option) any later version. See the file
Chris@378 13 COPYING included with this distribution for more information.
Chris@378 14 */
Chris@378 15
Chris@1319 16 #include "CSVAudioFormatDialog.h"
Chris@378 17
Chris@561 18 #include "layer/LayerFactory.h"
Chris@561 19
Chris@674 20 #include "TextAbbrev.h"
Chris@674 21
Chris@378 22 #include <QFrame>
Chris@378 23 #include <QGridLayout>
Chris@378 24 #include <QPushButton>
Chris@378 25 #include <QHBoxLayout>
Chris@378 26 #include <QVBoxLayout>
Chris@378 27 #include <QTableWidget>
Chris@378 28 #include <QComboBox>
Chris@378 29 #include <QLabel>
Chris@512 30 #include <QDialogButtonBox>
Chris@378 31
Chris@560 32 #include <iostream>
Chris@913 33 #include <cmath>
Chris@378 34
Chris@682 35 #include "base/Debug.h"
Chris@682 36
Chris@1319 37 CSVAudioFormatDialog::CSVAudioFormatDialog(QWidget *parent, CSVFormat format,
Chris@1319 38 int maxDisplayCols) :
Chris@378 39 QDialog(parent),
Chris@581 40 m_format(format),
Chris@581 41 m_maxDisplayCols(maxDisplayCols),
Chris@581 42 m_fuzzyColumn(-1)
Chris@378 43 {
Chris@378 44 setModal(true);
Chris@1319 45 setWindowTitle(tr("Select Audio Data Format"));
Chris@378 46
Chris@378 47 QGridLayout *layout = new QGridLayout;
Chris@378 48
Chris@560 49 int row = 0;
Chris@378 50
Chris@1318 51 layout->addWidget
Chris@1318 52 (new QLabel(tr("Please select the correct data format for this file.")),
Chris@1318 53 row++, 0, 1, 4);
Chris@560 54
Chris@560 55 QFrame *exampleFrame = new QFrame;
Chris@560 56 exampleFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
Chris@560 57 exampleFrame->setLineWidth(2);
Chris@560 58 QGridLayout *exampleLayout = new QGridLayout;
Chris@561 59 exampleLayout->setSpacing(4);
Chris@560 60 exampleFrame->setLayout(exampleLayout);
Chris@560 61
Chris@560 62 QPalette palette = exampleFrame->palette();
Chris@560 63 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
Chris@560 64 exampleFrame->setPalette(palette);
Chris@560 65
Chris@560 66 QFont fp;
Chris@908 67 fp.setPointSize(int(floor(fp.pointSize() * 0.9)));
Chris@560 68
Chris@560 69 int columns = format.getColumnCount();
Chris@560 70 QList<QStringList> example = m_format.getExample();
Chris@560 71
Chris@560 72 for (int i = 0; i < columns; ++i) {
Chris@581 73
Chris@560 74 QComboBox *cpc = new QComboBox;
Chris@560 75 m_columnPurposeCombos.push_back(cpc);
Chris@560 76 exampleLayout->addWidget(cpc, 0, i);
Chris@581 77 connect(cpc, SIGNAL(activated(int)), this, SLOT(columnPurposeChanged(int)));
Chris@1318 78
Chris@581 79 if (i == m_maxDisplayCols && columns > i + 2) {
Chris@581 80 m_fuzzyColumn = i;
Chris@1318 81
Chris@1319 82 cpc->addItem(tr("<ignore>"));
Chris@1319 83 cpc->addItem(tr("Audio channels"));
Chris@1319 84 cpc->setCurrentIndex
Chris@1319 85 (m_format.isColumnNumeric(i-1) ? 1 : 0);
Chris@1319 86
Chris@1318 87 exampleLayout->addWidget
Chris@1318 88 (new QLabel(tr("(%1 more)").arg(columns - i)), 1, i);
Chris@581 89 break;
Chris@581 90 }
Chris@560 91
Chris@1319 92 cpc->addItem(tr("<ignore>"));
Chris@1319 93 cpc->addItem(tr("Audio channel"));
Chris@1319 94 if (m_format.isColumnNumeric(i)) {
Chris@1319 95 cpc->setCurrentIndex(1);
Chris@1318 96 } else {
Chris@1319 97 cpc->setCurrentIndex(0);
Chris@1318 98 }
Chris@1318 99
Chris@581 100 for (int j = 0; j < example.size() && j < 6; ++j) {
Chris@866 101 if (i >= example[j].size()) {
Chris@866 102 continue;
Chris@866 103 }
Chris@581 104 QLabel *label = new QLabel;
Chris@581 105 label->setTextFormat(Qt::PlainText);
Chris@674 106 QString text = TextAbbrev::abbreviate(example[j][i], 35);
Chris@674 107 label->setText(text);
Chris@581 108 label->setFont(fp);
Chris@581 109 label->setPalette(palette);
Chris@581 110 label->setIndent(8);
Chris@581 111 exampleLayout->addWidget(label, j+1, i);
Chris@560 112 }
Chris@560 113 }
Chris@560 114
Chris@560 115 layout->addWidget(exampleFrame, row, 0, 1, 4);
Chris@560 116 layout->setColumnStretch(3, 10);
Chris@560 117 layout->setRowStretch(row++, 10);
Chris@560 118
Chris@378 119 m_sampleRateLabel = new QLabel(tr("Audio sample rate (Hz):"));
Chris@560 120 layout->addWidget(m_sampleRateLabel, row, 0);
Chris@378 121
Chris@807 122 int sampleRates[] = {
Chris@1266 123 8000, 11025, 12000, 22050, 24000, 32000,
Chris@1266 124 44100, 48000, 88200, 96000, 176400, 192000
Chris@378 125 };
Chris@378 126
Chris@378 127 m_sampleRateCombo = new QComboBox;
Chris@807 128 for (int i = 0; i < int(sizeof(sampleRates) / sizeof(sampleRates[0])); ++i) {
Chris@1266 129 m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i]));
Chris@1266 130 if (sampleRates[i] == m_format.getSampleRate()) {
Chris@560 131 m_sampleRateCombo->setCurrentIndex(i);
Chris@560 132 }
Chris@378 133 }
Chris@378 134 m_sampleRateCombo->setEditable(true);
Chris@378 135
Chris@560 136 layout->addWidget(m_sampleRateCombo, row++, 1);
Chris@378 137 connect(m_sampleRateCombo, SIGNAL(activated(QString)),
Chris@1266 138 this, SLOT(sampleRateChanged(QString)));
Chris@378 139 connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)),
Chris@1266 140 this, SLOT(sampleRateChanged(QString)));
Chris@378 141
Chris@512 142 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@512 143 QDialogButtonBox::Cancel);
Chris@560 144 layout->addWidget(bb, row++, 0, 1, 4);
Chris@512 145 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@512 146 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@378 147
Chris@512 148 setLayout(layout);
Chris@378 149
Chris@1319 150 updateFormatFromDialog();
Chris@378 151 }
Chris@378 152
Chris@1319 153 CSVAudioFormatDialog::~CSVAudioFormatDialog()
Chris@378 154 {
Chris@378 155 }
Chris@378 156
Chris@378 157 CSVFormat
Chris@1319 158 CSVAudioFormatDialog::getFormat() const
Chris@378 159 {
Chris@560 160 return m_format;
Chris@378 161 }
Chris@378 162
Chris@378 163 void
Chris@1319 164 CSVAudioFormatDialog::sampleRateChanged(QString rateString)
Chris@378 165 {
Chris@378 166 bool ok = false;
Chris@378 167 int sampleRate = rateString.toInt(&ok);
Chris@560 168 if (ok) m_format.setSampleRate(sampleRate);
Chris@378 169 }
Chris@378 170
Chris@378 171 void
Chris@1319 172 CSVAudioFormatDialog::columnPurposeChanged(int)
Chris@378 173 {
Chris@1318 174 updateFormatFromDialog();
Chris@1318 175 }
Chris@1318 176
Chris@1318 177 void
Chris@1319 178 CSVAudioFormatDialog::updateFormatFromDialog()
Chris@1318 179 {
Chris@1319 180 m_format.setModelType(CSVFormat::WaveFileModel);
Chris@1319 181 m_format.setTimingType(CSVFormat::ImplicitTiming);
Chris@1319 182 m_format.setTimeUnits(CSVFormat::TimeAudioFrames);
Chris@1318 183
Chris@959 184 for (int i = 0; i < m_columnPurposeCombos.size(); ++i) {
Chris@959 185
Chris@959 186 QComboBox *thisCombo = m_columnPurposeCombos[i];
Chris@959 187
Chris@1319 188 CSVFormat::ColumnPurpose purpose = (thisCombo->currentIndex() == 1 ?
Chris@1319 189 CSVFormat::ColumnValue :
Chris@1319 190 CSVFormat::ColumnUnknown);
Chris@1319 191
Chris@959 192 if (i == m_fuzzyColumn) {
Chris@992 193 for (int j = i; j < m_format.getColumnCount(); ++j) {
Chris@1319 194 m_format.setColumnPurpose(j, purpose);
Chris@959 195 }
Chris@992 196 } else {
Chris@992 197 m_format.setColumnPurpose(i, purpose);
Chris@959 198 }
Chris@561 199 }
Chris@560 200 }
Chris@560 201
Chris@560 202
Chris@959 203