summaryrefslogtreecommitdiff
path: root/src/export-import/CsvDialog.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/export-import/CsvDialog.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/export-import/CsvDialog.cpp')
-rw-r--r--src/export-import/CsvDialog.cpp176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/export-import/CsvDialog.cpp b/src/export-import/CsvDialog.cpp
new file mode 100644
index 0000000..b19d029
--- /dev/null
+++ b/src/export-import/CsvDialog.cpp
@@ -0,0 +1,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() );
+}