summaryrefslogtreecommitdiff
path: root/src/export-import/CsvImportDialog.cpp
blob: 51c3c8761dbe45d1d05796b67da3b0fbacb50957 (plain)
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
#include "CsvImportDialog.h"

#include "../main-view/DictTableView.h"
#include "../main-view/DictTableModel.h"
#include "../dictionary/Dictionary.h"
#include "../dictionary/DicCsvReader.h"

CsvImportDialog::CsvImportDialog(QWidget* parent, QString filePath, const AppModel* appModel):
    CsvDialog(parent),
    filePath(filePath),
    dictionary(NULL),
    appModel(appModel)
{
    init();
    setWindowTitle(tr("Import from CSV"));

    connect( textDelimiterCB, SIGNAL(stateChanged(int)), this, SLOT(updateTextDelimiterCombo()) );
    connect( commentCharCB, SIGNAL(stateChanged(int)), this, SLOT(UpdateCommentCharacterCombo()) );
    connect( this, SIGNAL(rejected()), this, SLOT(DeleteDictionary()) );

    connect( charSetCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePreview()) );
    connect( fromLineSpin, SIGNAL(valueChanged(int)), this, SLOT(updatePreview()) );
    connect( firstLineIsHeaderCB, SIGNAL(stateChanged(int)), this, SLOT(updatePreview()) );
    connect( anyCharacterRB, SIGNAL(toggled(bool)), this, SLOT(updatePreview()) );
    connect( anyCombinationRB, SIGNAL(toggled(bool)), this, SLOT(updatePreview()) );
    connect( exactStringRB, SIGNAL(toggled(bool)), this, SLOT(updatePreview()) );
    connect( commentCharCB, SIGNAL(stateChanged(int)), this, SLOT(updatePreview()) );
    connect( colsToImportSpin, SIGNAL(valueChanged(int)), this, SLOT(updatePreview()) );
}

CsvImportDialog::~CsvImportDialog()
{
    delete iPreviewModel;
    delete dicReader;
}

QLayout* CsvImportDialog::createLeftGroupLayout()
{
    QFormLayout* lt = new QFormLayout;
    lt->addRow(tr("C&haracter set:"), createCharSetCombo());
    lt->addRow(tr("From &line:"), createFromLineSpin());
    lt->addRow(tr("Number of colum&ns:"), createColsToImportSpin());
    lt->addRow(createFirstLineIsHeaderCB());
    return lt;
}

QWidget* CsvImportDialog::createFromLineSpin()
{
    fromLineSpin = new QSpinBox();
    fromLineSpin->setRange(1, 100);
    return fromLineSpin;
}

QWidget* CsvImportDialog::createColsToImportSpin()
{
    colsToImportSpin = new QSpinBox();
    colsToImportSpin->setRange(0, 9);
    colsToImportSpin->setSpecialValueText(tr("All"));
    return colsToImportSpin;
}

QWidget* CsvImportDialog::createFirstLineIsHeaderCB()
{
    firstLineIsHeaderCB = new QCheckBox(tr("&First line has field names"));
    firstLineIsHeaderCB->setChecked(false);
    return firstLineIsHeaderCB;
}

QLayout* CsvImportDialog::createSeparatorsLayout()
{
    createSeparationRadioButtons();

    QFormLayout* lt = new QFormLayout;
    lt->addRow(tr("Field &separator:"),
        createFieldSeparatorWidget());
    lt->addRow(tr("Separation mode:"), anyCharacterRB);
    // The label is a workaround to make enough space for the radio button text
    lt->addRow(" ", anyCombinationRB);
    lt->addRow(NULL, exactStringRB);
    lt->addRow(createTextDelimiterBox(), createTextDelimiterCombo());
    lt->addRow(createCommentCharBox(), createCommentCharCombo());
    return lt;
}

void CsvImportDialog::createSeparationRadioButtons()
{
    anyCharacterRB = new QRadioButton(tr("An&y character"));
    anyCharacterRB->setToolTip(tr("Fields are separated by any separator character"));
    anyCombinationRB = new QRadioButton(tr("A co&mbination of characters"));
    anyCombinationRB->setToolTip(tr("Fields are separated by a combination of separator characters, in any order"));
    anyCombinationRB->setChecked( true );
    exactStringRB = new QRadioButton(tr("E&xact string"));
    exactStringRB->setToolTip(tr("Fields are separated by the exact string of separators, in the above defined order"));
}

QWidget* CsvImportDialog::createCommentCharBox()
{
    commentCharCB = new QCheckBox(tr("&Comment character:"));
    commentCharCB->setChecked(true);
    return commentCharCB;
}

QLayout* CsvImportDialog::createPreviewLt()
{
    QVBoxLayout* lt = new QVBoxLayout;
    lt->addWidget(createPreview());
    return lt;
}

QWidget* CsvImportDialog::createPreview()
{
    dictionary = new Dictionary( filePath + Dictionary::DictFileExtension, true, appModel );
    dicReader = new DicCsvReader( dictionary );
    iPreviewModel = new DictTableModel( dictionary );
    previewTable = new DictTableView(iPreviewModel);
    return previewTable;
}

void CsvImportDialog::UpdateCommentCharacterCombo()
{
    commentCharCombo->setEnabled( commentCharCB->isChecked() );
}

void CsvImportDialog::updatePreview()
{
    CsvImportData params;
    params.textCodec = getTextCodec();
    params.fromLine = fromLineSpin->value();
    params.fieldSeparators = setCharVisibility(separatorsEdit->text(), false);
    params.fieldSeparationMode = getSeparationMode();
    params.commentChar = commentCharCB->isChecked()?
        commentCharCombo->currentText()[0]: QChar(0);
    params.textDelimiter = getTextDelimiterChar();
    params.colsToImport = colsToImportSpin->value();
    params.firstLineIsHeader = firstLineIsHeaderCB->isChecked();

    dicReader->readDict( filePath, params );
    iPreviewModel->resetData();
}

FieldSeparationMode CsvImportDialog::getSeparationMode()
{
    if(anyCharacterRB->isChecked())
        return EFieldSeparatorAnyCharacter;
    else if (anyCombinationRB->isChecked())
        return EFieldSeparatorAnyCombination;
    else
        return EFieldSeparatorExactString;
}

void CsvImportDialog::DeleteDictionary()
{
delete dictionary;
dictionary = NULL;
}