BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
defaultcelldecorator.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/viewmodel/mvvm/viewmodel/defaultcelldecorator.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
21 #include <QModelIndex>
22 #include <QStyleOptionViewItem>
23 
24 using namespace ModelView;
25 
26 bool DefaultCellDecorator::hasCustomDecoration(const QModelIndex& index) const
27 {
28  return cellText(index).has_value();
29 }
30 
31 std::optional<std::string> DefaultCellDecorator::cellText(const QModelIndex& index) const
32 {
33  auto variant = index.data();
34 
35  if (Utils::IsComboVariant(variant))
36  return std::optional<std::string>{variant.value<ComboProperty>().label()};
37 
38  else if (Utils::IsBoolVariant(variant))
39  return variant.value<bool>() ? std::optional<std::string>{"True"}
40  : std::optional<std::string>{"False"};
41 
42  else if (Utils::IsExtPropertyVariant(variant))
43  return std::optional<std::string>{variant.value<ExternalProperty>().text()};
44 
45  else if (Utils::IsColorVariant(variant))
46  return std::optional<std::string>{std::string()};
47 
48  else if (Utils::IsDoubleVariant(variant))
49  return std::optional<std::string>{
50  ScientificSpinBox::toString(index.data(Qt::EditRole).value<double>(),
52  .toStdString()};
53 
54  return {};
55 }
56 
57 void DefaultCellDecorator::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index)
58 {
59  if (!hasCustomDecoration(index))
60  return;
61 
62  auto value = cellText(index).value();
63  option->text = QString::fromStdString(value);
64  if (value.empty())
65  option->features &= ~QStyleOptionViewItem::HasDisplay;
66 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
std::string value() const
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) override
virtual std::optional< std::string > cellText(const QModelIndex &index) const
bool hasCustomDecoration(const QModelIndex &index) const override
Property to carry text, color and identifier.
static QString toString(double val, int decimal_points)
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const int default_double_decimals
MVVM_MODEL_EXPORT bool IsColorVariant(const Variant &variant)
Returns true in the case of QColor based variant.
MVVM_MODEL_EXPORT bool IsDoubleVariant(const Variant &variant)
Returns true in the case of double value based variant.
MVVM_MODEL_EXPORT bool IsBoolVariant(const Variant &variant)
Returns true in the case of double value based variant.
MVVM_MODEL_EXPORT bool IsExtPropertyVariant(const Variant &variant)
Returns true in the case of ExternalProperty based variant.
MVVM_MODEL_EXPORT bool IsComboVariant(const Variant &variant)
Returns true in the case of double value based variant.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?