From 9536e7a09532fbacb44e879a43d7064b821ca962 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Mon, 6 May 2013 18:44:58 +0200 Subject: [PATCH] SqlExecutionArea: Support exporting result to CSV file Add a button which allows the user to save the query results to a CSV file. Add some basic preparations for saving the result as a view. --- src/ExportCsvDialog.cpp | 36 ++++++++++++---- src/ExportCsvDialog.h | 4 +- src/ExportCsvDialog.ui | 2 +- src/MainWindow.cpp | 1 + src/SqlExecutionArea.cpp | 27 +++++++++++- src/SqlExecutionArea.h | 8 +++- src/SqlExecutionArea.ui | 88 +++++++++++++++++++++++++++++++++++---- src/icons/icons.qrc | 1 + src/icons/table_save.png | Bin 0 -> 723 bytes src/sqlitetablemodel.h | 1 + 10 files changed, 146 insertions(+), 22 deletions(-) create mode 100644 src/icons/table_save.png diff --git a/src/ExportCsvDialog.cpp b/src/ExportCsvDialog.cpp index c97beadf..d2d722cb 100644 --- a/src/ExportCsvDialog.cpp +++ b/src/ExportCsvDialog.cpp @@ -8,18 +8,27 @@ #include "PreferencesDialog.h" #include "sqlitetablemodel.h" -ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent) +ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent, const QString& query) : QDialog(parent), ui(new Ui::ExportCsvDialog), - pdb(db) + pdb(db), + m_sQuery(query) { // 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()); + // If a SQL query was specified hide the table combo box. If not fill it with tables to export + if(query.isEmpty()) + { + // 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()); + } else { + // Hide table combo box + ui->labelTable->setVisible(false); + ui->comboTable->setVisible(false); + } } ExportCsvDialog::~ExportCsvDialog() @@ -39,9 +48,18 @@ void ExportCsvDialog::accept() // Only if the user hasn't clicked the cancel button if(fileName.size() > 0) { + unsigned int first_column; + // Get data from selected table SqliteTableModel tableModel(this, pdb); - tableModel.setTable(ui->comboTable->currentText()); + if(m_sQuery.isEmpty()) + { + tableModel.setTable(ui->comboTable->currentText()); + first_column = 1; + } else { + tableModel.setQuery(m_sQuery); + first_column = 0; + } while(tableModel.canFetchMore()) tableModel.fetchMore(); @@ -62,7 +80,7 @@ void ExportCsvDialog::accept() // Put field names in first row if user wants to have them if(ui->checkHeader->isChecked()) { - for(int i=1;i - Export table to CSV + Export data as CSV diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index e54b11bd..4d827ea9 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -675,6 +675,7 @@ void MainWindow::executeQuery() { statusMessage = tr("%1 Rows returned from: %2").arg( sqlWidget->getModel()->totalRowCount()).arg(queryPart); + sqlWidget->enableSaveButton(true); sql3status = SQLITE_OK; } else diff --git a/src/SqlExecutionArea.cpp b/src/SqlExecutionArea.cpp index 3a6c4b6a..b71481db 100644 --- a/src/SqlExecutionArea.cpp +++ b/src/SqlExecutionArea.cpp @@ -6,9 +6,12 @@ #include "sqlitetablemodel.h" #include "sqlitedb.h" #include "PreferencesDialog.h" +#include "ExportCsvDialog.h" +#include -SqlExecutionArea::SqlExecutionArea(QWidget* parent, DBBrowserDB* db) : +SqlExecutionArea::SqlExecutionArea(QWidget* parent, DBBrowserDB* _db) : QWidget(parent), + db(_db), ui(new Ui::SqlExecutionArea) { // Create UI @@ -20,6 +23,12 @@ SqlExecutionArea::SqlExecutionArea(QWidget* parent, DBBrowserDB* db) : // Create model model = new SqliteTableModel(this, db, PreferencesDialog::getSettingsValue("db", "prefetchsize").toInt()); ui->tableResult->setModel(model); + + // Create popup menu for save button + menuPopupSave = new QMenu(this); + menuPopupSave->addAction(ui->actionExportCsv); + //menuPopupSave->addAction(ui->actionSaveAsView); + ui->buttonSave->setMenu(menuPopupSave); } SqlExecutionArea::~SqlExecutionArea() @@ -52,3 +61,19 @@ SqlTextEdit* SqlExecutionArea::getEditor() { return ui->editEditor; } + +void SqlExecutionArea::enableSaveButton(bool enable) +{ + ui->buttonSave->setEnabled(enable); +} + +void SqlExecutionArea::saveAsCsv() +{ + ExportCsvDialog dialog(db, this, model->query()); + dialog.exec(); +} + +void SqlExecutionArea::saveAsView() +{ + // TODO +} diff --git a/src/SqlExecutionArea.h b/src/SqlExecutionArea.h index 9b61f118..6e920857 100644 --- a/src/SqlExecutionArea.h +++ b/src/SqlExecutionArea.h @@ -6,6 +6,7 @@ class SQLiteSyntaxHighlighter; class SqliteTableModel; class DBBrowserDB; +class QMenu; namespace Ui { class SqlExecutionArea; @@ -16,7 +17,7 @@ class SqlExecutionArea : public QWidget Q_OBJECT public: - explicit SqlExecutionArea(QWidget* parent = 0, DBBrowserDB* db = 0); + explicit SqlExecutionArea(QWidget* parent = 0, DBBrowserDB* _db = 0); ~SqlExecutionArea(); QString getSql() const; @@ -28,11 +29,16 @@ public: public slots: virtual void setTableNames(const QStringList& tables); virtual void finishExecution(const QString& result); + virtual void enableSaveButton(bool enable); + virtual void saveAsCsv(); + virtual void saveAsView(); private: + DBBrowserDB* db; Ui::SqlExecutionArea* ui; SQLiteSyntaxHighlighter* highlighter; SqliteTableModel* model; + QMenu* menuPopupSave; }; #endif diff --git a/src/SqlExecutionArea.ui b/src/SqlExecutionArea.ui index e724e350..b8659f39 100644 --- a/src/SqlExecutionArea.ui +++ b/src/SqlExecutionArea.ui @@ -6,8 +6,8 @@ 0 0 - 434 - 451 + 367 + 432 @@ -35,13 +35,44 @@ - - - - - + + + + + + + + + + + + false + + + + :/icons/save_table:/icons/save_table + + + QToolButton::InstantPopup + + + + + + + Export to &CSV + + + + + Save as &view + + + Save as view + + @@ -55,6 +86,45 @@
ExtendedTableWidget.h
- - + + + + + + actionExportCsv + activated() + SqlExecutionArea + saveAsCsv() + + + -1 + -1 + + + 183 + 215 + + + + + actionSaveAsView + activated() + SqlExecutionArea + saveAsView() + + + -1 + -1 + + + 183 + 215 + + + + + + saveAsCsv() + saveAsView() + diff --git a/src/icons/icons.qrc b/src/icons/icons.qrc index 5446532b..c614a1d4 100644 --- a/src/icons/icons.qrc +++ b/src/icons/icons.qrc @@ -30,6 +30,7 @@ page_white_database.png plugin_add.png plugin_delete.png + table_save.png oldimages/128.png diff --git a/src/icons/table_save.png b/src/icons/table_save.png new file mode 100644 index 0000000000000000000000000000000000000000..25b74d18f740ff208435f72333a1633569c0e833 GIT binary patch literal 723 zcmV;^0xbQBP)9=EBdhkBh&jKg_`&J(Mr{5K9?YB z7%NS3^ZC#w7o(tnOANaU&GbDgva8U-z`c)j6qd7VU7r32pE$mM#UEBK-6|qt9$Fs- z6>t{%pB71ynwG1c@8bc0>raNK6Dt8qRVe;QF-EXH2&zgZfzI{KWSyaNeKXb?5(OWH zv)$_uQKBdbLe2@*YL(JNiVIR}Vqy(9UXB8Al#S?e6$ef`OJ z*^@Dj?#>2+q<{qlHA2?aai;eJ-%1m>7t)ru4w<;#O*nKUzaX~*8Diz*A!^1TLcz