GUI: Add Project class to handle project files and projects.

This commit separates logic more from the GUI. The dialog class is
only responsible from showing the dialog and handling data in it.
Other related classes do the project file reading/saving/etc.
This commit is contained in:
Kimmo Varis 2010-07-12 20:21:45 +03:00
parent 1504747030
commit bf1b49b370
4 changed files with 74 additions and 101 deletions

View File

@ -53,6 +53,7 @@ HEADERS += mainwindow.h \
common.h \
erroritem.h \
fileviewdialog.h \
project.h \
projectfile.h \
projectfiledialog.h \
report.h \
@ -72,6 +73,7 @@ SOURCES += main.cpp \
applicationdialog.cpp \
aboutdialog.cpp \
fileviewdialog.cpp \
project.cpp \
projectfile.cpp \
projectfiledialog.cpp \
erroritem.cpp \

View File

@ -31,7 +31,7 @@
#include "threadhandler.h"
#include "fileviewdialog.h"
#include "projectfile.h"
#include "projectfiledialog.h"
#include "project.h"
#include "report.h"
#include "../lib/filelister.h"
@ -693,13 +693,24 @@ void MainWindow::ShowProjectFileDialog()
if (!filepath.isEmpty())
{
ProjectFileDialog dlg(filepath, this);
dlg.exec();
Project prj(filepath, this);
if (prj.Open())
prj.Edit();
}
}
void MainWindow::NewProjectFileDialog()
{
ProjectFileDialog dlg(QString(), this);
dlg.exec();
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
QString filepath = QFileDialog::getSaveFileName(this,
tr("Select Project Filename"),
QString(),
filter);
if (!filepath.isEmpty())
{
Project prj(filepath, this);
prj.Create();
prj.Edit();
}
}

View File

@ -17,102 +17,17 @@
*/
#include <QStringList>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include "projectfiledialog.h"
#include "projectfile.h"
ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
: QDialog(parent)
, mFileName(path)
, mDataSaved(false)
{
mUI.setupUi(this);
connect(mUI.mButtons, SIGNAL(accepted()), this, SLOT(accept()));
connect(this, SIGNAL(accepted()), this, SLOT(DialogAccepted()));
mPFile = new ProjectFile(path, this);
if (QFile::exists(path))
{
ReadProjectFile();
}
}
void ProjectFileDialog::ReadProjectFile()
{
if (!mPFile->Read())
{
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Could not read the project file."),
QMessageBox::Ok,
this);
msg.exec();
mFileName = QString();
mPFile->SetFilename(mFileName);
}
QStringList includes = mPFile->GetIncludeDirs();
QString includestr;
QString dir;
foreach(dir, includes)
{
includestr += dir;
includestr += ";";
}
mUI.mEditIncludePaths->setText(includestr);
QStringList defines = mPFile->GetDefines();
QString definestr;
QString define;
foreach(define, defines)
{
definestr += define;
definestr += ";";
}
mUI.mEditDefines->setText(definestr);
}
void ProjectFileDialog::DialogAccepted()
{
if (mDataSaved)
return;
UpdateProjectFileData();
bool writesuccess = false;
if (mFileName.isEmpty())
{
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
QString filepath = QFileDialog::getSaveFileName(this,
tr("Save Project File"),
QString(),
filter);
if (!filepath.isEmpty())
{
writesuccess = mPFile->Write(filepath);
}
}
else
{
writesuccess = mPFile->Write();
}
if (!writesuccess)
{
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Could not write the project file."),
QMessageBox::Ok,
this);
msg.exec();
}
mDataSaved = true;
}
void ProjectFileDialog::UpdateProjectFileData()
QStringList ProjectFileDialog::GetIncludePaths() const
{
QString include = mUI.mEditIncludePaths->text();
QStringList includes;
@ -124,8 +39,11 @@ void ProjectFileDialog::UpdateProjectFileData()
else
includes.append(include);
}
mPFile->SetIncludes(includes);
return includes;
}
QStringList ProjectFileDialog::GetDefines() const
{
QString define = mUI.mEditDefines->text();
QStringList defines;
if (!define.isEmpty())
@ -136,5 +54,29 @@ void ProjectFileDialog::UpdateProjectFileData()
else
defines.append(define);
}
mPFile->SetDefines(defines);
return defines;
}
void ProjectFileDialog::SetIncludepaths(const QStringList &includes)
{
QString includestr;
QString dir;
foreach(dir, includes)
{
includestr += dir;
includestr += ";";
}
mUI.mEditIncludePaths->setText(includestr);
}
void ProjectFileDialog::SetDefines(const QStringList &defines)
{
QString definestr;
QString define;
foreach(define, defines)
{
definestr += define;
definestr += ";";
}
mUI.mEditDefines->setText(definestr);
}

View File

@ -29,24 +29,42 @@ class ProjectFile;
/// @addtogroup GUI
/// @{
/**
* @brief A dialog for editing project file data.
*/
class ProjectFileDialog : public QDialog
{
Q_OBJECT
public:
ProjectFileDialog(const QString &path, QWidget *parent = 0);
protected slots:
void DialogAccepted();
/**
* @brief Return include paths from the dialog control.
* @return List of include paths.
*/
QStringList GetIncludePaths() const;
protected:
void ReadProjectFile();
void UpdateProjectFileData();
/**
* @brief Return define names from the dialog control.
* @return List of define names.
*/
QStringList GetDefines() const;
/**
* @brief Set include paths to dialog control.
* @param includes List of include paths to set to dialog control.
*/
void SetIncludepaths(const QStringList &includes);
/**
* @brief Set define names to dialog control.
* @param defines List of define names to set to dialog control.
*/
void SetDefines(const QStringList &defines);
private:
Ui::ProjectFile mUI;
QString mFileName;
ProjectFile *mPFile;
bool mDataSaved;
};
/// @}