BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
gui2::DataViewModel Class Reference

View model for ExperimentalDataModel with drag-and-drop support. More...

Inheritance diagram for gui2::DataViewModel:
[legend]
Collaboration diagram for gui2::DataViewModel:
[legend]

Public Member Functions

 DataViewModel (ExperimentalDataModel *model, QObject *parent=nullptr)
 
void appendRow (ViewItem *parent, std::vector< std::unique_ptr< ViewItem >> items)
 Appends row of items to given parent. More...
 
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. More...
 
void clearRows (ViewItem *parent)
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
 
std::vector< ViewItem * > findViews (const ModelView::SessionItem *item) const
 Returns vector of all ViewItem's representing given SessionItem. More...
 
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. More...
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex indexFromItem (const ViewItem *item) const
 Returns the QModelIndex associated with the given item. More...
 
QModelIndexList indexOfSessionItem (const SessionItem *item) const
 Returns list of model indices representing given SessionItem. More...
 
void insertRow (ViewItem *parent, int row, std::vector< std::unique_ptr< ViewItem >> items)
 Insert a row of items at index 'row' to given parent. More...
 
ViewItemitemFromIndex (const QModelIndex &index) const
 Returns a pointer to the RefViewItem associated with the given index. More...
 
QMimeData * mimeData (const QModelIndexList &index_list) const override
 Generate the mime data for all selected items. More...
 
QModelIndex parent (const QModelIndex &child) const override
 
void removeRow (ViewItem *parent, int row)
 
ViewItemrootItem () const
 Returns a pointer to invisible root item. More...
 
SessionItemrootSessionItem ()
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
SessionItemsessionItemFromIndex (const QModelIndex &index) const
 
SessionModelsessionModel () const
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 
void setRootSessionItem (SessionItem *item)
 
Qt::DropActions supportedDragActions () const override
 Supported drag actions. More...
 
Qt::DropActions supportedDropActions () const override
 Supported drop actions. More...
 
ViewItemviewItemFromIndex (const QModelIndex &index) const
 

Private Member Functions

void setRootViewItem (std::unique_ptr< ViewItem > root_item)
 Sets new root item. Previous item will be deleted, model will be reset. More...
 

Private Attributes

std::unique_ptr< ViewModelControllerm_controller
 
std::unique_ptr< ViewModelBaseImplp_impl
 

Detailed Description

View model for ExperimentalDataModel with drag-and-drop support.

Definition at line 31 of file dataviewmodel.h.

Constructor & Destructor Documentation

◆ DataViewModel()

gui2::DataViewModel::DataViewModel ( ExperimentalDataModel model,
QObject *  parent = nullptr 
)

Definition at line 34 of file dataviewmodel.cpp.

36 {
37 }
View model to show top level items of SessionModel in Qt trees and tables.
QModelIndex parent(const QModelIndex &child) const override

Member Function Documentation

◆ appendRow()

void ViewModelBase::appendRow ( ViewItem parent,
std::vector< std::unique_ptr< ViewItem >>  items 
)
inherited

Appends row of items to given parent.

Definition at line 165 of file viewmodelbase.cpp.

166 {
167  insertRow(parent, parent->rowCount(), std::move(items));
168 }
void insertRow(ViewItem *parent, int row, std::vector< std::unique_ptr< ViewItem >> items)
Insert a row of items at index 'row' to given parent.

References ModelView::ViewModelBase::insertRow(), and ModelView::ViewModelBase::parent().

Referenced by ModelView::ViewModelController::ViewModelControllerImpl::iterate(), and TEST_F().

Here is the call graph for this function:

◆ canDropMimeData()

bool gui2::DataViewModel::canDropMimeData ( const QMimeData *  data,
Qt::DropAction  action,
int  row,
int  column,
const QModelIndex &  parent 
) const
override

Returns true if we can drop item here.

Definition at line 88 of file dataviewmodel.cpp.

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 }
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
SessionItem * sessionItemFromIndex(const QModelIndex &index) const
Definition: viewmodel.cpp:59
const std::string CanvasItemType

References gui2::Constants::CanvasItemType, ModelView::ViewModelBase::data(), ModelView::ViewModelBase::parent(), and ModelView::ViewModel::sessionItemFromIndex().

