File: CSVExportDialog.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (238 lines) | stat: -rw-r--r-- 6,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "CSVExportDialog.h"

#include "view/ViewManager.h"

#include <QVBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QDialogButtonBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QCheckBox>
#include <QComboBox>

#include <vector>

using namespace std;

//!!! todo: remember & re-apply last set of options chosen for this layer type

namespace sv {

CSVExportDialog::CSVExportDialog(Configuration config, QWidget *parent) :
    QDialog(parent),
    m_config(config)
{
    setWindowTitle(tr("Export Layer"));

    QString intro = tr("Exporting layer \"%1\" to %2 file.")
        .arg(config.layerName)
        .arg(config.fileExtension.toUpper());

    QVBoxLayout *vbox = new QVBoxLayout;
    
    QLabel *label = new QLabel(intro);
    label->setWordWrap(true);
    vbox->addWidget(label);

    int space = ViewManager::scalePixelSize(2);

    vbox->addSpacing(space);
    
    QGroupBox *rowColGroup = new QGroupBox(tr("Row and column options:"));
    QGridLayout *rowColLayout = new QGridLayout;
    rowColGroup->setLayout(rowColLayout);

    vector<pair<QString, QChar>> separators {
        { tr("Comma"), ',' },
        { tr("Tab"), '\t' },
        { tr("Space"), ' ' },
        { tr("Pipe"), '|' },
        { tr("Slash"), '/' },
        { tr("Colon"), ':' }
    };
    
    QChar defaultSeparator = ',';
    if (m_config.fileExtension != "csv") {
        defaultSeparator = '\t';
    }
    
    rowColLayout->addWidget(new QLabel(tr("Column separator:")));
    m_separatorCombo = new QComboBox;
    for (auto p: separators) {
        if (p.second == '\t' || p.second == ' ') {
            m_separatorCombo->addItem(p.first, p.second);
        } else {
            m_separatorCombo->addItem(tr("%1 '%2'").arg(p.first).arg(p.second),
                                      p.second);
        }
        if (p.second == defaultSeparator) {
            m_separatorCombo->setCurrentIndex(m_separatorCombo->count()-1);
        }
    }
    m_separatorCombo->setEditable(false);
    rowColLayout->addWidget(m_separatorCombo, 0, 1);
    rowColLayout->setColumnStretch(2, 10);

    m_header = new QCheckBox
        (tr("Include a header row before the data rows"));
    m_timestamps = new QCheckBox
        (tr("Include a timestamp column before the data columns"));
    rowColLayout->addWidget(m_header, 1, 0, 1, 3);
    rowColLayout->addWidget(m_timestamps, 2, 0, 1, 3);
    
    if (!m_config.isDense) {
        m_timestamps->setChecked(true);
        m_timestamps->setEnabled(false);
    }

    vbox->addWidget(rowColGroup);
    
    vbox->addSpacing(space);
    
    QGroupBox *framesGroup = new QGroupBox
        (tr("Timing format:"));

    m_seconds = new QRadioButton
        (tr("Write times in seconds"));
    m_frames = new QRadioButton
        (tr("Write times in audio sample frames"));
    m_seconds->setChecked(true);

    QVBoxLayout *framesLayout = new QVBoxLayout;
    framesLayout->addWidget(m_seconds);
    framesLayout->addWidget(m_frames);
    framesGroup->setLayout(framesLayout);
    vbox->addWidget(framesGroup);
    
    vbox->addSpacing(space);

    if (m_config.isDense) {
        m_seconds->setEnabled(false);
        m_frames->setEnabled(false);
    }
    
    QGroupBox *rangeGroup = new QGroupBox
        (tr("Range to export:"));

    QButtonGroup *selectionGroup = new QButtonGroup(rangeGroup);
    QButtonGroup *viewGroup = new QButtonGroup(rangeGroup);
    
    m_selectionOnly = new QRadioButton
        (tr("Export only the current selection"));
    QRadioButton *fullDuration = new QRadioButton
        (tr("Export the full duration of the layer"));

    selectionGroup->addButton(m_selectionOnly);
    selectionGroup->addButton(fullDuration);

    if (m_config.haveSelection) {
        m_selectionOnly->setChecked(true);
    } else {
        m_selectionOnly->setEnabled(false);
        fullDuration->setEnabled(false);
        fullDuration->setChecked(true);
    }

    QVBoxLayout *rangeLayout = new QVBoxLayout;
    rangeLayout->addWidget(m_selectionOnly);
    rangeLayout->addWidget(fullDuration);

    if (m_config.haveView && m_config.isDense) {

        m_viewOnly = new QRadioButton
            (tr("Export only the height of the visible view"));
        QRadioButton *fullHeight = new QRadioButton
            (tr("Export the full height of the layer"));

        viewGroup->addButton(m_viewOnly);
        viewGroup->addButton(fullHeight);

        m_viewOnly->setChecked(true);
    
        rangeLayout->addSpacing(space);
    
        rangeLayout->addWidget(m_viewOnly);
        rangeLayout->addWidget(fullHeight);

    } else {
        m_viewOnly = nullptr;
    }

    rangeGroup->setLayout(rangeLayout);
    vbox->addWidget(rangeGroup);
    
    vbox->addSpacing(space);
    
    QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
                                                QDialogButtonBox::Cancel);
    vbox->addWidget(bb);
    connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
    connect(bb, SIGNAL(rejected()), this, SLOT(reject()));

    connect(m_timestamps, SIGNAL(toggled(bool)),
            this, SLOT(timestampsToggled(bool)));

    setLayout(vbox);
}

void
CSVExportDialog::timestampsToggled(bool on)
{
    m_seconds->setEnabled(on);
    m_frames->setEnabled(on);
}

QString
CSVExportDialog::getDelimiter() const
{
    return m_separatorCombo->currentData().toChar();
}

bool
CSVExportDialog::shouldIncludeHeader() const
{
    return m_header && m_header->isChecked();
}

bool
CSVExportDialog::shouldIncludeTimestamps() const
{
    return m_timestamps && m_timestamps->isChecked();
}

bool
CSVExportDialog::shouldWriteTimeInFrames() const
{
    return shouldIncludeTimestamps() && m_frames && m_frames->isChecked();
}

bool
CSVExportDialog::shouldConstrainToViewHeight() const
{
    return m_viewOnly && m_viewOnly->isChecked();
}

bool
CSVExportDialog::shouldConstrainToSelection() const
{
    return m_selectionOnly && m_selectionOnly->isChecked();
}

} // end namespace sv