BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterTuningModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Model/ParameterTuningModel.cpp
6 //! @brief Implements class ParameterTuningModel
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QMimeData>
20 
21 ParameterTuningModel::ParameterTuningModel(QObject* rootObject, QObject* parent)
22  : QAbstractItemModel(parent)
23  , m_rootObject(rootObject)
24 {
25 }
26 
27 QVariant ParameterTuningModel::headerData(int section, Qt::Orientation orientation, int role) const
28 {
29  if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
30  return {};
31  return (section == 0) ? "Name" : "Value";
32 }
33 
34 QVariant ParameterTuningModel::data(const QModelIndex& index, int role) const
35 {
36  if (!index.isValid())
37  return {};
38 
39  if (const auto* label = toParameterLabelItem(index)) {
40  if (role == Qt::DisplayRole && index.column() == 0)
41  return label->title();
42  return {};
43  }
44 
45  if (auto var = toParameterItem(index)) {
46  if (role == Qt::DisplayRole || role == Qt::EditRole) {
47  if (index.column() == 0)
48  return var->title();
49  return var->valueOfLink();
50  }
51  return {};
52  }
53 
54  return {};
55 }
56 
57 Qt::ItemFlags ParameterTuningModel::flags(const QModelIndex& index) const
58 {
59  Qt::ItemFlags result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
60  if (toParameterItem(index)) {
61  result |= Qt::ItemIsDragEnabled;
62  if (index.column() == 1)
63  result |= Qt::ItemIsEditable;
64  }
65  return result;
66 }
67 
68 QModelIndex ParameterTuningModel::index(int row, int column, const QModelIndex& parent) const
69 {
70  if (!hasIndex(row, column, parent))
71  return {};
72 
73  if (!parent.isValid())
74  return createIndex(row, column, m_rootObject->children()[row]);
75 
76  if (auto label = toParameterLabelItem(parent))
77  return createIndex(row, column, label->children()[row]);
78 
79  return {};
80 }
81 
82 QModelIndex ParameterTuningModel::parent(const QModelIndex& index) const
83 {
84  if (!index.isValid())
85  return {};
86 
87  QObject* item = static_cast<QObject*>(index.internalPointer());
88  if (item->parent() == m_rootObject)
89  return {};
90 
91  const int row = item->parent()->parent()->children().indexOf(item->parent());
92  return createIndex(row, 0, item->parent());
93 }
94 
95 int ParameterTuningModel::rowCount(const QModelIndex& index) const
96 {
97  if (index.column() > 0)
98  return 0;
99 
100  if (!index.isValid())
101  return m_rootObject->children().size();
102 
103  QObject* item = static_cast<QObject*>(index.internalPointer());
104  return item->children().size();
105 }
106 
107 int ParameterTuningModel::columnCount(const QModelIndex&) const
108 {
109  return 2;
110 }
111 
112 QMimeData* ParameterTuningModel::mimeData(const QModelIndexList& indexes) const
113 {
114  auto* mimeData = new QMimeData();
115 
116  for (auto index : indexes) {
117  if (ParameterItem* parameterItem = toParameterItem(index)) {
118  QByteArray data;
119  data.setNum(reinterpret_cast<qlonglong>(parameterItem));
121  break;
122  }
123  }
124  return mimeData;
125 }
126 
128 {
129  return Qt::CopyAction;
130 }
131 
133 {
134  return Qt::IgnoreAction;
135 }
136 
138 {
139  return toParameterItem(index);
140 }
141 
143 {
144  if (item == nullptr)
145  return {};
146 
147  if (item->parent()) {
148  const int row = item->parent()->children().indexOf(item);
149  return createIndex(row, 0, item);
150  }
151 
152  return {};
153 }
154 
156 {
157  QObject* item = static_cast<QObject*>(index.internalPointer());
158  return dynamic_cast<ParameterItem*>(item);
159 }
160 
162 {
163  QObject* item = static_cast<QObject*>(index.internalPointer());
164  return dynamic_cast<ParameterLabelItem*>(item);
165 }
Defines namespace FitParameterHelper.
Defines classes for ParameterTreeItems.
Defines class ParameterTuningModel.
Defines reader and writer classes for SessionModel.
The ParameterItem class represent a tuning value in a parameter tuning tree.
ParameterTreeItems is a collection of items necessary to form a tuning tree for real time widget.
QModelIndex indexForItem(ParameterItem *item) const
Qt::DropActions supportedDropActions() const override
ParameterTuningModel(QObject *rootObject, QObject *parent=nullptr)
static ParameterLabelItem * toParameterLabelItem(const QModelIndex &index)
static ParameterItem * toParameterItem(const QModelIndex &index)
Qt::ItemFlags flags(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
ParameterItem * getParameterItem(const QModelIndex &index) const
Returns ParameterItem from given index.
QMimeData * mimeData(const QModelIndexList &indexes) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Qt::DropActions supportedDragActions() const override
QModelIndex parent(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
constexpr auto LinkMimeType
Definition: SessionXML.h:31