Referenced by dropMimeData().

Here is the call graph for this function:

◆ clearRows()

void ViewModelBase::clearRows ( ViewItem parent)
inherited

Definition at line 135 of file viewmodelbase.cpp.

136 {
137  if (!p_impl->item_belongs_to_model(parent))
138  throw std::runtime_error(
139  "Error in ViewModelBase: attempt to use parent from another model");
140 
141  if (!parent->rowCount())
142  return;
143 
144  beginRemoveRows(indexFromItem(parent), 0, parent->rowCount() - 1);
145  parent->clear();
146  endRemoveRows();
147 }
std::unique_ptr< ViewModelBaseImpl > p_impl
Definition: viewmodelbase.h:69
QModelIndex indexFromItem(const ViewItem *item) const
Returns the QModelIndex associated with the given item.

References ModelView::ViewModelBase::indexFromItem(), ModelView::ViewModelBase::p_impl, and ModelView::ViewModelBase::parent().

Referenced by ModelView::ViewModelController::ViewModelControllerImpl::remove_children_of_view(), and TEST_F().

Here is the call graph for this function:

◆ columnCount()

int ViewModelBase::columnCount ( const QModelIndex &  parent = QModelIndex()) const
overrideinherited

Definition at line 70 of file viewmodelbase.cpp.

71 {
72  auto parent_item = itemFromIndex(parent);
73  return parent_item ? parent_item->columnCount() : rootItem()->columnCount();
74 }
int columnCount() const
Returns the number of child item columns that the item has.
Definition: viewitem.cpp:120
ViewItem * rootItem() const
Returns a pointer to invisible root item.
ViewItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the RefViewItem associated with the given index.

References ModelView::ViewItem::columnCount(), ModelView::ViewModelBase::itemFromIndex(), ModelView::ViewModelBase::parent(), and ModelView::ViewModelBase::rootItem().

Referenced by ModelView::ViewModelBase::index(), and TEST_F().

Here is the call graph for this function:

◆ data()

QVariant ViewModelBase::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
overrideinherited

Definition at line 76 of file viewmodelbase.cpp.

77 {
78  if (!rootItem())
79  return QVariant();
80 
81  auto item = itemFromIndex(index);
82  return item ? item->data(role) : QVariant();
83 }
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override

References ModelView::ViewModelBase::index(), ModelView::ViewModelBase::itemFromIndex(), and ModelView::ViewModelBase::rootItem().

Referenced by canDropMimeData(), dropMimeData(), ModelView::ViewModel::headerData(), and TEST_F().

Here is the call graph for this function:

◆ dropMimeData()

bool gui2::DataViewModel::dropMimeData ( const QMimeData *  data,
Qt::DropAction  action,
int  row,
int  column,
const QModelIndex &  parent 
)
override

Definition at line 98 of file dataviewmodel.cpp.

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 }
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.
SessionModel * sessionModel() const
Definition: viewmodel.cpp:40
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.
MVVM_VIEW_EXPORT QStringList deserialize(const QByteArray &byteArray)
Converts byte array to vector of strings.

References canDropMimeData(), ModelView::ViewModelBase::data(), ModelView::Utils::deserialize(), ModelView::SessionModel::findItem(), ModelView::SessionModel::moveItem(), ModelView::ViewModelBase::parent(), ModelView::ViewModel::sessionItemFromIndex(), and ModelView::ViewModel::sessionModel().

Here is the call graph for this function:

◆ findViews()

std::vector< ViewItem * > ViewModel::findViews ( const ModelView::SessionItem item) const
inherited

Returns vector of all ViewItem's representing given SessionItem.

Definition at line 81 of file viewmodel.cpp.

82 {
83  return m_controller->findViews(item);
84 }
std::unique_ptr< ViewModelController > m_controller
Definition: viewmodel.h:54

References ModelView::ViewModel::m_controller.

Referenced by TEST_F().

◆ flags()

Qt::ItemFlags gui2::DataViewModel::flags ( const QModelIndex &  index) const
override

Return the Qt flags for given index. We allow GraphItem drag, they can be dropped on CanvasItem.

