annotate widgets/CSVFormatDialog.cpp @ 896:78e041e45ff0

Scale drag thresholds according to actual display resolution
author Chris Cannam
date Thu, 15 Jan 2015 10:42:00 +0000
parents d854c72dcaa1
children 4a578a360011
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@378 7 This file copyright 2006 Chris Cannam.
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@378 16 #include "CSVFormatDialog.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@378 33
Chris@682 34 #include "base/Debug.h"
Chris@682 35
Chris@581 36 CSVFormatDialog::CSVFormatDialog(QWidget *parent, CSVFormat format,
Chris@581 37 int maxDisplayCols) :
Chris@378 38 QDialog(parent),
Chris@581 39 m_format(format),
Chris@581 40 m_maxDisplayCols(maxDisplayCols),
Chris@581 41 m_fuzzyColumn(-1)
Chris@378 42 {
Chris@378 43 setModal(true);
Chris@378 44 setWindowTitle(tr("Select Data Format"));
Chris@378 45
Chris@378 46 QGridLayout *layout = new QGridLayout;
Chris@378 47
Chris@560 48 int row = 0;
Chris@378 49
Chris@560 50 layout->addWidget(new QLabel(tr("Please select the correct data format for this file.")),
Chris@560 51 row++, 0, 1, 4);
Chris@560 52
Chris@560 53 QFrame *exampleFrame = new QFrame;
Chris@560 54 exampleFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
Chris@560 55 exampleFrame->setLineWidth(2);
Chris@560 56 QGridLayout *exampleLayout = new QGridLayout;
Chris@561 57 exampleLayout->setSpacing(4);
Chris@560 58 exampleFrame->setLayout(exampleLayout);
Chris@560 59
Chris@560 60 QPalette palette = exampleFrame->palette();
Chris@560 61 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
Chris@560 62 exampleFrame->setPalette(palette);
Chris@560 63
Chris@560 64 QFont fp;
Chris@561 65 fp.setPointSize(fp.pointSize() * 0.9);
Chris@561 66 // fp.setFixedPitch(true);
Chris@561 67 // fp.setStyleHint(QFont::TypeWriter);
Chris@561 68 // fp.setFamily("Monospaced");
Chris@560 69
Chris@560 70 int columns = format.getColumnCount();
Chris@560 71 QList<QStringList> example = m_format.getExample();
Chris@560 72
Chris@560 73 for (int i = 0; i < columns; ++i) {
Chris@581 74
Chris@560 75 QComboBox *cpc = new QComboBox;
Chris@560 76 m_columnPurposeCombos.push_back(cpc);
Chris@560 77 exampleLayout->addWidget(cpc, 0, i);
Chris@581 78 connect(cpc, SIGNAL(activated(int)), this, SLOT(columnPurposeChanged(int)));
Chris@581 79
Chris@581 80 if (i == m_maxDisplayCols && columns > i + 2) {
Chris@581 81 m_fuzzyColumn = i;
Chris@581 82 cpc->addItem(tr("<ignore>"));
Chris@581 83 cpc->addItem(tr("Values"));
Chris@581 84 cpc->setCurrentIndex
Chris@581 85 (m_format.getColumnPurpose(i-1) == CSVFormat::ColumnUnknown ? 0 : 1);
Chris@581 86 exampleLayout->addWidget(new QLabel(tr("(%1 more)").arg(columns - i)),
Chris@581 87 1, i);
Chris@581 88 break;
Chris@581 89 }
Chris@560 90
Chris@560 91 // NB must be in the same order as the CSVFormat::ColumnPurpose enum
Chris@560 92 cpc->addItem(tr("<ignore>")); // ColumnUnknown
Chris@560 93 cpc->addItem(tr("Time")); // ColumnStartTime
Chris@560 94 cpc->addItem(tr("End time")); // ColumnEndTime
Chris@560 95 cpc->addItem(tr("Duration")); // ColumnDuration
Chris@560 96 cpc->addItem(tr("Value")); // ColumnValue
Chris@763 97 cpc->addItem(tr("Pitch")); // ColumnPitch
Chris@560 98 cpc->addItem(tr("Label")); // ColumnLabel
Chris@560 99 cpc->setCurrentIndex(int(m_format.getColumnPurpose(i)));
Chris@560 100
Chris@581 101 for (int j = 0; j < example.size() && j < 6; ++j) {
Chris@866 102 if (i >= example[j].size()) {
Chris@866 103 continue;
Chris@866 104 }
Chris@581 105 QLabel *label = new QLabel;
Chris@581 106 label->setTextFormat(Qt::PlainText);
Chris@674 107 QString text = TextAbbrev::abbreviate(example[j][i], 35);
Chris@674 108 label->setText(text);
Chris@581 109 label->setFont(fp);
Chris@581 110 label->setPalette(palette);
Chris@581 111 label->setIndent(8);
Chris@581 112 exampleLayout->addWidget(label, j+1, i);
Chris@560 113 }
Chris@560 114 }
Chris@560 115
Chris@560 116 layout->addWidget(exampleFrame, row, 0, 1, 4);
Chris@560 117 layout->setColumnStretch(3, 10);
Chris@560 118 layout->setRowStretch(row++, 10);
Chris@560 119
Chris@560 120 layout->addWidget(new QLabel(tr("Timing is specified:")), row, 0);
Chris@378 121
Chris@378 122 m_timingTypeCombo = new QComboBox;
Chris@560 123 m_timingTypeCombo->addItem(tr("Explicitly, in seconds"));
Chris@865 124 m_timingTypeCombo->addItem(tr("Explicitly, in milliseconds"));
Chris@560 125 m_timingTypeCombo->addItem(tr("Explicitly, in audio sample frames"));
Chris@560 126 m_timingTypeCombo->addItem(tr("Implicitly: rows are equally spaced in time"));
Chris@560 127 layout->addWidget(m_timingTypeCombo, row++, 1, 1, 2);
Chris@378 128 connect(m_timingTypeCombo, SIGNAL(activated(int)),
Chris@378 129 this, SLOT(timingTypeChanged(int)));
Chris@560 130 m_timingTypeCombo->setCurrentIndex
Chris@560 131 (m_format.getTimingType() == CSVFormat::ExplicitTiming ?
Chris@865 132 m_format.getTimeUnits() == CSVFormat::TimeSeconds ? 0 : 2 : 3);
Chris@559 133
Chris@378 134 m_sampleRateLabel = new QLabel(tr("Audio sample rate (Hz):"));
Chris@560 135 layout->addWidget(m_sampleRateLabel, row, 0);
Chris@378 136
Chris@807 137 int sampleRates[] = {
Chris@378 138 8000, 11025, 12000, 22050, 24000, 32000,
Chris@378 139 44100, 48000, 88200, 96000, 176400, 192000
Chris@378 140 };
Chris@378 141
Chris@378 142 m_sampleRateCombo = new QComboBox;
Chris@807 143 for (int i = 0; i < int(sizeof(sampleRates) / sizeof(sampleRates[0])); ++i) {
Chris@378 144 m_sampleRateCombo->addItem(QString("%1").arg(sampleRates[i]));
Chris@560 145 if (sampleRates[i] == m_format.getSampleRate()) {
Chris@560 146 m_sampleRateCombo->setCurrentIndex(i);
Chris@560 147 }
Chris@378 148 }
Chris@378 149 m_sampleRateCombo->setEditable(true);
Chris@378 150
Chris@560 151 layout->addWidget(m_sampleRateCombo, row++, 1);
Chris@378 152 connect(m_sampleRateCombo, SIGNAL(activated(QString)),
Chris@378 153 this, SLOT(sampleRateChanged(QString)));
Chris@378 154 connect(m_sampleRateCombo, SIGNAL(editTextChanged(QString)),
Chris@378 155 this, SLOT(sampleRateChanged(QString)));
Chris@378 156
Chris@378 157 m_windowSizeLabel = new QLabel(tr("Frame increment between rows:"));
Chris@560 158 layout->addWidget(m_windowSizeLabel, row, 0);
Chris@378 159
Chris@378 160 m_windowSizeCombo = new QComboBox;
Chris@378 161 for (int i = 0; i <= 16; ++i) {
Chris@378 162 int value = 1 << i;
Chris@378 163 m_windowSizeCombo->addItem(QString("%1").arg(value));
Chris@560 164 if (value == int(m_format.getWindowSize())) {
Chris@560 165 m_windowSizeCombo->setCurrentIndex(i);
Chris@560 166 }
Chris@378 167 }
Chris@378 168 m_windowSizeCombo->setEditable(true);
Chris@378 169
Chris@560 170 layout->addWidget(m_windowSizeCombo, row++, 1);
Chris@378 171 connect(m_windowSizeCombo, SIGNAL(activated(QString)),
Chris@378 172 this, SLOT(windowSizeChanged(QString)));
Chris@378 173 connect(m_windowSizeCombo, SIGNAL(editTextChanged(QString)),
Chris@378 174 this, SLOT(windowSizeChanged(QString)));
Chris@378 175
Chris@561 176 m_modelLabel = new QLabel;
Chris@561 177 QFont f(m_modelLabel->font());
Chris@561 178 f.setItalic(true);
Chris@561 179 m_modelLabel->setFont(f);
Chris@561 180 layout->addWidget(m_modelLabel, row++, 0, 1, 4);
Chris@561 181
Chris@512 182 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@512 183 QDialogButtonBox::Cancel);
Chris@560 184 layout->addWidget(bb, row++, 0, 1, 4);
Chris@512 185 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@512 186 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@378 187
Chris@512 188 setLayout(layout);
Chris@378 189
Chris@378 190 timingTypeChanged(m_timingTypeCombo->currentIndex());
Chris@561 191 updateModelLabel();
Chris@378 192 }
Chris@378 193
Chris@378 194 CSVFormatDialog::~CSVFormatDialog()
Chris@378 195 {
Chris@378 196 }
Chris@378 197
Chris@378 198 CSVFormat
Chris@378 199 CSVFormatDialog::getFormat() const
Chris@378 200 {
Chris@560 201 return m_format;
Chris@378 202 }
Chris@378 203
Chris@378 204 void
Chris@561 205 CSVFormatDialog::updateModelLabel()
Chris@378 206 {
Chris@561 207 LayerFactory *f = LayerFactory::getInstance();
Chris@561 208
Chris@561 209 QString s;
Chris@561 210 switch (m_format.getModelType()) {
Chris@561 211 case CSVFormat::OneDimensionalModel:
Chris@561 212 s = f->getLayerPresentationName(LayerFactory::TimeInstants);
Chris@561 213 break;
Chris@561 214 case CSVFormat::TwoDimensionalModel:
Chris@561 215 s = f->getLayerPresentationName(LayerFactory::TimeValues);
Chris@561 216 break;
Chris@561 217 case CSVFormat::TwoDimensionalModelWithDuration:
Chris@561 218 s = f->getLayerPresentationName(LayerFactory::Regions);
Chris@561 219 break;
Chris@763 220 case CSVFormat::TwoDimensionalModelWithDurationAndPitch:
Chris@763 221 s = f->getLayerPresentationName(LayerFactory::Notes);
Chris@763 222 break;
Chris@561 223 case CSVFormat::ThreeDimensionalModel:
Chris@561 224 s = f->getLayerPresentationName(LayerFactory::Colour3DPlot);
Chris@561 225 break;
Chris@561 226 }
Chris@561 227
Chris@578 228 m_modelLabel->setText("\n" + tr("Data will be displayed in a %1 layer.").arg(s));
Chris@378 229 }
Chris@378 230
Chris@378 231 void
Chris@378 232 CSVFormatDialog::timingTypeChanged(int type)
Chris@378 233 {
Chris@378 234 switch (type) {
Chris@378 235
Chris@378 236 case 0:
Chris@560 237 m_format.setTimingType(CSVFormat::ExplicitTiming);
Chris@560 238 m_format.setTimeUnits(CSVFormat::TimeSeconds);
Chris@378 239 m_sampleRateCombo->setEnabled(false);
Chris@378 240 m_sampleRateLabel->setEnabled(false);
Chris@378 241 m_windowSizeCombo->setEnabled(false);
Chris@378 242 m_windowSizeLabel->setEnabled(false);
Chris@378 243 break;
Chris@378 244
Chris@378 245 case 1:
Chris@560 246 m_format.setTimingType(CSVFormat::ExplicitTiming);
Chris@865 247 m_format.setTimeUnits(CSVFormat::TimeMilliseconds);
Chris@865 248 m_sampleRateCombo->setEnabled(true);
Chris@865 249 m_sampleRateLabel->setEnabled(true);
Chris@865 250 m_windowSizeCombo->setEnabled(false);
Chris@865 251 m_windowSizeLabel->setEnabled(false);
Chris@865 252 break;
Chris@865 253
Chris@865 254 case 2:
Chris@865 255 m_format.setTimingType(CSVFormat::ExplicitTiming);
Chris@560 256 m_format.setTimeUnits(CSVFormat::TimeAudioFrames);
Chris@378 257 m_sampleRateCombo->setEnabled(true);
Chris@378 258 m_sampleRateLabel->setEnabled(true);
Chris@378 259 m_windowSizeCombo->setEnabled(false);
Chris@378 260 m_windowSizeLabel->setEnabled(false);
Chris@378 261 break;
Chris@378 262
Chris@865 263 case 3:
Chris@560 264 m_format.setTimingType(CSVFormat::ImplicitTiming);
Chris@560 265 m_format.setTimeUnits(CSVFormat::TimeWindows);
Chris@378 266 m_sampleRateCombo->setEnabled(true);
Chris@378 267 m_sampleRateLabel->setEnabled(true);
Chris@378 268 m_windowSizeCombo->setEnabled(true);
Chris@378 269 m_windowSizeLabel->setEnabled(true);
Chris@378 270 break;
Chris@378 271 }
Chris@559 272 }
Chris@559 273
Chris@559 274 void
Chris@378 275 CSVFormatDialog::sampleRateChanged(QString rateString)
Chris@378 276 {
Chris@378 277 bool ok = false;
Chris@378 278 int sampleRate = rateString.toInt(&ok);
Chris@560 279 if (ok) m_format.setSampleRate(sampleRate);
Chris@378 280 }
Chris@378 281
Chris@378 282 void
Chris@378 283 CSVFormatDialog::windowSizeChanged(QString sizeString)
Chris@378 284 {
Chris@378 285 bool ok = false;
Chris@378 286 int size = sizeString.toInt(&ok);
Chris@560 287 if (ok) m_format.setWindowSize(size);
Chris@378 288 }
Chris@560 289
Chris@560 290 void
Chris@561 291 CSVFormatDialog::columnPurposeChanged(int p)
Chris@560 292 {
Chris@560 293 QObject *o = sender();
Chris@561 294
Chris@560 295 QComboBox *cb = qobject_cast<QComboBox *>(o);
Chris@560 296 if (!cb) return;
Chris@561 297
Chris@561 298 CSVFormat::ColumnPurpose purpose = (CSVFormat::ColumnPurpose)p;
Chris@561 299
Chris@561 300 bool haveStartTime = false;
Chris@561 301 bool haveDuration = false;
Chris@763 302 bool havePitch = false;
Chris@561 303 int valueCount = 0;
Chris@561 304
Chris@560 305 for (int i = 0; i < m_columnPurposeCombos.size(); ++i) {
Chris@561 306
Chris@561 307 CSVFormat::ColumnPurpose cp = m_format.getColumnPurpose(i);
Chris@561 308
Chris@561 309 bool thisChanged = (cb == m_columnPurposeCombos[i]);
Chris@561 310
Chris@561 311 if (thisChanged) {
Chris@561 312
Chris@682 313 cerr << "i == " << i << ", fuzzy == " << m_fuzzyColumn
Chris@682 314 << ", p == " << p << endl;
Chris@581 315
Chris@581 316 if (i == m_fuzzyColumn) {
Chris@581 317 for (int j = i; j < m_format.getColumnCount(); ++j) {
Chris@581 318 if (p == 0) { // Ignore
Chris@581 319 m_format.setColumnPurpose(j, CSVFormat::ColumnUnknown);
Chris@581 320 } else { // Value
Chris@581 321 m_format.setColumnPurpose(j, CSVFormat::ColumnValue);
Chris@581 322 ++valueCount;
Chris@581 323 }
Chris@581 324 }
Chris@581 325 continue;
Chris@581 326 }
Chris@581 327
Chris@561 328 cp = purpose;
Chris@561 329
Chris@561 330 } else {
Chris@561 331
Chris@581 332 if (i == m_fuzzyColumn) continue;
Chris@581 333
Chris@561 334 // We can only have one ColumnStartTime column, and only
Chris@561 335 // one of either ColumnDuration or ColumnEndTime
Chris@561 336
Chris@561 337 if (purpose == CSVFormat::ColumnStartTime) {
Chris@561 338 if (cp == purpose) {
Chris@561 339 cp = CSVFormat::ColumnValue;
Chris@561 340 }
Chris@561 341 } else if (purpose == CSVFormat::ColumnDuration ||
Chris@561 342 purpose == CSVFormat::ColumnEndTime) {
Chris@561 343 if (cp == CSVFormat::ColumnDuration ||
Chris@561 344 cp == CSVFormat::ColumnEndTime) {
Chris@561 345 cp = CSVFormat::ColumnValue;
Chris@561 346 }
Chris@561 347 }
Chris@561 348
Chris@561 349 // And we can only have one label
Chris@561 350 if (purpose == CSVFormat::ColumnLabel) {
Chris@561 351 if (cp == purpose) {
Chris@561 352 cp = CSVFormat::ColumnUnknown;
Chris@561 353 }
Chris@561 354 }
Chris@561 355 }
Chris@561 356
Chris@561 357 if (cp == CSVFormat::ColumnStartTime) {
Chris@561 358 haveStartTime = true;
Chris@561 359 }
Chris@561 360 if (cp == CSVFormat::ColumnEndTime ||
Chris@561 361 cp == CSVFormat::ColumnDuration) {
Chris@561 362 haveDuration = true;
Chris@561 363 }
Chris@763 364 if (cp == CSVFormat::ColumnPitch) {
Chris@763 365 havePitch = true;
Chris@763 366 }
Chris@561 367 if (cp == CSVFormat::ColumnValue) {
Chris@561 368 ++valueCount;
Chris@561 369 }
Chris@561 370
Chris@561 371 m_columnPurposeCombos[i]->setCurrentIndex(int(cp));
Chris@561 372 m_format.setColumnPurpose(i, cp);
Chris@561 373 }
Chris@561 374
Chris@561 375 if (!haveStartTime) {
Chris@561 376 m_timingTypeCombo->setCurrentIndex(2);
Chris@561 377 timingTypeChanged(2);
Chris@561 378 }
Chris@561 379
Chris@561 380 if (haveStartTime && haveDuration) {
Chris@763 381 if (havePitch) {
Chris@763 382 m_format.setModelType(CSVFormat::TwoDimensionalModelWithDurationAndPitch);
Chris@763 383 } else {
Chris@763 384 m_format.setModelType(CSVFormat::TwoDimensionalModelWithDuration);
Chris@763 385 }
Chris@561 386 } else {
Chris@561 387 if (valueCount > 1) {
Chris@561 388 m_format.setModelType(CSVFormat::ThreeDimensionalModel);
Chris@561 389 } else if (valueCount > 0) {
Chris@561 390 m_format.setModelType(CSVFormat::TwoDimensionalModel);
Chris@561 391 } else {
Chris@561 392 m_format.setModelType(CSVFormat::OneDimensionalModel);
Chris@560 393 }
Chris@560 394 }
Chris@561 395
Chris@561 396 updateModelLabel();
Chris@560 397 }
Chris@560 398
Chris@560 399