Refactorization: Avoid copying Application instances.

This commit is contained in:
PKEuS 2012-10-27 11:35:00 +02:00
parent 0fe2deafe5
commit c56170acfa
6 changed files with 31 additions and 43 deletions

View File

@ -26,9 +26,10 @@
ApplicationDialog::ApplicationDialog(const QString &title,
const Application &app,
Application &app,
QWidget *parent) :
QDialog(parent)
QDialog(parent),
mApplication(app)
{
mUI.setupUi(this);
@ -67,15 +68,6 @@ void ApplicationDialog::Browse()
}
}
Application ApplicationDialog::GetApplication() const
{
Application app;
app.setName(mUI.mName->text());
app.setPath(mUI.mPath->text());
app.setParameters(mUI.mParameters->text());
return app;
}
void ApplicationDialog::Ok()
{
if (mUI.mName->text().isEmpty() || mUI.mPath->text().isEmpty() ||
@ -90,7 +82,9 @@ void ApplicationDialog::Ok()
} else {
// Convert possible native (Windows) path to internal presentation format
mUI.mPath->setText(QDir::fromNativeSeparators(mUI.mPath->text()));
mApplication.setName(mUI.mName->text());
mApplication.setPath(QDir::fromNativeSeparators(mUI.mPath->text()));
mApplication.setParameters(mUI.mParameters->text());
accept();
}
}

View File

@ -45,16 +45,10 @@ public:
* @param app Application definition.
* @param parent Parent widget.
*/
ApplicationDialog(const QString &title, const Application &app,
ApplicationDialog(const QString &title, Application &app,
QWidget *parent = 0);
virtual ~ApplicationDialog();
/**
* @brief Get modified application
* @return Modified name
*/
Application GetApplication() const;
protected slots:
void Ok();
@ -72,6 +66,13 @@ protected:
*
*/
Ui::ApplicationDialog mUI;
private:
/**
* @brief Underlying Application
*/
Application& mApplication;
};
/// @}
#endif // APPLICATIONDIALOG_H

View File

@ -105,7 +105,7 @@ void ApplicationList::SaveSettings()
QStringList params;
for (int i = 0; i < GetApplicationCount(); i++) {
Application app = GetApplication(i);
const Application& app = GetApplication(i);
names << app.getName();
paths << app.getPath();
params << app.getParameters();
@ -115,7 +115,6 @@ void ApplicationList::SaveSettings()
settings.setValue(SETTINGS_APPLICATION_PATHS, paths);
settings.setValue(SETTINGS_APPLICATION_PARAMS, params);
settings.setValue(SETTINGS_APPLICATION_DEFAULT, mDefaultApplicationIndex);
}
int ApplicationList::GetApplicationCount() const
@ -123,20 +122,24 @@ int ApplicationList::GetApplicationCount() const
return mApplications.size();
}
Application ApplicationList::GetApplication(const int index) const
Application& ApplicationList::GetApplication(const int index)
{
if (index >= 0 && index < mApplications.size()) {
return mApplications[index];
}
return Application(QString(), QString(), QString());
static Application dummy; // TODO: Throw exception instead?
return dummy;
}
void ApplicationList::SetApplication(int index, const Application &app)
const Application& ApplicationList::GetApplication(const int index) const
{
if (index >= 0 && index < mApplications.size()) {
mApplications.replace(index, app);
return mApplications[index];
}
static const Application dummy; // TODO: Throw exception instead?
return dummy;
}
void ApplicationList::AddApplication(const Application &app)
@ -167,7 +170,7 @@ void ApplicationList::Copy(const ApplicationList *list)
Clear();
for (int i = 0; i < list->GetApplicationCount(); i++) {
const Application app = list->GetApplication(i);
const Application& app = list->GetApplication(i);
AddApplication(app);
}
mDefaultApplicationIndex = list->GetDefaultApplication();

View File

@ -62,7 +62,8 @@ public:
* @param index Index of the application whose name to get
* @return Name of the application
*/
Application GetApplication(const int index) const;
const Application& GetApplication(const int index) const;
Application& GetApplication(const int index);
/**
* @brief Return the default application.
@ -72,14 +73,6 @@ public:
return mDefaultApplicationIndex;
}
/**
* @brief Modify an application
*
* @param index Index of the application to modify
* @param app Application with new data.
*/
void SetApplication(int index, const Application &app);
/**
* @brief Add a new application
*

View File

@ -503,7 +503,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
//Go through all applications and add them to the context menu
for (int i = 0; i < mApplications->GetApplicationCount(); i++) {
//Create an action for the application
const Application app = mApplications->GetApplication(i);
const Application& app = mApplications->GetApplication(i);
QAction *start = new QAction(app.getName(), &menu);
if (multipleSelection)
start->setDisabled(true);
@ -642,7 +642,7 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
file.append("\"");
}
const Application app = mApplications->GetApplication(application);
const Application& app = mApplications->GetApplication(application);
QString params = app.getParameters();
params.replace("(file)", file, Qt::CaseInsensitive);

View File

@ -201,7 +201,6 @@ void SettingsDialog::AddApplication()
ApplicationDialog dialog(tr("Add a new application"), app, this);
if (dialog.exec() == QDialog::Accepted) {
const Application app = dialog.GetApplication();
mTempApplications->AddApplication(app);
mUI.mListWidget->addItem(app.getName());
}
@ -231,13 +230,11 @@ void SettingsDialog::EditApplication()
QListWidgetItem *item = 0;
foreach(item, selected) {
int row = mUI.mListWidget->row(item);
const Application app = mTempApplications->GetApplication(row);
Application& app = mTempApplications->GetApplication(row);
ApplicationDialog dialog(tr("Modify an application"), app, this);
if (dialog.exec() == QDialog::Accepted) {
const Application app2 = dialog.GetApplication();
mTempApplications->SetApplication(row, app2);
item->setText(app2.getName());
item->setText(app.getName());
}
}
}
@ -257,7 +254,7 @@ void SettingsDialog::PopulateApplicationList()
{
const int defapp = mTempApplications->GetDefaultApplication();
for (int i = 0; i < mTempApplications->GetApplicationCount(); i++) {
Application app = mTempApplications->GetApplication(i);
const Application& app = mTempApplications->GetApplication(i);
QString name = app.getName();
if (i == defapp) {
name += " ";