Definition at line 41 of file dataviewmodel.cpp.

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 }
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags for the given index.
const model_type GraphItemType
Definition: mvvm_types.h:53

References gui2::Constants::CanvasItemType, ModelView::ViewModelBase::flags(), ModelView::Constants::GraphItemType, ModelView::ViewModelBase::index(), and ModelView::ViewModel::sessionItemFromIndex().

Here is the call graph for this function:

◆ headerData()

QVariant ViewModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const
overrideinherited

Definition at line 30 of file viewmodel.cpp.

31 {
32  if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
33  auto data = m_controller->horizontalHeaderLabels();
34  if (section < data.size())
35  return data.at(section);
36  }
37  return QVariant();
38 }

References ModelView::ViewModelBase::data(), and ModelView::ViewModel::m_controller.

Here is the call graph for this function:

◆ index()

QModelIndex ViewModelBase::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const
overrideinherited

Definition at line 42 of file viewmodelbase.cpp.

43 {
44  auto parent_item = itemFromIndex(parent) ? itemFromIndex(parent) : rootItem();
45  const bool is_valid_row = row >= 0 && row < rowCount(parent);
46  const bool is_valid_column = column >= 0 && column < columnCount(parent);
47  return is_valid_row && is_valid_column
48  ? createIndex(row, column, parent_item->child(row, column))
49  : QModelIndex();
50 }
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override

References ModelView::ViewModelBase::columnCount(), ModelView::ViewModelBase::itemFromIndex(), ModelView::ViewModelBase::parent(), ModelView::ViewModelBase::rootItem(), and ModelView::ViewModelBase::rowCount().

Referenced by ModelView::ViewModelBase::data(), flags(), ModelView::ViewModelBase::flags(), ModelView::ViewModelBase::itemFromIndex(), ModelView::ViewModel::sessionItemFromIndex(), ModelView::ViewModelBase::setData(), TEST_F(), and ModelView::ViewModel::viewItemFromIndex().

Here is the call graph for this function:

◆ indexFromItem()

QModelIndex ViewModelBase::indexFromItem ( const ViewItem item) const
inherited

Returns the QModelIndex associated with the given item.

Definition at line 117 of file viewmodelbase.cpp.

118 {
119  return item && item->parent()
120  ? createIndex(item->row(), item->column(), const_cast<ViewItem*>(item))
121  : QModelIndex();
122 }
Represents the view of SessionItem's data in a single cell of ViewModel.
Definition: viewitem.h:29
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
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

References ModelView::ViewItem::column(), ModelView::ViewItem::parent(), and ModelView::ViewItem::row().

Referenced by ModelView::ViewModelBase::clearRows(), ModelView::ViewModel::indexOfSessionItem(), ModelView::ViewModelBase::insertRow(), ModelView::ViewModelBase::ViewModelBaseImpl::item_belongs_to_model(), ModelView::ViewModelBase::removeRow(), and TEST_F().

Here is the call graph for this function:

◆ indexOfSessionItem()

QModelIndexList ViewModel::indexOfSessionItem ( const SessionItem item) const
inherited

Returns list of model indices representing given SessionItem.

Definition at line 71 of file viewmodel.cpp.

72 {
73  QModelIndexList result;
74  for (auto view : m_controller->findViews(item))
75  result.push_back(indexFromItem(view));
76  return result;
77 }

References ModelView::ViewModelBase::indexFromItem(), and ModelView::ViewModel::m_controller.

Referenced by gui2::DataSelectionModel::selectItems(), gui2::LayerSelectionModel::selectItems(), gui2::MaterialSelectionModel::selectItems(), and TEST_F().

Here is the call graph for this function:

◆ insertRow()

void ViewModelBase::insertRow ( ViewItem parent,
int  row,
std::vector< std::unique_ptr< ViewItem >>  items 
)
inherited

Insert a row of items at index 'row' to given parent.

Definition at line 151 of file viewmodelbase.cpp.

153 {
154  if (!p_impl->item_belongs_to_model(parent))
155  throw std::runtime_error(
156  "Error in ViewModelBase: attempt to use parent from another model");
157 
158  beginInsertRows(indexFromItem(parent), row, row);
159  parent->insertRow(row, std::move(items));
160  endInsertRows();
161 }

