Fixed #7173 (Library Editor: Save As button and error messages)

This commit is contained in:
Aleksandr Pikalev 2015-11-28 20:31:56 +03:00 committed by Daniel Marjamäki
parent f5194accfd
commit fe8cedade5
3 changed files with 58 additions and 1 deletions

View File

@ -20,12 +20,14 @@
#include "ui_librarydialog.h" #include "ui_librarydialog.h"
#include "libraryaddfunctiondialog.h" #include "libraryaddfunctiondialog.h"
#include "libraryeditargdialog.h" #include "libraryeditargdialog.h"
#include "path.h"
#include <QFile> #include <QFile>
#include <QSettings> #include <QSettings>
#include <QFileDialog> #include <QFileDialog>
#include <QTextStream> #include <QTextStream>
#include <QInputDialog> #include <QInputDialog>
#include <QMessageBox>
// TODO: get/compare functions from header // TODO: get/compare functions from header
@ -49,6 +51,7 @@ LibraryDialog::LibraryDialog(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
ui->buttonSave->setEnabled(false); ui->buttonSave->setEnabled(false);
ui->buttonSaveAs->setEnabled(false);
ui->sortFunctions->setEnabled(false); ui->sortFunctions->setEnabled(false);
ui->filter->setEnabled(false); ui->filter->setEnabled(false);
ui->addFunction->setEnabled(false); ui->addFunction->setEnabled(false);
@ -84,13 +87,13 @@ void LibraryDialog::openCfg()
&selectedFilter); &selectedFilter);
if (!selectedFile.isEmpty()) { if (!selectedFile.isEmpty()) {
mFileName.clear();
QFile file(selectedFile); QFile file(selectedFile);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ignoreChanges = true; ignoreChanges = true;
data.open(file); data.open(file);
mFileName = selectedFile; mFileName = selectedFile;
ui->buttonSave->setEnabled(false); ui->buttonSave->setEnabled(false);
ui->buttonSaveAs->setEnabled(true);
ui->filter->clear(); ui->filter->clear();
ui->functions->clear(); ui->functions->clear();
for (struct CppcheckLibraryData::Function &function : data.functions) { for (struct CppcheckLibraryData::Function &function : data.functions) {
@ -102,6 +105,13 @@ void LibraryDialog::openCfg()
ui->filter->setEnabled(!data.functions.empty()); ui->filter->setEnabled(!data.functions.empty());
ui->addFunction->setEnabled(true); ui->addFunction->setEnabled(true);
ignoreChanges = false; ignoreChanges = false;
} else {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Can not open file %1.").arg(selectedFile),
QMessageBox::Ok,
this);
msg.exec();
} }
} }
} }
@ -115,9 +125,31 @@ void LibraryDialog::saveCfg()
QTextStream ts(&file); QTextStream ts(&file);
ts << data.toString() << '\n'; ts << data.toString() << '\n';
ui->buttonSave->setEnabled(false); ui->buttonSave->setEnabled(false);
} else {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Can not save file %1.").arg(mFileName),
QMessageBox::Ok,
this);
msg.exec();
} }
} }
void LibraryDialog::saveCfgAs()
{
const QString filter(tr("Library files (*.cfg)"));
const QString path = Path::getPathFromFilename(mFileName.toStdString()).c_str();
QString selectedFile = QFileDialog::getSaveFileName(this,
tr("Save the library as"),
path,
filter);
if (!selectedFile.endsWith(".cfg", Qt::CaseInsensitive))
selectedFile += ".cfg";
mFileName = selectedFile;
saveCfg();
}
void LibraryDialog::addFunction() void LibraryDialog::addFunction()
{ {
LibraryAddFunctionDialog *d = new LibraryAddFunctionDialog; LibraryAddFunctionDialog *d = new LibraryAddFunctionDialog;

View File

@ -40,6 +40,7 @@ public:
private slots: private slots:
void openCfg(); void openCfg();
void saveCfg(); void saveCfg();
void saveCfgAs();
void addFunction(); void addFunction();
void changeFunction(); void changeFunction();
void editArg(); void editArg();

View File

@ -30,6 +30,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="buttonSaveAs">
<property name="text">
<string>Save as</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">
@ -499,6 +506,22 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>buttonSaveAs</sender>
<signal>clicked()</signal>
<receiver>LibraryDialog</receiver>
<slot>saveCfgAs()</slot>
<hints>
<hint type="sourcelabel">
<x>211</x>
<y>31</y>
</hint>
<hint type="destinationlabel">
<x>232</x>
<y>33</y>
</hint>
</hints>
</connection>
</connections> </connections>
<slots> <slots>
<slot>addFunction()</slot> <slot>addFunction()</slot>
@ -511,5 +534,6 @@
<slot>selectFunction()</slot> <slot>selectFunction()</slot>
<slot>sortFunctions(bool)</slot> <slot>sortFunctions(bool)</slot>
<slot>editFunctionName(QListWidgetItem*)</slot> <slot>editFunctionName(QListWidgetItem*)</slot>
<slot>saveCfgAs()</slot>
</slots> </slots>
</ui> </ui>