File: PartunerQModel.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (205 lines) | stat: -rw-r--r-- 5,607 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Tuning/PartunerQModel.cpp
//! @brief     Implements class PartunerQModel.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Tuning/PartunerQModel.h"
#include "GUI/Model/Par/ParameterTreeItems.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Util/UtilXML.h"
#include <QFont>
#include <QMimeData>

PartunerQModel::PartunerQModel(QObject* rootObject, QObject* parent)
    : QAbstractItemModel(parent)
    , m_root_object(rootObject)
{
}

QVariant PartunerQModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
        return {};
    return (section == 0) ? "Name" : "Value";
}

QVariant PartunerQModel::parentColor(const QModelIndex& index) const
{
    if (!index.isValid())
        return {};

    if (const auto* label = toParameterLabelItem(index))
        if (label->color().isValid())
            return QVariant(label->color());

    return parentColor(index.parent());
}

QVariant PartunerQModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
        return {};

    if (role == Qt::BackgroundRole)
        return parentColor(index);

    if (const auto* label = toParameterLabelItem(index)) {
        if (role == Qt::FontRole) {
            QFont boldFont;
            boldFont.setBold(true);
            return boldFont;
        }
        if (role == Qt::DisplayRole && index.column() == 0)
            return label->title();
        if (role == Qt::UserRole) {
            return label->collapsed();
        }
        return {};
    }

    if (auto* var = toParameterItem(index)) {
        if (role == Qt::DisplayRole || role == Qt::EditRole) {
            if (index.column() == 0)
                return var->title();
            return var->valueOfLink();
        }
        return {};
    }

    return {};
}

Qt::ItemFlags PartunerQModel::flags(const QModelIndex& index) const
{
    Qt::ItemFlags result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    if (toParameterItem(index)) {
        result |= Qt::ItemIsDragEnabled;
        if (index.column() == 1)
            result |= Qt::ItemIsEditable;
    }
    return result;
}

QModelIndex PartunerQModel::index(int row, int column, const QModelIndex& parent) const
{
    if (!hasIndex(row, column, parent))
        return {};

    if (!parent.isValid())
        return createIndex(row, column, m_root_object->children()[row]);

    if (auto* label = toParameterLabelItem(parent))
        return createIndex(row, column, label->children()[row]);

    return {};
}

QModelIndex PartunerQModel::parent(const QModelIndex& index) const
{
    if (!index.isValid())
        return {};

    auto* item = static_cast<QObject*>(index.internalPointer());
    if (item->parent() == m_root_object)
        return {};

    const int row = item->parent()->parent()->children().indexOf(item->parent());
    return createIndex(row, 0, item->parent());
}

int PartunerQModel::rowCount(const QModelIndex& parent) const
{
    if (parent.column() > 0)
        return 0;

    if (!parent.isValid())
        return m_root_object->children().size();

    auto* item = static_cast<QObject*>(parent.internalPointer());
    return item->children().size();
}

int PartunerQModel::columnCount(const QModelIndex&) const
{
    return 2;
}

QMimeData* PartunerQModel::mimeData(const QModelIndexList& indexes) const
{
    auto* mimeData = new QMimeData;

    for (auto index : indexes) {
        if (ParameterItem* parameterItem = toParameterItem(index)) {
            QByteArray data;
            data.setNum(reinterpret_cast<qlonglong>(parameterItem));
            mimeData->setData(XML::LinkMimeType, data);
            break;
        }
    }
    return mimeData;
}

Qt::DropActions PartunerQModel::supportedDragActions() const
{
    return Qt::CopyAction;
}

Qt::DropActions PartunerQModel::supportedDropActions() const
{
    return Qt::IgnoreAction;
}

ParameterItem* PartunerQModel::getParameterItem(const QModelIndex& index) const
{
    return toParameterItem(index);
}

QModelIndex PartunerQModel::indexForItem(ParameterItem* item) const
{
    if (item == nullptr)
        return {};

    if (item->parent()) {
        const int row = item->parent()->children().indexOf(item);
        return createIndex(row, 0, item);
    }

    return {};
}

ParameterItem* PartunerQModel::toParameterItem(const QModelIndex& index)
{
    auto* item = static_cast<QObject*>(index.internalPointer());
    return dynamic_cast<ParameterItem*>(item);
}

ParameterLabelItem* PartunerQModel::toParameterLabelItem(const QModelIndex& index)
{
    auto* item = static_cast<QObject*>(index.internalPointer());
    return dynamic_cast<ParameterLabelItem*>(item);
}

void PartunerQModel::setExpanded(const QModelIndex& index) const
{
    if (auto* label = toParameterLabelItem(index)) {
        label->setCollapsed(false);
        gDoc->setModified();
    }
}

void PartunerQModel::setCollapsed(const QModelIndex& index) const
{
    if (auto* label = toParameterLabelItem(index)) {
        label->setCollapsed(true);
        gDoc->setModified();
    }
}