Skip to content

Commit

Permalink
Merge pull request sqlitebrowser#606 from justinclift/addnewlineoptio…
Browse files Browse the repository at this point in the history
…ntocsvdialog

Add newline option to the Export CSV dialog
  • Loading branch information
justinclift committed May 25, 2016
2 parents 2d80e9f + 0457d2c commit 6622022
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 13 deletions.
58 changes: 47 additions & 11 deletions src/ExportCsvDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent, const QString
bool firstRow = PreferencesDialog::getSettingsValue("exportcsv", "firstrowheader").toBool();
int separatorChar = PreferencesDialog::getSettingsValue("exportcsv", "separator").toInt();
int quoteChar = PreferencesDialog::getSettingsValue("exportcsv", "quotecharacter").toInt();
QString newLineString = PreferencesDialog::getSettingsValue("exportcsv", "newlinecharacters").toString();

// Set the widget values using the retrieved preferences
ui->checkHeader->setChecked(firstRow);
setSeparatorChar(separatorChar);
setQuoteChar(quoteChar);
setNewLineString(newLineString);

// Update the visible/hidden status of the "Other" line edit fields
showCustomCharEdits();
Expand Down Expand Up @@ -73,16 +75,10 @@ bool ExportCsvDialog::exportQuery(const QString& sQuery, const QString& sFilenam
QChar quoteChar = currentQuoteChar();
QString quotequoteChar = QString(quoteChar) + quoteChar;
QChar sepChar = currentSeparatorChar();
QString newlineStr = currentNewLineString();

// Choose appropriate newline character for the platform
#ifdef Q_OS_WIN
QString newlineChar = "\r\n";
#else
QString newlineChar = "\n";
#endif

// chars that require escaping
std::string special_chars = newlineChar.toStdString() + sepChar.toLatin1() + quoteChar.toLatin1();
// Chars that require escaping
std::string special_chars = newlineStr.toStdString() + sepChar.toLatin1() + quoteChar.toLatin1();

// Open file
QFile file(sFilename);
Expand Down Expand Up @@ -110,7 +106,7 @@ bool ExportCsvDialog::exportQuery(const QString& sQuery, const QString& sFilenam
if(i != columns - 1)
stream << sepChar;
}
stream << newlineChar;
stream << newlineStr;
}

QApplication::setOverrideCursor(Qt::WaitCursor);
Expand All @@ -130,7 +126,7 @@ bool ExportCsvDialog::exportQuery(const QString& sQuery, const QString& sFilenam
if(i != columns - 1)
stream << sepChar;
}
stream << newlineChar;
stream << newlineStr;
if(counter % 1000 == 0)
qApp->processEvents();
counter++;
Expand Down Expand Up @@ -233,6 +229,7 @@ void ExportCsvDialog::accept()
PreferencesDialog::setSettingsValue("exportcsv", "firstrowheader", ui->checkHeader->isChecked(), false);
PreferencesDialog::setSettingsValue("exportcsv", "separator", currentSeparatorChar(), false);
PreferencesDialog::setSettingsValue("exportcsv", "quotecharacter", currentQuoteChar(), false);
PreferencesDialog::setSettingsValue("exportcsv", "newlinecharacters", currentNewLineString(), false);

// Notify the user the export has completed
QMessageBox::information(this, QApplication::applicationName(), tr("Export completed."));
Expand All @@ -244,6 +241,7 @@ void ExportCsvDialog::showCustomCharEdits()
// Show/hide custom quote/separator input fields
ui->editCustomQuote->setVisible(ui->comboQuoteCharacter->currentIndex() == ui->comboQuoteCharacter->count()-1);
ui->editCustomSeparator->setVisible(ui->comboFieldSeparator->currentIndex() == ui->comboFieldSeparator->count()-1);
ui->editCustomNewLine->setVisible(ui->comboNewLineString->currentIndex() == ui->comboNewLineString->count()-1);
}

