Skip to content

Commit

Permalink
Distinguish Save and Save As when saving SQL files in the Execute SQL…
Browse files Browse the repository at this point in the history
… tab

When saving a SQL file in the Execute Query tab don't always ask for a
new file name but use the file name used when last saving the tab or
used when a file was opened. Add a 'Save As' menu option with the old
behaviour of always asking for a file name.

See issue sqlitebrowser#152.
  • Loading branch information
MKleusberg committed Nov 15, 2014
1 parent 33e7f6b commit f829f06
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
38 changes: 32 additions & 6 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ void MainWindow::init()
popupTableMenu->addSeparator();
popupTableMenu->addAction(ui->actionExportCsvPopup);

popupSaveSqlFileMenu = new QMenu(this);
popupSaveSqlFileMenu->addAction(ui->actionSqlSaveFile);
popupSaveSqlFileMenu->addAction(ui->actionSqlSaveFileAs);
ui->actionSqlSaveFilePopup->setMenu(popupSaveSqlFileMenu);

// Add menu item for log dock
ui->viewMenu->insertAction(ui->viewDBToolbarAction, ui->dockLog->toggleViewAction());
ui->viewMenu->actions().at(0)->setShortcut(QKeySequence(tr("Ctrl+L")));
Expand Down Expand Up @@ -1260,25 +1265,46 @@ void MainWindow::openSqlFile()
else
index = openSqlTab();

qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->widget(index))->getEditor()->setPlainText(f.readAll());
SqlExecutionArea* sqlarea = qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->widget(index));
sqlarea->getEditor()->setPlainText(f.readAll());
sqlarea->setFileName(file);
QFileInfo fileinfo(file);
ui->tabSqlAreas->setTabText(index, fileinfo.fileName());
}
}

void MainWindow::saveSqlFile()
{
SqlExecutionArea* sqlarea = qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->currentWidget());

// If this SQL file hasn't been saved before open the Save As dialog. Otherwise just use the old file name for saving
if(sqlarea->fileName().isEmpty())
{
saveSqlFileAs();
} else {
QFile f(sqlarea->fileName());
f.open(QIODevice::WriteOnly);
f.write(sqlarea->getSql().toUtf8());

QFileInfo fileinfo(sqlarea->fileName());
ui->tabSqlAreas->setTabText(ui->tabSqlAreas->currentIndex(), fileinfo.fileName());
}
}

void MainWindow::saveSqlFileAs()
{
QString file = QFileDialog::getSaveFileName(
this,
tr("Select file name"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
tr("Text files(*.sql *.txt);;All files(*)"));

QFile f(file);
f.open(QIODevice::WriteOnly);
f.write(qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->currentWidget())->getSql().toUtf8());
QFileInfo fileinfo(file);
ui->tabSqlAreas->setTabText(ui->tabSqlAreas->currentIndex(), fileinfo.fileName());
if(!file.isEmpty())
{
// Just set the selected file name and call the standard save action which is going to use it
qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->currentWidget())->setFileName(file);
saveSqlFile();
}
}

void MainWindow::loadExtension()
Expand Down
2 changes: 2 additions & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class MainWindow : public QMainWindow
SqliteTableModel* m_currentPlotModel;
QMenu *popupTableMenu;
QMenu *recentFilesMenu;
QMenu *popupSaveSqlFileMenu;

QLabel* statusEncodingLabel;
QLabel* statusEncryptionLabel;
Expand Down Expand Up @@ -159,6 +160,7 @@ private slots:
void closeSqlTab(int index, bool force = false);
void openSqlFile();
void saveSqlFile();
void saveSqlFileAs();
void loadExtension();
void reloadSettings();
void httpresponse(QNetworkReply* reply);
Expand Down
63 changes: 60 additions & 3 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>294</width>
<height>444</height>
<width>575</width>
<height>458</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
Expand Down Expand Up @@ -733,7 +733,7 @@
</property>
<addaction name="actionSqlOpenTab"/>
<addaction name="actionSqlOpenFile"/>
<addaction name="actionSqlSaveFile"/>
<addaction name="actionSqlSaveFilePopup"/>
<addaction name="separator"/>
<addaction name="actionExecuteSql"/>
<addaction name="actionSqlExecuteLine"/>
Expand Down Expand Up @@ -1533,6 +1533,30 @@
<string>Set Encryption</string>
</property>
</action>
<action name="actionSqlSaveFileAs">
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/save_sql</normaloff>:/icons/save_sql</iconset>
</property>
<property name="text">
<string>Save SQL file as</string>
</property>
<property name="toolTip">
<string>Save SQL file as</string>
</property>
</action>
<action name="actionSqlSaveFilePopup">
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/save_sql</normaloff>:/icons/save_sql</iconset>
</property>
<property name="text">
<string>Save SQL file</string>
</property>
<property name="toolTip">
<string>Save SQL file</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down Expand Up @@ -2340,6 +2364,38 @@
</hint>
</hints>
</connection>
<connection>
<sender>actionSqlSaveFilePopup</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>saveSqlFile()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>499</x>
<y>314</y>
</hint>
</hints>
</connection>
<connection>
<sender>actionSqlSaveFileAs</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>saveSqlFileAs()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>499</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
Expand Down Expand Up @@ -2390,5 +2446,6 @@
<slot>saveProject()</slot>
<slot>fileAttach()</slot>
<slot>editEncryption()</slot>
<slot>saveSqlFileAs()</slot>
</slots>
</ui>
4 changes: 4 additions & 0 deletions src/SqlExecutionArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class SqlExecutionArea : public QWidget
QString getSql() const;
QString getSelectedSql() const;

QString fileName() const { return sqlFileName; }
void setFileName(const QString& filename) { sqlFileName = filename; }

SqliteTableModel* getModel() { return model; }
QTextEdit* getResultView();
SqlTextEdit* getEditor();
Expand All @@ -40,6 +43,7 @@ public slots:
Ui::SqlExecutionArea* ui;
SqliteTableModel* model;
QMenu* menuPopupSave;
QString sqlFileName;
};

#endif

0 comments on commit f829f06

Please sign in to comment.