BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SessionModelView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Toplevel/SessionModelView.cpp
6 //! @brief Implements class SessionModelView
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 
20 #include "GUI/Util/ComboProperty.h"
24 #include <QApplication>
25 #include <QStyledItemDelegate>
26 #include <QToolBar>
27 #include <QToolButton>
28 #include <QTreeView>
29 #include <QVBoxLayout>
30 
31 namespace {
32 
33 //! The SessionModelDelegate class presents the content of SessionModel items in
34 //! standard QTreeView. Extents base QItemDelegate with possibility to show/edit
35 //! our custom QVariant's.
36 
37 class SessionModelDelegate : public QStyledItemDelegate {
38 public:
39  SessionModelDelegate(QObject* parent)
40  : QStyledItemDelegate(parent)
41  {
42  }
43 
44  void paint(QPainter* painter, const QStyleOptionViewItem& option,
45  const QModelIndex& index) const override
46  {
47  if (index.data().canConvert<ComboProperty>())
48  paintCustomLabel(painter, option, index, index.data().value<ComboProperty>().label());
49  else
50  QStyledItemDelegate::paint(painter, option, index);
51  }
52 
53 private:
54  void paintCustomLabel(QPainter* painter, const QStyleOptionViewItem& option,
55  const QModelIndex& index, const QString& text) const
56  {
57  QStyleOptionViewItem opt = option;
58  initStyleOption(&opt, index); // calling original method to take into accounts colors etc
59  opt.text = displayText(text, option.locale); // by overriding text with ours
60  const QWidget* widget = opt.widget;
61  QStyle* style = widget ? widget->style() : QApplication::style();
62  style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
63  }
64 };
65 
66 } // namespace
67 
69  : QWidget(parent)
70 {
71  auto* layout = new QVBoxLayout(this);
72  layout->setMargin(0);
73 
74  auto* tabs = new QTabWidget(this);
75  auto* m_expandButton = new QToolButton(this);
76  m_expandButton->setText("Expand tree");
77  m_expandButton->setIcon(QIcon(":/images/toolbar_expand_collapse_tree.svg"));
78  m_expandButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
79  connect(m_expandButton, &QToolButton::clicked, [=]() {
80  auto* tree = static_cast<QTreeView*>(tabs->widget(tabs->currentIndex()));
81  tree->expandAll();
82  tree->resizeColumnToContents(0);
83  tree->resizeColumnToContents(1);
84  });
85 
86  auto* m_collapseButton = new QToolButton(this);
87  m_collapseButton->setText("Collapse tree");
88  m_collapseButton->setIcon(QIcon(":/images/toolbar_expand_collapse_tree.svg"));
89  m_collapseButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
90  connect(m_collapseButton, &QToolButton::clicked, [=]() {
91  auto* tree = static_cast<QTreeView*>(tabs->widget(tabs->currentIndex()));
92  tree->collapseAll();
93  });
94 
95  auto* toolbar = new QToolBar;
96  toolbar->addWidget(m_expandButton);
97  toolbar->addWidget(m_collapseButton);
98  layout->addWidget(toolbar);
99  layout->addWidget(tabs);
100 
101  QList<SessionModel*> models{document->realDataModel(), document->jobModel()};
102 
103  for (auto* model : models) {
104  auto* treeView = new QTreeView(this);
106  treeView->setItemDelegate(new SessionModelDelegate(this));
107  treeView->setModel(new SessionDecorationModel(treeView, model));
108  treeView->setEditTriggers(QAbstractItemView::NoEditTriggers); // read-only!
109  tabs->addTab(treeView, model->getModelTag());
110  }
111 }
Defines class ComboProperty.
Defines abstract item with a material property.
Defines class JobModel.
Defines class MaterialItems.
Defines class ProjectManager.
Defines class RealDataModel.
Defines class SessionDecorationModel.
Defines class SessionModelView.
Defines GUI::StyleUtils namespace.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
QString label() const
Returns the label to show.
Project document class handles all data related to the opened project (sample, job,...
JobModel * jobModel() const
RealDataModel * realDataModel() const
Provides decorations (text color, icons, etc) for SessionModel in SessionModelView context....
SessionModelView(QWidget *parent, ProjectDocument *document)
void setPropertyStyle(QTreeView *tree)
Sets style for the tree to use in property editors.
Definition: StyleUtils.cpp:49