References ModelView::ViewModelBase::indexFromItem(), ModelView::ViewModelBase::p_impl, and ModelView::ViewModelBase::parent().

Referenced by ModelView::ViewModelBase::appendRow(), ModelView::ViewModelController::ViewModelControllerImpl::insert_view(), and TEST_F().

Here is the call graph for this function:

◆ itemFromIndex()

ViewItem * ViewModelBase::itemFromIndex ( const QModelIndex &  index) const
inherited

Returns a pointer to the RefViewItem associated with the given index.

If index is invalid, returns nullptr.

Definition at line 110 of file viewmodelbase.cpp.

111 {
112  return index.isValid() ? static_cast<ViewItem*>(index.internalPointer()) : nullptr;
113 }

References ModelView::ViewModelBase::index().

Referenced by ModelView::ViewModelBase::columnCount(), ModelView::ViewModelBase::data(), ModelView::ViewModelController::ViewModelControllerImpl::findViews(), ModelView::ViewModelBase::flags(), ModelView::ViewModelBase::index(), ModelView::ViewModelBase::parent(), ModelView::ViewModelBase::rowCount(), ModelView::ViewModel::sessionItemFromIndex(), ModelView::ViewModelBase::setData(), TEST_F(), and ModelView::ViewModel::viewItemFromIndex().

Here is the call graph for this function:

◆ mimeData()

QMimeData * gui2::DataViewModel::mimeData ( const QModelIndexList &  index_list) const
override

Generate the mime data for all selected items.

Definition at line 57 of file dataviewmodel.cpp.

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 }
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.

References ModelView::Utils::serialize(), and ModelView::Utils::UniqueItemsFromIndex().

Here is the call graph for this function:

◆ parent()

QModelIndex ViewModelBase::parent ( const QModelIndex &  child) const
overrideinherited

Definition at line 52 of file viewmodelbase.cpp.

53 {
54  if (auto child_item = itemFromIndex(child); child_item) {
55  auto parent_item = child_item->parent();
56  return parent_item == rootItem()
57  ? QModelIndex()
58  : createIndex(parent_item->row(), parent_item->column(), parent_item);
59  }
60 
61  return QModelIndex();
62 }

References ModelView::ViewModelBase::itemFromIndex(), and ModelView::ViewModelBase::rootItem().

Referenced by ModelView::ViewModelBase::appendRow(), canDropMimeData(), ModelView::ViewModelBase::clearRows(), ModelView::ViewModelBase::columnCount(), dropMimeData(), ModelView::ViewModelBase::index(), ModelView::ViewModelBase::insertRow(), ModelView::ViewModelBase::removeRow(), ModelView::ViewModelBase::rowCount(), and TEST_F().

Here is the call graph for this function:

◆ removeRow()

void ViewModelBase::removeRow ( ViewItem parent,
int  row 
)
inherited

Definition at line 124 of file viewmodelbase.cpp.

125 {
126  if (!p_impl->item_belongs_to_model(parent))
127  throw std::runtime_error(
128  "Error in ViewModelBase: attempt to use parent from another model");
129 
130  beginRemoveRows(indexFromItem(parent), row, row);
131  parent->removeRow(row);
132  endRemoveRows();
133 }

References ModelView::ViewModelBase::indexFromItem(), ModelView::ViewModelBase::p_impl, and ModelView::ViewModelBase::parent().

Referenced by ModelView::ViewModelController::ViewModelControllerImpl::remove_row_of_views(), and TEST_F().

Here is the call graph for this function:

◆ rootItem()

◆ rootSessionItem()

SessionItem * ViewModel::rootSessionItem ( )
inherited

Definition at line 45 of file viewmodel.cpp.

46 {
47  return m_controller->rootSessionItem();
48 }

References ModelView::ViewModel::m_controller.

◆ rowCount()

int ViewModelBase::rowCount ( const QModelIndex &  parent = QModelIndex()) const
overrideinherited

Definition at line 64 of file viewmodelbase.cpp.

