comparison widgets/CSVFormatDialog.cpp @ 378:22b72f0f6a4e

* More work to abstract out interactive components used in the data library, so that it does not need to depend on QtGui.
author Chris Cannam
date Fri, 14 Mar 2008 17:14:21 +0000
parents
children ed65126e503d
comparison
equal deleted inserted replaced
377:0bcb449d15f4 378:22b72f0f6a4e
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 "CSVFormatDialog.h"
17
18 #include <QFrame>
19 #include <QGridLayout>
20 #include <QPushButton>
21 #include <QHBoxLayout>
22 #include <QVBoxLayout>
23 #include <QTableWidget>
24 #include <QComboBox>
25 #include <QLabel>
26
27
28 CSVFormatDialog::CSVFormatDialog(QWidget *parent, CSVFormat format,
29 size_t defaultSampleRate) :
30 QDialog(parent),
31 m_modelType(CSVFormat::OneDimensionalModel),
32 m_timingType(CSVFormat::ExplicitTiming),
33 m_timeUnits(CSVFormat::TimeAudioFrames),
34 m_separator(""),
35 m_behaviour(QString::KeepEmptyParts)
36 {
37 setModal(true);
38 setWindowTitle(tr("Select Data Format"));
39
40 m_modelType = format.getModelType();
41 m_timingType = format.getTimingType();
42 m_timeUnits = format.getTimeUnits();
43 m_separator = format.getSeparator();
44 m_sampleRate = format.getSampleRate();
45 m_windowSize = format.getWindowSize();
46 m_behaviour = format.getSplitBehaviour();
47 m_example = format.getExample();
48 m_maxExampleCols = format.getMaxExampleCols();
49
50 QGridLayout *layout = new QGridLayout;
51
52 layout->addWidget(new QLabel(tr("<b>Select Data Format</b><p>Please select the correct data format for this file.")),
53 0, 0, 1, 4);
54
55 layout->addWidget(new QLabel(tr("Each row specifies:")), 1, 0);
56
57 m_modelTypeCombo = new QComboBox;
58 m_modelTypeCombo->addItem(tr("A point in time"));
59 m_modelTypeCombo->addItem(tr("A value at a time"));
60 m_modelTypeCombo->addItem(tr("A set of values"));
61 layout->addWidget(m_modelTypeCombo, 1, 1, 1, 2);
62 connect(m_modelTypeCombo, SIGNAL(activated(int)),
63 this, SLOT(modelTypeChanged(int)));
64 m_modelTypeCombo->setCurrentIndex(int(m_modelType));
65
66 layout->addWidget(new QLabel(tr("The first column contains:")), 2, 0);
67
68 m_timingTypeCombo = new QComboBox;
69 m_timingTypeCombo->addItem(tr("Time, in seconds"));
70 m_timingTypeCombo->addItem(tr("Time, in audio sample frames"));
71 m_timingTypeCombo->addItem(tr("Data (rows are consecutive in time)"));
72 layout->addWidget(m_timingTypeCombo, 2, 1, 1, 2);
73 connect(m_timingTypeCombo, SIGNAL(activated(int)),
74 this, SLOT(timingTypeChanged(int)));
75 m_timingTypeCombo->setCurrentIndex(m_timingType == CSVFormat::ExplicitTiming ?
76 m_timeUnits == CSVFormat::TimeSeconds ? 0 : 1 : 2);
77
78 m_sampleRateLabel = new QLabel(tr("Audio sample rate (Hz):"));
79 layout->addWidget(m_sampleRateLabel, 3, 0);
80
81 size_t sampleRates[] = {
82 8000, 11025, 12000, 22050, 24000, 32000,
83 44100, 48000, 88200, 96000, 176400, 192000
84 };
85
86 m_sampleRateCombo = new QComboBox;
87 m_sampleRate = defaultSampleRate;
88 for (size_t i = 0; i < sizeof(sampleRates) / sizeof(sampleRates[0]); ++i) {
89 m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i]));
90 if (sampleRates[i] == m_sampleRate) m_sampleRateCombo->setCurrentIndex(i);
91 }
92 m_sampleRateCombo->setEditable(true);
93
94 layout->addWidget(m_sampleRateCombo, 3, 1);
95 connect(m_sampleRateCombo, SIGNAL(activated(QString)),
96 this, SLOT(sampleRateChanged(QString)));
97 connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)),
98 this, SLOT(sampleRateChanged(QString)));
99
100 m_windowSizeLabel = new QLabel(tr("Frame increment between rows:"));
101 layout->addWidget(m_windowSizeLabel, 4, 0);
102
103 m_windowSizeCombo = new QComboBox;
104 m_windowSize = 1024;
105 for (int i = 0; i <= 16; ++i) {
106 int value = 1 << i;
107 m_windowSizeCombo->addItem(QString("%1").arg(value));
108 if (value == int(m_windowSize)) m_windowSizeCombo->setCurrentIndex(i);
109 }
110 m_windowSizeCombo->setEditable(true);
111
112 layout->addWidget(m_windowSizeCombo, 4, 1);
113 connect(m_windowSizeCombo, SIGNAL(activated(QString)),
114 this, SLOT(windowSizeChanged(QString)));
115 connect(m_windowSizeCombo, SIGNAL(editTextChanged(QString)),
116 this, SLOT(windowSizeChanged(QString)));
117
118 layout->addWidget(new QLabel(tr("\nExample data from file:")), 5, 0, 1, 4);
119
120 m_exampleWidget = new QTableWidget
121 (std::min(10, m_example.size()), m_maxExampleCols);
122
123 layout->addWidget(m_exampleWidget, 6, 0, 1, 4);
124 layout->setColumnStretch(3, 10);
125 layout->setRowStretch(4, 10);
126
127 QPushButton *ok = new QPushButton(tr("OK"));
128 connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
129 ok->setDefault(true);
130
131 QPushButton *cancel = new QPushButton(tr("Cancel"));
132 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
133
134 QHBoxLayout *buttonLayout = new QHBoxLayout;
135 buttonLayout->addStretch(1);
136 buttonLayout->addWidget(ok);
137 buttonLayout->addWidget(cancel);
138
139 QVBoxLayout *mainLayout = new QVBoxLayout;
140 mainLayout->addLayout(layout);
141 mainLayout->addLayout(buttonLayout);
142
143 setLayout(mainLayout);
144
145 timingTypeChanged(m_timingTypeCombo->currentIndex());
146 }
147
148 CSVFormatDialog::~CSVFormatDialog()
149 {
150 }
151
152 CSVFormat
153 CSVFormatDialog::getFormat() const
154 {
155 CSVFormat format;
156 format.setModelType(m_modelType);
157 format.setTimingType(m_timingType);
158 format.setTimeUnits(m_timeUnits);
159 format.setSeparator(m_separator);
160 format.setSampleRate(m_sampleRate);
161 format.setWindowSize(m_windowSize);
162 format.setSplitBehaviour(m_behaviour);
163 return format;
164 }
165
166 void
167 CSVFormatDialog::populateExample()
168 {
169 m_exampleWidget->setColumnCount
170 (m_timingType == CSVFormat::ExplicitTiming ?
171 m_maxExampleCols - 1 : m_maxExampleCols);
172
173 m_exampleWidget->setHorizontalHeaderLabels(QStringList());
174
175 for (int i = 0; i < m_example.size(); ++i) {
176 for (int j = 0; j < m_example[i].size(); ++j) {
177
178 QTableWidgetItem *item = new QTableWidgetItem(m_example[i][j]);
179
180 if (j == 0) {
181 if (m_timingType == CSVFormat::ExplicitTiming) {
182 m_exampleWidget->setVerticalHeaderItem(i, item);
183 continue;
184 } else {
185 QTableWidgetItem *header =
186 new QTableWidgetItem(QString("%1").arg(i));
187 header->setFlags(Qt::ItemIsEnabled);
188 m_exampleWidget->setVerticalHeaderItem(i, header);
189 }
190 }
191 int index = j;
192 if (m_timingType == CSVFormat::ExplicitTiming) --index;
193 item->setFlags(Qt::ItemIsEnabled);
194 m_exampleWidget->setItem(i, index, item);
195 }
196 }
197 }
198
199 void
200 CSVFormatDialog::modelTypeChanged(int type)
201 {
202 m_modelType = (CSVFormat::ModelType)type;
203
204 if (m_modelType == CSVFormat::ThreeDimensionalModel) {
205 // We can't load 3d models with explicit timing, because the 3d
206 // model is dense so we need a fixed sample increment
207 m_timingTypeCombo->setCurrentIndex(2);
208 timingTypeChanged(2);
209 }
210 }
211
212 void
213 CSVFormatDialog::timingTypeChanged(int type)
214 {
215 switch (type) {
216
217 case 0:
218 m_timingType = CSVFormat::ExplicitTiming;
219 m_timeUnits = CSVFormat::TimeSeconds;
220 m_sampleRateCombo->setEnabled(false);
221 m_sampleRateLabel->setEnabled(false);
222 m_windowSizeCombo->setEnabled(false);
223 m_windowSizeLabel->setEnabled(false);
224 if (m_modelType == CSVFormat::ThreeDimensionalModel) {
225 m_modelTypeCombo->setCurrentIndex(1);
226 modelTypeChanged(1);
227 }
228 break;
229
230 case 1:
231 m_timingType = CSVFormat::ExplicitTiming;
232 m_timeUnits = CSVFormat::TimeAudioFrames;
233 m_sampleRateCombo->setEnabled(true);
234 m_sampleRateLabel->setEnabled(true);
235 m_windowSizeCombo->setEnabled(false);
236 m_windowSizeLabel->setEnabled(false);
237 if (m_modelType == CSVFormat::ThreeDimensionalModel) {
238 m_modelTypeCombo->setCurrentIndex(1);
239 modelTypeChanged(1);
240 }
241 break;
242
243 case 2:
244 m_timingType = CSVFormat::ImplicitTiming;
245 m_timeUnits = CSVFormat::TimeWindows;
246 m_sampleRateCombo->setEnabled(true);
247 m_sampleRateLabel->setEnabled(true);
248 m_windowSizeCombo->setEnabled(true);
249 m_windowSizeLabel->setEnabled(true);
250 break;
251 }
252
253 populateExample();
254 }
255
256 void
257 CSVFormatDialog::sampleRateChanged(QString rateString)
258 {
259 bool ok = false;
260 int sampleRate = rateString.toInt(&ok);
261 if (ok) m_sampleRate = sampleRate;
262 }
263
264 void
265 CSVFormatDialog::windowSizeChanged(QString sizeString)
266 {
267 bool ok = false;
268 int size = sizeString.toInt(&ok);
269 if (ok) m_windowSize = size;
270 }