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 00000000..25b74d18 Binary files /dev/null and b/src/icons/table_save.png differ diff --git a/src/sqlitetablemodel.h b/src/sqlitetablemodel.h index eeeab22c..d8f67042 100644 --- a/src/sqlitetablemodel.h +++ b/src/sqlitetablemodel.h @@ -26,6 +26,7 @@ public: bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()); void setQuery(const QString& sQuery, bool dontClearHeaders = false); + QString query() const { return m_sQuery; } void setTable(const QString& table); void setChunkSize(size_t chunksize); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);