BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DataProperties.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/DataProperties.cpp
6 //! @brief Implements class DataPresentationProperties and its descendants
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
21 
22 namespace {
23 // set of simple colors for representation of 1D graphs
24 const std::vector<std::pair<QString, Qt::GlobalColor>> color_queue = {
25  {"Black", Qt::GlobalColor::black}, {"Blue", Qt::GlobalColor::blue},
26  {"Red", Qt::GlobalColor::darkRed}, {"Cyan", Qt::GlobalColor::darkCyan},
27  {"Gray", Qt::GlobalColor::darkGray}, {"Magenta", Qt::GlobalColor::darkMagenta}};
28 
29 struct ColorNameComparator {
30  ColorNameComparator(const QString& value_to_comp) : m_value_to_comp(value_to_comp) {}
31  bool operator()(const std::pair<QString, Qt::GlobalColor>& value) const
32  {
33  return value.first == m_value_to_comp;
34  }
35 
36  const QString m_value_to_comp;
37 };
38 
39 ComboProperty defaultColorCombo();
40 } // namespace
41 
42 const QString DataProperties::P_DATALINK = "data link";
43 
45 {
47 }
48 
50 {
51  const QString& path = ModelPath::getPathFromIndex(item->index());
52  setItemValue(P_DATALINK, path);
53 }
54 
56 {
57  SessionModel* hosting_model = this->model();
58  const QString& path = getItemValue(P_DATALINK).toString();
59  auto item_index = ModelPath::getIndexFromPath(hosting_model, path);
60  if (!item_index.isValid()) {
61  std::stringstream os;
62  os << "Error in DataProperties::dataItem: index produced by path";
63  os << path.toStdString() << " is invalid" << std::endl;
64  throw GUIHelpers::Error(QString::fromStdString(os.str()));
65  }
66  auto item = hosting_model->itemForIndex(item_index);
67  return dynamic_cast<DataItem*>(item);
68 }
69 
70 const QString Data1DProperties::P_COLOR = "Color";
71 
73 {
74  addProperty(P_COLOR, defaultColorCombo().variant());
75 }
76 
78 {
79  const QString& color_name = getItemValue(P_COLOR).value<ComboProperty>().getValue();
80  auto iter =
81  std::find_if(color_queue.begin(), color_queue.end(), ColorNameComparator(color_name));
82  if (iter == color_queue.end())
83  throw GUIHelpers::Error("Error in Data1DPresentationProperties::color: unknown color name");
84  return QColor(iter->second);
85 }
86 
88 {
89  if (!properties)
90  return color_queue.front().first;
91  const auto& current_color = properties->getItemValue(P_COLOR).value<ComboProperty>().getValue();
92  auto iter =
93  std::find_if(color_queue.begin(), color_queue.end(), ColorNameComparator(current_color));
94  if (iter == color_queue.end() || *iter == color_queue.back())
95  return color_queue.front().first;
96  return (++iter)->first;
97 }
98 
99 void Data1DProperties::setColorProperty(const QString& color_name)
100 {
101  auto color_combo = defaultColorCombo();
102  color_combo.setValue(color_name);
103  setItemValue(P_COLOR, color_combo.variant());
104 }
105 
106 namespace {
107 ComboProperty defaultColorCombo()
108 {
109  ComboProperty result;
110  for (auto& color : color_queue)
111  result << color.first;
112  result.setValue(color_queue.front().first);
113  return result;
114 }
115 } // namespace
Defines class ComboProperty.
Declares class DataItem.
Defines class DataProperties and its descendants.
Defines class GUIHelpers functions.
Defines ModelPath namespace.
Defines class SessionModel.
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
void setValue(const QString &name)
Holds data required for 1D DataItem representation.
void setColorProperty(const QString &color_name)
Returns set up color ComboProperty.
static const QString & nextColorName(Data1DProperties *properties)
Returns the name of the color, which follows the color of passes property container.
QColor color()
Creates and returns a color object from color name in Data1DPresentationProperties.
static const QString P_COLOR
Provides common functionality for IntensityDataItem and SpecularDataItem.
Definition: DataItem.h:29
Implements a link to DataItem.
static const QString P_DATALINK
void setDataItem(DataItem *item)
DataProperties(const QString &model_type)
DataItem * dataItem()
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
T * item(const QString &tag) const
Definition: SessionItem.h:151
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.
SessionItem * itemForIndex(const QModelIndex &index) const
QModelIndex getIndexFromPath(const SessionModel *model, const QString &path)
Definition: ModelPath.cpp:36
QString getPathFromIndex(const QModelIndex &index)
Definition: ModelPath.cpp:19
std::string model_type
Definition: types.h:23