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
|
#include "CsvDialog.h"
QChar CsvDialog::SpaceChar(0x2423);
QChar CsvDialog::TabChar(0x21A6);
QString CsvDialog::ExtendedTab(QString(QChar(0x21A6)) + "\t");
CsvDialog::CsvDialog(QWidget* parent):
QDialog(parent)
{
resize(600, 550);
}
void CsvDialog::init()
{
setLayout(createMainLayout());
updatePreview();
connectControlsToPreview();
}
void CsvDialog::connectControlsToPreview()
{
connect(separatorsEdit, SIGNAL(textChanged(QString)), SLOT(updatePreview()) );
connect(textDelimiterCB, SIGNAL(stateChanged(int)), SLOT(updatePreview()) );
connect(textDelimiterCombo, SIGNAL(editTextChanged(QString)), SLOT(updatePreview()) );
connect(commentCharCombo, SIGNAL(editTextChanged(QString)), SLOT(updatePreview()) );
}
QLayout* CsvDialog::createMainLayout()
{
QVBoxLayout* lt = new QVBoxLayout;
lt->addLayout(createTopLayout());
lt->addWidget(new QLabel(tr("Preview:")));
lt->addLayout(createPreviewLt());
lt->addWidget(createButtonBox());
return lt;
}
QLayout* CsvDialog::createTopLayout()
{
QHBoxLayout* lt = new QHBoxLayout;
lt->addWidget(createLeftGroup());
lt->addWidget(createSeparatorsGroup());
return lt;
}
QWidget* CsvDialog::createCharSetCombo()
{
charSetCombo = new QComboBox;
charSetCombo->setEditable(false);
QList<QByteArray> codecNames = QTextCodec::availableCodecs();
qSort( codecNames );
for( int i=0; i<codecNames.size(); i++)
charSetCombo->addItem(codecNames[i],
QTextCodec::codecForName(codecNames[i] )->mibEnum());
int utf8Ix = codecNames.indexOf( "UTF-8" );
charSetCombo->setCurrentIndex( utf8Ix );
return charSetCombo;
}
QGroupBox* CsvDialog::createLeftGroup()
{
QGroupBox* group = new QGroupBox(getLeftGroupTitle());
group->setLayout(createLeftGroupLayout());
return group;
}
QGroupBox* CsvDialog::createSeparatorsGroup()
{
QGroupBox* group = new QGroupBox(tr("Separators"));
group->setLayout(createSeparatorsLayout());
return group;
}
QDialogButtonBox* CsvDialog::createButtonBox()
{
QDialogButtonBox* buttonBox = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
return buttonBox;
}
QWidget* CsvDialog::createFieldSeparatorWidget(const QString& additionalSeparators)
{
separatorsEdit = new QLineEdit(QString(TabChar) + "&" + additionalSeparators);
connect(separatorsEdit, SIGNAL(textChanged(const QString&)),
SLOT(showInvisibleInSeparatorsEdit()));
QPushButton* addSeparatorBtn = new QPushButton(tr("Ta&b"));
addSeparatorBtn->setMinimumWidth(40);
connect(addSeparatorBtn, SIGNAL(clicked()), SLOT(insertTabToSeparators()));
QHBoxLayout* lt = new QHBoxLayout;
lt->addWidget(separatorsEdit);
lt->addWidget(addSeparatorBtn);
lt->setContentsMargins(QMargins());
// Workaround for a bug in QFormLayout::addRow(text, layout)
QWidget* widget = new QWidget;
widget->setLayout(lt);
return widget;
}
QWidget* CsvDialog::createTextDelimiterBox()
{
textDelimiterCB = new QCheckBox(tr("&Text delimiter:"));
textDelimiterCB->setChecked(true);
return textDelimiterCB;
}
QWidget* CsvDialog::createTextDelimiterCombo()
{
return textDelimiterCombo = createEditableCombo(QStringList() << "\"" << "\'");
}
QWidget* CsvDialog::createCommentCharCombo()
{
return commentCharCombo = createEditableCombo(QStringList() << "#" << ";");
}
QComboBox* CsvDialog::createEditableCombo(const QStringList& items)
{
QComboBox* combo = new QComboBox;
combo->setEditable(true);
combo->setInsertPolicy(QComboBox::NoInsert);
combo->setValidator(new QRegExpValidator(QRegExp("\\S{1}"), this));
combo->addItems(items);
return combo;
}
QChar CsvDialog::getTextDelimiterChar() const
{
if(textDelimiterCB->isChecked() && !textDelimiterCombo->currentText().isEmpty())
return textDelimiterCombo->currentText()[0];
else
return QChar(0);
}
void CsvDialog::showInvisibleInSeparatorsEdit()
{
separatorsEdit->setText(setCharVisibility(separatorsEdit->text(), true));
}
void CsvDialog::insertTabToSeparators()
{
separatorsEdit->insert(TabChar);
}
QTextCodec* CsvDialog::getTextCodec()
{
return QTextCodec::codecForMib(
charSetCombo->itemData(charSetCombo->currentIndex()).toInt());
}
/// Convert invisible chars like space and tab to visible equivalents or vice versa.
QString CsvDialog::setCharVisibility(const QString& input, bool visible)
{
QString res = input;
if(visible)
{
res.replace(' ', SpaceChar);
res.replace('\t', ExtendedTab);
}
else
{
res.replace(SpaceChar, ' ');
res.replace(ExtendedTab, "\t");
res.replace(TabChar, '\t');
}
return res;
}
void CsvDialog::updateTextDelimiterCombo()
{
textDelimiterCombo->setEnabled( textDelimiterCB->isChecked() );
}
|