aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/bindingeditor/abstracteditordialog.cpp
blob: 5ccfc00fbab66ca70d1ab917fc5aaf86821265d6 (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
// Copyright (C) 2020 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 "abstracteditordialog.h"

#include <texteditor/texteditor.h>

#include <qmldesigner/qmldesignerplugin.h>
#include <qmljseditor/qmljseditor.h>
#include <qmljseditor/qmljseditordocument.h>
#include <texteditor/textdocument.h>

#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPlainTextEdit>

namespace QmlDesigner {

AbstractEditorDialog::AbstractEditorDialog(QWidget *parent, const QString &title)
    : QDialog(parent)
    , m_titleString(title)
{
    setWindowFlag(Qt::Tool, true);
    setWindowTitle(defaultTitle());
    setModal(true);

    setupJSEditor();
    setupUIComponents();

    QObject::connect(m_buttonBox, &QDialogButtonBox::accepted,
                     this, &AbstractEditorDialog::accepted);
    QObject::connect(m_buttonBox, &QDialogButtonBox::rejected,
                     this, &AbstractEditorDialog::rejected);
    QObject::connect(m_editorWidget, &BindingEditorWidget::returnKeyClicked,
                     this, &AbstractEditorDialog::accepted);
    QObject::connect(m_editorWidget, &QPlainTextEdit::textChanged,
                     this, &AbstractEditorDialog::textChanged);
}

AbstractEditorDialog::~AbstractEditorDialog()
{
    delete m_editor; // m_editorWidget is handled by basetexteditor destructor
    delete m_buttonBox;
    delete m_comboBoxLayout;
    delete m_verticalLayout;
}

void AbstractEditorDialog::showWidget()
{
    this->show();
    this->raise();
    m_editorWidget->setFocus();
}

void AbstractEditorDialog::showWidget(int x, int y)
{
    showWidget();
    move(QPoint(x, y));
}

QString AbstractEditorDialog::editorValue() const
{
    if (!m_editorWidget)
        return {};

    return m_editorWidget->document()->toPlainText();
}

void AbstractEditorDialog::setEditorValue(const QString &text)
{
    if (m_editorWidget)
        m_editorWidget->document()->setPlainText(text);
}

void AbstractEditorDialog::unregisterAutoCompletion()
{
    if (m_editorWidget)
        m_editorWidget->unregisterAutoCompletion();
}

QString AbstractEditorDialog::defaultTitle() const
{
    return m_titleString;
}

void AbstractEditorDialog::setupJSEditor()
{
    static BindingEditorFactory f;
    m_editor = qobject_cast<TextEditor::BaseTextEditor*>(f.createEditor());
    Q_ASSERT(m_editor);

    m_editorWidget = qobject_cast<BindingEditorWidget*>(m_editor->editorWidget());
    Q_ASSERT(m_editorWidget);

    auto qmlDesignerEditor = QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor();

    m_editorWidget->qmljsdocument = qobject_cast<QmlJSEditor::QmlJSEditorWidget *>(
                qmlDesignerEditor->widget())->qmlJsEditorDocument();

    m_editorWidget->setLineNumbersVisible(false);
    m_editorWidget->setMarksVisible(false);
    m_editorWidget->setCodeFoldingSupported(false);
    m_editorWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_editorWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_editorWidget->setTabChangesFocus(true);
}

void AbstractEditorDialog::setupUIComponents()
{
    m_verticalLayout = new QVBoxLayout(this);

    m_comboBoxLayout = new QHBoxLayout;

    m_editorWidget->setParent(this);
    m_editorWidget->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
    m_editorWidget->show();

    m_buttonBox = new QDialogButtonBox(this);
    m_buttonBox->setOrientation(Qt::Horizontal);
    m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    m_buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);

    m_verticalLayout->addLayout(m_comboBoxLayout);
    m_verticalLayout->addWidget(m_editorWidget);
    m_verticalLayout->addWidget(m_buttonBox);

    this->resize(660, 240);
}

bool AbstractEditorDialog::isNumeric(const TypeName &type)
{
    static QList<TypeName> numericTypes = {"double", "int", "real"};
    return numericTypes.contains(type);
}

bool AbstractEditorDialog::isColor(const TypeName &type)
{
    static QList<TypeName> colorTypes = {"QColor", "color"};
    return colorTypes.contains(type);
}

bool AbstractEditorDialog::isVariant(const TypeName &type)
{
    static QList<TypeName> variantTypes = {"alias", "unknown", "variant", "var"};
    return variantTypes.contains(type);
}

void AbstractEditorDialog::textChanged()
{
    if (m_lock)
        return;

    m_lock = true;
    adjustProperties();
    m_lock = false;
}

} // QmlDesigner namespace