Mantid
Loading...
Searching...
No Matches
InterfaceManager.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------
8// Includes
9//----------------------------------
19
23#include "MantidKernel/Logger.h"
24
25#include <Poco/Environment.h>
26#include <QPointer>
27#include <QStringList>
28
29using namespace MantidQt::API;
32
33namespace {
34// static logger
35Mantid::Kernel::Logger g_log("InterfaceManager");
36
37// Load libraries once
38std::once_flag DLLS_LOADED;
39
40// Track if message saying offline help is unavailable has been shown
41bool offlineHelpMsgDisplayed = false;
42
43QList<QPointer<UserSubWindow>> &existingInterfaces() {
44 static QList<QPointer<UserSubWindow>> existingInterfaces;
45 return existingInterfaces;
46}
47
48} // namespace
49
50// initialise HelpWindow factory
52
53//----------------------------------
54// Public member functions
55//----------------------------------
71AlgorithmDialog *InterfaceManager::createDialog(const std::shared_ptr<Mantid::API::IAlgorithm> &alg, QWidget *parent,
72 bool forScript, const QHash<QString, QString> &presetValues,
73 const QString &optionalMsg, const QStringList &enabled,
74 const QStringList &disabled) {
75 AlgorithmDialog *dlg = nullptr;
76 if (AlgorithmDialogFactory::Instance().exists(alg->name() + "Dialog")) {
77 g_log.debug() << "Creating a specialised dialog for " << alg->name() << '\n';
78 dlg = AlgorithmDialogFactory::Instance().createUnwrapped(alg->name() + "Dialog");
79 } else {
80 dlg = new GenericDialog;
81 g_log.debug() << "No specialised dialog exists for the " << alg->name()
82 << " algorithm: a generic one has been created\n";
83 }
84
85 // The parent so that the dialog appears on top of it
86 dlg->setParent(parent);
87 dlg->setAttribute(Qt::WA_DeleteOnClose, true);
88
89 // Set the QDialog window flags to ensure the dialog ends up on top
90 Qt::WindowFlags flags = Qt::WindowFlags();
91#ifdef Q_OS_MAC
92 // Work around to ensure that floating windows remain on top of the main
93 // application window, but below other applications on Mac
94 // Note: Qt::Tool cannot have both a max and min button on OSX
95 flags |= Qt::Tool;
96 flags |= Qt::CustomizeWindowHint;
97 flags |= Qt::WindowMinimizeButtonHint;
98 flags |= Qt::WindowCloseButtonHint;
99#else
100 flags |= Qt::Dialog;
101 flags |= Qt::WindowCloseButtonHint;
102#endif
103 dlg->setWindowFlags(flags);
104
105 // Set the content
106 dlg->setAlgorithm(alg);
107 dlg->setPresetValues(presetValues);
108 dlg->isForScript(forScript);
109 dlg->setOptionalMessage(optionalMsg);
110 dlg->addEnabledAndDisableLists(enabled, disabled);
111
112 // Setup the layout
113 dlg->initializeLayout();
114
115 if (forScript)
116 dlg->executeOnAccept(false); // override default
117 return dlg;
118}
119
133AlgorithmDialog *InterfaceManager::createDialogFromName(const QString &algorithmName, const int version,
134 QWidget *parent, bool forScript,
135 const QHash<QString, QString> &presetValues,
136 const QString &optionalMsg, const QStringList &enabled,
137 const QStringList &disabled) {
138 // Create the algorithm. This should throw if the algorithm can't be found.
139 auto alg = Mantid::API::AlgorithmManager::Instance().create(algorithmName.toStdString(), version);
140
141 // Forward call.
142 return createDialog(alg, parent, forScript, presetValues, optionalMsg, enabled, disabled);
143}
144
151UserSubWindow *InterfaceManager::createSubWindow(const QString &interface_name, QWidget *parent, bool isWindow) {
152 UserSubWindow *user_win = nullptr;
153 std::string iname = interface_name.toStdString();
154 try {
155 user_win = UserSubWindowFactory::Instance().createUnwrapped(iname);
157 user_win = nullptr;
158 }
159 if (user_win) {
160 g_log.debug() << "Created a specialised interface for " << iname << '\n';
161
162 // set the parent. Note - setParent without flags parameter resets the flags
163 // ie window becomes a child widget
164 if (isWindow) {
165 user_win->setParent(parent, user_win->windowFlags());
166 } else {
167 user_win->setParent(parent);
168 }
169
170 user_win->setInterfaceName(interface_name);
171 user_win->initializeLayout();
172
173 notifyExistingInterfaces(user_win);
174
175 } else {
176 g_log.error() << "Error creating interface " << iname << "\n";
177 }
178 return user_win;
179}
180
190 auto &existingWindows = existingInterfaces();
191 existingWindows.erase(std::remove_if(existingWindows.begin(), existingWindows.end(),
192 [](QPointer<UserSubWindow> &window) { return window.isNull(); }),
193 existingWindows.end());
194
195 for (auto &window : existingWindows)
196 window->otherUserSubWindowCreated(newWindow);
197
198 newWindow->otherUserSubWindowCreated(existingWindows);
199
200 existingWindows.append(newWindow);
201}
202
209
210//----------------------------------
211// Private member functions
212//----------------------------------
215 // Attempt to load libraries that may contain custom interface classes
216 std::call_once(DLLS_LOADED, []() { loadPluginsFromCfgPath("mantidqt.plugins.directory"); });
217}
218
221
223 m_helpViewer = factory;
224}
225
227 if (m_helpViewer == nullptr) {
228 if (!offlineHelpMsgDisplayed) {
229 g_log.information("Offline help is not available in this version of Workbench.");
230 offlineHelpMsgDisplayed = true;
231 }
232 return nullptr;
233 } else {
234 MantidHelpInterface *interface = this->m_helpViewer->createUnwrappedInstance();
235 if (!interface) {
236 g_log.error("Error creating help window");
237 }
238 return interface;
239 }
240}
241
242void InterfaceManager::showHelpPage(const QString &url) {
243 auto window = createHelpWindow();
244 window->showPage(url);
245}
246
247void InterfaceManager::showWikiPage(const QString &page) {
248 auto window = createHelpWindow();
249 window->showWikiPage(page);
250}
251
252void InterfaceManager::showAlgorithmHelp(const QString &name, const int version) {
253 auto window = createHelpWindow();
254 window->showAlgorithm(name, version);
255}
256
257void InterfaceManager::showConceptHelp(const QString &name) {
258 auto window = createHelpWindow();
259 window->showConcept(name);
260}
261
262void InterfaceManager::showFitFunctionHelp(const QString &name) {
263 auto window = createHelpWindow();
264 window->showFitFunction(name);
265}
266
267void InterfaceManager::showCustomInterfaceHelp(const QString &name, const QString &area, const QString &section) {
268 auto window = createHelpWindow();
269 window->showCustomInterface(name, area, section);
270}
271
273
275 if (MantidHelpWindow::helpWindowExists()) {
276 auto window = createHelpWindow();
277 window->shutdown();
278 }
279}
This class should be the basis for all customised algorithm dialogs.
void addEnabledAndDisableLists(const QStringList &enabled, const QStringList &disabled)
Set comma-separated-list of enabled parameter names.
void setAlgorithm(const Mantid::API::IAlgorithm_sptr &)
The following methods were made public for testing in GenericDialogDemo.cpp.
bool isForScript() const
Get the usage boolean value.
void setOptionalMessage(const QString &message)
Set an optional message to be displayed at the top of the dialog.
void executeOnAccept(bool on)
If true then execute the algorithm on acceptance.
void initializeLayout()
Create the layout of the widget. Can only be called once.
void setPresetValues(const QHash< QString, QString > &presetValues)
Set a list of suggested values.
This class gives a basic dialog that is not tailored to a particular algorithm.
Definition: GenericDialog.h:37
AlgorithmDialog * createDialog(const std::shared_ptr< Mantid::API::IAlgorithm > &alg, QWidget *parent=nullptr, bool forScript=false, const QHash< QString, QString > &presetValues=(QHash< QString, QString >()), const QString &optional_msg=QString(), const QStringList &enabled=QStringList(), const QStringList &disabled=QStringList())
Create a new instance of the correct type of AlgorithmDialog.
void showFitFunctionHelp(const QString &name=QString())
AlgorithmDialog * createDialogFromName(const QString &algorithmName, const int version=-1, QWidget *parent=nullptr, bool forScript=false, const QHash< QString, QString > &presetValues=(QHash< QString, QString >()), const QString &optionalMsg=QString(), const QStringList &enabled=QStringList(), const QStringList &disabled=QStringList())
Create an algorithm dialog for a given name and version.
UserSubWindow * createSubWindow(const QString &interface_name, QWidget *parent=nullptr, bool isWindow=true)
Create a new instance of the correct type of UserSubWindow.
void showCustomInterfaceHelp(const QString &name, const QString &area=QString(), const QString &section=QString())
void showConceptHelp(const QString &name)
static Mantid::Kernel::AbstractInstantiator< MantidHelpInterface > * m_helpViewer
Handle to the help window factory.
QStringList getUserSubWindowKeys() const
The keys associated with UserSubWindow classes.
void showWebPage(const QString &url)
void showAlgorithmHelp(const QString &name, const int version=-1)
MantidHelpInterface * createHelpWindow() const
Function that instantiates the help window.
void showWikiPage(const QString &page=QString())
static void registerHelpWindowFactory(Mantid::Kernel::AbstractInstantiator< MantidHelpInterface > *factory)
Registration function for the help window factory.
void showHelpPage(const QString &url=QString())
virtual ~InterfaceManager()
Destructor.
void notifyExistingInterfaces(UserSubWindow *newWindow)
Notifies the existing interfaces that a new interface has been created, and then notifies the new int...
static bool openUrl(const QUrl &url)
Opens a url in the appropriate web browser.
This is the base class all customised user interfaces that do not wish to be tied to a specific Manti...
Definition: UserSubWindow.h:70
void setInterfaceName(const QString &iface_name)
Set the interface name, made public so possible from Python.
virtual void otherUserSubWindowCreated(QPointer< UserSubWindow > window)
To be overridden in order to connect a signal between two interfaces.
void initializeLayout()
Create the layout of the widget. Can only be called once.
The base class for instantiators.
Definition: Instantiator.h:25
Exception for when an item is not found in a collection.
Definition: Exception.h:145
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
EXPORT_OPT_MANTIDQT_COMMON int loadPluginsFromCfgPath(const std::string &key)
Load plugins from a path given by the key in the config service.
Kernel::Logger g_log("ExperimentInfo")
static logger object