File: ItemStackWidget.cpp

package info (click to toggle)
bornagain 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,800 kB
  • sloc: cpp: 469,684; python: 38,920; xml: 805; awk: 630; sh: 286; ansic: 37; makefile: 25
file content (110 lines) | stat: -rw-r--r-- 3,170 bytes parent folder | download
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
// ************************************************************************** //
//
//  BornAgain: simulate and fit scattering at grazing incidence
//
//! @file      GUI/coregui/Views/CommonWidgets/ItemStackWidget.cpp
//! @brief     Implements class ItemStackWidget
//!
//! @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/ItemStackWidget.h"
#include "GUI/coregui/Models/SessionModel.h"
#include "GUI/coregui/utils/GUIHelpers.h"
#include <QStackedWidget>
#include <QVBoxLayout>

ItemStackWidget::ItemStackWidget(QWidget* parent)
    : QWidget(parent), m_stackedWidget(new QStackedWidget), m_model(nullptr),
      m_size_hint(QSize(1024, 1024))
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QVBoxLayout* layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_stackedWidget);
    setLayout(layout);
}

void ItemStackWidget::setModel(SessionModel* model)
{
    if (model == m_model)
        return;

    disconnectModel();
    m_model = model;
    connectModel();
}

QSize ItemStackWidget::sizeHint() const
{
    return m_size_hint;
}

QSize ItemStackWidget::minimumSizeHint() const
{
    return QSize(25, 25);
}

void ItemStackWidget::setSizeHint(const QSize& size_hint)
{
    m_size_hint = size_hint;
}

void ItemStackWidget::onModelAboutToBeReset()
{
    removeWidgets();
}

void ItemStackWidget::onRowsAboutToBeRemoved(const QModelIndex& parent, int first, int)
{
    SessionItem* item = m_model->itemForIndex(m_model->index(first, 0, parent));
    removeWidgetForItem(item);
}

void ItemStackWidget::connectModel()
{
    if (!m_model)
        return;

    connect(m_model, SIGNAL(modelAboutToBeReset()), this, SLOT(onModelAboutToBeReset()),
            Qt::UniqueConnection);

    connect(m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this,
            SLOT(onRowsAboutToBeRemoved(QModelIndex, int, int)), Qt::UniqueConnection);
}

void ItemStackWidget::disconnectModel()
{
    if (!m_model)
        return;

    disconnect(m_model, SIGNAL(modelAboutToBeReset()), this, SLOT(onModelAboutToBeReset()));

    disconnect(m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this,
               SLOT(onRowsAboutToBeRemoved(QModelIndex, int, int)));
}

//! Checks if model was set correctly.

void ItemStackWidget::validateItem(SessionItem* item)
{
    if (!item)
        return;

    if (m_model) {
        if (m_model != item->model())
            // in principle it should be possible, but should be tested
            throw GUIHelpers::Error("ItemStackWidget::validateItem() -> Error. "
                                    "Attempt to use items from different models.");
    } else {
        setModel(item->model());
    }
}