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 \ common.h \
erroritem.h \ erroritem.h \
fileviewdialog.h \ fileviewdialog.h \
project.h \
projectfile.h \ projectfile.h \
projectfiledialog.h \ projectfiledialog.h \
report.h \ report.h \
@ -72,6 +73,7 @@ SOURCES += main.cpp \
applicationdialog.cpp \ applicationdialog.cpp \
aboutdialog.cpp \ aboutdialog.cpp \
fileviewdialog.cpp \ fileviewdialog.cpp \
project.cpp \
projectfile.cpp \ projectfile.cpp \
projectfiledialog.cpp \ projectfiledialog.cpp \
erroritem.cpp \ erroritem.cpp \

View File

@ -31,7 +31,7 @@
#include "threadhandler.h" #include "threadhandler.h"
#include "fileviewdialog.h" #include "fileviewdialog.h"
#include "projectfile.h" #include "projectfile.h"
#include "projectfiledialog.h" #include "project.h"
#include "report.h" #include "report.h"
#include "../lib/filelister.h" #include "../lib/filelister.h"
@ -693,13 +693,24 @@ void MainWindow::ShowProjectFileDialog()
if (!filepath.isEmpty()) if (!filepath.isEmpty())
{ {
ProjectFileDialog dlg(filepath, this); Project prj(filepath, this);
dlg.exec(); if (prj.Open())
prj.Edit();
} }
} }
void MainWindow::NewProjectFileDialog() void MainWindow::NewProjectFileDialog()
{ {
ProjectFileDialog dlg(QString(), this); const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
dlg.exec(); 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 <QStringList>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include "projectfiledialog.h" #include "projectfiledialog.h"
#include "projectfile.h"
ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
: QDialog(parent) : QDialog(parent)
, mFileName(path)
, mDataSaved(false)
{ {
mUI.setupUi(this); mUI.setupUi(this);
connect(mUI.mButtons, SIGNAL(accepted()), this, SLOT(accept())); 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() QStringList ProjectFileDialog::GetIncludePaths() const
{
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()
{ {
QString include = mUI.mEditIncludePaths->text(); QString include = mUI.mEditIncludePaths->text();
QStringList includes; QStringList includes;
@ -124,8 +39,11 @@ void ProjectFileDialog::UpdateProjectFileData()
else else
includes.append(include); includes.append(include);
} }
mPFile->SetIncludes(includes); return includes;
}
QStringList ProjectFileDialog::GetDefines() const
{
QString define = mUI.mEditDefines->text(); QString define = mUI.mEditDefines->text();
QStringList defines; QStringList defines;
if (!define.isEmpty()) if (!define.isEmpty())
@ -136,5 +54,29 @@ void ProjectFileDialog::UpdateProjectFileData()
else else
defines.append(define); 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 /// @addtogroup GUI
/// @{ /// @{
/**
* @brief A dialog for editing project file data.
*/
class ProjectFileDialog : public QDialog class ProjectFileDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
ProjectFileDialog(const QString &path, QWidget *parent = 0); 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(); * @brief Return define names from the dialog control.
void UpdateProjectFileData(); * @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: private:
Ui::ProjectFile mUI; Ui::ProjectFile mUI;
QString mFileName;
ProjectFile *mPFile;
bool mDataSaved;
}; };
/// @} /// @}