void ExportCsvDialog::setQuoteChar(const QChar& c)
Expand Down Expand Up @@ -297,3 +295,41 @@ char ExportCsvDialog::currentSeparatorChar() const

return ui->comboFieldSeparator->currentText() == tr("Tab") ? '\t' : ui->comboFieldSeparator->currentText().at(0).toLatin1();
}

void ExportCsvDialog::setNewLineString(const QString& s)
{
QComboBox* combo = ui->comboNewLineString;

// Set the combo and/or Other box to the correct selection
if (s == "\r\n") {
// For Windows style newlines, set the combo box to option 0
combo->setCurrentIndex(0);
} else if (s == "\n") {
// For Unix style newlines, set the combo box to option 1
combo->setCurrentIndex(1);
} else {
// For everything else, set the combo box to option 2 ('Other') and
// place the desired string into the matching edit line box
combo->setCurrentIndex(2);
ui->editCustomNewLine->setText(s);
}
}

QString ExportCsvDialog::currentNewLineString() const
{
QComboBox* combo = ui->comboNewLineString;

switch (combo->currentIndex()) {
case 0:
// Windows style newlines
return QString("\r\n");

case 1:
// Unix style newlines
return QString("\n");

default:
// Return the text from the 'Other' box
return QString(ui->editCustomNewLine->text().toLatin1());
}
}
3 changes: 3 additions & 0 deletions src/ExportCsvDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ private slots:
void setSeparatorChar(const QChar& c);
char currentSeparatorChar() const;

void setNewLineString(const QString& s);
QString currentNewLineString() const;

bool exportQuery(const QString& sQuery, const QString& sFilename);

private:
Expand Down
112 changes: 110 additions & 2 deletions src/ExportCsvDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>540</width>
<height>300</height>
<width>579</width>
<height>403</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -28,6 +28,12 @@
</item>
<item row="0" column="1">
<widget class="QListWidget" name="listTables">
<property name="minimumSize">
<size>
<width>360</width>
<height>0</height>
</size>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
</property>
Expand Down Expand Up @@ -70,6 +76,18 @@
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QComboBox" name="comboFieldSeparator">
<property name="minimumSize">
<size>
<width>180</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>180</width>
<height>16777215</height>
</size>
</property>
<item>
<property name="text">
<string>,</string>
Expand Down Expand Up @@ -133,6 +151,18 @@
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QComboBox" name="comboQuoteCharacter">
<property name="minimumSize">
<size>
<width>180</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>180</width>
<height>16777215</height>
</size>
</property>
<item>
<property name="text">
<string>&quot;</string>
Expand Down Expand Up @@ -177,6 +207,68 @@
</item>
</layout>
</item>
<item row="4" column="0">
<widget class="QLabel" name="labelNewLineCharacters">
<property name="text">
<string>New line characters</string>
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0,0">
<item>
<widget class="QComboBox" name="comboNewLineString">
<property name="minimumSize">
<size>
<width>180</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>180</width>
<height>16777215</height>
</size>
</property>
<item>
<property name="text">
<string>Windows: CR+LF (\r\n)</string>
</property>
</item>
<item>
<property name="text">
<string>Unix: LF (\n)</string>
</property>
</item>
<item>
<property name="text">
<string>Other</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QLineEdit" name="editCustomNewLine">
<property name="maxLength">
<number>5</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
<item row="1" column="0">
Expand Down Expand Up @@ -265,6 +357,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>comboNewLineString</sender>
<signal>currentIndexChanged(int)</signal>
<receiver>ExportCsvDialog</receiver>
<slot>showCustomCharEdits()</slot>
<hints>
<hint type="sourcelabel">
<x>231</x>
<y>327</y>
</hint>
<hint type="destinationlabel">
<x>296</x>
<y>248</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>showCustomCharEdits()</slot>
Expand Down

0 comments on commit 6622022

Please sign in to comment.