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
|
#include "CsvExportDialog.h"
#include "../strings.h"
#include "../dictionary/Dictionary.h"
#include "../dictionary/DicCsvWriter.h"
CsvExportDialog::CsvExportDialog(QWidget* parent, const Dictionary* dict):
CsvDialog(parent),
dictionary(dict),
dicWriter(new DicCsvWriter(dict))
{
init();
setWindowTitle(tr("Export to CSV"));
connect( textDelimiterCB, SIGNAL(stateChanged(int)), this, SLOT(updateTextDelimiterCombo()) );
connect( showInvisibleCharsCB, SIGNAL(stateChanged(int)), this, SLOT(UpdateCharVisibility()) );
connect( textDelimiterCB, SIGNAL(stateChanged(int)), this, SLOT(UpdateQuoteAllFieldsCB()) );
connect( usedColsEdit, SIGNAL(textChanged(QString)), this, SLOT(updatePreview()) );
connect( quoteAllFieldsCB, SIGNAL(stateChanged(int)), this, SLOT(updatePreview()) );
connect( writeColumnNamesCB, SIGNAL(stateChanged(int)), this, SLOT(updatePreview()) );
}
CsvExportDialog::~CsvExportDialog()
{
delete dicWriter;
}
QLayout* CsvExportDialog::createLeftGroupLayout()
{
QFormLayout* lt = new QFormLayout;
lt->addRow(tr("C&haracter set:"), createCharSetCombo());
lt->addRow(tr("Used &columns:"), createUsedColsEdit());
lt->addRow(createWriteColumnNamesBox());
return lt;
}
QWidget* CsvExportDialog::createUsedColsEdit()
{
usedColsEdit = new QLineEdit("123");
usedColsEdit->setValidator(new QRegExpValidator(QRegExp("[1-9]+"), this));
return usedColsEdit;
}
QWidget* CsvExportDialog::createWriteColumnNamesBox()
{
writeColumnNamesCB = new QCheckBox(tr("Write column &names"));
writeColumnNamesCB->setChecked( true );
return writeColumnNamesCB;
}
QLayout* CsvExportDialog::createSeparatorsLayout()
{
quoteAllFieldsCB = new QCheckBox(tr("&Quote all fields"));
QFormLayout* lt = new QFormLayout;
lt->addRow(tr("Field &separator:"), createFieldSeparatorWidget(SpaceChar));
lt->addRow(createTextDelimiterBox(), createTextDelimiterCombo());
lt->addRow(quoteAllFieldsCB);
lt->addRow(tr("Co&mment character:"), createCommentCharCombo());
return lt;
}
QLayout* CsvExportDialog::createPreviewLt()
{
QVBoxLayout* lt = new QVBoxLayout;
lt->addWidget(createPreview());
lt->addWidget(createShowInvisibleBox());
return lt;
}
QWidget* CsvExportDialog::createPreview()
{
csvPreview = new QTextEdit;
csvPreview->setReadOnly(true);
csvPreview->setLineWrapMode( QTextEdit::NoWrap );
csvPreview->setFontPointSize( 10 );
return csvPreview;
}
QCheckBox* CsvExportDialog::createShowInvisibleBox()
{
showInvisibleCharsCB = new QCheckBox(tr("Show &invisible characters"));
showInvisibleCharsCB->setChecked( false );
return showInvisibleCharsCB;
}
void CsvExportDialog::UpdateQuoteAllFieldsCB()
{
quoteAllFieldsCB->setEnabled( textDelimiterCB->isChecked() );
}
void CsvExportDialog::updatePreview()
{
CsvExportData params;
params.usedCols = getUsedColumns();
params.fieldSeparators = setCharVisibility( separatorsEdit->text(), false );
params.textDelimiter = getTextDelimiterChar();
params.quoteAllFields = quoteAllFieldsCB->isChecked();
params.commentChar = commentCharCombo->currentText()[0];
params.writeColumnNames = writeColumnNamesCB->isChecked();
QString csvString = dicWriter->toCsvString( params );
csvPreview->setPlainText(
setCharVisibility(csvString, showInvisibleCharsCB->isChecked()));
}
QList<int> CsvExportDialog::getUsedColumns()
{
QList<int> res;
for(int i = 0; i < usedColsEdit->text().length(); i++)
{
int col = usedColsEdit->text()[i].digitValue();
if(col > 0 && col <= dictionary->fieldsNum())
res << col - 1;
}
return res;
}
void CsvExportDialog::SaveCSVToFile( const QString& aFilePath )
{
QFile file( aFilePath );
if( !file.open( QIODevice::WriteOnly | QFile::Text ) ) // \r\n --> \n
{
QMessageBox::warning( this, Strings::errorTitle(), tr("Cannot save to file:\n %1.").arg(aFilePath) );
return;
}
QTextStream outStream( &file );
outStream.setCodec( getTextCodec() );
outStream << setCharVisibility( csvPreview->toPlainText(), false );
}
void CsvExportDialog::UpdateCharVisibility()
{
csvPreview->setPlainText(
setCharVisibility(csvPreview->toPlainText(), showInvisibleCharsCB->isChecked()) );
}
|