diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/FitPropertyBrowser.h b/qt/widgets/common/inc/MantidQtWidgets/Common/FitPropertyBrowser.h index fb58b67aab93f626b0242a5fe27ae6728b2dc8fa..c789eb5abf29b75fab23d4de34b8609597be7e5b 100644 --- a/qt/widgets/common/inc/MantidQtWidgets/Common/FitPropertyBrowser.h +++ b/qt/widgets/common/inc/MantidQtWidgets/Common/FitPropertyBrowser.h @@ -21,6 +21,7 @@ #include "MantidAPI/IFunction.h" #include "MantidAPI/IPeakFunction.h" #include "MantidAPI/MatrixWorkspace_fwd.h" +#include "MantidQtWidgets/Common/SelectFunctionDialog.h" #include "MantidQtWidgets/Common/IWorkspaceFitControl.h" #include "MantidQtWidgets/Common/WorkspaceObserver.h" @@ -54,6 +55,7 @@ namespace MantidQt { namespace MantidWidgets { class PropertyHandler; +class SelectFunctionDialog; /** * Class FitPropertyBrowser implements QtPropertyBrowser to display * and control fitting function parameters and settings. @@ -629,7 +631,7 @@ private: QLabel *m_status; // The widget for choosing the fit function. - QDialog *m_fitSelector; + SelectFunctionDialog *m_fitSelector; // The tree widget containing the fit functions. QTreeWidget *m_fitTree; diff --git a/qt/widgets/common/src/FitPropertyBrowser.cpp b/qt/widgets/common/src/FitPropertyBrowser.cpp index 7e87c559ffee5e3657c7d5be241269e2a40ee90b..e6300520b4ac3bfa0c24f527155d6e992f8038ff 100644 --- a/qt/widgets/common/src/FitPropertyBrowser.cpp +++ b/qt/widgets/common/src/FitPropertyBrowser.cpp @@ -737,59 +737,10 @@ void FitPropertyBrowser::addFunction() { return; // Declare new widget for picking fit functions - m_fitSelector = new QDialog(); - m_fitSelector->setModal(true); - // QTreeWidget *m_fitTree = new QTreeWidget(); - m_fitTree = new QTreeWidget; - - // Add functions to each of the categories. If it appears in more than one - // category then add to both - // Store in a map. Key = category. Value = vector of fit functions belonging - // to that category. - std::map<std::string, std::vector<std::string>> categories; - for (int i = 0; i < m_registeredFunctions.size(); ++i) { - boost::shared_ptr<Mantid::API::IFunction> f = - Mantid::API::FunctionFactory::Instance().createFunction( - m_registeredFunctions[i].toStdString()); - std::vector<std::string> tempCategories = f->categories(); - for (size_t j = 0; j < tempCategories.size(); ++j) { - categories[tempCategories[boost::lexical_cast<int>(j)]].emplace_back( - m_registeredFunctions[i].toStdString()); - } - } - - // Construct the QTreeWidget based on the map information of categories and - // their respective fit functions. - std::map<std::string, std::vector<std::string>>::const_iterator sItr = - categories.end(); - for (std::map<std::string, std::vector<std::string>>::const_iterator itr = - categories.begin(); - itr != sItr; ++itr) { - QTreeWidgetItem *category = new QTreeWidgetItem(m_fitTree); - category->setText(0, QString::fromStdString(itr->first)); - - std::vector<std::string>::const_iterator fitItrEnd = itr->second.end(); - for (std::vector<std::string>::const_iterator fitItrBegin = - itr->second.begin(); - fitItrBegin != fitItrEnd; ++fitItrBegin) { - QTreeWidgetItem *fit = new QTreeWidgetItem(category); - fit->setText(0, QString::fromStdString(fitItrBegin[0])); - } - } - - // Set the layout of the widget. - m_fitTree->setToolTip("Select a function type and press OK."); - m_fitTree->setHeaderLabel("Fit - Select function type"); - QDialogButtonBox *buttonBox = - new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptFit())); - connect(buttonBox, SIGNAL(rejected()), this, SLOT(closeFit())); - connect(m_fitTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, - SLOT(acceptFit())); - QVBoxLayout *layout = new QVBoxLayout(); - layout->addWidget(m_fitTree); - layout->addWidget(buttonBox); - m_fitSelector->setLayout(layout); + m_fitSelector = new SelectFunctionDialog(this); + + connect(m_fitSelector, SIGNAL(accepted()), this, SLOT(acceptFit())); + connect(m_fitSelector, SIGNAL(rejected()), this, SLOT(closeFit())); m_fitSelector->show(); } @@ -799,16 +750,9 @@ void FitPropertyBrowser::acceptFit() { getHandler()->findCompositeFunction(ci); if (!cf) return; - - QList<QTreeWidgetItem *> items(m_fitTree->selectedItems()); - if (items.size() != 1) - return; - - if (items[0]->parent() == nullptr) - return; - + auto function = m_fitSelector->getFunction(); PropertyHandler *h = getHandler()->findHandler(cf); - h->addFunction(items[0]->text(0).toStdString()); + h->addFunction(function.toStdString()); emit functionChanged(); closeFit(); diff --git a/qt/widgets/common/src/SelectFunctionDialog.cpp b/qt/widgets/common/src/SelectFunctionDialog.cpp index a2988281f2e5783a24e084c05bd50c6dec957505..b6880bc5e21d3f7c743b7dd82c1789d0433c0e11 100644 --- a/qt/widgets/common/src/SelectFunctionDialog.cpp +++ b/qt/widgets/common/src/SelectFunctionDialog.cpp @@ -93,45 +93,55 @@ void SelectFunctionDialog::constructFunctionTree( QMap<QString, QTreeWidgetItem *> categories; for (const auto &entry : categoryFunctionsMap) { - // if (showCategory(entry.first)) {} + QString categoryName = QString::fromStdString(entry.first); QStringList subCategories = categoryName.split('\\'); if (!categories.contains(categoryName)) { if (subCategories.size() == 1) { - QTreeWidgetItem *catItem = - new QTreeWidgetItem(QStringList(categoryName)); - categories.insert(categoryName, catItem); - m_form->fitTree->addTopLevelItem(catItem); - for (const auto &function : entry.second) { - QTreeWidgetItem *fit = new QTreeWidgetItem(catItem); - fit->setText(0, QString::fromStdString(function)); + if (showCategory(entry.first)) { + QTreeWidgetItem *catItem = + new QTreeWidgetItem(QStringList(categoryName)); + categories.insert(categoryName, catItem); + m_form->fitTree->addTopLevelItem(catItem); + for (const auto &function : entry.second) { + QTreeWidgetItem *fit = new QTreeWidgetItem(catItem); + fit->setText(0, QString::fromStdString(function)); + } } } else { // go through the path and add the folders if they don't already exist QString currentPath = subCategories[0]; QTreeWidgetItem *catItem = nullptr; int n = subCategories.size(); + bool show = false; for (int j = 0; j < n; j++) { - if (categories.contains(currentPath)) { - catItem = categories[currentPath]; - } else { - QTreeWidgetItem *newCatItem = - new QTreeWidgetItem(QStringList(subCategories[j])); - categories.insert(currentPath, newCatItem); - if (!catItem) { - m_form->fitTree->addTopLevelItem(newCatItem); + if (showCategory(subCategories[j].toStdString())) { + show = true; + } + } + if (show) { + for (int j = 0; j < n; j++) { + if (categories.contains(currentPath)) { + catItem = categories[currentPath]; } else { - catItem->addChild(newCatItem); + QTreeWidgetItem *newCatItem = + new QTreeWidgetItem(QStringList(subCategories[j])); + categories.insert(currentPath, newCatItem); + if (!catItem) { + m_form->fitTree->addTopLevelItem(newCatItem); + } else { + catItem->addChild(newCatItem); + } + catItem = newCatItem; } - catItem = newCatItem; - } - if (j != n - 1) - currentPath += "\\" + subCategories[j + 1]; - else { - // This is the end of the path so add the functions - for (const auto &function : entry.second) { - QTreeWidgetItem *fit = new QTreeWidgetItem(catItem); - fit->setText(0, QString::fromStdString(function)); + if (j != n - 1) + currentPath += "\\" + subCategories[j + 1]; + else { + // This is the end of the path so add the functions + for (const auto &function : entry.second) { + QTreeWidgetItem *fit = new QTreeWidgetItem(catItem); + fit->setText(0, QString::fromStdString(function)); + } } } }