Skip to content
Snippets Groups Projects
BasicFitOptionsBrowser.cpp 29.9 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidQtWidgets/Common/BasicFitOptionsBrowser.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/IFuncMinimizer.h"
#include "MantidAPI/IWorkspaceProperty.h"
#include "MantidKernel/PropertyWithValue.h"

#include "MantidQtWidgets/Common/QtPropertyBrowser/qtpropertymanager.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/qttreepropertybrowser.h"
// Suppress a warning coming out of code that isn't ours
#if defined(__INTEL_COMPILER)
#pragma warning disable 1125
#elif defined(__GNUC__)
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic push
#endif
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
#endif
#include "MantidQtWidgets/Common/QtPropertyBrowser/ButtonEditorFactory.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/CompositeEditorFactory.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/DoubleEditorFactory.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/qteditorfactory.h"
#if defined(__INTEL_COMPILER)
#pragma warning enable 1125
#elif defined(__GNUC__)
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic pop
#endif
#endif

#include <QMessageBox>
#include <QSettings>
#include <QVBoxLayout>
#include <limits>

namespace MantidQt {
namespace MantidWidgets {

/**
 * Constructor
 * @param parent :: The parent widget.
 * @param fitType :: The type of the underlying fitting algorithm.
 */
BasicFitOptionsBrowser::BasicFitOptionsBrowser(QWidget *parent,
                                               FittingMode fitType)
    : QWidget(parent), m_fittingTypeProp(nullptr), m_minimizer(nullptr),
      m_decimals(6), m_fittingType(fitType) {
  // create m_browser
  createBrowser();
  createProperties();

  auto *layout = new QVBoxLayout(this);
  layout->addWidget(m_browser);
  layout->setContentsMargins(0, 0, 0, 0);
}

BasicFitOptionsBrowser::~BasicFitOptionsBrowser() {
  m_browser->unsetFactoryForManager(m_stringManager);
  m_browser->unsetFactoryForManager(m_doubleManager);
  m_browser->unsetFactoryForManager(m_intManager);
  m_browser->unsetFactoryForManager(m_boolManager);
  m_browser->unsetFactoryForManager(m_enumManager);
}

/**
 * Create the Qt property browser and set up property managers.
 */
void BasicFitOptionsBrowser::createBrowser() {
  /* Create property managers: they create, own properties, get and set values
   */
  m_stringManager = new QtStringPropertyManager(this);
  m_doubleManager = new QtDoublePropertyManager(this);
  m_intManager = new QtIntPropertyManager(this);
  m_boolManager = new QtBoolPropertyManager(this);
  m_enumManager = new QtEnumPropertyManager(this);
  m_groupManager = new QtGroupPropertyManager(this);

  // create editor factories
  auto *spinBoxFactory = new QtSpinBoxFactory(this);
  auto *doubleEditorFactory = new DoubleEditorFactory(this);
  auto *lineEditFactory = new QtLineEditFactory(this);
  auto *checkBoxFactory = new QtCheckBoxFactory(this);
  auto *comboBoxFactory = new QtEnumEditorFactory(this);

  m_browser = new QtTreePropertyBrowser(nullptr, QStringList(), false);
  // assign factories to property managers
  m_browser->setFactoryForManager(m_stringManager, lineEditFactory);
  m_browser->setFactoryForManager(m_doubleManager, doubleEditorFactory);
  m_browser->setFactoryForManager(m_intManager, spinBoxFactory);
  m_browser->setFactoryForManager(m_boolManager, checkBoxFactory);
  m_browser->setFactoryForManager(m_enumManager, comboBoxFactory);

  // m_browser->setContextMenuPolicy(Qt::CustomContextMenu);
  // connect(m_browser, SIGNAL(customContextMenuRequested(const QPoint &)),
  // this, SLOT(popupMenu(const QPoint &)));

  connect(m_enumManager, SIGNAL(propertyChanged(QtProperty *)), this,
          SLOT(enumChanged(QtProperty *)));
  connect(m_doubleManager, SIGNAL(propertyChanged(QtProperty *)), this,
          SLOT(doubleChanged(QtProperty *)));
  // Fill in getter and setter maps
}

/**
 * @brief Initialize the fitting type property. Show in the browser
 * only if user can switch fit type.
 */
void BasicFitOptionsBrowser::initFittingTypeProp() {
  m_fittingTypeProp = m_enumManager->addProperty("Fitting");
  QStringList types;
  types << "Sequential"
        << "Simultaneous";
  m_enumManager->setEnumNames(m_fittingTypeProp, types);
  if (m_fittingType == FittingMode::SEQUENTIAL_AND_SIMULTANEOUS) {
    m_browser->addProperty(m_fittingTypeProp);
  } else if (m_fittingType == FittingMode::SIMULTANEOUS ||
             m_fittingType == FittingMode::SEQUENTIAL) {
    this->lockCurrentFittingType(m_fittingType);
  }
}

/**
 * Create browser's QtProperties
 */
void BasicFitOptionsBrowser::createProperties() {
  initFittingTypeProp();
  createCommonProperties();
  if (m_fittingType == FittingMode::SIMULTANEOUS ||
      m_fittingType == FittingMode::SEQUENTIAL_AND_SIMULTANEOUS) {
    createSimultaneousFitProperties();
  }
  if (m_fittingType == FittingMode::SEQUENTIAL ||
      m_fittingType == FittingMode::SEQUENTIAL_AND_SIMULTANEOUS) {
    createSequentialFitProperties();
  }
  switchFitType();
}

void BasicFitOptionsBrowser::createCommonProperties() {
  // Create MaxIterations property
  m_maxIterations = m_intManager->addProperty("Max Iterations");
  {
    m_intManager->setValue(m_maxIterations, 500);
    m_intManager->setMinimum(m_maxIterations, 0);
    m_browser->addProperty(m_maxIterations);

    addProperty("MaxIterations", m_maxIterations,
                &BasicFitOptionsBrowser::getIntProperty,
                &BasicFitOptionsBrowser::setIntProperty);
  }

  // Set up the minimizer property.
  //
  // Create the enclosing group property. This group contains
  // the minimizer name plus any properties of its own
  m_minimizerGroup = m_groupManager->addProperty("Minimizer");
  {
    // Add the name property to the group
    m_minimizer = m_enumManager->addProperty("Name");
    m_minimizerGroup->addSubProperty(m_minimizer);

    // Get names of registered minimizers from the factory
    std::vector<std::string> minimizerOptions =
        Mantid::API::FuncMinimizerFactory::Instance().getKeys();
    QStringList minimizers;

    // Store them in the m_minimizer enum property
    for (auto &minimizerOption : minimizerOptions) {
      minimizers << QString::fromStdString(minimizerOption);
    }
    m_enumManager->setEnumNames(m_minimizer, minimizers);
    int i =
        m_enumManager->enumNames(m_minimizer).indexOf("Levenberg-Marquardt");
    if (i >= 0) {
      m_enumManager->setValue(m_minimizer, i);
    }
    m_browser->addProperty(m_minimizerGroup);
    addProperty("Minimizer", m_minimizer, &BasicFitOptionsBrowser::getMinimizer,
                &BasicFitOptionsBrowser::setMinimizer);
  }

  // Create cost function property
  m_costFunction = m_enumManager->addProperty("Cost Function");
  {
    // Get names of registered cost functions from the factory
    std::vector<std::string> costOptions =
        Mantid::API::CostFunctionFactory::Instance().getKeys();
    QStringList costFunctions;
    // Store them in the m_minimizer enum property
    for (auto &costOption : costOptions) {
      costFunctions << QString::fromStdString(costOption);
    }
    m_enumManager->setEnumNames(m_costFunction, costFunctions);
    m_browser->addProperty(m_costFunction);
    addProperty("CostFunction", m_costFunction,
                &BasicFitOptionsBrowser::getStringEnumProperty,
                &BasicFitOptionsBrowser::setStringEnumProperty);
  }

  // Create EvaluationType property
  m_evaluationType = m_enumManager->addProperty("Evaluate Function As");
  {
    QStringList evaluationTypes;
    evaluationTypes << "CentrePoint"
                    << "Histogram";
    m_enumManager->setEnumNames(m_evaluationType, evaluationTypes);
    m_browser->addProperty(m_evaluationType);
    addProperty("EvaluationType", m_evaluationType,
                &BasicFitOptionsBrowser::getStringEnumProperty,
                &BasicFitOptionsBrowser::setStringEnumProperty);
  }
  // Create PeakRadius property
  m_peakRadius = m_intManager->addProperty("Peak Radius");
  {
    m_intManager->setValue(m_peakRadius, 0);
    m_intManager->setMinimum(m_peakRadius, 0);
    m_browser->addProperty(m_peakRadius);
    addProperty("PeakRadius", m_peakRadius,
                &BasicFitOptionsBrowser::getIntProperty,
                &BasicFitOptionsBrowser::setIntProperty);
  }
}

void BasicFitOptionsBrowser::createSimultaneousFitProperties() {
  // Create Output property
  m_output = m_stringManager->addProperty("Output");
  {
    m_browser->addProperty(m_output);
    addProperty("Output", m_output, &BasicFitOptionsBrowser::getStringProperty,
                &BasicFitOptionsBrowser::setStringProperty);
    m_simultaneousProperties << m_output;
  }

  // Create Ignore property
  m_ignoreInvalidData = m_boolManager->addProperty("Ignore Invalid Data");
  {
    m_browser->addProperty(m_ignoreInvalidData);
    addProperty("IgnoreInvalidData", m_ignoreInvalidData,
                &BasicFitOptionsBrowser::getBoolProperty,
                &BasicFitOptionsBrowser::setBoolProperty);
    m_simultaneousProperties << m_ignoreInvalidData;
  }
}

void BasicFitOptionsBrowser::createSequentialFitProperties() {
  // Create FitType property, a property of algorithm PlotPeakByLogValue
  m_plotPeakByLogValueFitType = m_enumManager->addProperty("Fit Type");
  {
    QStringList types;
    types << "Sequential"
          << "Individual";
    m_enumManager->setEnumNames(m_plotPeakByLogValueFitType, types);
    m_enumManager->setValue(m_plotPeakByLogValueFitType, 0);
    addProperty("FitType", m_plotPeakByLogValueFitType,
                &BasicFitOptionsBrowser::getStringEnumProperty,
                &BasicFitOptionsBrowser::setStringEnumProperty);
    m_sequentialProperties << m_plotPeakByLogValueFitType;
  }

  // Create OutputWorkspace property
  m_outputWorkspace = m_stringManager->addProperty("OutputWorkspace");
  {
    addProperty("OutputWorkspace", m_outputWorkspace,
                &BasicFitOptionsBrowser::getStringProperty,
                &BasicFitOptionsBrowser::setStringProperty);
    m_sequentialProperties << m_outputWorkspace;
  }

  // Create CreateOutput property
  auto prop = m_boolManager->addProperty("Create Output");
  {
    addProperty("CreateOutput", prop, &BasicFitOptionsBrowser::getBoolProperty,
                &BasicFitOptionsBrowser::setBoolProperty);
    m_sequentialProperties << prop;
  }

  // Create OutputCompositeMembers property
  prop = m_boolManager->addProperty("Output Composite Members");
  {
    addProperty("OutputCompositeMembers", prop,
                &BasicFitOptionsBrowser::getBoolProperty,
                &BasicFitOptionsBrowser::setBoolProperty);
    m_sequentialProperties << prop;
  }

  // Create ConvolveMembers property
  prop = m_boolManager->addProperty("Convolve Members");
  {
    addProperty("ConvolveMembers", prop,
                &BasicFitOptionsBrowser::getBoolProperty,
                &BasicFitOptionsBrowser::setBoolProperty);
    m_sequentialProperties << prop;
  }

  // Create PassWSIndexToFunction property
  prop = m_boolManager->addProperty("Pass WS Index To Function");
  {
    addProperty("PassWSIndexToFunction", prop,
                &BasicFitOptionsBrowser::getBoolProperty,
                &BasicFitOptionsBrowser::setBoolProperty);
    m_sequentialProperties << prop;
  }

  // Create LogValue property
  m_logValue = m_enumManager->addProperty("Log Value");
  {
    // m_enumManager->setValue(m_logValue,0);
    addProperty("LogValue", m_logValue,
                &BasicFitOptionsBrowser::getStringEnumProperty,
                &BasicFitOptionsBrowser::setStringEnumProperty);
    m_sequentialProperties << m_logValue;
  }

  // Create LogValue property
  m_plotParameter = m_enumManager->addProperty("Plot parameter");
  {
    addProperty("PlotParameter", m_plotParameter,
                &BasicFitOptionsBrowser::getStringEnumProperty,
                &BasicFitOptionsBrowser::setStringEnumProperty);
    m_sequentialProperties << m_plotParameter;
  }
}

void BasicFitOptionsBrowser::addProperty(
    const QString &name, QtProperty *prop,
    QString (BasicFitOptionsBrowser::*getter)(QtProperty *) const,
    void (BasicFitOptionsBrowser::*setter)(QtProperty *, const QString &)) {
  m_propertyNameMap[name] = prop;
  m_getters[prop] = getter;
  m_setters[prop] = setter;
}

/**
 * Remove a property previously added with addProperty
 * (If property doesn't exist, does nothing).
 * @param name :: [input] Name of property to remove
 */
void BasicFitOptionsBrowser::removeProperty(const QString &name) {
  if (m_propertyNameMap.contains(name)) {
    const auto prop = m_propertyNameMap[name];
    m_getters.remove(prop);
    m_setters.remove(prop);
    m_propertyNameMap.remove(name);
  }
}

/*                *********************
 *                **  Private Slots  **
 *                *********************/

/**
 * Update the browser when an enum property changes.
 * @param prop :: Property that changed its value.
 */
void BasicFitOptionsBrowser::enumChanged(QtProperty *prop) {
  if (prop == m_minimizer) {
    updateMinimizer();
  } else if (prop == m_fittingTypeProp) {
    switchFitType();
  }
}

/**
 * @brief pass the signal emitted by m_doubleManager
 */
void BasicFitOptionsBrowser::doubleChanged(QtProperty *property) {
  emit doublePropertyChanged(property->propertyName());
}

/**
 * Update the browser when minimizer changes.
 */
void BasicFitOptionsBrowser::updateMinimizer() {
  int i = m_enumManager->value(m_minimizer);
  QString minimizerName = m_enumManager->enumNames(m_minimizer)[i];
  m_minimizerGroup->setPropertyName("Minimizer " + minimizerName);

  // Remove properties of the old minimizer
  auto subProperties = m_minimizerGroup->subProperties();
  foreach (QtProperty *prop, subProperties) {
    if (prop != m_minimizer) {
      m_minimizerGroup->removeSubProperty(prop);
      removeProperty(prop->propertyName());
    }
  }

  // Check if the new minimizer has its own properties
  auto minimizer =
      Mantid::API::FuncMinimizerFactory::Instance().createMinimizer(
          minimizerName.toStdString());

  // Create and add properties to the minimizer group
  auto minimizerProperties = minimizer->getProperties();
  for (auto &minimizerProperty : minimizerProperties) {
    auto prop = createPropertyProperty(minimizerProperty);
    if (!minimizerProperty)
      continue;
    m_minimizerGroup->addSubProperty(prop);
  }
}

/**
 * Switch the current fit type according to the value in the FitType property.
 */
void BasicFitOptionsBrowser::switchFitType() {
  const auto fittingMode = getCurrentFittingType();
  if (fittingMode == FittingMode::SIMULTANEOUS) {
    displayNormalFitProperties();
  } else {
    displaySequentialFitProperties();
  }
}

/**
 * Show normal Fit properties and hide the others.
 */
void BasicFitOptionsBrowser::displayNormalFitProperties() {
  foreach (QtProperty *prop, m_simultaneousProperties) {
    m_browser->addProperty(prop);
  }
  foreach (QtProperty *prop, m_sequentialProperties) {
    m_browser->removeProperty(prop);
  }
  emit changedToSimultaneousFitting();
}

/**
 * Create a QtProperty for an Algorithm Property
 * and attach it to the correct manager.
 * @param property :: An algorithm property.
 */
QtProperty *BasicFitOptionsBrowser::createPropertyProperty(
    Mantid::Kernel::Property *property) {
  QString propName = QString::fromStdString(property->name());
  QtProperty *prop = nullptr;
  if (auto prp =
          dynamic_cast<Mantid::Kernel::PropertyWithValue<bool> *>(property)) {
    prop = m_boolManager->addProperty(propName);
    bool val = *prp;
    m_boolManager->setValue(prop, val);
  } else if (auto prp =
                 dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(
                     property)) {
    prop = this->addDoubleProperty(propName);
    double val = *prp;
    m_doubleManager->setValue(prop, val);
  } else if (auto prp = dynamic_cast<Mantid::Kernel::PropertyWithValue<int> *>(
                 property)) {
    prop = m_intManager->addProperty(propName);
    int val = *prp;
    m_intManager->setValue(prop, val);
  } else if (auto prp =
                 dynamic_cast<Mantid::Kernel::PropertyWithValue<size_t> *>(
                     property)) {
    prop = m_intManager->addProperty(propName);
    size_t val = *prp;
    m_intManager->setValue(prop, static_cast<int>(val));
  } else if (auto prp =
                 dynamic_cast<Mantid::Kernel::PropertyWithValue<std::string> *>(
                     property)) {
    prop = m_stringManager->addProperty(propName);
    QString val = QString::fromStdString(prp->value());
    m_stringManager->setValue(prop, val);
  } else if (dynamic_cast<Mantid::API::IWorkspaceProperty *>(property)) {
    prop = m_stringManager->addProperty(propName);
    m_stringManager->setValue(prop, QString::fromStdString(property->value()));
  } else {
    QMessageBox::warning(this, "Mantid - Error",
                         "Type of minimizer's property " + propName +
                             " is not yet supported by the browser.");
    return nullptr;
  }

  // Something bad happened in QtPropertyBrowser.
  if (!prop) {
    throw std::runtime_error("Failed to create a QtProperty.");
  }

  // set the tooltip from property doc string
  QString toolTip = QString::fromStdString(property->documentation());
  if (!toolTip.isEmpty()) {
    prop->setToolTip(toolTip);
  }

  return prop;
}

/**
 * Copy values of the properties to an algorithm.
 * @param fit :: An instance of the Fit algorithm.
 */
void BasicFitOptionsBrowser::copyPropertiesToAlgorithm(
    Mantid::API::IAlgorithm &fit) const {
  for (auto p = m_propertyNameMap.constBegin();
       p != m_propertyNameMap.constEnd(); ++p) {
    auto propertyName = p.key().toStdString();
    if (fit.existsProperty(propertyName)) {
      auto prop = p.value();
      auto f = m_getters[prop];
      fit.setPropertyValue(propertyName, (this->*f)(prop).toStdString());
    }
  }
}

/**
 * Get a string representation of a Fit's property value.
 * @param name :: The name of a Fit's property.
 */
QString BasicFitOptionsBrowser::getProperty(const QString &name) const {
  if (!m_propertyNameMap.contains(name)) {
    throw std::runtime_error("Property " + name.toStdString() +
                             " isn't supported by the browser.");
  }
  auto prop = m_propertyNameMap[name];
  auto f = m_getters[prop];
  return (this->*f)(prop);
}

/**
 * Set a new value to a Fit's property.
 * @param name :: The name of a Fit's property.
 * @param value :: The new value as a string.
 */
void BasicFitOptionsBrowser::setProperty(const QString &name,
                                         const QString &value) {
  if (!m_propertyNameMap.contains(name)) {
    throw std::runtime_error("Property " + name.toStdString() +
                             " isn't supported by the browser.");
  }
  auto prop = m_propertyNameMap[name];
  auto f = m_setters[prop];
  (this->*f)(prop, value);
}

/**
 * Get the value of the Minimizer property.
 */
QString BasicFitOptionsBrowser::getMinimizer(QtProperty * /*unused*/) const {
  int i = m_enumManager->value(m_minimizer);
  QString minimStr = m_enumManager->enumNames(m_minimizer)[i];

  auto subProperties = m_minimizerGroup->subProperties();
  if (subProperties.size() > 1) {
    foreach (QtProperty *prop, subProperties) {
      if (prop == m_minimizer)
        continue;
      if (prop->propertyManager() == m_stringManager) {
        QString value = m_stringManager->value(prop);
        if (!value.isEmpty()) {
          minimStr += "," + prop->propertyName() + "=" + value;
        }
      } else {
        minimStr += "," + prop->propertyName() + "=";
        if (prop->propertyManager() == m_intManager) {
          minimStr += QString::number(m_intManager->value(prop));
        } else if (prop->propertyManager() == m_doubleManager) {
          minimStr += QString::number(m_doubleManager->value(prop));
        } else if (prop->propertyManager() == m_boolManager) {
          minimStr += QString::number(m_boolManager->value(prop));
        } else {
          throw std::runtime_error("The fit browser doesn't support the type "
                                   "of minimizer's property " +
                                   prop->propertyName().toStdString());
        }
      }
    } // foreach
  }
  return minimStr;
}

/**
 * Set new value to the Minimizer property.
 * @param value :: The new value.
 */
void BasicFitOptionsBrowser::setMinimizer(QtProperty * /*unused*/,
                                          const QString &value) {
  QStringList terms = value.split(',');
  int i = m_enumManager->enumNames(m_minimizer).indexOf(terms[0]);
  m_enumManager->setValue(m_minimizer, i);
}

// ------------------------- Generic setters and getters
// ------------------------------//

/**
 * Get the value of an integer algorithm property.
 * @param prop :: The corresponding QtProperty.
 */
QString BasicFitOptionsBrowser::getIntProperty(QtProperty *prop) const {
  return QString::number(m_intManager->value(prop));
}

/**
 * Set a new value of an integer algorithm property.
 * @param prop :: The corresponding QtProperty.
 * @param value :: The new value.
 */
void BasicFitOptionsBrowser::setIntProperty(QtProperty *prop,
                                            const QString &value) {
  m_intManager->setValue(prop, value.toInt());
}

/**
 * Get the value of a double algorithm property.
 * @param prop :: The corresponding QtProperty.
 * @return the stored value
 */
QString BasicFitOptionsBrowser::getDoubleProperty(QtProperty *prop) const {
  return QString::number(m_doubleManager->value(prop));
}

/**
 * Set a new value of a double algorithm property.
 * @param prop :: The corresponding QtProperty.
 * @param value :: The new value.
 */
void BasicFitOptionsBrowser::setDoubleProperty(QtProperty *prop,
                                               const QString &value) {
  m_doubleManager->setValue(prop, value.toDouble());
}

/**
 * Get the value of a bool algorithm property.
 * @param prop :: The corresponding QtProperty.
 */
QString BasicFitOptionsBrowser::getBoolProperty(QtProperty *prop) const {
  return QString::number(m_boolManager->value(prop));
}

/**
 * Set a new value of a bool algorithm property.
 * @param prop :: The corresponding QtProperty.
 * @param value :: The new value.
 */
void BasicFitOptionsBrowser::setBoolProperty(QtProperty *prop,
                                             const QString &value) {
  bool boolValue = (value == "1") || (value.toLower() == "true");
  m_boolManager->setValue(prop, boolValue);
}

/**
 * Get the value of a string algorithm property with predefined set of values.
 * @param prop :: The corresponding QtProperty.
 */
QString BasicFitOptionsBrowser::getStringEnumProperty(QtProperty *prop) const {
  int i = m_enumManager->value(prop);
  if (i < 0)
    return "";
  return m_enumManager->enumNames(prop)[i];
}

/**
 * Set a new value of a string algorithm property with predefined set of values.
 * @param prop :: The corresponding QtProperty.
 * @param value :: The new value.
 */
void BasicFitOptionsBrowser::setStringEnumProperty(QtProperty *prop,
                                                   const QString &value) {
  int i = m_enumManager->enumNames(prop).indexOf(value);
  if (i >= 0)
    m_enumManager->setValue(prop, i);
}

/**
 * Get the value of a string algorithm property.
 * @param prop :: The corresponding QtProperty.
 */
QString BasicFitOptionsBrowser::getStringProperty(QtProperty *prop) const {
  return m_stringManager->value(prop);
}

/**
 * Set a new value of a string algorithm property.
 * @param prop :: The corresponding QtProperty.
 * @param value :: The new value.
 */
void BasicFitOptionsBrowser::setStringProperty(QtProperty *prop,
                                               const QString &value) {
  m_stringManager->setValue(prop, value);
}

// ------------------------------------------------------------------------------------//

/**
 * Save the last property values in settings.
 * @param settings :: A QSettings instance provided by the user of this class.
 */
void BasicFitOptionsBrowser::saveSettings(QSettings &settings) const {
  for (auto p = m_propertyNameMap.constBegin();
       p != m_propertyNameMap.constEnd(); ++p) {
    auto prop = p.value();
    auto f = m_getters[prop];
    settings.setValue(p.key(), (this->*f)(prop));
  }
}

/**
 * Load property values from settings.
 * @param settings :: A QSettings instance provided by the user of this class.
 */
void BasicFitOptionsBrowser::loadSettings(const QSettings &settings) {
  for (auto p = m_propertyNameMap.constBegin();
       p != m_propertyNameMap.constEnd(); ++p) {
    QString value = settings.value(p.key()).toString();
    if (!value.isEmpty()) {
      auto prop = p.value();
      auto f = m_setters[prop];
      (this->*f)(prop, value);
    }
  }
}

/**
 * Get the current fitting type, ie which algorithm to use:
 *    Simultaneous for Fit and Sequential for PlotPeakByLogValue.
 */
FittingMode BasicFitOptionsBrowser::getCurrentFittingType() const {
  auto value = m_enumManager->value(m_fittingTypeProp);
  return static_cast<FittingMode>(value);
}

/**
 * Set the current fitting type, ie which algorithm to use:
 *    Simultaneous for Fit and Sequential for PlotPeakByLogValue.
 */
void BasicFitOptionsBrowser::setCurrentFittingType(FittingMode fitType) {
  if (fitType == FittingMode::SIMULTANEOUS) {
    m_enumManager->setValue(m_fittingTypeProp, 1);
  } else {
    m_enumManager->setValue(m_fittingTypeProp, 0);
  }
}

/**
 * Lock the browser in a particular fitting type state. Disable the switch
 * option.
 * @param fitType :: Fitting type to lock the browser in.
 */
void BasicFitOptionsBrowser::lockCurrentFittingType(FittingMode fitType) {
  setCurrentFittingType(fitType);
  m_fittingTypeProp->setEnabled(false);
}

/**
 * Make the fitting type changeable again.
 */
void BasicFitOptionsBrowser::unlockCurrentFittingType() {
  m_fittingTypeProp->setEnabled(true);
}

/**
 * Set values for an enum property.
 * @param prop :: A property to set the values to.
 * @param values :: New enum values.
 */
void BasicFitOptionsBrowser::setPropertyEnumValues(QtProperty *prop,
                                                   const QStringList &values) {
  auto i = m_enumManager->value(prop);
  if (!values.isEmpty() && values.front().isEmpty()) {
    m_enumManager->setEnumNames(prop, values);
  } else {
    QStringList names = values;
    names.insert(0, "");
    m_enumManager->setEnumNames(prop, names);
  }
  if (i < values.size()) {
    m_enumManager->setValue(prop, i);
  } else {
    m_enumManager->setValue(prop, 0);
  }
}

/**
 * Define log names to use with the LogValue property.
 * @param logNames :: The log names
 */
void BasicFitOptionsBrowser::setLogNames(const QStringList &logNames) {
  setPropertyEnumValues(m_logValue, logNames);
}

/**
 * Define names of function parameters that can be plotted against the LogValue.
 */
void BasicFitOptionsBrowser::setParameterNamesForPlotting(
    const QStringList &parNames) {
  setPropertyEnumValues(m_plotParameter, parNames);
}

/**
 * Get name of a function parameter to plot against LogValue after sequential
 * fitting.
 */
QString BasicFitOptionsBrowser::getParameterToPlot() const {
  auto i = m_enumManager->value(m_plotParameter);
  if (i < 0)
    i = 0;
  return m_enumManager->enumNames(m_plotParameter)[i];
}

/*                *************************
 *                **  Protected Members  **
 *                *************************/

/**
 * @brief Declares a property of type double, inserting it in the QMap
 * attributes.
 * Note: It does not add it to the browser. Use displayProperty() for this.
 * @exception std::runtime_error if property already declared
 * @return a raw pointer to the created property.
 */
QtProperty *
BasicFitOptionsBrowser::addDoubleProperty(const QString &propertyName) {
  if (m_propertyNameMap.contains(propertyName)) {
    throw std::runtime_error("Property " + propertyName.toStdString() +
                             " already added.");
  }
  QtProperty *property = m_doubleManager->addProperty(propertyName);
  m_doubleManager->setDecimals(property, m_decimals);
  m_doubleManager->setRange(property, std::numeric_limits<double>::lowest(),
                            std::numeric_limits<double>::max());
  this->addProperty(propertyName, property,
                    &BasicFitOptionsBrowser::getDoubleProperty,
                    &BasicFitOptionsBrowser::setDoubleProperty);
  return property;
}

/**
 * @brief Show or hide in the browser a supported property
 * @param propertyName name of the existing property
 * @param show toggles the visibility of the property on/off
 * @pre if property is to be shown, property should not have been previously
 * added to the browser
 * @pre if property is to be hidden, property should not have been previously
 * removed from the browser
 */
void BasicFitOptionsBrowser::displayProperty(const QString &propertyName,
                                             bool show) {
  if (!m_propertyNameMap.contains(propertyName)) {
    throw std::runtime_error("Property " + propertyName.toStdString() +
                             " isn't supported by the browser.");
  }
  auto prop = m_propertyNameMap[propertyName];
  if (show) {
    m_browser->addProperty(prop);
  } else {
    m_browser->removeProperty(prop);
  }
}

/**
 * Adds the property with the given name to a blacklist of properties to hide
 */
bool BasicFitOptionsBrowser::addPropertyToBlacklist(const QString &name) {
  if (!m_propertyNameMap.contains(name)) {
    return false;
  }
  auto prop = m_propertyNameMap[name];
  m_sequentialProperties.removeAll(prop);
  m_simultaneousProperties.removeAll(prop);
  m_browser->removeProperty(prop);
  return true;
}

/**
 * Show sequential fit (PlotPeakByLogValue) properties and hide the others.
 */
void BasicFitOptionsBrowser::displaySequentialFitProperties() {
  foreach (QtProperty *prop, m_sequentialProperties) {
    m_browser->addProperty(prop);
  }
  foreach (QtProperty *prop, m_simultaneousProperties) {
    m_browser->removeProperty(prop);
  }
  emit changedToSequentialFitting();
}

} // namespace MantidWidgets
} // namespace MantidQt