BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
dataviewmodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/importdataview/dataviewmodel.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 Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
21 #include <QByteArray>
22 #include <QDataStream>
23 #include <QMimeData>
24 
25 using namespace ModelView;
26 
27 namespace {
28 const std::string ExperimentalDataMimeType = "darefl/ExperimentalDataMime";
29 
30 } // namespace
31 
32 namespace gui2 {
33 
34 DataViewModel::DataViewModel(ExperimentalDataModel* model, QObject* parent)
35  : ModelView::TopItemsViewModel(model, parent)
36 {
37 }
38 
39 //! Return the Qt flags for given index. We allow GraphItem drag, they can be dropped on CanvasItem.
40 
41 Qt::ItemFlags DataViewModel::flags(const QModelIndex& index) const
42 {
43  Qt::ItemFlags result = ViewModel::flags(index);
44 
45  if (auto item = sessionItemFromIndex(index); item) {
46  if (item->modelType() == ModelView::Constants::GraphItemType)
47  result |= Qt::ItemIsDragEnabled;
48  else if (item->modelType() == Constants::CanvasItemType)
49  result |= Qt::ItemIsDropEnabled;
50  }
51 
52  return result;
53 }
54 
55 //! Generate the mime data for all selected items.
56 
57 QMimeData* DataViewModel::mimeData(const QModelIndexList& index_list) const
58 {
59  auto result = new QMimeData;
60 
61  // Get the list of the identifiers
62  QStringList identifiers;
63  for (auto item : Utils::UniqueItemsFromIndex(index_list))
64  identifiers.append(QString::fromStdString(item->identifier()));
65 
66  result->setData(QString::fromStdString(ExperimentalDataMimeType),
67  Utils::serialize(identifiers));
68 
69  return result;
70 }
71 
72 //! Supported drag actions.
73 
74 Qt::DropActions DataViewModel::supportedDragActions() const
75 {
76  return Qt::TargetMoveAction;
77 }
78 
79 //! Supported drop actions.
80 
81 Qt::DropActions DataViewModel::supportedDropActions() const
82 {
83  return Qt::TargetMoveAction;
84 }
85 
86 //! Returns true if we can drop item here.
87 
88 bool DataViewModel::canDropMimeData(const QMimeData* data, Qt::DropAction, int, int,
89  const QModelIndex& parent) const
90 {
91  if (data->hasFormat(QString::fromStdString(ExperimentalDataMimeType)))
92  if (auto target = sessionItemFromIndex(parent); target)
93  return target->modelType() == Constants::CanvasItemType;
94 
95  return false;
96 }
97 
98 bool DataViewModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column,
99  const QModelIndex& parent)
100 {
101  if (!canDropMimeData(data, action, row, column, parent))
102  return false;
103 
104  auto target = sessionItemFromIndex(parent);
105 
106  int requested_row = parent.isValid() ? parent.row() : row;
107 
108  // retrieving list of item identifiers and accessing items
109  auto identifiers =
110  Utils::deserialize(data->data(QString::fromStdString(ExperimentalDataMimeType)));
111  for (auto id : identifiers) {
112  auto item = sessionModel()->findItem(id.toStdString());
113 
114  int row = std::clamp(requested_row, 0, item->parent()->itemCount(item->tagRow().tag) - 1);
115  sessionModel()->moveItem(item, target, {"", row});
116  }
117 
118  return true;
119 }
120 
121 } // namespace gui2
void moveItem(SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
Move item from it's current parent to a new parent under given tag and row.
SessionItem * findItem(const identifier_type &id)
Returns SessionItem for given identifier.
View model to show top level items of SessionModel in Qt trees and tables.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags for the given index.
QModelIndex parent(const QModelIndex &child) const override
SessionModel * sessionModel() const
Definition: viewmodel.cpp:40
SessionItem * sessionItemFromIndex(const QModelIndex &index) const
Definition: viewmodel.cpp:59
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
Returns true if we can drop item here.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Qt::DropActions supportedDropActions() const override
Supported drop actions.
Qt::DropActions supportedDragActions() const override
Supported drag actions.
Qt::ItemFlags flags(const QModelIndex &index) const override
Return the Qt flags for given index. We allow GraphItem drag, they can be dropped on CanvasItem.
QMimeData * mimeData(const QModelIndexList &index_list) const override
Generate the mime data for all selected items.
The model to store imported reflectometry data.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const model_type GraphItemType
Definition: mvvm_types.h:53
MVVM_VIEW_EXPORT QStringList deserialize(const QByteArray &byteArray)
Converts byte array to vector of strings.
MVVM_VIEW_EXPORT QByteArray serialize(const QStringList &data)
Converts vector of strings to byte array.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > UniqueItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list. Removes repetitions.
materialitems.h Collection of materials to populate MaterialModel.
const std::string CanvasItemType
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?