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 <QStringList>
27#include <QUrl>
28
29using namespace MantidQt::API;
31
32namespace {
33// static logger
34Mantid::Kernel::Logger g_log("InterfaceManager");
35
36// Load libraries once
37std::once_flag DLLS_LOADED;
38
39// Track if message saying offline help is unavailable has been shown
40// This might now refer to the new Python help system if it's not registered via the factory
41bool helpSystemNotAvailableMsgDisplayed = false;
42
43} // namespace
44
45// initialise HelpWindow factory
47
48//----------------------------------
49// Public member functions
50//----------------------------------
66AlgorithmDialog *InterfaceManager::createDialog(const std::shared_ptr<Mantid::API::IAlgorithm> &alg, QWidget *parent,
67 bool forScript, const QHash<QString, QString> &presetValues,
68 const QString &optionalMsg, const QStringList &enabled,
69 const QStringList &disabled) {
70 AlgorithmDialog *dlg = nullptr;
71 if (AlgorithmDialogFactory::Instance().exists(alg->name() + "Dialog")) {
72 g_log.debug() << "Creating a specialised dialog for " << alg->name() << '\n';
73 dlg = AlgorithmDialogFactory::Instance().createUnwrapped(alg->name() + "Dialog");
74 } else {
75 dlg = new GenericDialog;
76 g_log.debug() << "No specialised dialog exists for the " << alg->name()
77 << " algorithm: a generic one has been created\n";
78 }
79
80 // The parent so that the dialog appears on top of it
81 dlg->setParent(parent);
82 dlg->setAttribute(Qt::WA_DeleteOnClose, true);
83
84 // Set the QDialog window flags to ensure the dialog ends up on top
85 Qt::WindowFlags flags = Qt::WindowFlags();
86#ifdef Q_OS_MAC
87 // Work around to ensure that floating windows remain on top of the main
88 // application window, but below other applications on Mac
89 // Note: Qt::Tool cannot have both a max and min button on OSX
90 flags |= Qt::Tool;
91 flags |= Qt::CustomizeWindowHint;
92 flags |= Qt::WindowMinimizeButtonHint;
93 flags |= Qt::WindowCloseButtonHint;
94#else
95 flags |= Qt::Dialog;
96 flags |= Qt::WindowCloseButtonHint;
97#endif
98 dlg->setWindowFlags(flags);
99
100 // Set the content
101 dlg->setAlgorithm(alg);
102 dlg->setPresetValues(presetValues);
103 dlg->isForScript(forScript);
104 dlg->setOptionalMessage(optionalMsg);
105 dlg->addEnabledAndDisableLists(enabled, disabled);
106
107 // Setup the layout
108 dlg->initializeLayout();
109
110 if (forScript)
111 dlg->executeOnAccept(false); // override default
112 return dlg;
113}
114
128AlgorithmDialog *InterfaceManager::createDialogFromName(const QString &algorithmName, const int version,
129 QWidget *parent, bool forScript,
130 const QHash<QString, QString> &presetValues,
131 const QString &optionalMsg, const QStringList &enabled,
132 const QStringList &disabled) {
133 // Create the algorithm. This should throw if the algorithm can't be found.
134 auto alg = Mantid::API::AlgorithmManager::Instance().create(algorithmName.toStdString(), version);
135
136 // Forward call.
137 return createDialog(alg, parent, forScript, presetValues, optionalMsg, enabled, disabled);
138}
139
146UserSubWindow *InterfaceManager::createSubWindow(const QString &interface_name, QWidget *parent, bool isWindow) {
147 UserSubWindow *user_win = nullptr;
148 std::string iname = interface_name.toStdString();
149 try {
150 user_win = UserSubWindowFactory::Instance().createUnwrapped(iname);
152 user_win = nullptr;
153 }
154 if (user_win) {
155 g_log.debug() << "Created a specialised interface for " << iname << '\n';
156
157 // set the parent. Note - setParent without flags parameter resets the flags
158 // ie window becomes a child widget
159 if (isWindow) {
160 user_win->setParent(parent, user_win->windowFlags());
161 } else {
162 user_win->setParent(parent);
163 }
164
165 user_win->setInterfaceName(interface_name);
166 user_win->initializeLayout();
167
168 notifyExistingInterfaces(user_win);
169
170 } else {
171 g_log.error() << "Error creating interface " << iname << "\n";
172 }
173 return user_win;
174}
175
185 auto &existingWindows = existingInterfaces();
186
187 for (auto &window : existingWindows)
188 window->otherUserSubWindowCreated(newWindow);
189
190 newWindow->otherUserSubWindowCreated(existingWindows);
191
192 existingWindows.append(newWindow);
193}
194
196 static QList<QPointer<UserSubWindow>> existingSubWindows;
197 existingSubWindows.erase(std::remove_if(existingSubWindows.begin(), existingSubWindows.end(),
198 [](QPointer<UserSubWindow> &window) { return window.isNull(); }),
199 existingSubWindows.end());
200 return existingSubWindows;
201}
202
208QStringList InterfaceManager::getUserSubWindowKeys() const { return UserSubWindowFactory::Instance().keys(); }
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
225
227 if (m_helpViewer == nullptr) {
228 if (!helpSystemNotAvailableMsgDisplayed) {
229 g_log.information("Help system not available (no viewer registered via factory).");
230 helpSystemNotAvailableMsgDisplayed = 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 via factory registration.");
237 }
238 return interface;
239 }
240}
241
242void InterfaceManager::showHelpPage(const QString &url) {
243 auto window = createHelpWindow();
244 if (window)
245 window->showPage(url);
246}
247
248void InterfaceManager::showAlgorithmHelp(const QString &name, const int version) {
249 auto window = createHelpWindow();
250 if (window)
251 window->showAlgorithm(name, version);
252}
253
255 auto window = createHelpWindow();
256 if (window)
257 window->showConcept(name);
258}
259
261 auto window = createHelpWindow();
262 if (window)
263 window->showFitFunction(name);
264}
265
266void InterfaceManager::showCustomInterfaceHelp(const QString &name, const QString &area, const QString &section) {
267 auto window = createHelpWindow();
268 if (window) {
269 QString pagePath = QString("interfaces/%1/%2.html").arg(area).arg(name);
270 if (!section.isEmpty()) {
271 pagePath += QString("#%1").arg(section);
272 }
273 window->showPage(pagePath);
274 }
275}
276
277void InterfaceManager::showWebPage(const QString &url) { MantidDesktopServices::openUrl(QUrl(url)); }
278
280 g_log.debug("InterfaceManager::closeHelpWindow() called. Note: Direct management of a single help window instance is "
281 "deprecated.");
282}
283
285 auto window = createHelpWindow();
286 if (window)
287 window->showPage(QString());
288}
289
290void InterfaceManager::cleanup() { g_log.debug("InterfaceManager::cleanup() called."); }
std::string name
Definition Run.cpp:60
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.
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.
static QList< QPointer< UserSubWindow > > & existingInterfaces()
Returns a list of existing UserSubWindows.
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.
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...
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.
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:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
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("DetermineSpinStateOrder")