Skip to content

Commit fe8ceda

Browse files
Aleksandr Pikalevdanmar
authored andcommitted
Fixed cppcheck-opensource#7173 (Library Editor: Save As button and error messages)
1 parent f5194ac commit fe8ceda

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

gui/librarydialog.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
#include "ui_librarydialog.h"
2121
#include "libraryaddfunctiondialog.h"
2222
#include "libraryeditargdialog.h"
23+
#include "path.h"
2324

2425
#include <QFile>
2526
#include <QSettings>
2627
#include <QFileDialog>
2728
#include <QTextStream>
2829
#include <QInputDialog>
30+
#include <QMessageBox>
2931

3032
// TODO: get/compare functions from header
3133

@@ -49,6 +51,7 @@ LibraryDialog::LibraryDialog(QWidget *parent) :
4951
{
5052
ui->setupUi(this);
5153
ui->buttonSave->setEnabled(false);
54+
ui->buttonSaveAs->setEnabled(false);
5255
ui->sortFunctions->setEnabled(false);
5356
ui->filter->setEnabled(false);
5457
ui->addFunction->setEnabled(false);
@@ -84,13 +87,13 @@ void LibraryDialog::openCfg()
8487
&selectedFilter);
8588

8689
if (!selectedFile.isEmpty()) {
87-
mFileName.clear();
8890
QFile file(selectedFile);
8991
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
9092
ignoreChanges = true;
9193
data.open(file);
9294
mFileName = selectedFile;
9395
ui->buttonSave->setEnabled(false);
96+
ui->buttonSaveAs->setEnabled(true);
9497
ui->filter->clear();
9598
ui->functions->clear();
9699
for (struct CppcheckLibraryData::Function &function : data.functions) {
@@ -102,6 +105,13 @@ void LibraryDialog::openCfg()
102105
ui->filter->setEnabled(!data.functions.empty());
103106
ui->addFunction->setEnabled(true);
104107
ignoreChanges = false;
108+
} else {
109+
QMessageBox msg(QMessageBox::Critical,
110+
tr("Cppcheck"),
111+
tr("Can not open file %1.").arg(selectedFile),
112+
QMessageBox::Ok,
113+
this);
114+
msg.exec();
105115
}
106116
}
107117
}
@@ -115,9 +125,31 @@ void LibraryDialog::saveCfg()
115125
QTextStream ts(&file);
116126
ts << data.toString() << '\n';
117127
ui->buttonSave->setEnabled(false);
128+
} else {
129+
QMessageBox msg(QMessageBox::Critical,
130+
tr("Cppcheck"),
131+
tr("Can not save file %1.").arg(mFileName),
132+
QMessageBox::Ok,
133+
this);
134+
msg.exec();
118135
}
119136
}
120137

138+
void LibraryDialog::saveCfgAs()
139+
{
140+
const QString filter(tr("Library files (*.cfg)"));
141+
const QString path = Path::getPathFromFilename(mFileName.toStdString()).c_str();
142+
QString selectedFile = QFileDialog::getSaveFileName(this,
143+
tr("Save the library as"),
144+
path,
145+
filter);
146+
if (!selectedFile.endsWith(".cfg", Qt::CaseInsensitive))
147+
selectedFile += ".cfg";
148+
149+
mFileName = selectedFile;
150+
saveCfg();
151+
}
152+
121153
void LibraryDialog::addFunction()
122154
{
123155
LibraryAddFunctionDialog *d = new LibraryAddFunctionDialog;

gui/librarydialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class LibraryDialog : public QDialog {
4040
private slots:
4141
void openCfg();
4242
void saveCfg();
43+
void saveCfgAs();
4344
void addFunction();
4445
void changeFunction();
4546
void editArg();

gui/librarydialog.ui

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
</property>
3131
</widget>
3232
</item>
33+
<item>
34+
<widget class="QPushButton" name="buttonSaveAs">
35+
<property name="text">
36+
<string>Save as</string>
37+
</property>
38+
</widget>
39+
</item>
3340
<item>
3441
<spacer name="horizontalSpacer">
3542
<property name="orientation">
@@ -499,6 +506,22 @@
499506
</hint>
500507
</hints>
501508
</connection>
509+
<connection>
510+
<sender>buttonSaveAs</sender>
511+
<signal>clicked()</signal>
512+
<receiver>LibraryDialog</receiver>
513+
<slot>saveCfgAs()</slot>
514+
<hints>
515+
<hint type="sourcelabel">
516+
<x>211</x>
517+
<y>31</y>
518+
</hint>
519+
<hint type="destinationlabel">
520+
<x>232</x>
521+
<y>33</y>
522+
</hint>
523+
</hints>
524+
</connection>
502525
</connections>
503526
<slots>
504527
<slot>addFunction()</slot>
@@ -511,5 +534,6 @@
511534
<slot>selectFunction()</slot>
512535
<slot>sortFunctions(bool)</slot>
513536
<slot>editFunctionName(QListWidgetItem*)</slot>
537+
<slot>saveCfgAs()</slot>
514538
</slots>
515539
</ui>

0 commit comments

Comments
 (0)