BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemSelectorWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.cpp
6 //! @brief Implements class ItemSelectorWidget
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QListView>
20 #include <QVBoxLayout>
21 
23  : QWidget(parent), m_listView(new QListView), m_model(nullptr)
24 {
25  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
26 
27  QVBoxLayout* layout = new QVBoxLayout;
28  layout->setMargin(0);
29 
30  layout->addWidget(m_listView);
31  setLayout(layout);
32 
33  m_listView->setContextMenuPolicy(Qt::CustomContextMenu);
34  m_listView->setAttribute(Qt::WA_MacShowFocusRect, false);
35 
36  connect(m_listView, &QListView::customContextMenuRequested, this,
38 }
39 
41 
43 {
45 }
46 
48 {
49  return QSize(25, 25);
50 }
51 
53 {
54  if (model == m_model)
55  return;
56 
58  m_model = model;
59  connectModel();
60 }
61 
62 void ItemSelectorWidget::setItemDelegate(QAbstractItemDelegate* delegate)
63 {
64  m_listView->setItemDelegate(delegate);
65 }
66 
67 QItemSelectionModel* ItemSelectorWidget::selectionModel()
68 {
69  return m_listView->selectionModel();
70 }
71 
73 {
74  return m_listView;
75 }
76 
77 void ItemSelectorWidget::select(const QModelIndex& index,
78  QItemSelectionModel::SelectionFlags command)
79 {
80  selectionModel()->select(index, command);
81 }
82 
83 //! select last item if no selection exists
84 
86 {
87  if (!selectionModel()->hasSelection())
88  selectLast();
89 }
90 
92 {
93  QModelIndex itemIndex = m_model->index(m_model->rowCount(QModelIndex()) - 1, 0, QModelIndex());
94  selectionModel()->select(itemIndex, QItemSelectionModel::ClearAndSelect);
95 }
96 
97 void ItemSelectorWidget::onSelectionChanged(const QItemSelection& selected, const QItemSelection&)
98 {
99  QModelIndexList indexes = selected.indexes();
100  SessionItem* selectedItem(0);
101 
102  if (!indexes.empty())
103  selectedItem = m_model->itemForIndex(indexes.back());
104 
105  emit selectionChanged(selectedItem);
106 }
107 
109 {
110  emit contextMenuRequest(m_listView->mapToGlobal(point), m_listView->indexAt(point));
111 }
112 
114 {
115  if (!m_model)
116  return;
117 
118  m_decorationModel = std::make_unique<SessionDecorationModel>(nullptr, m_model);
119  m_listView->setModel(m_decorationModel.get());
120 
121  connect(m_listView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
122  &ItemSelectorWidget::onSelectionChanged, Qt::UniqueConnection);
123 }
124 
126 {
127  m_listView->setModel(nullptr);
128  m_model = nullptr;
129 }
130 
131 //! provide default selection when widget is shown
133 {
134  if (!m_model || !selectionModel())
135  return;
136 
137  if (selectionModel()->selectedIndexes().isEmpty() && m_model->rowCount(QModelIndex()) != 0)
138  selectionModel()->select(m_model->index(0, 0, QModelIndex()), QItemSelectionModel::Select);
139 }
Defines class ItemSelectorWidget.
Defines class SessionDecorationModel.
Defines class SessionModel.
QSize minimumSizeHint() const
void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
ItemSelectorWidget(QWidget *parent=0)
QItemSelectionModel * selectionModel()
void setItemDelegate(QAbstractItemDelegate *delegate)
void contextMenuRequest(const QPoint &point, const QModelIndex &index)
void showEvent(class QShowEvent *)
provide default selection when widget is shown
std::unique_ptr< SessionDecorationModel > m_decorationModel
void onCustomContextMenuRequested(const QPoint &point)
void updateSelection()
select last item if no selection exists
void setModel(SessionModel *model)
void onSelectionChanged(const QItemSelection &selected, const QItemSelection &)
void selectionChanged(SessionItem *item)
SessionModel * m_model
SessionItem * itemForIndex(const QModelIndex &index) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual int rowCount(const QModelIndex &parent) const
Defines namespace Constants.
const unsigned int ITEM_SELECTOR_WIDGET_WIDTH
const unsigned int ITEM_SELECTOR_WIDGET_HEIGHT