Skip to content
Snippets Groups Projects
Commit 3595efca authored by t.knopff's avatar t.knopff
Browse files

Make DataLoaders1D independent of concrete DataLoaders

parent dc6c23f3
No related branches found
No related tags found
1 merge request!212Reorg dirs: remove cyclic dependecy of `GUI/Models` on `GUI/DataLoaders`
Pipeline #41664 passed
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/DataLoaders/UserDefinedDataLoader1D.cpp
//! @brief Implements class UserDefinedDataLoader1D
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/DataLoaders/DataLoaderUtil.h"
#include "GUI/DataLoaders/UserDefinedDataLoader1D.h"
#include "GUI/Models/DataLoaders1D.h"
void cloneAsUserDefinedLoader(AbstractDataLoader* loader, const QString& name)
{
loader->applyImportSettings();
auto clonedLoader = dynamic_cast<AbstractDataLoader1D*>(loader->clone());
const auto defaultProperties = loader->serialize();
DataLoaders1D::instance().addUserDefinedLoader
(new UserDefinedDataLoader1D(clonedLoader, name, defaultProperties));
}
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/DataLoaders/UserDefinedDataLoader1D.h
//! @brief Utility functions for data loaders
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#ifndef BORNAGAIN_GUI_DATALOADERS_DATALOADERUTIL_H
#define BORNAGAIN_GUI_DATALOADERS_DATALOADERUTIL_H
class AbstractDataLoader;
class QString;
//! clones the loader as a user defined loader and puts it in DataLoaders1D store
void cloneAsUserDefinedLoader(AbstractDataLoader* loader, const QString& name);
#endif // BORNAGAIN_GUI_DATALOADERS_DATALOADERUTIL_H
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/DataLoaders/DataLoadersRegistration.cpp
//! @brief Perform the registration of the dataloaders at the corresponding sites
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/Models/DataLoaders1D.h"
#include "GUI/DataLoaders/AutomaticDataLoader1D.h"
#include "GUI/DataLoaders/QREDataLoader.h"
namespace {
QREDataLoader* createQREDataLoader()
{
return new QREDataLoader();
}
AutomaticDataLoader1D* createAutomaticDataLoader1D()
{
return new AutomaticDataLoader1D();
}
bool register_dataloaders() {
// the ordering in here defines the ordering in the selection combo box in the open file dialog
// and also in the selection combo box on the format configuration page. Furthermore the first
// returned loader will be used as default in the open file dialog if it is started for the
// first time
DataLoaders1D::instance().addBuiltInLoader
(QREDataLoader().persistentClassName(), createQREDataLoader);
DataLoaders1D::instance().addBuiltInLoader
(AutomaticDataLoader1D().persistentClassName(), createAutomaticDataLoader1D);
return true;
}
bool registration = register_dataloaders();
}
...@@ -13,23 +13,7 @@ ...@@ -13,23 +13,7 @@
// ************************************************************************************************ // ************************************************************************************************
#include "GUI/Models/DataLoaders1D.h" #include "GUI/Models/DataLoaders1D.h"
#include "GUI/DataLoaders/AutomaticDataLoader1D.h" #include "GUI/Models/AbstractDataLoader1D.h"
#include "GUI/DataLoaders/QREDataLoader.h"
#include "GUI/DataLoaders/UserDefinedDataLoader1D.h"
namespace {
// the one and only instance of DataLoaders1D
DataLoaders1D dataLoaders1D;
} // namespace
DataLoaders1D* DataLoaders1D::m_instance = nullptr;
DataLoaders1D::DataLoaders1D()
{
m_instance = this;
}
DataLoaders1D::~DataLoaders1D() DataLoaders1D::~DataLoaders1D()
{ {
...@@ -39,23 +23,25 @@ DataLoaders1D::~DataLoaders1D() ...@@ -39,23 +23,25 @@ DataLoaders1D::~DataLoaders1D()
DataLoaders1D& DataLoaders1D::instance() DataLoaders1D& DataLoaders1D::instance()
{ {
return *m_instance; static DataLoaders1D inst;
return inst;
}
void DataLoaders1D::addBuiltInLoader(const QString& name,
const std::function<AbstractDataLoader1D*()>& create)
{
m_createLoaders.emplace(name, create);
m_builtInLoaders.push_back(create());
} }
void DataLoaders1D::initBuiltInLoaders() void DataLoaders1D::addUserDefinedLoader(AbstractDataLoader* loader)
{ {
// the ordering in here defines the ordering in the selection combo box in the open file dialog m_userDefinedLoaders.push_back(loader);
// and also in the selection combo box on the format configuration page. Furthermore the first
// returned loader will be used as default in the open file dialog if it is started for the
// first time
m_builtInLoaders << new QREDataLoader();
m_builtInLoaders << new AutomaticDataLoader1D();
} }
QVector<AbstractDataLoader*> DataLoaders1D::loaders() const QVector<AbstractDataLoader*> DataLoaders1D::loaders() const
{ {
if (m_builtInLoaders.isEmpty())
const_cast<DataLoaders1D*>(this)->initBuiltInLoaders();
return m_builtInLoaders + m_userDefinedLoaders; return m_builtInLoaders + m_userDefinedLoaders;
} }
...@@ -64,22 +50,13 @@ QVector<AbstractDataLoader*> DataLoaders1D::recentlyUsedLoaders() const ...@@ -64,22 +50,13 @@ QVector<AbstractDataLoader*> DataLoaders1D::recentlyUsedLoaders() const
return m_recentlyUsedLoaders; return m_recentlyUsedLoaders;
} }
void DataLoaders1D::cloneAsUserDefinedLoader(AbstractDataLoader* loader, const QString& name)
{
loader->applyImportSettings();
auto clonedLoader = dynamic_cast<AbstractDataLoader1D*>(loader->clone());
const auto defaultProperties = loader->serialize();
m_userDefinedLoaders << new UserDefinedDataLoader1D(clonedLoader, name, defaultProperties);
}
AbstractDataLoader1D* DataLoaders1D::createFromPersistentName(const QString& persistentClassName) AbstractDataLoader1D* DataLoaders1D::createFromPersistentName(const QString& persistentClassName)
{ {
if (persistentClassName == AutomaticDataLoader1D().persistentClassName()) std::map<QString,std::function<AbstractDataLoader1D*()>>::const_iterator it =
return new AutomaticDataLoader1D(); m_createLoaders.find(persistentClassName);
if (persistentClassName == QREDataLoader().persistentClassName())
return new QREDataLoader();
return nullptr; if (it != m_createLoaders.end())
return it->second();
else
return nullptr;
} }
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
#include <QVector> #include <QVector>
#include <map>
#include <functional>
class AbstractDataLoader; class AbstractDataLoader;
class AbstractDataLoader1D; class AbstractDataLoader1D;
...@@ -24,12 +27,18 @@ class AbstractDataLoader1D; ...@@ -24,12 +27,18 @@ class AbstractDataLoader1D;
class DataLoaders1D { class DataLoaders1D {
public: public:
DataLoaders1D();
~DataLoaders1D(); ~DataLoaders1D();
//! The one and only instance //! The one and only instance
static DataLoaders1D& instance(); static DataLoaders1D& instance();
//! Register a built-in loader with the given class name and factory function
void addBuiltInLoader(const QString& persistentClassName,
const std::function<AbstractDataLoader1D*()>& create);
//! Register a built-in loader with the given class name and factory function
void addUserDefinedLoader(AbstractDataLoader* loader);
//! all defined loaders. A null element in the list defines a separator //! all defined loaders. A null element in the list defines a separator
//! The returned pointers are the same over the lifetime of the DataLoaders instance, therefore //! The returned pointers are the same over the lifetime of the DataLoaders instance, therefore
//! they can be used for comparison. //! they can be used for comparison.
...@@ -41,22 +50,20 @@ public: ...@@ -41,22 +50,20 @@ public:
//! Notify loader was recently used //! Notify loader was recently used
void setRecentlyUsedLoader(const AbstractDataLoader* loader); void setRecentlyUsedLoader(const AbstractDataLoader* loader);
//! Clone the loader and create a user defined loader with its current settings and the given
//! name
void cloneAsUserDefinedLoader(AbstractDataLoader* loader, const QString& name);
//! Create loader from the given persistent name //! Create loader from the given persistent name
AbstractDataLoader1D* createFromPersistentName(const QString& persistentClassName); AbstractDataLoader1D* createFromPersistentName(const QString& persistentClassName);
private: private:
DataLoaders1D() = default;
//! create all default built in loaders //! create all default built in loaders
void initBuiltInLoaders(); void initBuiltInLoaders();
private: private:
static DataLoaders1D* m_instance;
QVector<AbstractDataLoader*> m_builtInLoaders; QVector<AbstractDataLoader*> m_builtInLoaders;
QVector<AbstractDataLoader*> m_recentlyUsedLoaders; QVector<AbstractDataLoader*> m_recentlyUsedLoaders;
QVector<AbstractDataLoader*> m_userDefinedLoaders; QVector<AbstractDataLoader*> m_userDefinedLoaders;
std::map<QString,std::function<AbstractDataLoader1D*()>> m_createLoaders;
}; };
#endif // BORNAGAIN_GUI_MODELS_DATALOADERS1D_H #endif // BORNAGAIN_GUI_MODELS_DATALOADERS1D_H
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// ************************************************************************************************ // ************************************************************************************************
#include "GUI/Views/SpecularDataWidgets/SpecularDataImportWidget.h" #include "GUI/Views/SpecularDataWidgets/SpecularDataImportWidget.h"
#include "GUI/DataLoaders/DataLoaderUtil.h"
#include "GUI/Models/AbstractDataLoaderResultModel.h" #include "GUI/Models/AbstractDataLoaderResultModel.h"
#include "GUI/Models/DataItemUtils.h" #include "GUI/Models/DataItemUtils.h"
#include "GUI/Models/DataLoaders1D.h" #include "GUI/Models/DataLoaders1D.h"
...@@ -333,7 +334,7 @@ void SpecularDataImportWidget::onCreateNewFormatButton() ...@@ -333,7 +334,7 @@ void SpecularDataImportWidget::onCreateNewFormatButton()
if (!ok || name.isEmpty()) if (!ok || name.isEmpty())
return; return;
DataLoaders1D::instance().cloneAsUserDefinedLoader(m_loader, name); cloneAsUserDefinedLoader(m_loader, name);
fillLoaderCombo(); fillLoaderCombo();
m_ui->formatSelectionComboBox->setCurrentText(name); m_ui->formatSelectionComboBox->setCurrentText(name);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment