Skip to content
Snippets Groups Projects
Commit 628c4c36 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

rm obsolete ItemSelectorWidget

parent 55e5a889
No related branches found
No related tags found
1 merge request!476Reduce SessionModelView ("Model tech view")
Pipeline #50239 failed
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Common/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/View/Common/ItemSelectorWidget.h"
#include "GUI/Model/Session/SessionDecorationModel.h"
#include "GUI/Model/Session/SessionModel.h"
#include "GUI/View/Tool/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);
auto* 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, &QListView::customContextMenuRequested, this,
&ItemSelectorWidget::onCustomContextMenuRequested);
}
ItemSelectorWidget::~ItemSelectorWidget() = default;
QSize ItemSelectorWidget::sizeHint() const
{
return QSize(GUI::Constants::ITEM_SELECTOR_WIDGET_WIDTH,
GUI::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(nullptr);
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(), &QItemSelectionModel::selectionChanged, this,
&ItemSelectorWidget::onSelectionChanged, 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);
}
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Common/ItemSelectorWidget.h
//! @brief Defines 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)
//
// ************************************************************************************************
#ifndef BORNAGAIN_GUI_VIEW_COMMON_ITEMSELECTORWIDGET_H
#define BORNAGAIN_GUI_VIEW_COMMON_ITEMSELECTORWIDGET_H
#include <QItemSelectionModel>
#include <QWidget>
#include <memory>
class SessionModel;
class SessionItem;
class QItemSelection;
class QModelIndex;
class QAbstractItemDelegate;
class QListView;
class SessionDecorationModel;
//! The ItemSelectorWidget class holds QListView to show top level items of SessionModel.
//! Used in InstrumentView and JobSelectorView to switch between items.
class ItemSelectorWidget : public QWidget {
Q_OBJECT
public:
ItemSelectorWidget(QWidget* parent = nullptr);
~ItemSelectorWidget() override;
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
void setModel(SessionModel* model);
void setItemDelegate(QAbstractItemDelegate* delegate);
QItemSelectionModel* selectionModel();
QListView* listView();
public slots:
void select(const QModelIndex& index, QItemSelectionModel::SelectionFlags command);
void updateSelection();
void selectLast();
signals:
void selectionChanged(SessionItem* item);
void contextMenuRequest(const QPoint& point, const QModelIndex& index);
private slots:
void onSelectionChanged(const QItemSelection& selected, const QItemSelection&);
void onCustomContextMenuRequested(const QPoint& point);
protected:
void connectModel();
void disconnectModel();
void showEvent(class QShowEvent*) override;
QListView* m_listView;
SessionModel* m_model;
std::unique_ptr<SessionDecorationModel> m_decorationModel;
};
#endif // BORNAGAIN_GUI_VIEW_COMMON_ITEMSELECTORWIDGET_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment