comparison 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
comparison
equal deleted inserted replaced
1318:b149b53df365 1319:fbda05431ce0
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-2018 Chris Cannam and QMUL.
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 "CSVAudioFormatDialog.h"
17
18 #include "layer/LayerFactory.h"
19
20 #include "TextAbbrev.h"
21
22 #include <QFrame>
23 #include <QGridLayout>
24 #include <QPushButton>
25 #include <QHBoxLayout>
26 #include <QVBoxLayout>
27 #include <QTableWidget>
28 #include <QComboBox>
29 #include <QLabel>
30 #include <QDialogButtonBox>
31
32 #include <iostream>
33 #include <cmath>
34
35 #include "base/Debug.h"
36
37 CSVAudioFormatDialog::CSVAudioFormatDialog(QWidget *parent, CSVFormat format,
38 int maxDisplayCols) :
39 QDialog(parent),
40 m_format(format),
41 m_maxDisplayCols(maxDisplayCols),
42 m_fuzzyColumn(-1)
43 {
44 setModal(true);
45 setWindowTitle(tr("Select Audio Data Format"));
46
47 QGridLayout *layout = new QGridLayout;
48
49 int row = 0;
50
51 layout->addWidget
52 (new QLabel(tr("Please select the correct data format for this file.")),
53 row++, 0, 1, 4);
54
55 QFrame *exampleFrame = new QFrame;
56 exampleFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
57 exampleFrame->setLineWidth(2);
58 QGridLayout *exampleLayout = new QGridLayout;
59 exampleLayout->setSpacing(4);
60 exampleFrame->setLayout(exampleLayout);
61
62 QPalette palette = exampleFrame->palette();
63 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
64 exampleFrame->setPalette(palette);
65
66 QFont fp;
67 fp.setPointSize(int(floor(fp.pointSize() * 0.9)));
68
69 int columns = format.getColumnCount();
70 QList<QStringList> example = m_format.getExample();
71
72 for (int i = 0; i < columns; ++i) {
73
74 QComboBox *cpc = new QComboBox;
75 m_columnPurposeCombos.push_back(cpc);
76 exampleLayout->addWidget(cpc, 0, i);
77 connect(cpc, SIGNAL(activated(int)), this, SLOT(columnPurposeChanged(int)));
78
79 if (i == m_maxDisplayCols && columns > i + 2) {
80 m_fuzzyColumn = i;
81
82 cpc->addItem(tr("<ignore>"));
83 cpc->addItem(tr("Audio channels"));
84 cpc->setCurrentIndex
85 (m_format.isColumnNumeric(i-1) ? 1 : 0);
86
87 exampleLayout->addWidget
88 (new QLabel(tr("(%1 more)").arg(columns - i)), 1, i);
89 break;
90 }
91
92 cpc->addItem(tr("<ignore>"));
93 cpc->addItem(tr("Audio channel"));
94 if (m_format.isColumnNumeric(i)) {
95 cpc->setCurrentIndex(1);
96 } else {
97 cpc->setCurrentIndex(0);
98 }
99
100 for (int j = 0; j < example.size() && j < 6; ++j) {
101 if (i >= example[j].size()) {
102 continue;
103 }
104 QLabel *label = new QLabel;
105 label->setTextFormat(Qt::PlainText);
106 QString text = TextAbbrev::abbreviate(example[j][i], 35);
107 label->setText(text);
108 label->setFont(fp);
109 label->setPalette(palette);
110 label->setIndent(8);
111 exampleLayout->addWidget(label, j+1, i);
112 }
113 }
114
115 layout->addWidget(exampleFrame, row, 0, 1, 4);
116 layout->setColumnStretch(3, 10);
117 layout->setRowStretch(row++, 10);
118
119 m_sampleRateLabel = new QLabel(tr("Audio sample rate (Hz):"));
120 layout->addWidget(m_sampleRateLabel, row, 0);
121
122 int sampleRates[] = {
123 8000, 11025, 12000, 22050, 24000, 32000,
124 44100, 48000, 88200, 96000, 176400, 192000
125 };
126
127 m_sampleRateCombo = new QComboBox;
128 for (int i = 0; i < int(sizeof(sampleRates) / sizeof(sampleRates[0])); ++i) {
129 m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i]));
130 if (sampleRates[i] == m_format.getSampleRate()) {
131 m_sampleRateCombo->setCurrentIndex(i);
132 }
133 }
134 m_sampleRateCombo->setEditable(true);
135
136 layout->addWidget(m_sampleRateCombo, row++, 1);
137 connect(m_sampleRateCombo, SIGNAL(activated(QString)),
138 this, SLOT(sampleRateChanged(QString)));
139 connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)),
140 this, SLOT(sampleRateChanged(QString)));
141
142 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
143 QDialogButtonBox::Cancel);
144 layout->addWidget(bb, row++, 0, 1, 4);
145 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
146 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
147
148 setLayout(layout);
149
150 updateFormatFromDialog();
151 }
152
153 CSVAudioFormatDialog::~CSVAudioFormatDialog()
154 {
155 }
156
157 CSVFormat
158 CSVAudioFormatDialog::getFormat() const
159 {
160 return m_format;
161 }
162
163 void
164 CSVAudioFormatDialog::sampleRateChanged(QString rateString)
165 {
166 bool ok = false;
167 int sampleRate = rateString.toInt(&ok);
168 if (ok) m_format.setSampleRate(sampleRate);
169 }
170
171 void
172 CSVAudioFormatDialog::columnPurposeChanged(int)
173 {
174 updateFormatFromDialog();
175 }
176
177 void
178 CSVAudioFormatDialog::updateFormatFromDialog()
179 {
180 m_format.setModelType(CSVFormat::WaveFileModel);
181 m_format.setTimingType(CSVFormat::ImplicitTiming);
182 m_format.setTimeUnits(CSVFormat::TimeAudioFrames);
183
184 for (int i = 0; i < m_columnPurposeCombos.size(); ++i) {
185
186 QComboBox *thisCombo = m_columnPurposeCombos[i];
187
188 CSVFormat::ColumnPurpose purpose = (thisCombo->currentIndex() == 1 ?
189 CSVFormat::ColumnValue :
190 CSVFormat::ColumnUnknown);
191
192 if (i == m_fuzzyColumn) {
193 for (int j = i; j < m_format.getColumnCount(); ++j) {
194 m_format.setColumnPurpose(j, purpose);
195 }
196 } else {
197 m_format.setColumnPurpose(i, purpose);
198 }
199 }
200 }
201
202
203