BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewmodelutils.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/viewmodelutils.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 
18 #include "mvvm/model/itemutils.h"
19 #include "mvvm/model/mvvm_types.h"
20 #include "mvvm/model/sessionitem.h"
23 #include <QStandardItemModel>
24 #include <iterator>
25 #include <set>
26 
27 using namespace ModelView;
28 
29 void Utils::iterate_model(const QAbstractItemModel* model, const QModelIndex& parent,
30  const std::function<void(const QModelIndex& child)>& fun)
31 {
32  if (!model)
33  return;
34 
35  for (int row = 0; row < model->rowCount(parent); ++row) {
36  for (int col = 0; col < model->columnCount(parent); ++col) {
37  auto index = model->index(row, col, parent);
38  if (index.isValid())
39  fun(index);
40  }
41  for (int col = 0; col < model->columnCount(parent); ++col) {
42  auto index = model->index(row, col, parent);
43  iterate_model(model, index, fun);
44  }
45  }
46 }
47 
48 //! Translates SessionItem's data role to vector of Qt roles.
49 
50 QVector<int> Utils::ItemRoleToQtRole(int role)
51 {
52  QVector<int> result;
53  // In Qt when we are editing the data in a view two roles are emmited.
54  if (role == ItemDataRole::DISPLAY || role == ItemDataRole::DATA)
55  result = {Qt::DisplayRole, Qt::EditRole};
56  else if (role == ItemDataRole::APPEARANCE)
57 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
58  result = {Qt::ForegroundRole};
59 #else
60  result = {Qt::TextColorRole};
61 #endif
62  else if (role == ItemDataRole::TOOLTIP)
63  result = {Qt::ToolTipRole};
64 
65  return result;
66 }
67 
68 QVariant Utils::TextColorRole(const SessionItem& item)
69 {
70  return item.isEnabled() ? QVariant() : QColor(Qt::gray);
71 }
72 
73 QVariant Utils::CheckStateRole(const SessionItem& item)
74 {
75  auto value = item.data<QVariant>();
76  if (Utils::IsBoolVariant(value))
77  return value.value<bool>() ? Qt::Checked : Qt::Unchecked;
78  return QVariant();
79 }
80 
81 QVariant Utils::DecorationRole(const SessionItem& item)
82 {
83  auto value = item.data<QVariant>();
84  if (Utils::IsColorVariant(value))
85  return value;
86  else if (Utils::IsExtPropertyVariant(value))
87  return value.value<ExternalProperty>().color();
88  return QVariant();
89 }
90 
91 QVariant Utils::ToolTipRole(const SessionItem& item)
92 {
93  return item.hasData(ItemDataRole::TOOLTIP) ? Variant(QString::fromStdString(item.toolTip()))
94  : QVariant();
95 }
96 
97 std::vector<SessionItem*> Utils::ItemsFromIndex(const QModelIndexList& index_list)
98 {
99  if (index_list.empty())
100  return {};
101 
102  std::vector<SessionItem*> result;
103 
104  if (auto model = dynamic_cast<const ViewModelBase*>(index_list.front().model()))
105  std::transform(index_list.begin(), index_list.end(), std::back_inserter(result),
106  [model](auto index) { return model->itemFromIndex(index)->item(); });
107 
108  return result;
109 }
110 
111 std::vector<SessionItem*> Utils::UniqueItemsFromIndex(const QModelIndexList& index_list)
112 {
113  return Utils::UniqueItems(Utils::ItemsFromIndex(index_list));
114 }
115 
116 std::vector<SessionItem*> Utils::ParentItemsFromIndex(const QModelIndexList& index_list)
117 {
118  std::set<SessionItem*> unique_parents;
119  for (auto item : ItemsFromIndex(index_list))
120  if (item)
121  unique_parents.insert(item->parent());
122 
123  std::vector<SessionItem*> result;
124  std::copy(unique_parents.begin(), unique_parents.end(), std::back_inserter(result));
125  return result;
126 }
Property to carry text, color and identifier.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
bool hasData(int role=ItemDataRole::DATA) const
Returns true if item has data on board with given role.
bool isEnabled() const
Returns true if this item has enabled flag set.
std::string toolTip() const
Returns item tooltip, if exists.
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
Base class for all view models to show content of SessionModel in Qt views.
Definition: viewmodelbase.h:31
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const int TOOLTIP
tooltip for item's data
Definition: mvvm_types.h:34
const int DATA
main data role
Definition: mvvm_types.h:30
const int APPEARANCE
appearance flag
Definition: mvvm_types.h:32
const int DISPLAY
display name
Definition: mvvm_types.h:31
MVVM_VIEWMODEL_EXPORT QVariant TextColorRole(const SessionItem &item)
Returns text color for given item.
MVVM_VIEWMODEL_EXPORT QVariant DecorationRole(const SessionItem &item)
Returns decoration role for given item.
MVVM_VIEWMODEL_EXPORT QVariant ToolTipRole(const SessionItem &item)
Returns tooltip role for given item.
MVVM_VIEWMODEL_EXPORT QVector< int > ItemRoleToQtRole(int role)
Returns vector of Qt roles corresponding to given ItemDataRole.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list.
MVVM_VIEWMODEL_EXPORT void iterate_model(const QAbstractItemModel *model, const QModelIndex &parent, const std::function< void(const QModelIndex &child)> &fun)
Iterates through QAbstractItem model.
MVVM_MODEL_EXPORT std::vector< SessionItem * > UniqueItems(const std::vector< SessionItem * > &items)
Returns vector with duplicates and 'nullptr' filtered out.
Definition: itemutils.cpp:170
MVVM_VIEWMODEL_EXPORT QVariant CheckStateRole(const SessionItem &item)
Returns check state role of given item.
MVVM_MODEL_EXPORT bool IsColorVariant(const Variant &variant)
Returns true in the case of QColor 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_VIEWMODEL_EXPORT std::vector< SessionItem * > UniqueItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list. Removes repetitions.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ParentItemsFromIndex(const QModelIndexList &index_list)
Returns vector of parent items from given index list.
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.
Defines class CLASS?
QVariant Variant
Definition: variant.h:23
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?