annotate widgets/CSVFormatDialog.cpp @ 674:d8bd193ad17d

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