Mantid
Loading...
Searching...
No Matches
SelectFunctionDialog.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//-------------------------------------------
11#include "ui_SelectFunctionDialog.h"
12
14#include "MantidAPI/IFunction.h"
16
17#include <boost/lexical_cast.hpp>
18
19#include <QButtonGroup>
20#include <QCheckBox>
21#include <QComboBox>
22#include <QCompleter>
23#include <QGridLayout>
24#include <QGroupBox>
25#include <QIcon>
26#include <QLabel>
27#include <QPushButton>
28#include <QTreeWidget>
29#include <QVBoxLayout>
30
32
38 : SelectFunctionDialog(parent, std::vector<std::string>()) {}
39
45SelectFunctionDialog::SelectFunctionDialog(QWidget *parent, const std::vector<std::string> &restrictions)
46 : MantidDialog(parent), m_form(new Ui::SelectFunctionDialog) {
47 setWindowModality(Qt::WindowModal);
48 setWindowIcon(QIcon(":/images/MantidIcon.ico"));
49 m_form->setupUi(this);
50 m_form->errorMessage->hide();
51
53 auto registeredFunctions = factory.getFunctionNamesGUI();
54 // Add functions to each of the categories. If it appears in more than one
55 // category then add to both
56 // Store in a map. Key = category. Value = vector of fit functions belonging
57 // to that category.
58 std::map<std::string, std::vector<std::string>> categories;
59 for (const auto &registeredFunction : registeredFunctions) {
60 auto f = factory.createFunction(registeredFunction);
61 std::vector<std::string> tempCategories = f->categories();
62 for (size_t j = 0; j < tempCategories.size(); ++j) {
63 categories[tempCategories[boost::lexical_cast<int>(j)]].emplace_back(registeredFunction);
64 }
65 }
66
67 // Set up the search box
68 m_form->searchBox->completer()->setCompletionMode(QCompleter::PopupCompletion);
69 m_form->searchBox->completer()->setFilterMode(Qt::MatchContains);
70
71 connect(m_form->searchBox, SIGNAL(editTextChanged(const QString &)), this, SLOT(searchBoxChanged(const QString &)));
72
73 // Construct the QTreeWidget based on the map information of categories and
74 // their respective fit functions.
75 constructFunctionTree(categories, restrictions);
77
78 connect(m_form->fitTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
79 SLOT(functionDoubleClicked(QTreeWidgetItem *)));
80 m_form->fitTree->setToolTip("Select a function type and press OK.");
81
82 connect(m_form->buttonBox, SIGNAL(accepted()), this, SLOT(acceptFunction()));
83 connect(m_form->buttonBox, SIGNAL(rejected()), this, SLOT(rejectFunction()));
84
85 m_form->searchBox->setCurrentIndex(-1);
86
87 connect(m_form->helpButton, SIGNAL(clicked()), this, SLOT(helpClicked()));
88}
89
98 const std::map<std::string, std::vector<std::string>> &categoryFunctionsMap,
99 const std::vector<std::string> &restrictions) {
100 // Don't show category if it's not in the list (unless list is empty)
101 auto showCategory = [&restrictions](const std::string &name) {
102 if (restrictions.empty()) {
103 return true;
104 } else {
105 return (std::find(restrictions.begin(), restrictions.end(), name) != restrictions.end());
106 }
107 };
108
109 // keeps track of categories added to the tree
110 QMap<QString, QTreeWidgetItem *> categories;
111
112 for (const auto &entry : categoryFunctionsMap) {
113
114 QString categoryName = QString::fromStdString(entry.first);
115 QStringList subCategories = categoryName.split('\\');
116 if (!categories.contains(categoryName)) {
117 if (subCategories.size() == 1) {
118 if (showCategory(entry.first)) {
119 QTreeWidgetItem *catItem = new QTreeWidgetItem(QStringList(categoryName));
120 categories.insert(categoryName, catItem);
121 m_form->fitTree->addTopLevelItem(catItem);
122 for (const auto &function : entry.second) {
123 QTreeWidgetItem *fit = new QTreeWidgetItem(catItem);
124 fit->setText(0, QString::fromStdString(function));
125 m_form->searchBox->addItem(QString::fromStdString(function));
126 }
127 }
128 } else {
129 // go through the path and add the folders if they don't already exist
130 QString currentPath = subCategories[0];
131 QTreeWidgetItem *catItem = nullptr;
132 int subCategoryNo = subCategories.size();
133 bool show = false;
134 for (int j = 0; j < subCategoryNo; ++j) {
135 if (showCategory(subCategories[j].toStdString())) {
136 show = true;
137 }
138 }
139 if (show) {
140 for (int j = 0; j < subCategoryNo; ++j) {
141 if (categories.contains(currentPath)) {
142 catItem = categories[currentPath];
143 } else {
144 QTreeWidgetItem *newCatItem = new QTreeWidgetItem(QStringList(subCategories[j]));
145 categories.insert(currentPath, newCatItem);
146 if (!catItem) {
147 m_form->fitTree->addTopLevelItem(newCatItem);
148 } else {
149 catItem->addChild(newCatItem);
150 }
151 catItem = newCatItem;
152 }
153 if (j != subCategoryNo - 1)
154 currentPath += "\\" + subCategories[j + 1];
155 else {
156 // This is the end of the path so add the functions
157 for (const auto &function : entry.second) {
158 QTreeWidgetItem *fit = new QTreeWidgetItem(catItem);
159 fit->setText(0, QString::fromStdString(function));
160 m_form->searchBox->addItem(QString::fromStdString(function));
161 }
162 }
163 }
164 }
165 }
166 }
167 }
168}
169
171
177 auto const numberOfTopLevelItems = m_form->fitTree->topLevelItemCount();
178 if (numberOfTopLevelItems > 0) {
179 auto const firstItem = m_form->fitTree->topLevelItem(0);
180 auto const itemHeight = m_form->fitTree->visualItemRect(firstItem).height();
181 m_form->fitTree->setMinimumHeight(itemHeight * numberOfTopLevelItems);
182 }
183}
184
189 const auto searchText = m_form->searchBox->currentText();
190 QList<QTreeWidgetItem *> items(m_form->fitTree->selectedItems());
191 if (items.size() == 1 && items[0]->childCount() == 0) {
192 return items[0]->text(0);
193 } else if (m_form->searchBox->findText(searchText) >= 0) {
194 return searchText;
195 }
196 return "";
197}
198
199void SelectFunctionDialog::clearSearchBoxText() const { m_form->searchBox->clearEditText(); }
200
204void SelectFunctionDialog::searchBoxChanged(const QString &text) {
205 if (text.isEmpty()) {
206 return;
207 }
208 m_form->fitTree->setCurrentIndex(QModelIndex());
209
210 const auto index = m_form->searchBox->findText(text);
211 if (index >= 0)
212 m_form->searchBox->setCurrentIndex(index);
213}
214
216 if (item->childCount() == 0)
218}
219
221 const auto func = getFunction();
222 if (func.isEmpty()) {
223 m_form->errorMessage->setText(QString("<span style='color:red'> Function not found</span> "));
224 m_form->errorMessage->show();
225 } else {
226 m_form->errorMessage->hide();
227 accept();
228 }
229}
230
232 m_form->errorMessage->hide();
233 reject();
234}
235
237 auto function = getFunction();
238 if (!function.isEmpty()) {
239 MantidQt::API::HelpWindow::showFitFunction(function.toStdString());
240 } else { // No function selected open fit function index
242 }
243}
244
245} // namespace MantidQt::MantidWidgets
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
static void showFitFunction(const std::string &name=std::string())
Definition: HelpWindow.cpp:80
Select a function type out of a list of available ones.
void clearSearchBoxText() const
Clear the text in the search box.
void searchBoxChanged(const QString &text)
Called when the text in the search box changes.
QString getFunction() const
Return selected function.
void constructFunctionTree(const std::map< std::string, std::vector< std::string > > &categoryFunctionsMap, const std::vector< std::string > &restrictions)
Construct QTreeWidget with categories and functions.
Ui::SelectFunctionDialog * m_form
Ui elements form.
SelectFunctionDialog(QWidget *parent=nullptr)
Default constructor.
void setMinimumHeightOfFunctionTree()
Sets the minimum height of the function tree so that all catagories are visible.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
STL namespace.