65 {
66  auto parent_item = itemFromIndex(parent);
67  return parent_item ? parent_item->rowCount() : rootItem()->rowCount();
68 }
int rowCount() const
Returns the number of child item rows that the item has.
Definition: viewitem.cpp:113

References ModelView::ViewModelBase::itemFromIndex(), ModelView::ViewModelBase::parent(), ModelView::ViewModelBase::rootItem(), and ModelView::ViewItem::rowCount().

Referenced by ModelView::ViewModelBase::index(), and TEST_F().

Here is the call graph for this function:

◆ sessionItemFromIndex()

SessionItem * ViewModel::sessionItemFromIndex ( const QModelIndex &  index) const
inherited

Definition at line 59 of file viewmodel.cpp.

60 {
61  return index.isValid() ? itemFromIndex(index)->item() : m_controller->rootSessionItem();
62 }
SessionItem * item() const
Definition: viewitem.cpp:168

References ModelView::ViewModelBase::index(), ModelView::ViewItem::item(), ModelView::ViewModelBase::itemFromIndex(), and ModelView::ViewModel::m_controller.

Referenced by canDropMimeData(), dropMimeData(), flags(), and TEST_F().

Here is the call graph for this function:

◆ sessionModel()

SessionModel * ViewModel::sessionModel ( ) const
inherited

Definition at line 40 of file viewmodel.cpp.

41 {
42  return m_controller->sessionModel();
43 }

References ModelView::ViewModel::m_controller.

Referenced by ModelView::ViewModel::ViewModel(), and dropMimeData().

◆ setData()

bool ViewModelBase::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role 
)
overrideinherited

Definition at line 85 of file viewmodelbase.cpp.

86 {
87  if (!index.isValid())
88  return false;
89 
90  if (auto item = itemFromIndex(index); item) {
91  bool result = item->setData(value, role);
92  if (result)
93  dataChanged(index, index, QVector<int>() << role);
94  return result;
95  }
96 
97  return false;
98 }

References ModelView::ViewModelBase::index(), and ModelView::ViewModelBase::itemFromIndex().

Referenced by TEST_F().

Here is the call graph for this function:

◆ setRootSessionItem()

void ViewModel::setRootSessionItem ( SessionItem item)
inherited

Definition at line 52 of file viewmodel.cpp.

53 {
54  if (!item)
55  throw std::runtime_error("Error in ViewModel: atttemp to set nulptr as root item");
56  m_controller->setRootSessionItem(item);
57 }

References ModelView::ViewModel::m_controller.

Referenced by gui2::ImportDataEditor::ImportDataEditor(), ModelView::PropertyTreeView::setItem(), and TEST_F().

◆ setRootViewItem()

void ViewModelBase::setRootViewItem ( std::unique_ptr< ViewItem root_item)
privateinherited

Sets new root item. Previous item will be deleted, model will be reset.

Definition at line 182 of file viewmodelbase.cpp.

183 {
184  p_impl->root = std::move(root_item);
185 }

References ModelView::ViewModelBase::p_impl.

Referenced by ModelView::ViewModelBase::ViewModelBase(), and ModelView::ViewModelController::ViewModelControllerImpl::setRootSessionItemIntern().

◆ supportedDragActions()

Qt::DropActions gui2::DataViewModel::supportedDragActions ( ) const
override

Supported drag actions.

Definition at line 74 of file dataviewmodel.cpp.

75 {
76  return Qt::TargetMoveAction;
77 }

◆ supportedDropActions()

Qt::DropActions gui2::DataViewModel::supportedDropActions ( ) const
override

Supported drop actions.

Definition at line 81 of file dataviewmodel.cpp.

82 {
83  return Qt::TargetMoveAction;
84 }

◆ viewItemFromIndex()

ViewItem * ViewModel::viewItemFromIndex ( const QModelIndex &  index) const
inherited

Definition at line 64 of file viewmodel.cpp.

65 {
66  return itemFromIndex(index);
67 }

References ModelView::ViewModelBase::index(), and ModelView::ViewModelBase::itemFromIndex().

Here is the call graph for this function:

Member Data Documentation

◆ m_controller

◆ p_impl


The documentation for this class was generated from the following files: