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

make "model tech view" read-only

...and remove obsolete code.

The "model tech view" is only kept for further migration (to understand the SessionModel). However, it is not allowed anymore to edit the model in this view.
parent 48febd08
No related branches found
No related tags found
1 merge request!476Reduce SessionModelView ("Model tech view")
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/PropertyEditor/SessionModelDelegate.cpp
//! @brief Implements class SessionModelDelegate
//!
//! @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/PropertyEditor/SessionModelDelegate.h"
#include "GUI/Model/Session/SessionItem.h"
#include "GUI/View/PropertyEditor/CustomEditors.h"
#include "GUI/View/PropertyEditor/CustomEventFilters.h"
#include "GUI/View/PropertyEditor/PropertyEditorFactory.h"
#include <QApplication>
SessionModelDelegate::SessionModelDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
void SessionModelDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (GUI::View::PropertyEditorFactory::hasStringRepresentation(index)) {
QString text = GUI::View::PropertyEditorFactory::toString(index);
paintCustomLabel(painter, option, index, text);
} else
QStyledItemDelegate::paint(painter, option, index);
}
QWidget* SessionModelDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
auto* result = createEditorFromIndex(index, parent);
if (auto* customEditor = dynamic_cast<CustomEditor*>(result)) {
new TabFromFocusProxy(customEditor);
connect(customEditor, &CustomEditor::dataChanged, this,
&SessionModelDelegate::onCustomEditorDataChanged);
}
if (!result) // falling back to default behaviour
result = QStyledItemDelegate::createEditor(parent, option, index);
return result;
}
//! Propagates changed data from the editor to the model.
void SessionModelDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const
{
if (!index.isValid())
return;
if (auto* customEditor = dynamic_cast<CustomEditor*>(editor))
model->setData(index, customEditor->editorData());
else
QStyledItemDelegate::setModelData(editor, model, index);
}
//! Propagates the data change from the model to the editor (if it is still opened).
void SessionModelDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
if (!index.isValid())
return;
if (auto* customEditor = dynamic_cast<CustomEditor*>(editor))
customEditor->setData(index.data());
else
QStyledItemDelegate::setEditorData(editor, index);
}
//! Increases height of the row by 20% wrt the default.
QSize SessionModelDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QSize result = QStyledItemDelegate::sizeHint(option, index);
result.setHeight(static_cast<int>(result.height() * 1.2));
return result;
}
//! Makes an editor occupying whole available space in a cell. If cell contains an icon
//! as a decoration (i.e. icon of material property), it will be hidden as soon as editor
//! up and running.
void SessionModelDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
editor->setGeometry(option.rect);
}
QWidget* SessionModelDelegate::createEditorFromIndex(const QModelIndex& index,
QWidget* parent) const
{
if (index.internalPointer()) {
auto* item = static_cast<SessionItem*>(index.internalPointer());
return GUI::View::PropertyEditorFactory::CreateEditor(*item, parent);
}
return nullptr;
}
//! Notifies everyone that the editor has completed editing the data.
void SessionModelDelegate::onCustomEditorDataChanged(const QVariant&)
{
auto* editor = qobject_cast<CustomEditor*>(sender());
ASSERT(editor);
emit commitData(editor);
}
//! Paints custom text in a a place corresponding given index.
void SessionModelDelegate::paintCustomLabel(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index, const QString& text) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); // calling original method to take into accounts colors etc
opt.text = displayText(text, option.locale); // by overriding text with ours
const QWidget* widget = opt.widget;
QStyle* style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
}
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/PropertyEditor/SessionModelDelegate.h
//! @brief Defines class SessionModelDelegate
//!
//! @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_PROPERTYEDITOR_SESSIONMODELDELEGATE_H
#define BORNAGAIN_GUI_VIEW_PROPERTYEDITOR_SESSIONMODELDELEGATE_H
#include <QStyledItemDelegate>
//! The SessionModelDelegate class presents the content of SessionModel items in
//! standard QTreeView. Extents base QItemDelegate with possibility to show/edit
//! our custom QVariant's.
class SessionModelDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
SessionModelDelegate(QObject* parent);
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const override;
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const override;
void setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const override;
void setEditorData(QWidget* editor, const QModelIndex& index) const override;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
const QModelIndex& index) const override;
protected:
virtual QWidget* createEditorFromIndex(const QModelIndex& index, QWidget* parent) const;
public slots:
void onCustomEditorDataChanged(const QVariant&);
private:
void paintCustomLabel(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index, const QString& text) const;
};
#endif // BORNAGAIN_GUI_VIEW_PROPERTYEDITOR_SESSIONMODELDELEGATE_H
......@@ -17,16 +17,61 @@
#include "GUI/Model/Instrument/InstrumentModel.h"
#include "GUI/Model/Job/JobModel.h"
#include "GUI/Model/Material/MaterialModel.h"
#include "GUI/Model/Sample/ItemWithMaterial.h"
#include "GUI/Model/Sample/SampleModel.h"
#include "GUI/Model/Session/SessionDecorationModel.h"
#include "GUI/Util/ComboProperty.h"
#include "GUI/View/Main/ProjectManager.h"
#include "GUI/View/PropertyEditor/SessionModelDelegate.h"
#include "GUI/View/Tool/StyleUtils.h"
#include <QApplication>
#include <QStyledItemDelegate>
#include <QToolBar>
#include <QToolButton>
#include <QTreeView>
#include <QVBoxLayout>
namespace {
//! The SessionModelDelegate class presents the content of SessionModel items in
//! standard QTreeView. Extents base QItemDelegate with possibility to show/edit
//! our custom QVariant's.
class SessionModelDelegate : public QStyledItemDelegate {
public:
SessionModelDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const override
{
const bool isMaterialProperty =
index.data(SessionFlags::CustomEditorRole) == SessionItem::EDITOR_TYPE_MATERIAL
&& index.column() == SessionFlags::ITEM_VALUE;
if (isMaterialProperty) {
const auto* materialItem = static_cast<SessionItem*>(index.internalPointer());
const auto* itemWithMaterial = dynamic_cast<ItemWithMaterial*>(materialItem->parent());
ASSERT(itemWithMaterial);
paintCustomLabel(painter, option, index, itemWithMaterial->materialName());
} else if (index.data().canConvert<ComboProperty>())
paintCustomLabel(painter, option, index, index.data().value<ComboProperty>().label());
else
QStyledItemDelegate::paint(painter, option, index);
}
private:
void paintCustomLabel(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index, const QString& text) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); // calling original method to take into accounts colors etc
opt.text = displayText(text, option.locale); // by overriding text with ours
const QWidget* widget = opt.widget;
QStyle* style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
}
};
} // namespace
SessionModelView::SessionModelView(QWidget* parent, ProjectDocument* document) : QWidget(parent)
{
auto* layout = new QVBoxLayout(this);
......@@ -68,6 +113,7 @@ SessionModelView::SessionModelView(QWidget* parent, ProjectDocument* document) :
GUI::Util::Style::setPropertyStyle(treeView);
treeView->setItemDelegate(new SessionModelDelegate(this));
treeView->setModel(new SessionDecorationModel(treeView, model));
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers); // read-only!
tabs->addTab(treeView, model->getModelTag());
}
}
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