GUI: Refactoring application definition.

Using Application class as method parameters instead of separate
application attributes.
This commit is contained in:
Kimmo Varis 2011-04-02 16:11:01 +03:00
parent f82021d643
commit 91345234e0
5 changed files with 77 additions and 101 deletions

View File

@ -24,10 +24,26 @@
/**
* @brief A class containing information of the application to execute.
*
* Each application has a name and a path. Name is displayed to the user
* and has no other meaning. It isn't used to start the application.
* Path contains the full path to the application containing the executable name.
* Parameters contains the command line arguments for the executable.
*
* User can also specify certain predefined strings to parameters. These strings
* will be replaced with appropriate values concerning the error. Strings are:
* (file) - Filename containing the error
* (line) - Line number containing the error
* (message) - Error message
* (severity) - Error severity
*
* Example opening a file with Kate and make Kate scroll to the correct line.
* Executable: kate
* Parameters: -l(line) (file)
*/
class Application
{
public:
Application() { }
Application(const QString &name, const QString &path, const QString &params);
/**

View File

@ -24,6 +24,7 @@
#include <stdlib.h>
#include "common.h"
#include "applicationlist.h"
#include "application.h"
ApplicationList::ApplicationList(QObject *parent) :
@ -63,14 +64,22 @@ bool ApplicationList::LoadSettings(QSettings *programSettings)
// use as default for gnome environments
if (QFileInfo("/usr/bin/gedit").isExecutable())
{
AddApplication("gedit", "/usr/bin/gedit", "+(line) (file)");
Application app;
app.setName("gedit");
app.setPath("/usr/bin/gedit");
app.setParameters("+(line) (file)");
AddApplication(app);
defapp = 0;
break;
}
// use as default for kde environments
if (QFileInfo("/usr/bin/kate").isExecutable())
{
AddApplication("kate", "/usr/bin/kate", "-l(line) (file)");
Application app;
app.setName("kate");
app.setPath("/usr/bin/kate");
app.setParameters("-l(line) (file)");
AddApplication(app);
defapp = 0;
break;
}
@ -86,7 +95,10 @@ bool ApplicationList::LoadSettings(QSettings *programSettings)
if (names.size() > 0 && (names.size() == paths.size()))
{
for (int i = 0; i < names.size(); i++)
AddApplication(names[i], paths[i], params[i]);
{
const Application app(names[i], paths[i], params[i]);
AddApplication(app);
}
if (defapp == -1)
mDefaultApplicationIndex = 0;
@ -106,9 +118,10 @@ void ApplicationList::SaveSettings(QSettings *programSettings)
for (int i = 0; i < GetApplicationCount(); i++)
{
names << GetApplicationName(i);
paths << GetApplicationPath(i);
params << GetApplicationParameters(i);
Application app = GetApplication(i);
names << app.getName();
paths << app.getPath();
params << app.getParameters();
}
programSettings->setValue(SETTINGS_APPLICATION_NAMES, names);
@ -123,58 +136,30 @@ int ApplicationList::GetApplicationCount() const
return mApplications.size();
}
QString ApplicationList::GetApplicationName(const int index) const
Application ApplicationList::GetApplication(const int index) const
{
if (index >= 0 && index < mApplications.size())
{
return mApplications[index].getName();
return mApplications[index];
}
return QString();
return Application(QString(), QString(), QString());
}
QString ApplicationList::GetApplicationPath(const int index) const
void ApplicationList::SetApplication(int index, const Application &app)
{
if (index >= 0 && index < mApplications.size())
{
return mApplications[index].getPath();
}
return QString();
}
QString ApplicationList::GetApplicationParameters(const int index) const
{
if (index >= 0 && index < mApplications.size())
{
return mApplications[index].getParameters();
}
return QString();
}
void ApplicationList::SetApplication(const int index,
const QString &name,
const QString &path,
const QString &parameters)
{
if (index >= 0 && index < mApplications.size())
{
Application app(name, path, parameters);
mApplications.replace(index, app);
}
}
void ApplicationList::AddApplication(const QString &name,
const QString &path,
const QString &parameters)
void ApplicationList::AddApplication(const Application &app)
{
if (name.isEmpty() || path.isEmpty())
if (app.getName().isEmpty() || app.getPath().isEmpty())
{
return;
}
Application app(name, path, parameters);
mApplications << app;
}
@ -201,8 +186,8 @@ void ApplicationList::Copy(const ApplicationList *list)
Clear();
for (int i = 0; i < list->GetApplicationCount(); i++)
{
AddApplication(list->GetApplicationName(i), list->GetApplicationPath(i),
list->GetApplicationParameters(i));
const Application app = list->GetApplication(i);
AddApplication(app);
}
mDefaultApplicationIndex = list->GetDefaultApplication();
}
@ -219,7 +204,11 @@ bool ApplicationList::FindDefaultWindowsEditor()
const QString notepadppPath = appPath + "\\Notepad++\\notepad++.exe";
if (QFileInfo(notepadppPath).isExecutable())
{
AddApplication("Notepad++", "\"" + notepadppPath + "\"", "-n(line) (file)");
Application app;
app.setName("Notepad++");
app.setPath("\"" + notepadppPath + "\"");
app.setParameters("-n(line) (file)");
AddApplication(app);
return true;
}
@ -227,7 +216,11 @@ bool ApplicationList::FindDefaultWindowsEditor()
const QString notepadPath = windowsPath + "\\system32\\notepad.exe";
if (QFileInfo(notepadPath).isExecutable())
{
AddApplication("Notepad", notepadPath, "(file)");
Application app;
app.setName("Notepad");
app.setPath(notepadPath);
app.setParameters("(file)");
AddApplication(app);
return true;
}
return false;

View File

@ -28,22 +28,7 @@
/**
* @brief List of applications user has specified to open errors with
* Each application has a name and a path. Name is displayed to the user
* and has no other meaning. It isn't used to start the application.
* Path contains the path to the application as well as the executable itself and
* any possible argument user might want to specify.
*
* User can also specify certain predefined strings to path. These strings
* will be replaced with appropriate values concerning the error. Strings are:
* (file) - Filename containing the error
* (line) - Line number containing the error
* (message) - Error message
* (severity) - Error severity
*
* Example opening a file with Kate and make Kate scroll to the correct line:
* kate -l(line) (file)
*
* @brief List of applications user has specified to open errors with.
*/
class ApplicationList : public QObject
{
@ -81,23 +66,7 @@ public:
* @param index Index of the application whose name to get
* @return Name of the application
*/
QString GetApplicationName(const int index) const;
/**
* @brief Get Application's path
*
* @param index of the application whose path to get
* @return Application's path
*/
QString GetApplicationPath(const int index) const;
/**
* @brief Get Application's parameters
*
* @param index of the application whose parameters to get
* @return Application's parameters
*/
QString GetApplicationParameters(const int index) const;
Application GetApplication(const int index) const;
/**
* @brief Return the default application.
@ -112,22 +81,16 @@ public:
* @brief Modify an application
*
* @param index Index of the application to modify
* @param name New name for the application
* @param path New path for the application
* @param parameters New parameters for the application
* @param app Application with new data.
*/
void SetApplication(const int index, const QString &name,
const QString &path, const QString &parameters);
void SetApplication(int index, const Application &app);
/**
* @brief Add a new application
*
* @param name Name of the application
* @param path Path to the application
* @param parameters Parameters for the application
* @param app Application to add.
*/
void AddApplication(const QString &name, const QString &path,
const QString &parameters);
void AddApplication(const Application &app);
/**
* @brief Remove an application from the list

View File

@ -44,6 +44,7 @@
#include "resultstree.h"
#include "report.h"
#include "xmlreport.h"
#include "application.h"
ResultsTree::ResultsTree(QWidget * parent) :
QTreeView(parent),
@ -560,7 +561,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
for (int i = 0; i < mApplications->GetApplicationCount(); i++)
{
//Create an action for the application
QAction *start = new QAction(mApplications->GetApplicationName(i), &menu);
const Application app = mApplications->GetApplication(i);
QAction *start = new QAction(app.getName(), &menu);
if (multipleSelection)
start->setDisabled(true);
@ -659,8 +661,6 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
QVariantMap data = target->data().toMap();
QString params = mApplications->GetApplicationParameters(application);
//Replace (file) with filename
QString file = data["file"].toString();
@ -697,6 +697,8 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
file.append("\"");
}
const Application app = mApplications->GetApplication(application);
QString params = app.getParameters();
params.replace("(file)", file, Qt::CaseInsensitive);
QVariant line = data["line"];
@ -705,7 +707,7 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
params.replace("(message)", data["message"].toString(), Qt::CaseInsensitive);
params.replace("(severity)", data["severity"].toString(), Qt::CaseInsensitive);
QString program = mApplications->GetApplicationPath(application);
QString program = app.getPath();
// In Windows we must surround paths including spaces with quotation marks.
#ifdef Q_WS_WIN

View File

@ -202,8 +202,9 @@ void SettingsDialog::AddApplication()
if (dialog.exec() == QDialog::Accepted)
{
mTempApplications->AddApplication(dialog.GetName(), dialog.GetPath(),
dialog.GetParams());
const Application app(dialog.GetName(), dialog.GetPath(),
dialog.GetParams());
mTempApplications->AddApplication(app);
mUI.mListWidget->addItem(dialog.GetName());
}
}
@ -234,17 +235,17 @@ void SettingsDialog::EditApplication()
foreach(item, selected)
{
int row = mUI.mListWidget->row(item);
ApplicationDialog dialog(mTempApplications->GetApplicationName(row),
mTempApplications->GetApplicationPath(row),
mTempApplications->GetApplicationParameters(row),
const Application app = mTempApplications->GetApplication(row);
ApplicationDialog dialog(app.getName(), app.getPath(),
app.getParameters(),
tr("Modify an application"), this);
if (dialog.exec() == QDialog::Accepted)
{
mTempApplications->SetApplication(row, dialog.GetName(),
dialog.GetPath(),
dialog.GetParams());
const Application app2(dialog.GetName(),
dialog.GetPath(),
dialog.GetParams());
mTempApplications->SetApplication(row, app2);
item->setText(dialog.GetName());
}
}
@ -267,7 +268,8 @@ void SettingsDialog::PopulateApplicationList()
const int defapp = mTempApplications->GetDefaultApplication();
for (int i = 0; i < mTempApplications->GetApplicationCount(); i++)
{
QString name = mTempApplications->GetApplicationName(i);
Application app = mTempApplications->GetApplication(i);
QString name = app.getName();
if (i == defapp)
{
name += " ";