mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 02:50:46 -06:00
Rewrite export to CSV dialog
Rewrite the dialog to export tables as CSV file using Qt Designer. Move the entire export functionality to this dialog instead of generating the file in the main window. Add some options for the user to change the layout of the CSV file.
This commit is contained in:
96
src/ExportCsvDialog.cpp
Normal file
96
src/ExportCsvDialog.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QMessageBox>
|
||||
#include <QFileDialog>
|
||||
#include "ExportCsvDialog.h"
|
||||
#include "ui_ExportCsvDialog.h"
|
||||
#include "sqlitedb.h"
|
||||
|
||||
ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QString deflocation, QWidget* parent)
|
||||
: QDialog(parent),
|
||||
ui(new Ui::ExportCsvDialog),
|
||||
pdb(db),
|
||||
defaultLocation(deflocation)
|
||||
{
|
||||
// Create UI
|
||||
ui->setupUi(this);
|
||||
|
||||
// Get list of tables to export
|
||||
objectMap objects = pdb->getBrowsableObjects();
|
||||
for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i)
|
||||
ui->comboTable->addItem(QIcon(QString(":icons/%1").arg(i.value().gettype())), i.value().getname());
|
||||
}
|
||||
|
||||
ExportCsvDialog::~ExportCsvDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void ExportCsvDialog::accept()
|
||||
{
|
||||
// Get filename
|
||||
QString fileName = QFileDialog::getSaveFileName(
|
||||
this,
|
||||
"Choose a filename to export data",
|
||||
defaultLocation,
|
||||
"Text files(*.csv *.txt)");
|
||||
|
||||
// Only if the user hasn't clicked the cancel button
|
||||
if(fileName.size() > 0)
|
||||
{
|
||||
// Get data from selected table
|
||||
pdb->browseTable(ui->comboTable->currentText());
|
||||
|
||||
// Prepare the quote and separating characters
|
||||
QString quoteChar = ui->comboQuoteCharacter->currentText();
|
||||
QString quotequoteChar = quoteChar + quoteChar;
|
||||
QString sepChar = ui->comboFieldSeparator->currentText();
|
||||
if(sepChar == "Tab") sepChar = "\t";
|
||||
QString newlineChar = "\n";
|
||||
|
||||
// Open file
|
||||
QFile file(fileName);
|
||||
if(file.open(QIODevice::WriteOnly))
|
||||
{
|
||||
// Open text stream to the file
|
||||
QTextStream stream(&file);
|
||||
|
||||
// Put field names in first row if user wants to have them
|
||||
if(ui->checkHeader->isChecked())
|
||||
{
|
||||
QStringList fields = pdb->browseFields;
|
||||
for(int i=0;i<fields.count();i++)
|
||||
{
|
||||
stream << quoteChar << fields.at(i) << quoteChar;
|
||||
if(i < fields.count() - 1)
|
||||
stream << sepChar;
|
||||
else
|
||||
stream << newlineChar;
|
||||
}
|
||||
}
|
||||
|
||||
// Get and write actual data
|
||||
rowList data = pdb->browseRecs;
|
||||
for(int i=0;i<data.size();i++)
|
||||
{
|
||||
QStringList& row = data[i];
|
||||
for(int j=1;j<row.size();j++)
|
||||
{
|
||||
QString content = row[j];
|
||||
stream << quoteChar << content.replace(quoteChar, quotequoteChar) << quoteChar;
|
||||
if(j < row.count() - 1)
|
||||
stream << sepChar;
|
||||
else
|
||||
stream << newlineChar;
|
||||
}
|
||||
}
|
||||
|
||||
// Done writing the file
|
||||
file.close();
|
||||
QMessageBox::information(this, QApplication::applicationName(), "Export completed.");
|
||||
QDialog::accept();
|
||||
} else {
|
||||
QMessageBox::warning(this, QApplication::applicationName(), "Could not open output file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user