GUI: Allow creating new project file.

Add new "New project file" item to File-menu and rename existing
"Project File" item to "Open Project File". Selecting new file
opens empty project file dialog. When the dialog is then closed
the user is asked to select a filename for the new project file.
This commit is contained in:
Kimmo Varis 2010-07-08 18:59:06 +03:00
parent 4958424adc
commit 25a9c75287
5 changed files with 430 additions and 377 deletions

View File

@ -73,7 +73,8 @@
<property name="title"> <property name="title">
<string>&amp;File</string> <string>&amp;File</string>
</property> </property>
<addaction name="mActionProjectFile"/> <addaction name="mActionNewProjectFile"/>
<addaction name="mActionOpenProjectFile"/>
<addaction name="mActionSave"/> <addaction name="mActionSave"/>
<addaction name="mActionQuit"/> <addaction name="mActionQuit"/>
</widget> </widget>
@ -348,9 +349,14 @@
<string>Error categories</string> <string>Error categories</string>
</property> </property>
</action> </action>
<action name="mActionProjectFile"> <action name="mActionOpenProjectFile">
<property name="text"> <property name="text">
<string>P&amp;roject File...</string> <string>Open P&amp;roject File...</string>
</property>
</action>
<action name="mActionNewProjectFile">
<property name="text">
<string>&amp;New Project File...</string>
</property> </property>
</action> </action>
</widget> </widget>

View File

@ -80,7 +80,8 @@ MainWindow::MainWindow() :
connect(mUI.mResults, SIGNAL(GotResults()), this, SLOT(ResultsAdded())); connect(mUI.mResults, SIGNAL(GotResults()), this, SLOT(ResultsAdded()));
connect(mUI.mMenuView, SIGNAL(aboutToShow()), this, SLOT(AboutToShowViewMenu())); connect(mUI.mMenuView, SIGNAL(aboutToShow()), this, SLOT(AboutToShowViewMenu()));
connect(mUI.mActionProjectFile, SIGNAL(triggered()), this, SLOT(ShowProjectFileDialog())); connect(mUI.mActionNewProjectFile, SIGNAL(triggered()), this, SLOT(NewProjectFileDialog()));
connect(mUI.mActionOpenProjectFile, SIGNAL(triggered()), this, SLOT(ShowProjectFileDialog()));
#ifdef WIN32 #ifdef WIN32
connect(mUI.mActionHelpContents, SIGNAL(triggered()), this, SLOT(OpenHelpContents())); connect(mUI.mActionHelpContents, SIGNAL(triggered()), this, SLOT(OpenHelpContents()));
@ -679,3 +680,9 @@ void MainWindow::ShowProjectFileDialog()
dlg.exec(); dlg.exec();
} }
} }
void MainWindow::NewProjectFileDialog()
{
ProjectFileDialog dlg(QString(), this);
dlg.exec();
}

View File

@ -125,6 +125,16 @@ public slots:
*/ */
void Save(); void Save();
/**
* @brief Slot to shown project file dialog for new project file.
*
*/
void NewProjectFileDialog();
/**
* @brief Slot to shown project file dialog for existing project file.
*
*/
void ShowProjectFileDialog(); void ShowProjectFileDialog();
protected slots: protected slots:

View File

@ -17,16 +17,30 @@
*/ */
#include <QStringList> #include <QStringList>
#include <QFile>
#include <QFileDialog>
#include "projectfiledialog.h" #include "projectfiledialog.h"
#include "projectfile.h" #include "projectfile.h"
ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
: QDialog(parent) : QDialog(parent)
, mFileName(path) , mFileName(path)
, mDataSaved(false)
{ {
mUI.setupUi(this); mUI.setupUi(this);
connect(mUI.mButtons, SIGNAL(accepted()), this, SLOT(accept()));
connect(this, SIGNAL(accepted()), this, SLOT(DialogAccepted()));
mPFile = new ProjectFile(path, this); mPFile = new ProjectFile(path, this);
if (QFile::exists(path))
{
ReadProjectFile();
}
}
void ProjectFileDialog::ReadProjectFile()
{
mPFile->Read(); mPFile->Read();
QStringList includes = mPFile->GetIncludeDirs(); QStringList includes = mPFile->GetIncludeDirs();
@ -48,18 +62,32 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
definestr += ";"; definestr += ";";
} }
mUI.mEditDefines->setText(definestr); mUI.mEditDefines->setText(definestr);
connect(mUI.mButtons, SIGNAL(accepted()), this, SLOT(accept()));
connect(this, SIGNAL(finished(int)), this, SLOT(DialogFinished(int)));
} }
void ProjectFileDialog::DialogFinished(int result) void ProjectFileDialog::DialogAccepted()
{ {
if (result == QDialog::Accepted) if (mDataSaved)
return;
UpdateProjectFileData();
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())
{
mPFile->Write(filepath);
}
}
else
{ {
UpdateProjectFileData();
mPFile->Write(); mPFile->Write();
} }
mDataSaved = true;
} }
void ProjectFileDialog::UpdateProjectFileData() void ProjectFileDialog::UpdateProjectFileData()

View File

@ -36,15 +36,17 @@ public:
ProjectFileDialog(const QString &path, QWidget *parent = 0); ProjectFileDialog(const QString &path, QWidget *parent = 0);
protected slots: protected slots:
void DialogFinished(int result); void DialogAccepted();
protected: protected:
void ReadProjectFile();
void UpdateProjectFileData(); void UpdateProjectFileData();
private: private:
Ui::ProjectFile mUI; Ui::ProjectFile mUI;
QString mFileName; QString mFileName;
ProjectFile *mPFile; ProjectFile *mPFile;
bool mDataSaved;
}; };
/// @} /// @}