BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemViewOverlayButtons Class Reference

Description

Definition at line 24 of file ItemViewOverlayButtons.h.

Inheritance diagram for ItemViewOverlayButtons:
[legend]
Collaboration diagram for ItemViewOverlayButtons:
[legend]

Public Types

using FnGetActions = std::function< QList< QAction * >(const QModelIndex &, bool)>
 

Static Public Member Functions

static void install (QAbstractItemView *view, FnGetActions fnGetActions)
 

Protected Member Functions

bool eventFilter (QObject *obj, QEvent *event) override
 

Private Member Functions

 ItemViewOverlayButtons (QObject *parent)
 
void installOverlay (const QModelIndex &index)
 
void update ()
 
void updateRecursive (const QModelIndex &index)
 

Private Attributes

FnGetActions m_getActions
 
QAbstractItemView * m_view
 

Member Typedef Documentation

◆ FnGetActions

using ItemViewOverlayButtons::FnGetActions = std::function<QList<QAction*>(const QModelIndex&, bool)>

Definition at line 27 of file ItemViewOverlayButtons.h.

Constructor & Destructor Documentation

◆ ItemViewOverlayButtons()

ItemViewOverlayButtons::ItemViewOverlayButtons ( QObject *  parent)
private

Definition at line 196 of file ItemViewOverlayButtons.cpp.

197  : QObject(parent)
198 {
199 }

Referenced by install().

Member Function Documentation

◆ eventFilter()

bool ItemViewOverlayButtons::eventFilter ( QObject *  obj,
QEvent *  event 
)
overrideprotected

Definition at line 180 of file ItemViewOverlayButtons.cpp.

181 {
182  // F2 would start the editing. Since OverlayWidgets are an editor already (indexWidget uses the
183  // editor internals), we remove the overlay at this place, so the view will properly open the
184  // editor
185  if (event->type() == QEvent::KeyPress) {
186  auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
187  if (keyEvent->key() == Qt::Key_F2) {
188  QModelIndex ci = m_view->currentIndex();
189  if (ci.isValid() && ci.flags().testFlag(Qt::ItemIsEditable))
190  m_view->setIndexWidget(ci, nullptr);
191  }
192  }
193  return QObject::eventFilter(obj, event);
194 }
QAbstractItemView * m_view

References m_view.

◆ install()

void ItemViewOverlayButtons::install ( QAbstractItemView *  view,
FnGetActions  fnGetActions 
)
static

Definition at line 160 of file ItemViewOverlayButtons.cpp.

161 {
162  auto* h = new ItemViewOverlayButtons(view);
163  h->m_getActions = fnGetActions;
164  h->m_view = view;
165  auto* d = new ItemViewOverlayDelegate;
166  view->setItemDelegate(d);
167  view->installEventFilter(h);
168  h->update();
169 
170  connect(d, &QAbstractItemDelegate::closeEditor, h, &ItemViewOverlayButtons::update);
171 
172  connect(view->model(), &QAbstractItemModel::modelReset, h, &ItemViewOverlayButtons::update,
173  Qt::QueuedConnection);
174  connect(view->model(), &QAbstractItemModel::rowsInserted, h, &ItemViewOverlayButtons::update,
175  Qt::QueuedConnection); // Queued: important!
176  connect(view->model(), &QAbstractItemModel::rowsRemoved, h, &ItemViewOverlayButtons::update,
177  Qt::QueuedConnection); // Queued: important!
178 }
ItemViewOverlayButtons(QObject *parent)

References ItemViewOverlayButtons(), and update().

Referenced by RealDataSelectorWidget::RealDataSelectorWidget(), SampleListView::SampleListView(), InstrumentLibraryEditor::execAdd(), and InstrumentLibraryEditor::execChoose().

Here is the call graph for this function:

◆ installOverlay()

void ItemViewOverlayButtons::installOverlay ( const QModelIndex &  index)
private

Definition at line 229 of file ItemViewOverlayButtons.cpp.

230 {
231  const auto permanentActions = m_getActions(index, false);
232  const auto hoverActions = m_getActions(index, true);
233 
234  if (permanentActions.isEmpty() && hoverActions.isEmpty())
235  return;
236 
237  auto* w = new ItemViewOverlayWidget(m_view, index);
238 
239  const auto setAlignment = [&](const QList<QAction*> actions) {
240  w->setHorizontalAlignment(Qt::AlignRight);
241  if (actions.first() == nullptr && actions.last() == nullptr)
242  w->setHorizontalAlignment(Qt::AlignCenter);
243  else if (actions.first() != nullptr && actions.last() == nullptr)
244  w->setHorizontalAlignment(Qt::AlignLeft);
245  };
246 
247  if (!permanentActions.isEmpty()) {
248  setAlignment(permanentActions);
249  w->addActions(permanentActions);
250  w->setHover(false);
251  } else if (!hoverActions.isEmpty()) {
252  setAlignment(hoverActions);
253  w->addActions(hoverActions);
254  w->setHover(true);
255  }
256 
257  w->create();
258  m_view->setIndexWidget(index, w);
259 }

References m_getActions, and m_view.

Referenced by updateRecursive().

◆ update()

void ItemViewOverlayButtons::update ( )
private

Definition at line 220 of file ItemViewOverlayButtons.cpp.

221 {
222  if (m_view->model() == nullptr)
223  return;
224  auto* m = m_view->model();
225  for (int row = 0; row < m->rowCount(); row++)
226  updateRecursive(m->index(row, 0));
227 }
void updateRecursive(const QModelIndex &index)

References m_view, and updateRecursive().

Referenced by install().

Here is the call graph for this function:

◆ updateRecursive()

void ItemViewOverlayButtons::updateRecursive ( const QModelIndex &  index)
private

Definition at line 201 of file ItemViewOverlayButtons.cpp.

202 {
203  const auto hoverIfNecessary = [&](QModelIndex index) {
204  QPoint viewPortCoordinatesOfMouse = m_view->mapFromGlobal(QCursor::pos());
205  if (m_view->indexAt(viewPortCoordinatesOfMouse) == index)
206  if (auto* w = dynamic_cast<ItemViewOverlayWidget*>(m_view->indexWidget(index)))
207  w->hover(true);
208  };
209 
210  if (m_view->indexWidget(index) == nullptr)
211  installOverlay(index);
212  hoverIfNecessary(index);
213 
214  auto* m = m_view->model();
215  for (int childRow = 0; childRow < m->rowCount(index); childRow++)
216  updateRecursive(m->index(childRow, 0, index));
217 }
void installOverlay(const QModelIndex &index)

References installOverlay(), and m_view.

Referenced by update().

Here is the call graph for this function:

Member Data Documentation

◆ m_getActions

FnGetActions ItemViewOverlayButtons::m_getActions
private

Definition at line 40 of file ItemViewOverlayButtons.h.

Referenced by installOverlay().

◆ m_view

QAbstractItemView* ItemViewOverlayButtons::m_view
private

Definition at line 41 of file ItemViewOverlayButtons.h.

Referenced by eventFilter(), installOverlay(), update(), and updateRecursive().


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