File: propertyenumeditor.cpp

package info (click to toggle)
gammaray 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,620 kB
  • sloc: cpp: 94,712; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 32
file content (265 lines) | stat: -rw-r--r-- 7,545 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
  propertyenumeditor.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "propertyenumeditor.h"

#include <common/enumrepository.h>
#include <common/objectbroker.h>

using namespace GammaRay;

#include <QAbstractItemModel>
#include <QDebug>
#include <QEvent>
#include <QListView>
#include <QStylePainter>

namespace GammaRay {
class PropertyEnumEditorModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit PropertyEnumEditorModel(QObject *parent = nullptr);
    ~PropertyEnumEditorModel() override;

    EnumValue value() const;
    void setValue(const EnumValue &value);
    void updateValue(int value);

    EnumDefinition definition() const;
    void setDefinition(const EnumDefinition &def);

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role) override;


private:
    EnumValue m_value;
    EnumDefinition m_def;
};

}

PropertyEnumEditorModel::PropertyEnumEditorModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

PropertyEnumEditorModel::~PropertyEnumEditorModel() = default;

EnumValue PropertyEnumEditorModel::value() const
{
    return m_value;
}

void PropertyEnumEditorModel::setValue(const EnumValue &value)
{
    beginResetModel();
    m_value = value;
    auto repo = ObjectBroker::object<EnumRepository *>();
    m_def = repo->definition(value.id());
    endResetModel();
}

void PropertyEnumEditorModel::updateValue(int value)
{
    Q_ASSERT(m_value.isValid());
    Q_ASSERT(m_def.isValid());

    m_value.setValue(value);
}

EnumDefinition PropertyEnumEditorModel::definition() const
{
    return m_def;
}

void PropertyEnumEditorModel::setDefinition(const EnumDefinition &def)
{
    beginResetModel();
    m_def = def;
    endResetModel();
}

int PropertyEnumEditorModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_def.elements().size();
}

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

    if (role == Qt::DisplayRole) {
        return m_def.elements().at(index.row()).name();
    } else if (role == Qt::CheckStateRole && m_def.isFlag()) {
        const auto &elem = m_def.elements().at(index.row());
        if (elem.value() == 0)
            return m_value.value() == 0 ? Qt::Checked : Qt::Unchecked;
        return (elem.value() & m_value.value()) == elem.value() ? Qt::Checked : Qt::Unchecked;
    }

    return QVariant();
}

Qt::ItemFlags PropertyEnumEditorModel::flags(const QModelIndex &index) const
{
    const auto f = QAbstractListModel::flags(index);
    if (index.isValid() && m_def.isFlag() && m_def.elements().at(index.row()).value() != 0) // 0 values can't be toggled
        return f | Qt::ItemIsUserCheckable;
    return f;
}

bool PropertyEnumEditorModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || !m_def.isFlag())
        return false;

    if (role == Qt::CheckStateRole) {
        const auto &elem = m_def.elements().at(index.row());
        if (value.toInt() == Qt::Checked)
            m_value.setValue(m_value.value() | elem.value());
        else if (value.toInt() == Qt::Unchecked)
            m_value.setValue(m_value.value() & ~elem.value());

        emit dataChanged(this->index(0, 0), this->index(rowCount() - 1, 0)); // mask flags can change multiple rows
        return true;
    }
    return QAbstractListModel::setData(index, value, role);
}

PropertyEnumEditor::PropertyEnumEditor(QWidget *parent)
    : QComboBox(parent)
    , m_model(new PropertyEnumEditorModel(this))
{
    setModel(m_model);
    connect(m_model, &PropertyEnumEditorModel::dataChanged, this, [this] {
        update();
    });

    auto repo = ObjectBroker::object<EnumRepository *>();
    connect(repo, &EnumRepository::definitionChanged, this, &PropertyEnumEditor::definitionChanged);

    setEnabled(false);
    connect(this, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
            this, &PropertyEnumEditor::slotCurrentIndexChanged);
}

PropertyEnumEditor::~PropertyEnumEditor() = default;

EnumValue PropertyEnumEditor::enumValue() const
{
    return m_model->value();
}

void PropertyEnumEditor::setEnumValue(const EnumValue &value)
{
    m_model->setValue(value);
    updateCurrentIndex();
    setupView();
}

void PropertyEnumEditor::definitionChanged(int id)
{
    if (!m_model->value().isValid() || id != m_model->value().id())
        return;

    auto repo = ObjectBroker::object<EnumRepository *>();
    const auto def = repo->definition(id);
    m_model->setDefinition(def);
    updateCurrentIndex();
    setupView();
}

void PropertyEnumEditor::updateCurrentIndex()
{
    const auto def = m_model->definition();
    if (!def.isValid() || !m_model->value().isValid())
        return;

    if (!def.isFlag()) {
        for (int i = 0; i < def.elements().size(); ++i) {
            if (def.elements().at(i).value() == m_model->value().value()) {
                setCurrentIndex(i);
                break;
            }
        }
    }
}

void PropertyEnumEditor::setupView()
{
    const auto def = m_model->definition();
    if (!def.isValid())
        return;
    setEnabled(true);

    if (def.isFlag() && view()->metaObject() != &QListView::staticMetaObject) {
        // the default view doesn't show check boxes (at least with the Fusion style)
        auto v = new QListView(this);
        setView(v);
        v->installEventFilter(this);
        v->viewport()->installEventFilter(this);
    }
}

void PropertyEnumEditor::slotCurrentIndexChanged(int index)
{
    const auto def = m_model->definition();
    if (!def.isValid() || def.isFlag() || index < 0)
        return;
    m_model->updateValue(def.elements().at(index).value());
}

void PropertyEnumEditor::paintEvent(QPaintEvent *event)
{
    const auto def = m_model->definition();

    if (def.isValid() && !def.isFlag()) {
        QComboBox::paintEvent(event);
        return;
    }

    // for flags we paint the combined value name as text
    QStylePainter painter(this);
    painter.setPen(palette().color(QPalette::Text));
    QStyleOptionComboBox opt;
    initStyleOption(&opt);

    if (!def.isValid())
        opt.currentText = tr("Loading...");
    else
        opt.currentText = def.valueToString(m_model->value());

    painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}

bool PropertyEnumEditor::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == view() || watched == view()->viewport()) {
        if (event->type() == QEvent::MouseButtonRelease && m_model->definition().isFlag()) {
            const auto state = view()->currentIndex().data(Qt::CheckStateRole).toInt();
            m_model->setData(view()->currentIndex(), state == Qt::Checked ? Qt::Unchecked : Qt::Checked, Qt::CheckStateRole);
            return true;
        }
    }
    return QComboBox::eventFilter(watched, event);
}

#include "propertyenumeditor.moc"