GUI: Remove Project class

This commit is contained in:
Daniel Marjamäki 2017-07-31 12:02:51 +02:00
parent 4986ffa2f1
commit 037fc6e3a4
8 changed files with 85 additions and 295 deletions

View File

@ -91,7 +91,6 @@ HEADERS += aboutdialog.h \
mainwindow.h \
platforms.h \
printablereport.h \
project.h \
projectfile.h \
projectfiledialog.h \
report.h \
@ -128,7 +127,6 @@ SOURCES += aboutdialog.cpp \
mainwindow.cpp\
platforms.cpp \
printablereport.cpp \
project.cpp \
projectfile.cpp \
projectfiledialog.cpp \
report.cpp \

View File

@ -35,7 +35,7 @@
#include "threadhandler.h"
#include "fileviewdialog.h"
#include "projectfile.h"
#include "project.h"
#include "projectfiledialog.h"
#include "report.h"
#include "scratchpad.h"
#include "statsdialog.h"
@ -56,7 +56,7 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
mTranslation(th),
mLogView(NULL),
mScratchPad(NULL),
mProject(NULL),
mProjectFile(NULL),
mPlatformActions(new QActionGroup(this)),
mCStandardActions(new QActionGroup(this)),
mCppStandardActions(new QActionGroup(this)),
@ -139,8 +139,8 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
loadSettings();
mThread->initialize(mUI.mResults);
if (mProject)
formatAndSetTitle(tr("Project:") + ' ' + mProject->getFilename());
if (mProjectFile)
formatAndSetTitle(tr("Project:") + ' ' + mProjectFile->getFilename());
else
formatAndSetTitle();
@ -173,8 +173,8 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
handleCLIParams(args);
}
mUI.mActionCloseProjectFile->setEnabled(mProject != nullptr);
mUI.mActionEditProjectFile->setEnabled(mProject != nullptr);
mUI.mActionCloseProjectFile->setEnabled(mProjectFile != nullptr);
mUI.mActionEditProjectFile->setEnabled(mProjectFile != nullptr);
for (int i = 0; i < mPlatforms.getCount(); i++) {
Platform plat = mPlatforms.mPlatforms[i];
@ -215,7 +215,7 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
MainWindow::~MainWindow()
{
delete mLogView;
delete mProject;
delete mProjectFile;
delete mScratchPad;
}
@ -321,7 +321,8 @@ void MainWindow::loadSettings()
QFileInfo inf(projectFile);
if (inf.exists() && inf.isReadable()) {
setPath(SETTINGS_LAST_PROJECT_PATH, projectFile);
mProject = new Project(projectFile, this);
mProjectFile = new ProjectFile(this);
mProjectFile->read(projectFile);
loadLastResults();
}
}
@ -365,7 +366,7 @@ void MainWindow::saveSettings() const
mSettings->setValue(SETTINGS_LANGUAGE, mTranslation->getCurrentLanguage());
mSettings->setValue(SETTINGS_OPEN_PROJECT, mProject ? mProject->getFilename() : QString());
mSettings->setValue(SETTINGS_OPEN_PROJECT, mProjectFile ? mProjectFile->getFilename() : QString());
mUI.mResults->saveSettings(mSettings);
}
@ -375,10 +376,10 @@ void MainWindow::doAnalyzeProject(ImportProject p)
clearResults();
mIsLogfileLoaded = false;
if (mProject) {
if (mProjectFile) {
std::vector<std::string> v;
foreach (const QString &s, mProject->getProjectFile()->getExcludedPaths()) {
v.push_back(s.toStdString());
foreach (const QString &i, mProjectFile->getExcludedPaths()) {
v.push_back(i.toStdString());
}
p.ignorePaths(v);
} else {
@ -400,8 +401,8 @@ void MainWindow::doAnalyzeProject(ImportProject p)
Settings checkSettings = getCppcheckSettings();
checkSettings.force = false;
if (mProject)
qDebug() << "Checking project file" << mProject->getProjectFile()->getFilename();
if (mProjectFile)
qDebug() << "Checking project file" << mProjectFile->getFilename();
if (!checkSettings.buildDir.empty()) {
std::list<std::string> sourcefiles;
@ -423,8 +424,8 @@ void MainWindow::doAnalyzeFiles(const QStringList &files)
mIsLogfileLoaded = false;
FileList pathList;
pathList.addPathList(files);
if (mProject) {
pathList.addExcludeList(mProject->getProjectFile()->getExcludedPaths());
if (mProjectFile) {
pathList.addExcludeList(mProjectFile->getExcludedPaths());
} else {
enableProjectActions(false);
}
@ -455,8 +456,8 @@ void MainWindow::doAnalyzeFiles(const QStringList &files)
mUI.mResults->setCheckDirectory(checkPath);
Settings checkSettings = getCppcheckSettings();
if (mProject)
qDebug() << "Checking project file" << mProject->getProjectFile()->getFilename();
if (mProjectFile)
qDebug() << "Checking project file" << mProjectFile->getFilename();
if (!checkSettings.buildDir.empty()) {
std::list<std::string> sourcefiles;
@ -497,7 +498,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
QStringList MainWindow::selectFilesToAnalyze(QFileDialog::FileMode mode)
{
if (mProject) {
if (mProjectFile) {
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
const QString msg(tr("You must close the project file before selecting new files or directories!"));
@ -656,8 +657,8 @@ Library::Error MainWindow::loadLibrary(Library *library, QString filename)
Library::Error ret;
// Try to load the library from the project folder..
if (mProject) {
QString path = QFileInfo(mProject->getProjectFile()->getFilename()).canonicalPath();
if (mProjectFile) {
QString path = QFileInfo(mProjectFile->getFilename()).canonicalPath();
ret = library->load(NULL, (path+"/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
@ -755,25 +756,24 @@ Settings MainWindow::getCppcheckSettings()
Settings result;
// If project file loaded, read settings from it
if (mProject) {
ProjectFile *pfile = mProject->getProjectFile();
QStringList dirs = pfile->getIncludeDirs();
if (mProjectFile) {
QStringList dirs = mProjectFile->getIncludeDirs();
addIncludeDirs(dirs, result);
const QStringList defines = pfile->getDefines();
const QStringList defines = mProjectFile->getDefines();
foreach (QString define, defines) {
if (!result.userDefines.empty())
result.userDefines += ";";
result.userDefines += define.toStdString();
}
const QStringList libraries = pfile->getLibraries();
const QStringList libraries = mProjectFile->getLibraries();
foreach (QString library, libraries) {
const QString filename = library + ".cfg";
tryLoadLibrary(&result.library, filename);
}
const QStringList suppressions = pfile->getSuppressions();
const QStringList suppressions = mProjectFile->getSuppressions();
foreach (QString suppression, suppressions) {
result.nomsg.addSuppressionLine(suppression.toStdString());
}
@ -782,9 +782,9 @@ Settings MainWindow::getCppcheckSettings()
if (!defines.isEmpty())
result.maxConfigs = 1;
QString buildDir = pfile->getBuildDir();
const QString &buildDir = mProjectFile->getBuildDir();
if (!buildDir.isEmpty()) {
QString prjpath = QFileInfo(pfile->getFilename()).absolutePath();
QString prjpath = QFileInfo(mProjectFile->getFilename()).absolutePath();
result.buildDir = (prjpath + '/' + buildDir).toStdString();
}
}
@ -852,7 +852,7 @@ void MainWindow::analysisDone()
enableCheckButtons(true);
mUI.mActionSettings->setEnabled(true);
mUI.mActionOpenXML->setEnabled(true);
if (mProject) {
if (mProjectFile) {
enableProjectActions(true);
} else if (mIsLogfileLoaded) {
mUI.mActionReanalyzeModified->setEnabled(false);
@ -867,9 +867,9 @@ void MainWindow::analysisDone()
if (mScratchPad)
mScratchPad->setEnabled(true);
if (mProject && !mProject->getProjectFile()->getBuildDir().isEmpty()) {
const QString prjpath = QFileInfo(mProject->getProjectFile()->getFilename()).absolutePath();
const QString buildDir = prjpath + '/' + mProject->getProjectFile()->getBuildDir();
if (mProjectFile && !mProjectFile->getBuildDir().isEmpty()) {
const QString prjpath = QFileInfo(mProjectFile->getFilename()).absolutePath();
const QString buildDir = prjpath + '/' + mProjectFile->getBuildDir();
if (QDir(buildDir).exists()) {
mUI.mResults->saveStatistics(buildDir + "/statistics.txt");
mUI.mResults->save(buildDir + "/lastResults.xml", Report::XMLV2);
@ -938,8 +938,8 @@ void MainWindow::reAnalyzeModified()
void MainWindow::reAnalyzeAll()
{
if (mProject)
analyzeProject(mProject);
if (mProjectFile)
analyzeProject(mProjectFile);
else
reAnalyze(true);
}
@ -959,8 +959,8 @@ void MainWindow::reAnalyzeSelected(QStringList files, bool all)
mCurrentDirectory = mUI.mResults->getCheckDirectory();
FileList pathList;
pathList.addPathList(files);
if (mProject)
pathList.addExcludeList(mProject->getProjectFile()->getExcludedPaths());
if (mProjectFile)
pathList.addExcludeList(mProjectFile->getExcludedPaths());
QStringList fileNames = pathList.getFileList();
checkLockDownUI(); // lock UI while checking
mUI.mResults->checkingStarted(fileNames.size());
@ -990,8 +990,8 @@ void MainWindow::reAnalyze(bool all)
checkLockDownUI(); // lock UI while checking
mUI.mResults->checkingStarted(files.size());
if (mProject)
qDebug() << "Rechecking project file" << mProject->getProjectFile()->getFilename();
if (mProjectFile)
qDebug() << "Rechecking project file" << mProjectFile->getFilename();
mThread->setCheckFiles(all);
mThread->check(getCppcheckSettings(), all);
@ -1041,7 +1041,7 @@ void MainWindow::openResults()
void MainWindow::loadResults(const QString selectedFile)
{
if (!selectedFile.isEmpty()) {
if (mProject)
if (mProjectFile)
closeProjectFile();
mIsLogfileLoaded = true;
mUI.mResults->clear(true);
@ -1063,7 +1063,7 @@ void MainWindow::enableCheckButtons(bool enable)
mUI.mActionStop->setEnabled(!enable);
mUI.mActionAnalyzeFiles->setEnabled(enable);
if (mProject) {
if (mProjectFile) {
mUI.mActionReanalyzeModified->setEnabled(false);
mUI.mActionReanalyzeAll->setEnabled(enable);
} else if (!enable || mThread->hasPreviousFiles()) {
@ -1329,21 +1329,19 @@ void MainWindow::loadProjectFile(const QString &filePath)
mIsLogfileLoaded = false;
mUI.mActionCloseProjectFile->setEnabled(true);
mUI.mActionEditProjectFile->setEnabled(true);
delete mProject;
mProject = new Project(filePath, this);
delete mProjectFile;
mProjectFile = new ProjectFile(filePath, this);
if (!loadLastResults())
analyzeProject(mProject);
analyzeProject(mProjectFile);
}
bool MainWindow::loadLastResults()
{
if (!mProject)
if (!mProjectFile)
return false;
if (!mProject->isOpen() && !mProject->open())
if (mProjectFile->getBuildDir().isEmpty())
return false;
if (mProject->getProjectFile()->getBuildDir().isEmpty())
return false;
const QString buildDir = QFileInfo(mProject->getFilename()).absolutePath() + '/' + mProject->getProjectFile()->getBuildDir();
const QString buildDir = QFileInfo(mProjectFile->getFilename()).absolutePath() + '/' + mProjectFile->getBuildDir();
const QString lastResults = buildDir + "/lastResults.xml";
if (!QFileInfo(lastResults).exists())
return false;
@ -1351,18 +1349,10 @@ bool MainWindow::loadLastResults()
return true;
}
void MainWindow::analyzeProject(Project *project)
void MainWindow::analyzeProject(const ProjectFile *projectFile)
{
if (!project->isOpen()) {
if (!project->open()) {
delete mProject;
mProject = 0;
return;
}
}
QFileInfo inf(project->getFilename());
const QString rootpath = project->getProjectFile()->getRootPath();
QFileInfo inf(projectFile->getFilename());
const QString rootpath = projectFile->getRootPath();
// If the root path is not given or is not "current dir", use project
// file's location directory as root path
@ -1373,8 +1363,8 @@ void MainWindow::analyzeProject(Project *project)
else
mCurrentDirectory = rootpath;
if (!project->getProjectFile()->getBuildDir().isEmpty()) {
QString buildDir = project->getProjectFile()->getBuildDir();
if (!projectFile->getBuildDir().isEmpty()) {
QString buildDir = projectFile->getBuildDir();
if (!QDir::isAbsolutePath(buildDir))
buildDir = mCurrentDirectory + '/' + buildDir;
if (!QDir(buildDir).exists()) {
@ -1389,15 +1379,15 @@ void MainWindow::analyzeProject(Project *project)
}
}
if (!project->getProjectFile()->getImportProject().isEmpty()) {
if (!projectFile->getImportProject().isEmpty()) {
ImportProject p;
QString prjfile = inf.canonicalPath() + '/' + project->getProjectFile()->getImportProject();
QString prjfile = inf.canonicalPath() + '/' + projectFile->getImportProject();
p.import(prjfile.toStdString());
doAnalyzeProject(p);
return;
}
QStringList paths = project->getProjectFile()->getCheckPaths();
QStringList paths = projectFile->getCheckPaths();
// If paths not given then check the root path (which may be the project
// file's location, see above). This is to keep the compatibility with
@ -1431,24 +1421,29 @@ void MainWindow::newProjectFile()
setPath(SETTINGS_LAST_PROJECT_PATH, filepath);
enableProjectActions(true);
QFileInfo inf(filepath);
const QString filename = inf.fileName();
formatAndSetTitle(tr("Project:") + QString(" ") + filename);
delete mProject;
mProject = new Project(filepath, this);
mProject->create();
if (mProject->edit()) {
delete mProjectFile;
mProjectFile = new ProjectFile(this);
ProjectFileDialog dlg(filepath, this);
dlg.loadFromProjectFile(mProjectFile);
if (dlg.exec() == QDialog::Accepted) {
dlg.saveToProjectFile(mProjectFile);
mProjectFile->write();
addProjectMRU(filepath);
analyzeProject(mProject);
analyzeProject(mProjectFile);
} else {
closeProjectFile();
}
}
void MainWindow::closeProjectFile()
{
delete mProject;
mProject = NULL;
delete mProjectFile;
mProjectFile = NULL;
enableProjectActions(false);
enableProjectOpenActions(true);
formatAndSetTitle();
@ -1456,7 +1451,7 @@ void MainWindow::closeProjectFile()
void MainWindow::editProjectFile()
{
if (!mProject) {
if (!mProjectFile) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
QString(tr("No project file loaded")),
@ -1465,8 +1460,14 @@ void MainWindow::editProjectFile()
msg.exec();
return;
}
if (mProject->edit())
analyzeProject(mProject);
ProjectFileDialog dlg(mProjectFile->getFilename(), this);
dlg.loadFromProjectFile(mProjectFile);
if (dlg.exec() == QDialog::Accepted) {
dlg.saveToProjectFile(mProjectFile);
mProjectFile->write();
analyzeProject(mProjectFile);
}
}
void MainWindow::showLogView()
@ -1484,9 +1485,7 @@ void MainWindow::showStatistics()
StatsDialog statsDialog(this);
// Show a dialog with the previous scan statistics and project information
if (mProject) {
statsDialog.setProject(*mProject);
}
statsDialog.setProject(mProjectFile);
statsDialog.setPathSelected(mCurrentDirectory);
statsDialog.setNumberOfFilesScanned(mThread->getPreviousFilesCount());
statsDialog.setScanDuration(mThread->getPreviousScanDuration() / 1000.0);

View File

@ -34,7 +34,7 @@ class ThreadHandler;
class TranslationHandler;
class ScratchPad;
class LogView;
class Project;
class ProjectFile;
class ErrorItem;
class QAction;
@ -237,7 +237,7 @@ private:
* @brief Analyze the project.
* @param project Pointer to the project to analyze.
*/
void analyzeProject(Project *project);
void analyzeProject(const ProjectFile *projectFile);
/**
* @brief Set current language
@ -413,7 +413,7 @@ private:
ScratchPad* mScratchPad;
/** @brief Project (file). */
Project *mProject;
ProjectFile *mProjectFile;
/** @brief Filter field in the Filter toolbar. */
QLineEdit* mLineEditFilter;

View File

@ -1,111 +0,0 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2016 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QWidget>
#include <QDialog>
#include <QFile>
#include <QMessageBox>
#include <QString>
#include <QStringList>
#include "project.h"
#include "projectfile.h"
#include "projectfiledialog.h"
Project::Project(QWidget *parent) :
QObject(parent),
mProjectFile(NULL),
mParentWidget(parent)
{
}
Project::Project(const QString &filename, QWidget *parent) :
QObject(parent),
mFilename(filename),
mProjectFile(NULL),
mParentWidget(parent)
{
}
Project::~Project()
{
delete mProjectFile;
}
QString Project::getFilename() const
{
return mFilename;
}
void Project::setFilename(const QString &filename)
{
mFilename = filename;
}
bool Project::isOpen() const
{
return mProjectFile != NULL;
}
bool Project::open()
{
mProjectFile = new ProjectFile(mFilename, this);
if (!QFile::exists(mFilename))
return false;
if (!mProjectFile->read()) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Could not read the project file."),
QMessageBox::Ok,
mParentWidget);
msg.exec();
mFilename = QString();
mProjectFile->setFilename(mFilename);
return false;
}
return true;
}
bool Project::edit()
{
ProjectFileDialog dlg(mFilename, mParentWidget);
dlg.loadFromProjectFile(mProjectFile);
if (dlg.exec() != QDialog::Accepted)
return false;
dlg.saveToProjectFile(mProjectFile);
if (!mProjectFile->write()) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Could not write the project file."),
QMessageBox::Ok,
mParentWidget);
msg.exec();
return false;
}
return true;
}
void Project::create()
{
mProjectFile = new ProjectFile(mFilename, this);
}

View File

@ -1,95 +0,0 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2016 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROJECT_H
#define PROJECT_H
#include <QObject>
#include <QString>
class QWidget;
class ProjectFile;
/// @addtogroup GUI
/// @{
/**
* @brief A class that contains project data and manages projects.
* Currently only project file creation and editing is implemented.
*/
class Project : public QObject {
Q_OBJECT
public:
explicit Project(QWidget *parent = 0);
Project(const QString &filename, QWidget *parent = 0);
~Project();
/**
* @brief Return the filename of the project.
* @return Project's filename.
*/
QString getFilename() const;
/**
* @brief Set filename for the project file.
* @param filename Filename.
*/
void setFilename(const QString &filename);
/**
* @brief Is the project open?
* The project is considered to be open if it has an opened project file.
* @return true if the project is open, false otherwise.
*/
bool isOpen() const;
/**
* @brief Open existing project file.
*/
bool open();
/**
* @brief Edit the project file.
* @return true if editing was successful.
*/
bool edit();
/**
* @brief Create new project file.
*/
void create();
/**
* @brief Return current project file.
* @return project file.
*/
ProjectFile * getProjectFile() const {
return mProjectFile;
}
private:
QString mFilename;
ProjectFile *mProjectFile;
QWidget *mParentWidget;
};
/// @}
#endif // PROJECT_H

View File

@ -60,6 +60,7 @@ ProjectFile::ProjectFile(const QString &filename, QObject *parent) :
QObject(parent),
mFilename(filename)
{
read();
}
bool ProjectFile::read(const QString &filename)

View File

@ -25,7 +25,6 @@
#include <QString>
#include <QClipboard>
#include <QMimeData>
#include "project.h"
#include "projectfile.h"
#include "statsdialog.h"
#include "checkstatistics.h"
@ -39,9 +38,8 @@ StatsDialog::StatsDialog(QWidget *parent)
connect(mUI.mPDFexport, SIGNAL(pressed()), this, SLOT(pdfExport()));
}
void StatsDialog::setProject(const Project& project)
void StatsDialog::setProject(const ProjectFile* projectFile)
{
ProjectFile *projectFile = project.getProjectFile();
if (projectFile) {
mUI.mProject->setText(projectFile->getRootPath());
mUI.mPaths->setText(projectFile->getCheckPaths().join(";"));

View File

@ -22,7 +22,7 @@
#include <QDialog>
#include "ui_stats.h"
class Project;
class ProjectFile;
class CheckStatistics;
/// @addtogroup GUI
@ -40,7 +40,7 @@ public:
/**
* @brief Sets the project to extract statistics from
*/
void setProject(const Project& project);
void setProject(const ProjectFile *projectFile);
/**
* @brief Sets the string to display beside "Path Selected:"