Skip to content

Commit

Permalink
Store presenters as unique ptrs in Data Manipulation Interface class
Browse files Browse the repository at this point in the history
  • Loading branch information
robertapplin committed May 23, 2024
1 parent 986fd4b commit a63f952
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------
// Includes
//----------------------
#include "DataManipulationInterface.h"

#include "Common/Settings.h"
Expand Down Expand Up @@ -39,16 +36,16 @@ DECLARE_SUBWINDOW(DataManipulationInterface)

DataManipulationInterface::DataManipulationInterface(QWidget *parent) : InelasticInterface(parent) {}

DataManipulationInterface::~DataManipulationInterface() {}
DataManipulationInterface::~DataManipulationInterface() = default;

std::string DataManipulationInterface::documentationPage() const { return "Inelastic Data Manipulation"; }

/**
* Called when the user clicks the Python export button.
*/
void DataManipulationInterface::exportTabPython() {
QString tabName = m_uiForm.twIDRTabs->tabText(m_uiForm.twIDRTabs->currentIndex());
m_tabs[tabName].second->exportPythonScript();
auto const tabName = m_uiForm.twIDRTabs->tabText(m_uiForm.twIDRTabs->currentIndex()).toStdString();
m_presenters[tabName]->exportPythonScript();
}

/**
Expand All @@ -73,27 +70,15 @@ void DataManipulationInterface::initLayout() {
// Connect the "Manage User Directories" Button
connect(m_uiForm.pbManageDirectories, SIGNAL(clicked()), this, SLOT(manageUserDirectories()));

auto const &facility = Mantid::Kernel::ConfigService::Instance().getFacility();
filterUiForFacility(QString::fromStdString(facility.name()));

InelasticInterface::initLayout();
}

void DataManipulationInterface::applySettings(std::map<std::string, QVariant> const &settings) {
for (auto tab = m_tabs.begin(); tab != m_tabs.end(); ++tab) {
for (auto tab = m_presenters.begin(); tab != m_presenters.end(); ++tab) {
tab->second->filterInputData(settings.at("RestrictInput").toBool());
}
}

/**
* This function is ran after initLayout(), and runPythonCode is unavailable
* before this function
* has run (because of the setup of the base class). For this reason, "setup"
* functions that require
* Python scripts are located here.
*/
void DataManipulationInterface::initLocalPython() {}

/**
* Gets a parameter from an instrument component as a string.
*
Expand Down Expand Up @@ -134,47 +119,4 @@ void DataManipulationInterface::instrumentLoadingDone(bool error) {
}
}

/**
* Remove the Poco observer on the config service when the interfaces is closed.
*
* @param close Close event (unused)
*/
void DataManipulationInterface::closeEvent(QCloseEvent *close) { UNUSED_ARG(close); }

/**
* Filters the displayed tabs based on the current facility.
*
* @param facility Name of facility
*/
void DataManipulationInterface::filterUiForFacility(const QString &facility) {
g_log.information() << "Facility selected: " << facility.toStdString() << '\n';
QStringList enabledTabs;
QStringList disabledInstruments;

// These tabs work at any facility (always at end of tabs)
enabledTabs << "Symmetrise"
<< "S(Q, w)"
<< "Moments"
<< "Elwin"
<< "Iqt";

// First remove all tabs
while (m_uiForm.twIDRTabs->count() > 0) {
QString tabName = m_uiForm.twIDRTabs->tabText(0);

// Remove the tab
m_uiForm.twIDRTabs->removeTab(0);

g_log.debug() << "Removing tab " << tabName.toStdString() << '\n';
}

// Add the required tabs
for (auto &enabledTab : enabledTabs) {
// Add the tab
m_uiForm.twIDRTabs->addTab(m_tabs[enabledTab].first, enabledTab);

g_log.debug() << "Adding tab " << enabledTab.toStdString() << '\n';
}
}

} // namespace MantidQt::CustomInterfaces
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "MantidGeometry/IComponent.h"

#include <unordered_map>

#include <QRegExp>
#include <QScrollArea>

Expand Down Expand Up @@ -43,16 +45,12 @@ class DataManipulationInterface : public InelasticInterface {

/// Initialize the layout
void initLayout() override;
/// Run Python-based initialisation commands
void initLocalPython() override;

signals:
/// Emitted when the instrument setup is changed
void newInstrumentConfiguration();

private slots:
/// Shows/hides tabs based on facility
void filterUiForFacility(const QString &facility);

/// Exports the current tab algorithms as a Python script
void exportTabPython();
Expand All @@ -67,9 +65,6 @@ private slots:

QString getInstrumentParameterFrom(const Mantid::Geometry::IComponent_const_sptr &comp, const std::string &param);

/// Set and show an instrument-specific widget
void closeEvent(QCloseEvent *close) override;

/**
* Adds an MVP tab to the cache of tabs that can be shown.
*
Expand All @@ -79,7 +74,7 @@ private slots:
* @param name Name to be displayed on tab
*/

template <typename TabPresenter, typename TabView> void addMVPTab(const QString &name) {
template <typename TabPresenter, typename TabView, typename TabModel> void addMVPTab(const std::string &name) {
QWidget *tabWidget = new QWidget(m_uiForm.twIDRTabs);
QVBoxLayout *tabLayout = new QVBoxLayout(tabWidget);
tabWidget->setLayout(tabLayout);
Expand All @@ -89,33 +84,29 @@ private slots:
tabScrollArea->setWidgetResizable(true);

QWidget *tabContent = new QWidget(tabScrollArea);
tabContent->setObjectName("tab" + QString(name).remove(QRegExp("[ ,()]")));
tabContent->setObjectName("tab" + QString::fromStdString(name).remove(QRegExp("[ ,()]")));
tabScrollArea->setWidget(tabContent);
tabScrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

DataManipulation *tabIDRContent = new TabPresenter(tabContent, new TabView(tabContent));
auto presenter = std::make_unique<TabPresenter>(tabContent, new TabView(tabContent));

tabIDRContent->setupTab();
presenter->setupTab();
tabContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

connect(tabIDRContent, SIGNAL(showMessageBox(const QString &)), this, SLOT(showMessageBox(const QString &)));
connect(presenter.get(), SIGNAL(showMessageBox(const QString &)), this, SLOT(showMessageBox(const QString &)));

// Add to the cache
m_tabs[name] = qMakePair(tabWidget, tabIDRContent);
m_presenters[name] = std::move(presenter);

// Add all tabs to UI initially
m_uiForm.twIDRTabs->addTab(tabWidget, name);
m_uiForm.twIDRTabs->addTab(tabWidget, QString::fromStdString(name));
}

friend class DataManipulation;
/// The .ui form generated by Qt Designer
Ui::DataManipulationInterface m_uiForm;

// All indirect tabs
QMap<QString, QPair<QWidget *, DataManipulation *>> m_tabs;

QString m_dataDir; ///< default data search directory
QString m_saveDir; ///< default data save directory
/// A map to hold the presenter corresponding to each tab
std::unordered_map<std::string, std::unique_ptr<DataManipulation>> m_presenters;
};

} // namespace CustomInterfaces
Expand Down

0 comments on commit a63f952

Please sign in to comment.