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
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/ParEdit/FitParameterDelegate.cpp
//! @brief Implements class FitParameterDelegate.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/ParEdit/FitParameterDelegate.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Descriptor/ComboProperty.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Tune/FitParameterItem.h"
#include "GUI/View/Base/CustomEventFilters.h"
#include "GUI/View/ParEdit/CustomEditors.h"
#include "GUI/View/ParEdit/ParSpinBox.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>().currentValue();
if (isDoubleProperty(variant) && index.internalPointer()) {
auto* item = static_cast<QObject*>(index.internalPointer());
auto* doubleItem = dynamic_cast<FitDoubleItem*>(item);
// only "Scientific SpinBoxes" in Fit-Window
return ParSpinBox::toString(doubleItem->value(), doubleItem->decimals());
}
return "";
}
QWidget* createEditorFromIndex(const QModelIndex& index, QWidget* parent)
{
if (!index.internalPointer())
return nullptr;
auto* item = static_cast<QObject*>(index.internalPointer());
CustomEditor* result(nullptr);
if (auto* doubleItem = dynamic_cast<FitDoubleItem*>(item)) {
// only Scientific SpinBoxes in Fit-Window
auto* editor = new ParSpinBoxEditor;
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, [=] { gDoc->setModified(); });
return result;
}
} // namespace
FitParameterDelegate::FitParameterDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
void FitParameterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (::hasStringRepresentation(index)) {
QString text = ::toString(index);
paintCustomLabel(painter, option, index, text);
} else
QStyledItemDelegate::paint(painter, option, index);
}
QWidget* FitParameterDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
auto* result = ::createEditorFromIndex(index, parent);
if (!result) // falling back to default behaviour
return QStyledItemDelegate::createEditor(parent, option, index);
if (auto* customEditor = dynamic_cast<CustomEditor*>(result)) {
new TabFromFocusProxy(customEditor);
connect(customEditor, &CustomEditor::dataChanged, this,
&FitParameterDelegate::onCustomEditorDataChanged);
}
return result;
}
//! Propagates changed data from the editor to the model.
void FitParameterDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const
{
if (!index.isValid())
return;
if (auto* customEditor = dynamic_cast<CustomEditor*>(editor))
model->setData(index, customEditor->editorData());
else
QStyledItemDelegate::setModelData(editor, model, index);
}
//! Propagates the data change from the model to the editor (if it is still opened).
void FitParameterDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
if (!index.isValid())
return;
if (auto* customEditor = dynamic_cast<CustomEditor*>(editor))
customEditor->setData(index.data());
else
QStyledItemDelegate::setEditorData(editor, index);
}
//! Increases height of the row by 20% wrt the default.
QSize FitParameterDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QSize result = QStyledItemDelegate::sizeHint(option, index);
result.setHeight(static_cast<int>(result.height() * 1.2));
return result;
}
//! Makes an editor occupying whole available space in a cell. If cell contains an icon
//! as a decoration (i.e. icon of material property), it will be hidden as soon as editor
//! up and running.
void FitParameterDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
editor->setGeometry(option.rect);
}
//! Notifies everyone that the editor has completed editing the data.
void FitParameterDelegate::onCustomEditorDataChanged(const QVariant&)
{
auto* editor = qobject_cast<CustomEditor*>(sender());
ASSERT(editor);
emit commitData(editor);
}
//! Paints custom text in a a place corresponding given index.
void FitParameterDelegate::paintCustomLabel(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index, const QString& text) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); // calling original method to take into accounts colors etc
opt.text = displayText(text, option.locale); // by overriding text with ours
const QWidget* widget = opt.widget;
QStyle* style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
}
|