summaryrefslogtreecommitdiff
path: root/src/export-import/CsvExportDialog.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/CsvExportDialog.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/export-import/CsvExportDialog.cpp')
-rw-r--r--src/export-import/CsvExportDialog.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/export-import/CsvExportDialog.cpp b/src/export-import/CsvExportDialog.cpp
new file mode 100644
index 0000000..9e4dc90
--- /dev/null
+++ b/src/export-import/CsvExportDialog.cpp
@@ -0,0 +1,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()) );
+}