aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/squish/propertyitemdelegate.cpp
blob: 7db1b6a12609c327927ba28885d04b7b848e8750 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "propertyitemdelegate.h"
#include "objectsmaptreeitem.h"

#include <utils/treemodel.h>

#include <QComboBox>
#include <QCompleter>
#include <QLineEdit>
#include <QMouseEvent>
#include <QRegularExpression>

namespace Squish {
namespace Internal {

enum ViewColumn { Name, Operator, Value };

PropertyItemDelegate::PropertyItemDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{}

void PropertyItemDelegate::paint(QPainter *painter,
                                 const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    // paint invalid values red
    if (index.column() == Value) {
        if (auto sortModel = qobject_cast<const PropertiesSortModel *>(index.model())) {
            if (auto propertiesModel = qobject_cast<PropertiesModel *>(sortModel->sourceModel())) {
                const QModelIndex idx = sortModel->mapToSource(index);
                PropertyTreeItem *item = static_cast<PropertyTreeItem *>(
                    propertiesModel->itemForIndex(idx));
                const Property &property = item->property();
                if (property.isContainer() || property.isRelativeWidget()) {
                    const ObjectsMapTreeItem *parent = propertiesModel->parentItem();
                    if (parent) {
                        if (const ObjectsMapModel *objMapModel
                            = qobject_cast<const ObjectsMapModel *>(parent->model())) {
                            if (!objMapModel->findItem(item->property().m_value))
                                opt.palette.setColor(QPalette::Text, QColor(0xff, 0, 0));
                        }
                    }
                }
            }
        }
    }
    QStyledItemDelegate::paint(painter, opt, index);
}

QWidget *PropertyItemDelegate::createEditor(QWidget *parent,
                                            const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const
{
    switch (index.column()) {
    case Name: {
        auto sortModel = qobject_cast<const PropertiesSortModel *>(index.model());
        PropertiesModel *pm = qobject_cast<PropertiesModel *>(sortModel->sourceModel());

        Utils::TreeItem *self = pm->itemForIndex(sortModel->mapToSource(index));
        QStringList forbidden;
        pm->forItemsAtLevel<1>([&self, &forbidden](Utils::TreeItem *it) {
            auto item = static_cast<PropertyTreeItem *>(it);
            if (item != self)
                forbidden.append(item->property().m_name);
        });

        return new ValidatingPropertyNameLineEdit(forbidden, parent);
    }
    case Operator: {
        if (index.data().toString() == Property::OPERATOR_IS)
            return nullptr;

        QComboBox *comboBox = new QComboBox(parent);
        comboBox->addItem(Property::OPERATOR_EQUALS);
        comboBox->addItem(Property::OPERATOR_WILDCARD);
        comboBox->addItem(Property::OPERATOR_REGEX);
        comboBox->setFocusPolicy(Qt::StrongFocus);
        comboBox->setAutoFillBackground(true);
        return comboBox;
    }
    case Value: {
        auto sortModel = qobject_cast<const PropertiesSortModel *>(index.model());
        PropertiesModel *pm = qobject_cast<PropertiesModel *>(sortModel->sourceModel());

        PropertyTreeItem *self = static_cast<PropertyTreeItem *>(
            pm->itemForIndex(sortModel->mapToSource(index)));
        if (self->property().isContainer() || self->property().isRelativeWidget()) {
            auto objMapModel = qobject_cast<ObjectsMapModel *>(pm->parentItem()->model());
            return new ValidatingPropertyContainerLineEdit(objMapModel->allSymbolicNames(), parent);
        }
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
    default:
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

void PropertyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (index.column() == Operator) {
        if (QComboBox *combo = qobject_cast<QComboBox *>(editor)) {
            combo->setCurrentText(index.data().toString());
            combo->showPopup();
        }
    } else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
        lineEdit->setText(index.data().toString());
    } else {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}

void PropertyItemDelegate::setModelData(QWidget *editor,
                                        QAbstractItemModel *model,
                                        const QModelIndex &index) const
{
    if (auto edit = qobject_cast<Utils::FancyLineEdit *>(editor)) {
        if (!edit->isValid())
            return;
    }

    QStyledItemDelegate::setModelData(editor, model, index);
}

/*********************************** ValidatingNameEdit ***************************************/

ValidatingPropertyNameLineEdit::ValidatingPropertyNameLineEdit(const QStringList &forbidden,
                                                               QWidget *parent)
    : Utils::FancyLineEdit(parent)
    , m_forbidden(forbidden)
{
    setValidationFunction([this](FancyLineEdit *edit, QString * /*errorMessage*/) {
        if (!edit)
            return false;

        const QRegularExpression identifier("^[a-zA2-Z0-9_]+$");
        const QString &value = edit->text();

        return !m_forbidden.contains(value) && identifier.match(value).hasMatch();
    });
}

/*********************************** ValidatingContainerEdit **********************************/

ValidatingPropertyContainerLineEdit::ValidatingPropertyContainerLineEdit(const QStringList &allowed,
                                                                         QWidget *parent)
    : Utils::FancyLineEdit(parent)
    , m_allowed(allowed)
{
    setSpecialCompleter(new QCompleter(allowed, this));
    setValidationFunction([this](FancyLineEdit *edit, QString * /*errorMessage*/) {
        return edit && m_allowed.contains(edit->text());
    });
}

} // namespace Internal
} // namespace Squish