diff --git a/GUI/View/FitObjective/FitParameterDelegate.cpp b/GUI/View/FitObjective/FitParameterDelegate.cpp index e4d39740b2545c084d12d33246a41d137471adad..bec5d14e7433ebe66b286e78d55ccc189b7f72d2 100644 --- a/GUI/View/FitObjective/FitParameterDelegate.cpp +++ b/GUI/View/FitObjective/FitParameterDelegate.cpp @@ -14,10 +14,85 @@ #include "GUI/View/FitObjective/FitParameterDelegate.h" #include "Base/Util/Assert.h" +#include "GUI/Model/Job/FitParameterItem.h" +#include "GUI/Model/Project/ProjectDocument.h" +#include "GUI/Support/Data/ComboProperty.h" #include "GUI/Support/Util/CustomEventFilters.h" +#include "GUI/View/Numeric/ScientificSpinBox.h" #include "GUI/View/PropertyEditor/CustomEditors.h" -#include "GUI/View/PropertyEditor/PropertyEditorFactory.h" #include <QApplication> +#include <QModelIndex> +#include <QSpinBox> + +namespace { + +double getStep(double val) +{ + return val == 0.0 ? 1.0 : val / 100.; +} + +bool isDoubleProperty(const QVariant& variant) +{ + return variant.type() == QVariant::Double; +} + +bool isComboProperty(const QVariant& variant) +{ + return variant.canConvert<ComboProperty>(); +} + +bool hasStringRepresentation(const QModelIndex& index) +{ + auto variant = index.data(); + if (isComboProperty(variant)) + return true; + if (isDoubleProperty(variant) && index.internalPointer()) + return true; + + return false; +} + +QString toString(const QModelIndex& index) +{ + auto variant = index.data(); + if (isComboProperty(variant)) + return variant.value<ComboProperty>().label(); + + if (isDoubleProperty(variant) && index.internalPointer()) { + auto* item = static_cast<QObject*>(index.internalPointer()); + FitDoubleItem* doubleItem = dynamic_cast<FitDoubleItem*>(item); + // only "Scientific SpinBoxes" in Fit-Window + return ScientificSpinBox::toString(doubleItem->value(), doubleItem->decimals()); + } + return ""; +} + +QWidget* createEditor(QObject* item, QWidget* parent) +{ + CustomEditor* result(nullptr); + + if (FitDoubleItem* doubleItem = dynamic_cast<FitDoubleItem*>(item)) { + // only Scientific SpinBoxes in Fit-Window + auto* editor = new ScientificSpinBoxEditor; + editor->setLimits(doubleItem->limits()); + editor->setDecimals(doubleItem->decimals()); + editor->setSingleStep(getStep(doubleItem->value())); + result = editor; + } else if (dynamic_cast<FitTypeItem*>(item)) + result = new ComboPropertyEditor; + + if (parent && result) { + result->setParent(parent); + + QObject::connect(result, &CustomEditor::dataChanged, + [=] { gProjectDocument.value()->setModified(); }); + } + + return result; +} + +} // namespace + FitParameterDelegate::FitParameterDelegate(QObject* parent) : QStyledItemDelegate(parent) @@ -27,8 +102,8 @@ FitParameterDelegate::FitParameterDelegate(QObject* parent) void FitParameterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - if (PropertyEditorFactory::hasStringRepresentation(index)) { - QString text = PropertyEditorFactory::toString(index); + if (::hasStringRepresentation(index)) { + QString text = ::toString(index); paintCustomLabel(painter, option, index, text); } else QStyledItemDelegate::paint(painter, option, index); @@ -105,7 +180,7 @@ QWidget* FitParameterDelegate::createEditorFromIndex(const QModelIndex& index, { if (index.internalPointer()) { auto* item = static_cast<QObject*>(index.internalPointer()); - return PropertyEditorFactory::CreateEditor(item, parent); + return ::createEditor(item, parent); } return nullptr; } diff --git a/GUI/View/PropertyEditor/PropertyEditorFactory.cpp b/GUI/View/PropertyEditor/PropertyEditorFactory.cpp deleted file mode 100644 index cd4ae7fa5ac38776359eae19c8c8d11435062017..0000000000000000000000000000000000000000 --- a/GUI/View/PropertyEditor/PropertyEditorFactory.cpp +++ /dev/null @@ -1,92 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit reflection and scattering -// -//! @file GUI/View/PropertyEditor/PropertyEditorFactory.cpp -//! @brief Implements PropertyEditorFactory namespace -//! -//! @homepage http://www.bornagainproject.org -//! @license GNU General Public License v3 or higher (see COPYING) -//! @copyright Forschungszentrum Jülich GmbH 2018 -//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) -// -// ************************************************************************************************ - -#include "GUI/View/PropertyEditor/PropertyEditorFactory.h" -#include "GUI/Model/Job/FitParameterItem.h" -#include "GUI/Model/Project/ProjectDocument.h" -#include "GUI/Support/Data/ComboProperty.h" -#include "GUI/View/Numeric/ScientificSpinBox.h" -#include "GUI/View/PropertyEditor/CustomEditors.h" -#include <QModelIndex> -#include <QSpinBox> - -namespace { - -double getStep(double val) -{ - return val == 0.0 ? 1.0 : val / 100.; -} - -bool isDoubleProperty(const QVariant& variant) -{ - return variant.type() == QVariant::Double; -} - -bool isComboProperty(const QVariant& variant) -{ - return variant.canConvert<ComboProperty>(); -} - -} // namespace - - -bool PropertyEditorFactory::hasStringRepresentation(const QModelIndex& index) -{ - auto variant = index.data(); - if (isComboProperty(variant)) - return true; - if (isDoubleProperty(variant) && index.internalPointer()) - return true; - - return false; -} - -QString PropertyEditorFactory::toString(const QModelIndex& index) -{ - auto variant = index.data(); - if (isComboProperty(variant)) - return variant.value<ComboProperty>().label(); - - if (isDoubleProperty(variant) && index.internalPointer()) { - auto* item = static_cast<QObject*>(index.internalPointer()); - FitDoubleItem* doubleItem = dynamic_cast<FitDoubleItem*>(item); - // only "Scientific SpinBoxes" in Fit-Window - return ScientificSpinBox::toString(doubleItem->value(), doubleItem->decimals()); - } - return ""; -} - -QWidget* PropertyEditorFactory::CreateEditor(QObject* item, QWidget* parent) -{ - CustomEditor* result(nullptr); - - if (FitDoubleItem* doubleItem = dynamic_cast<FitDoubleItem*>(item)) { - // only Scientific SpinBoxes in Fit-Window - auto* editor = new ScientificSpinBoxEditor; - editor->setLimits(doubleItem->limits()); - editor->setDecimals(doubleItem->decimals()); - editor->setSingleStep(getStep(doubleItem->value())); - result = editor; - } else if (dynamic_cast<FitTypeItem*>(item)) - result = new ComboPropertyEditor; - - if (parent && result) { - result->setParent(parent); - - QObject::connect(result, &CustomEditor::dataChanged, - [=] { gProjectDocument.value()->setModified(); }); - } - - return result; -} diff --git a/GUI/View/PropertyEditor/PropertyEditorFactory.h b/GUI/View/PropertyEditor/PropertyEditorFactory.h deleted file mode 100644 index ed6cb1b86f64248f2ff0ff6c50e976f117e3d777..0000000000000000000000000000000000000000 --- a/GUI/View/PropertyEditor/PropertyEditorFactory.h +++ /dev/null @@ -1,35 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit reflection and scattering -// -//! @file GUI/View/PropertyEditor/PropertyEditorFactory.h -//! @brief Defines namespace PropertyEditorFactory -//! -//! @homepage http://www.bornagainproject.org -//! @license GNU General Public License v3 or higher (see COPYING) -//! @copyright Forschungszentrum Jülich GmbH 2018 -//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) -// -// ************************************************************************************************ - -#ifndef BORNAGAIN_GUI_VIEW_PROPERTYEDITOR_PROPERTYEDITORFACTORY_H -#define BORNAGAIN_GUI_VIEW_PROPERTYEDITOR_PROPERTYEDITORFACTORY_H - -#include <QWidget> - -//! Creates editors for SessionItems. - -namespace PropertyEditorFactory { - -//! Returns true if the index data has known (custom) conversion to string. -bool hasStringRepresentation(const QModelIndex& index); - -//! Provides string representation of index data. -QString toString(const QModelIndex& index); - -//! Creates an editor suitable for editing of item.value() -QWidget* CreateEditor(QObject* item, QWidget* parent = nullptr); - -} // namespace PropertyEditorFactory - -#endif // BORNAGAIN_GUI_VIEW_PROPERTYEDITOR_PROPERTYEDITORFACTORY_H