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

Base class for all view models to show content of SessionModel in Qt views. More...

Inheritance diagram for ModelView::ViewModelBase:
[legend]
Collaboration diagram for ModelView::ViewModelBase:
[legend]

Classes

struct  ViewModelBaseImpl
 

Public Member Functions

 ViewModelBase (QObject *parent=nullptr)
 
 ~ViewModelBase () override
 
void appendRow (ViewItem *parent, std::vector< std::unique_ptr< ViewItem >> items)
 Appends row of items to given parent. More...
 
void clearRows (ViewItem *parent)
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 Returns the item flags for the given index. More...
 
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...
 
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...
 
QModelIndex parent (const QModelIndex &child) const override
 
void removeRow (ViewItem *parent, int row)
 
ViewItemrootItem () const
 Returns a pointer to invisible root item. More...
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 

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< ViewModelBaseImplp_impl
 

Friends

class ViewModelController
 

Detailed Description

Base class for all view models to show content of SessionModel in Qt views.

ViewModelBase is made of ViewItems, where each ViewItem represents some concrete data role of SessionItem. ViewModelBase doesn't have own logic and needs ViewModelController to listen for SessionModel changes.

Definition at line 31 of file viewmodelbase.h.

Constructor & Destructor Documentation

◆ ViewModelBase()

ViewModelBase::ViewModelBase ( QObject *  parent = nullptr)
explicit

Definition at line 32 of file viewmodelbase.cpp.

33  : QAbstractItemModel(parent), p_impl(std::make_unique<ViewModelBaseImpl>(this))
34 {
35  beginResetModel();
36  setRootViewItem(std::make_unique<RootViewItem>(nullptr));
37  endResetModel();
38 }
std::unique_ptr< ViewModelBaseImpl > p_impl
Definition: viewmodelbase.h:69
QModelIndex parent(const QModelIndex &child) const override
void setRootViewItem(std::unique_ptr< ViewItem > root_item)
Sets new root item. Previous item will be deleted, model will be reset.

References setRootViewItem().

Here is the call graph for this function:

◆ ~ViewModelBase()

ViewModelBase::~ViewModelBase ( )
overridedefault

Member Function Documentation

◆ appendRow()

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

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 insertRow(), and parent().

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

Here is the call graph for this function:

◆ clearRows()

void ViewModelBase::clearRows ( ViewItem parent)

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 }
QModelIndex indexFromItem(const ViewItem *item) const
Returns the QModelIndex associated with the given item.

References indexFromItem(), p_impl, and 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
override

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(), itemFromIndex(), parent(), and rootItem().

Referenced by index(), and TEST_F().

Here is the call graph for this function:

◆ data()

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

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 index(), itemFromIndex(), and rootItem().

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

Here is the call graph for this function:

◆ flags()

Qt::ItemFlags ViewModelBase::flags ( const QModelIndex &  index) const
override

Returns the item flags for the given index.

Definition at line 172 of file viewmodelbase.cpp.

173 {
174  Qt::ItemFlags result = QAbstractItemModel::flags(index);
175  if (auto item = itemFromIndex(index); item)
176  result |= item->flags();
177  return result;
178 }

References index(), and itemFromIndex().

Referenced by gui2::DataViewModel::flags(), and TEST_F().

Here is the call graph for this function:

◆ index()

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

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 columnCount(), itemFromIndex(), parent(), rootItem(), and rowCount().

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

Here is the call graph for this function:

◆ indexFromItem()

QModelIndex ViewModelBase::indexFromItem ( const ViewItem item) const

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 clearRows(), ModelView::ViewModel::indexOfSessionItem(), insertRow(), ModelView::ViewModelBase::ViewModelBaseImpl::item_belongs_to_model(), removeRow(), 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 
)

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 indexFromItem(), p_impl, and parent().

Referenced by 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

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 index().

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

Here is the call graph for this function:

◆ parent()

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

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 itemFromIndex(), and rootItem().

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

Here is the call graph for this function:

◆ removeRow()

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

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 indexFromItem(), p_impl, and parent().

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

Here is the call graph for this function:

◆ rootItem()

◆ rowCount()

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

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 itemFromIndex(), parent(), rootItem(), and ModelView::ViewItem::rowCount().

Referenced by index(), and TEST_F().

Here is the call graph for this function:

◆ setData()

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

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 index(), and itemFromIndex().

Referenced by TEST_F().

Here is the call graph for this function:

◆ setRootViewItem()

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

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

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

Friends And Related Function Documentation

◆ ViewModelController

friend class ViewModelController
friend

Definition at line 68 of file viewmodelbase.h.

Member Data Documentation

◆ p_impl

std::unique_ptr<ViewModelBaseImpl> ModelView::ViewModelBase::p_impl
private

Definition at line 70 of file viewmodelbase.h.

Referenced by clearRows(), insertRow(), removeRow(), rootItem(), and setRootViewItem().


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