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+
121153void LibraryDialog::addFunction ()
122154{
123155 LibraryAddFunctionDialog *d = new LibraryAddFunctionDialog;
0 commit comments