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