BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewitem.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/viewitem.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 
17 #include "mvvm/model/sessionitem.h"
20 #include <algorithm>
21 #include <stdexcept>
22 #include <vector>
23 
24 using namespace ModelView;
25 
27  std::vector<std::unique_ptr<ViewItem>> children; //! buffer to hold rows x columns
28  int rows{0};
29  int columns{0};
30  SessionItem* item{nullptr};
31  int role{0};
34 
35  void appendRow(std::vector<std::unique_ptr<ViewItem>> items)
36  {
37  insertRow(rows, std::move(items));
38  }
39 
40  void insertRow(int row, std::vector<std::unique_ptr<ViewItem>> items)
41  {
42  if (items.empty())
43  throw std::runtime_error("Error in ViewItemImpl: attempt to insert empty row");
44 
45  if (columns > 0 && items.size() != static_cast<size_t>(columns))
46  throw std::runtime_error("Error in ViewItemImpl: wrong number of columns.");
47 
48  if (row < 0 || row > rows)
49  throw std::runtime_error("Error in ViewItemImpl: invalid row index.");
50 
51  children.insert(std::next(children.begin(), row * columns),
52  std::make_move_iterator(items.begin()),
53  std::make_move_iterator(items.end()));
54 
55  columns = static_cast<int>(items.size());
56  ++rows;
57  }
58 
59  void removeRow(int row)
60  {
61  if (row < 0 || row >= rows)
62  throw std::runtime_error("Error in RefViewItem: invalid row index.");
63 
64  auto begin = std::next(children.begin(), row * columns);
65  auto end = std::next(begin, columns);
66  children.erase(begin, end);
67  --rows;
68  if (rows == 0)
69  columns = 0;
70  }
71 
72  ViewItem* child(int row, int column) const
73  {
74  if (row < 0 || row >= rows)
75  throw std::runtime_error("Error in RefViewItem: wrong row)");
76 
77  if (column < 0 || column >= columns)
78  throw std::runtime_error("Error in RefViewItem: wrong column)");
79 
80  return children.at(static_cast<size_t>(column + row * columns)).get();
81  }
82 
84 
86  {
87  return Utils::IndexOfItem(children.begin(), children.end(), child);
88  }
89 
90  //! Returns item data associated with this RefViewItem.
91 
92  QVariant data() const { return item ? item->data<QVariant>(role) : QVariant(); }
93 
94  //! Returns vector of children.
95 
96  std::vector<ViewItem*> get_children() const
97  {
98  std::vector<ViewItem*> result;
99  std::transform(children.begin(), children.end(), std::back_inserter(result),
100  [](const auto& x) { return x.get(); });
101  return result;
102  }
103 };
104 
105 ViewItem::ViewItem(SessionItem* item, int role) : p_impl(std::make_unique<ViewItemImpl>(item, role))
106 {
107 }
108 
109 ViewItem::~ViewItem() = default;
110 
111 //! Returns the number of child item rows that the item has.
112 
114 {
115  return p_impl->rows;
116 }
117 
118 //! Returns the number of child item columns that the item has.
119 
121 {
122  return p_impl->columns;
123 }
124 
125 //! Appends a row containing items. Number of items should be the same as columnCount()
126 //! (if there are already some rows). If it is a first row, then items can be of any size.
127 
128 void ViewItem::appendRow(std::vector<std::unique_ptr<ViewItem>> items)
129 {
130  for (auto& x : items)
131  x->setParent(this);
132  p_impl->appendRow(std::move(items));
133 }
134 
135 //! Insert a row of items at index 'row'.
136 
137 void ViewItem::insertRow(int row, std::vector<std::unique_ptr<ViewItem>> items)
138 {
139  for (auto& x : items)
140  x->setParent(this);
141  p_impl->insertRow(row, std::move(items));
142 }
143 
144 //! Removes row of items at given 'row'. Items will be deleted.
145 
146 void ViewItem::removeRow(int row)
147 {
148  p_impl->removeRow(row);
149 }
150 
152 {
153  p_impl->children.clear();
154  p_impl->rows = 0;
155  p_impl->columns = 0;
156 }
157 
159 {
160  return p_impl->parent();
161 }
162 
163 ViewItem* ViewItem::child(int row, int column) const
164 {
165  return p_impl->child(row, column);
166 }
167 
169 {
170  return p_impl->item;
171 }
172 
174 {
175  return p_impl->role;
176 }
177 
178 //! Returns the row where the item is located in its parent's child table, or -1 if the item has no
179 //! parent.
180 
181 int ViewItem::row() const
182 {
183  auto index = parent() ? parent()->p_impl->index_of_child(this) : -1;
184  return index >= 0 ? index / parent()->p_impl->columns : -1;
185 }
186 
187 //! Returns the column where the item is located in its parent's child table, or -1 if the item has
188 //! no parent.
189 
190 int ViewItem::column() const
191 {
192  auto index = parent() ? parent()->p_impl->index_of_child(this) : -1;
193  return index >= 0 ? index % parent()->p_impl->columns : -1;
194 }
195 
196 //! Returns the data for given role according to Qt::ItemDataRole namespace definitions.
197 //! Converts data and roles from underlying SessionItem to what Qt expects.
198 
199 QVariant ViewItem::data(int qt_role) const
200 {
201  if (!p_impl->item)
202  return QVariant();
203 
204  if (qt_role == Qt::DisplayRole || qt_role == Qt::EditRole)
205  return Utils::toQtVariant(p_impl->data());
206 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
207  else if (qt_role == Qt::ForegroundRole)
208 #else
209  else if (qt_role == Qt::TextColorRole)
210 #endif
211  return Utils::TextColorRole(*p_impl->item);
212  else if (qt_role == Qt::ToolTipRole)
213  return Utils::ToolTipRole(*p_impl->item);
214  else
215  return QVariant();
216 }
217 
218 //! Sets the data to underlying SessionItem.
219 //! Converts data and roles from Qt definitions to what SessionItem expects.
220 
221 bool ViewItem::setData(const QVariant& value, int qt_role)
222 {
223  if (p_impl->item && qt_role == Qt::EditRole)
224  return p_impl->item->setData(Utils::toCustomVariant(value), p_impl->role);
225  return false;
226 }
227 
228 //! Returns Qt's item flags.
229 //! Converts internal SessionItem's status enable/disabled/readonly to what Qt expects.
230 
231 Qt::ItemFlags ViewItem::flags() const
232 {
233  Qt::ItemFlags result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
234  return result;
235 }
236 
237 std::vector<ViewItem*> ViewItem::children() const
238 {
239  return p_impl->get_children();
240 }
241 
243 {
244  p_impl->parent_view_item = parent;
245 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
Represents the view of SessionItem's data in a single cell of ViewModel.
Definition: viewitem.h:29
int columnCount() const
Returns the number of child item columns that the item has.
Definition: viewitem.cpp:120
int row() const
Returns the row where the item is located in its parent's child table, or -1 if the item has no paren...
Definition: viewitem.cpp:181
ViewItem * child(int row, int column) const
Definition: viewitem.cpp:163
virtual QVariant data(int qt_role) const
Returns the data for given role according to Qt::ItemDataRole namespace definitions.
Definition: viewitem.cpp:199
void setParent(ViewItem *parent)
Definition: viewitem.cpp:242
ViewItem(SessionItem *item, int role)
Definition: viewitem.cpp:105
int item_role() const
Definition: viewitem.cpp:173
void insertRow(int row, std::vector< std::unique_ptr< ViewItem >> items)
Insert a row of items at index 'row'.
Definition: viewitem.cpp:137
std::vector< ViewItem * > children() const
Definition: viewitem.cpp:237
void appendRow(std::vector< std::unique_ptr< ViewItem >> items)
Appends a row containing items.
Definition: viewitem.cpp:128
virtual Qt::ItemFlags flags() const
Returns Qt's item flags.
Definition: viewitem.cpp:231
int rowCount() const
Returns the number of child item rows that the item has.
Definition: viewitem.cpp:113
int column() const
Returns the column where the item is located in its parent's child table, or -1 if the item has no pa...
Definition: viewitem.cpp:190
ViewItem * parent() const
Definition: viewitem.cpp:158
virtual bool setData(const QVariant &value, int qt_role)
Sets the data to underlying SessionItem.
Definition: viewitem.cpp:221
void removeRow(int row)
Removes row of items at given 'row'. Items will be deleted.
Definition: viewitem.cpp:146
std::unique_ptr< ViewItemImpl > p_impl
Definition: viewitem.h:70
SessionItem * item() const
Definition: viewitem.cpp:168
Defines class CLASS?
Defines class CLASS?
MVVM_VIEWMODEL_EXPORT QVariant TextColorRole(const SessionItem &item)
Returns text color for given item.
MVVM_VIEWMODEL_EXPORT QVariant ToolTipRole(const SessionItem &item)
Returns tooltip role for given item.
MVVM_MODEL_EXPORT Variant toCustomVariant(const Variant &standard)
Converts Qt variant to custom variant on board of SessionItem.
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
MVVM_MODEL_EXPORT Variant toQtVariant(const Variant &custom)
Converts custom variant to standard variant which Qt views will understand.
materialitems.h Collection of materials to populate MaterialModel.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.
Definition: filesystem.h:81
Defines class CLASS?
void appendRow(std::vector< std::unique_ptr< ViewItem >> items)
Definition: viewitem.cpp:35
QVariant data() const
Returns item data associated with this RefViewItem.
Definition: viewitem.cpp:92
int index_of_child(const ViewItem *child)
Definition: viewitem.cpp:85
std::vector< std::unique_ptr< ViewItem > > children
Definition: viewitem.cpp:27
int rows
buffer to hold rows x columns
Definition: viewitem.cpp:28
ViewItemImpl(SessionItem *item, int role)
Definition: viewitem.cpp:33
ViewItem * child(int row, int column) const
Definition: viewitem.cpp:72
std::vector< ViewItem * > get_children() const
Returns vector of children.
Definition: viewitem.cpp:96
void insertRow(int row, std::vector< std::unique_ptr< ViewItem >> items)
Definition: viewitem.cpp:40
Defines class CLASS?
Defines class CLASS?