From dff9221963ca1832be46207f5b528b661d7937e3 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Wed, 9 Jan 2013 20:03:48 +0100 Subject: [PATCH] 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. --- src/ExportCsvDialog.cpp | 96 +++++++++++++++++++++ src/ExportCsvDialog.h | 28 +++++++ src/ExportCsvDialog.ui | 166 +++++++++++++++++++++++++++++++++++++ src/ExportTableCSVForm.cpp | 44 ---------- src/ExportTableCSVForm.h | 130 ----------------------------- src/MainWindow.cpp | 78 +---------------- src/src.pro | 11 +-- 7 files changed, 299 insertions(+), 254 deletions(-) create mode 100644 src/ExportCsvDialog.cpp create mode 100644 src/ExportCsvDialog.h create mode 100644 src/ExportCsvDialog.ui delete mode 100644 src/ExportTableCSVForm.cpp delete mode 100644 src/ExportTableCSVForm.h diff --git a/src/ExportCsvDialog.cpp b/src/ExportCsvDialog.cpp new file mode 100644 index 00000000..77b5c215 --- /dev/null +++ b/src/ExportCsvDialog.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#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;ibrowseRecs; + for(int i=0;i +class DBBrowserDB; + +namespace Ui { +class ExportCsvDialog; +} + +class ExportCsvDialog : public QDialog +{ + Q_OBJECT + +public: + ExportCsvDialog(DBBrowserDB* db, QString deflocation, QWidget* parent = 0); + ~ExportCsvDialog(); + +public slots: + virtual void accept(); + +private: + Ui::ExportCsvDialog* ui; + DBBrowserDB* pdb; + QString defaultLocation; +}; + +#endif diff --git a/src/ExportCsvDialog.ui b/src/ExportCsvDialog.ui new file mode 100644 index 00000000..facd1031 --- /dev/null +++ b/src/ExportCsvDialog.ui @@ -0,0 +1,166 @@ + + + ExportCsvDialog + + + + 0 + 0 + 331 + 141 + + + + Export table to CSV + + + + + + + + &Table + + + comboTable + + + + + + + + + + &Column names in first line + + + checkHeader + + + + + + + + + + true + + + + + + + Field &separator + + + comboFieldSeparator + + + + + + + + , + + + + + ; + + + + + Tab + + + + + + + + &Quote character + + + comboQuoteCharacter + + + + + + + + " + + + + + ' + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + comboTable + checkHeader + comboFieldSeparator + comboQuoteCharacter + buttonBox + + + + + buttonBox + accepted() + ExportCsvDialog + accept() + + + 252 + 136 + + + 157 + 140 + + + + + buttonBox + rejected() + ExportCsvDialog + reject() + + + 320 + 136 + + + 286 + 140 + + + + + diff --git a/src/ExportTableCSVForm.cpp b/src/ExportTableCSVForm.cpp deleted file mode 100644 index aed92844..00000000 --- a/src/ExportTableCSVForm.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "ExportTableCSVForm.h" - -/* - * Constructs a exportTableCSVForm as a child of 'parent', with the - * name 'name' and widget flags set to 'f'. - * - * The dialog will by default be modeless, unless you set 'modal' to - * true to construct a modal dialog. - */ -exportTableCSVForm::exportTableCSVForm(QWidget* parent, Qt::WindowFlags fl) - : QDialog(parent, fl) -{ - setupUi(this); - -} - -/* - * Destroys the object and frees any allocated resources - */ -exportTableCSVForm::~exportTableCSVForm() -{ - // no need to delete child widgets, Qt does it all for us -} - -/* - * Sets the strings of the subwidgets using the current - * language. - */ -void exportTableCSVForm::languageChange() -{ - retranslateUi(this); -} - -void exportTableCSVForm::exportPressed() -{ - option = comboOptions->currentText(); - accept(); -} - -void exportTableCSVForm::populateOptions(QStringList entries) -{ - comboOptions->clear(); - comboOptions->addItems(entries); -} diff --git a/src/ExportTableCSVForm.h b/src/ExportTableCSVForm.h deleted file mode 100644 index 775deacf..00000000 --- a/src/ExportTableCSVForm.h +++ /dev/null @@ -1,130 +0,0 @@ -#ifndef EXPORTTABLECSVFORM_H -#define EXPORTTABLECSVFORM_H - -#include -#include -#include -#include -#include -#include -#include -#include - -class Ui_exportTableCSVForm -{ -public: - QVBoxLayout *vboxLayout; - QHBoxLayout *hboxLayout; - QLabel *textLabel2; - QComboBox *comboOptions; - QSpacerItem *spacer13; - QHBoxLayout *hboxLayout1; - QSpacerItem *spacer11; - QPushButton *buttonCancel; - QPushButton *buttonExport; - - void setupUi(QDialog *exportTableCSVForm) - { - if (exportTableCSVForm->objectName().isEmpty()) - exportTableCSVForm->setObjectName(QString::fromUtf8("exportTableCSVForm")); - exportTableCSVForm->resize(365, 150); - vboxLayout = new QVBoxLayout(exportTableCSVForm); - vboxLayout->setSpacing(6); - vboxLayout->setContentsMargins(11, 11, 11, 11); - vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); - hboxLayout = new QHBoxLayout(); - hboxLayout->setSpacing(6); - hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); - textLabel2 = new QLabel(exportTableCSVForm); - textLabel2->setObjectName(QString::fromUtf8("textLabel2")); - textLabel2->setWordWrap(false); - - hboxLayout->addWidget(textLabel2); - - comboOptions = new QComboBox(exportTableCSVForm); - comboOptions->setObjectName(QString::fromUtf8("comboOptions")); - - hboxLayout->addWidget(comboOptions); - - - vboxLayout->addLayout(hboxLayout); - - spacer13 = new QSpacerItem(20, 41, QSizePolicy::Minimum, QSizePolicy::Expanding); - - vboxLayout->addItem(spacer13); - - hboxLayout1 = new QHBoxLayout(); - hboxLayout1->setSpacing(6); - hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); - spacer11 = new QSpacerItem(29, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); - - hboxLayout1->addItem(spacer11); - - buttonCancel = new QPushButton(exportTableCSVForm); - buttonCancel->setObjectName(QString::fromUtf8("buttonCancel")); - buttonCancel->setDefault(false); - - hboxLayout1->addWidget(buttonCancel); - - buttonExport = new QPushButton(exportTableCSVForm); - buttonExport->setObjectName(QString::fromUtf8("buttonExport")); - - hboxLayout1->addWidget(buttonExport); - - - vboxLayout->addLayout(hboxLayout1); - - - retranslateUi(exportTableCSVForm); - QObject::connect(buttonExport, SIGNAL(clicked()), exportTableCSVForm, SLOT(exportPressed())); - QObject::connect(buttonCancel, SIGNAL(clicked()), exportTableCSVForm, SLOT(reject())); - - QMetaObject::connectSlotsByName(exportTableCSVForm); - } // setupUi - - void retranslateUi(QDialog *exportTableCSVForm) - { - exportTableCSVForm->setWindowTitle(QApplication::translate("exportTableCSVForm", "Choose table to export as CSV text", 0, QApplication::UnicodeUTF8)); - textLabel2->setText(QApplication::translate("exportTableCSVForm", "Table name:", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - comboOptions->setProperty("toolTip", QVariant(QApplication::translate("exportTableCSVForm", "Choose the table to delete", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - comboOptions->setProperty("whatsThis", QVariant(QApplication::translate("exportTableCSVForm", "Use this control to select the name of the table to be deleted", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - buttonCancel->setText(QApplication::translate("exportTableCSVForm", "Cancel", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonCancel->setProperty("toolTip", QVariant(QApplication::translate("exportTableCSVForm", "Cancel and close dialog box", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP - buttonExport->setText(QApplication::translate("exportTableCSVForm", "Export", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonExport->setProperty("toolTip", QVariant(QApplication::translate("exportTableCSVForm", "Delete the selected table", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP - } // retranslateUi - -}; - -namespace Ui { - class exportTableCSVForm: public Ui_exportTableCSVForm {}; -} // namespace Ui - -class exportTableCSVForm : public QDialog, public Ui::exportTableCSVForm -{ - Q_OBJECT - -public: - exportTableCSVForm(QWidget* parent = 0, Qt::WindowFlags fl = Qt::Window); - ~exportTableCSVForm(); - - QString option; - -public slots: - virtual void exportPressed(); - virtual void populateOptions( QStringList entries ); - -protected slots: - virtual void languageChange(); - -}; - -#endif // EXPORTTABLECSVFORM_H diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3addbc07..6a670822 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -16,7 +16,7 @@ #include "EditTableDialog.h" #include "EditFieldDialog.h" #include "ImportCSVForm.h" -#include "ExportTableCSVForm.h" +#include "ExportCsvDialog.h" #include "PreferencesDialog.h" #include "EditDialog.h" #include "FindDialog.h" @@ -838,80 +838,8 @@ void MainWindow::importTableFromCSV() void MainWindow::exportTableToCSV() { - exportTableCSVForm dialog(this); - dialog.populateOptions(db.getBrowsableObjectNames()); - if(dialog.exec()) - { - //load our table - db.browseTable(dialog.option); - - QString fileName = QFileDialog::getSaveFileName( - this, - "Choose a filename to export data", - defaultlocation, - "Text files(*.csv *.txt)"); - - if (fileName.size() > 0) - { - QFile file(fileName); - if ( file.open( QIODevice::WriteOnly ) ) - { - char quote = '"'; - char sep = ','; - char feed = 10; - int colNum = 0; - int colCount = 0; - QTextStream stream( &file ); - //fieldnames on first row - QStringList fields = db.browseFields; - colCount = fields.count(); - for ( QStringList::Iterator ct = fields.begin(); ct != fields.end(); ++ct ) { - stream << quote; - stream << *ct; - stream << quote; - colNum++; - if (colNum