Distinguish Save and Save As when saving SQL files in the Execute SQL 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 #152.
This commit is contained in:
Martin Kleusberg
2014-11-15 12:32:30 +01:00
parent 33e7f6bd55
commit f829f06050
4 changed files with 98 additions and 9 deletions

View File

@@ -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")));
@@ -1260,13 +1265,33 @@ 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,
@@ -1274,11 +1299,12 @@ void MainWindow::saveSqlFile()
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()

View File

@@ -70,6 +70,7 @@ private:
SqliteTableModel* m_currentPlotModel;
QMenu *popupTableMenu;
QMenu *recentFilesMenu;
QMenu *popupSaveSqlFileMenu;
QLabel* statusEncodingLabel;
QLabel* statusEncryptionLabel;
@@ -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);

View File

@@ -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">
@@ -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"/>
@@ -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>
@@ -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>
@@ -2390,5 +2446,6 @@
<slot>saveProject()</slot>
<slot>fileAttach()</slot>
<slot>editEncryption()</slot>
<slot>saveSqlFileAs()</slot>
</slots>
</ui>

View File

@@ -24,6 +24,9 @@ public:
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();
@@ -40,6 +43,7 @@ private:
Ui::SqlExecutionArea* ui;
SqliteTableModel* model;
QMenu* menuPopupSave;
QString sqlFileName;
};
#endif