From c4ee9799bc82c8c543918e859808aa257b88d06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 31 Jul 2017 16:14:01 +0200 Subject: [PATCH] GUI: load/save data automatically in the ProjectFileDialog --- gui/mainwindow.cpp | 14 ++++++-------- gui/projectfiledialog.cpp | 21 +++++++++++++++------ gui/projectfiledialog.h | 9 ++++++--- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 4e566a780..8277645e7 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -1410,7 +1410,7 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile) void MainWindow::newProjectFile() { - const QString filter = tr("Project files (*.cppcheck);;All files(*.*)"); + const QString filter = tr("Project files (*.cppcheck)"); QString filepath = QFileDialog::getSaveFileName(this, tr("Select Project Filename"), getPath(SETTINGS_LAST_PROJECT_PATH), @@ -1418,6 +1418,8 @@ void MainWindow::newProjectFile() if (filepath.isEmpty()) return; + if (!filepath.endsWith(".cppcheck", Qt::CaseInsensitive)) + filepath += ".cppcheck"; setPath(SETTINGS_LAST_PROJECT_PATH, filepath); @@ -1427,12 +1429,10 @@ void MainWindow::newProjectFile() delete mProjectFile; mProjectFile = new ProjectFile(this); + mProjectFile->setFilename(filepath); - ProjectFileDialog dlg(filepath, this); - dlg.loadFromProjectFile(mProjectFile); + ProjectFileDialog dlg(mProjectFile, this); if (dlg.exec() == QDialog::Accepted) { - dlg.saveToProjectFile(mProjectFile); - mProjectFile->write(); addProjectMRU(filepath); analyzeProject(mProjectFile); } else { @@ -1461,10 +1461,8 @@ void MainWindow::editProjectFile() return; } - ProjectFileDialog dlg(mProjectFile->getFilename(), this); - dlg.loadFromProjectFile(mProjectFile); + ProjectFileDialog dlg(mProjectFile, this); if (dlg.exec() == QDialog::Accepted) { - dlg.saveToProjectFile(mProjectFile); mProjectFile->write(); analyzeProject(mProjectFile); } diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index 4264106d3..70d1968b0 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -32,13 +32,13 @@ #include "cppcheck.h" #include "errorlogger.h" -ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) +ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent) : QDialog(parent) - , mFilePath(path) + , mProjectFile(projectFile) { mUI.setupUi(this); - const QFileInfo inf(path); + const QFileInfo inf(projectFile->getFilename()); QString filename = inf.fileName(); QString title = tr("Project file: %1").arg(filename); setWindowTitle(title); @@ -90,7 +90,7 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) mLibraryCheckboxes << checkbox; } - connect(mUI.mButtons, &QDialogButtonBox::accepted, this, &ProjectFileDialog::accept); + connect(mUI.mButtons, &QDialogButtonBox::accepted, this, &ProjectFileDialog::ok); connect(mUI.mBtnBrowseBuildDir, &QPushButton::clicked, this, &ProjectFileDialog::browseBuildDir); connect(mUI.mBtnClearImportProject, &QPushButton::clicked, this, &ProjectFileDialog::clearImportProject); connect(mUI.mBtnBrowseImportProject, &QPushButton::clicked, this, &ProjectFileDialog::browseImportProject); @@ -107,6 +107,8 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) connect(mUI.mBtnIncludeDown, &QPushButton::clicked, this, &ProjectFileDialog::moveIncludePathDown); connect(mUI.mBtnAddSuppression, &QPushButton::clicked, this, &ProjectFileDialog::addSuppression); connect(mUI.mBtnRemoveSuppression, &QPushButton::clicked, this, &ProjectFileDialog::removeSuppression); + + loadFromProjectFile(projectFile); } ProjectFileDialog::~ProjectFileDialog() @@ -155,9 +157,16 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const projectFile->setSuppressions(getSuppressions()); } +void ProjectFileDialog::ok() +{ + saveToProjectFile(mProjectFile); + mProjectFile->write(); + accept(); +} + QString ProjectFileDialog::getExistingDirectory(const QString &caption, bool trailingSlash) { - const QFileInfo inf(mFilePath); + const QFileInfo inf(mProjectFile->getFilename()); const QString rootpath = inf.absolutePath(); QString selectedDir = QFileDialog::getExistingDirectory(this, caption, @@ -211,7 +220,7 @@ void ProjectFileDialog::clearImportProject() void ProjectFileDialog::browseImportProject() { - const QFileInfo inf(mFilePath); + const QFileInfo inf(mProjectFile->getFilename()); const QDir &dir = inf.absoluteDir(); QString fileName = QFileDialog::getOpenFileName(this, tr("Import Project"), dir.canonicalPath(), diff --git a/gui/projectfiledialog.h b/gui/projectfiledialog.h index 2e27730b2..824aaff3e 100644 --- a/gui/projectfiledialog.h +++ b/gui/projectfiledialog.h @@ -40,13 +40,13 @@ class ProjectFile; class ProjectFileDialog : public QDialog { Q_OBJECT public: - ProjectFileDialog(const QString &path, QWidget *parent = 0); + ProjectFileDialog(ProjectFile *projectFile, QWidget *parent = 0); virtual ~ProjectFileDialog(); +private: void loadFromProjectFile(const ProjectFile *projectFile); void saveToProjectFile(ProjectFile *projectFile) const; -private: /** Enable and disable widgets in the 'Paths and Defines' tab */ void updatePathsAndDefines(); @@ -146,6 +146,9 @@ private: protected slots: + /** ok button pressed, save changes and accept */ + void ok(); + /** * @brief Browse for build dir. */ @@ -263,7 +266,7 @@ private: /** * @brief Projectfile path. */ - QString mFilePath; + ProjectFile *mProjectFile; /** @brief Library checkboxes */ QList mLibraryCheckboxes;