aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmljseditor/qmljscomponentnamedialog.cpp
blob: b29fe2d87ae32b99583650f00e289bf3d3e2b4dd (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
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmljscomponentnamedialog.h"
#include "qmljseditortr.h"

#include <algorithm>
#include <utils/array.h>
#include <utils/classnamevalidatinglineedit.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>

#include <QCheckBox>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QFileInfo>
#include <QLabel>
#include <QListWidget>
#include <QPlainTextEdit>
#include <QPushButton>

using namespace QmlJSEditor::Internal;

namespace {

bool isCheckedByDefault(std::u16string_view property)
{
    static constexpr auto properitesCheckedByDefault = Utils::to_sorted_array<std::u16string_view>(
        u"x",
        u"y",
        u"anchors.alignWhenCentered",
        u"anchors.baseline",
        u"anchors.baselineOffset",
        u"anchors.bottom",
        u"anchors.bottomMargin",
        u"anchors.centerIn",
        u"anchors.fill",
        u"anchors.horizontalCenter",
        u"anchors.horizontalCenterOffset",
        u"anchors.left",
        u"anchors.leftMargin",
        u"anchors.margins",
        u"anchors.right",
        u"anchors.rightMargin",
        u"anchors.top",
        u"anchors.topMargin",
        u"anchors.verticalCenter",
        u"anchors.verticalCenterOffset",
        u"Layout.alignment",
        u"Layout.bottomMargin",
        u"Layout.column",
        u"Layout.columnSpan",
        u"Layout.fillHeight",
        u"Layout.fillWidth",
        u"Layout.horizontalStretchFactor",
        u"Layout.leftMargin",
        u"Layout.margins",
        u"Layout.maximumHeight",
        u"Layout.maximumWidth",
        u"Layout.minimumHeight",
        u"Layout.minimumWidth",
        u"Layout.preferredHeight",
        u"Layout.preferredWidth",
        u"Layout.rightMargin",
        u"Layout.row",
        u"Layout.rowSpan",
        u"Layout.topMargin",
        u"Layout.useDefaultSizePolicy",
        u"Layout.verticalStretchFactor",
        u"materials");

    return std::ranges::binary_search(properitesCheckedByDefault, property);
}
} // namespace

ComponentNameDialog::ComponentNameDialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowTitle(Tr::tr("Move Component into Separate File"));
    m_componentNameEdit = new Utils::ClassNameValidatingLineEdit;
    m_componentNameEdit->setObjectName("componentNameEdit");
    m_componentNameEdit->setPlaceholderText(Tr::tr("Component Name"));
    m_messageLabel = new QLabel;
    m_pathEdit = new Utils::PathChooser;
    m_label = new QLabel;
    m_listWidget = new QListWidget;
    m_plainTextEdit = new QPlainTextEdit;
    m_checkBox = new QCheckBox(Tr::tr("ui.qml file"));
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    using namespace Layouting;
    Column {
        Form {
            Tr::tr("Component name:"), m_componentNameEdit, br,
            empty, m_messageLabel, br,
            Tr::tr("Path:"), m_pathEdit, br,
        },
        m_label,
        Row { m_listWidget, m_plainTextEdit },
        Row { m_checkBox, m_buttonBox },
    }.attachTo(this);

    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &ComponentNameDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &ComponentNameDialog::reject);
    connect(m_pathEdit, &Utils::PathChooser::rawPathChanged, this, &ComponentNameDialog::validate);
    connect(m_pathEdit, &Utils::PathChooser::validChanged, this, &ComponentNameDialog::validate);
    connect(m_componentNameEdit, &QLineEdit::textChanged, this, &ComponentNameDialog::validate);
}

bool ComponentNameDialog::go(QString *proposedName,
                             QString *proposedPath,
                             QString *proposedSuffix,
                             const QStringList &properties,
                             const QStringList &sourcePreview,
                             const QString &oldFileName,
                             QStringList *result,
                             QWidget *parent)
{
    Q_ASSERT(proposedName);
    Q_ASSERT(proposedPath);

    const bool isUiFile = QFileInfo(oldFileName).completeSuffix() == "ui.qml";

    ComponentNameDialog d(parent);
    d.m_componentNameEdit->setNamespacesEnabled(false);
    d.m_componentNameEdit->setLowerCaseFileName(false);
    d.m_componentNameEdit->setForceFirstCapitalLetter(true);
    if (proposedName->isEmpty())
        *proposedName = QLatin1String("MyComponent");
    d.m_componentNameEdit->setText(*proposedName);
    d.m_pathEdit->setExpectedKind(Utils::PathChooser::ExistingDirectory);
    d.m_pathEdit->setHistoryCompleter("QmlJs.Component.History");
    d.m_pathEdit->setPath(*proposedPath);
    d.m_label->setText(Tr::tr("Property assignments for %1:").arg(oldFileName));
    d.m_checkBox->setChecked(isUiFile);
    d.m_checkBox->setVisible(isUiFile);
    d.m_sourcePreview = sourcePreview;

    d.setProperties(properties);

    d.generateCodePreview();

    d.connect(d.m_listWidget, &QListWidget::itemChanged, &d, &ComponentNameDialog::generateCodePreview);
    d.connect(d.m_componentNameEdit, &QLineEdit::textChanged, &d, &ComponentNameDialog::generateCodePreview);

    if (QDialog::Accepted == d.exec()) {
        *proposedName = d.m_componentNameEdit->text();
        *proposedPath = d.m_pathEdit->filePath().toUrlishString();

        if (d.m_checkBox->isChecked())
            *proposedSuffix = "ui.qml";
        else
            *proposedSuffix = "qml";

        if (result)
            *result = d.propertiesToKeep();
        return true;
    }

    return false;
}

void ComponentNameDialog::setProperties(const QStringList &properties)
{
    m_listWidget->addItems(properties);

    for (int i = 0; i < m_listWidget->count(); ++i) {
        QListWidgetItem *item = m_listWidget->item(i);
        item->setFlags(Qt::ItemIsUserCheckable | Qt:: ItemIsEnabled);
#if QT_VERSION > QT_VERSION_CHECK(6, 7, 0)
        if (isCheckedByDefault(item->text()))
#else
        const QString text = item->text();
        const QStringView view = text;

        if (isCheckedByDefault(
                std::u16string_view{view.utf16(), static_cast<std::size_t>(view.size())}))
#endif
            m_listWidget->item(i)->setCheckState(Qt::Checked);
        else
            m_listWidget->item(i)->setCheckState(Qt::Unchecked);
    }
}

QStringList ComponentNameDialog::propertiesToKeep() const
{
    QStringList result;
    for (int i = 0; i < m_listWidget->count(); ++i) {
        QListWidgetItem *item = m_listWidget->item(i);

        if (item->checkState() == Qt::Checked)
            result.append(item->text());
    }

    return result;
}

void ComponentNameDialog::generateCodePreview()
{
     const QString componentName = m_componentNameEdit->text();

     m_plainTextEdit->clear();
     m_plainTextEdit->appendPlainText(componentName + QLatin1String(" {"));
     if (!m_sourcePreview.first().isEmpty())
        m_plainTextEdit->appendPlainText(m_sourcePreview.first());

     for (int i = 0; i < m_listWidget->count(); ++i) {
         QListWidgetItem *item = m_listWidget->item(i);

         if (item->checkState() == Qt::Checked)
             m_plainTextEdit->appendPlainText(m_sourcePreview.at(i + 1));
     }

     m_plainTextEdit->appendPlainText(QLatin1String("}"));
}

void ComponentNameDialog::validate()
{
    const QString message = isValid();
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(message.isEmpty());
    m_messageLabel->setText(message);
}

QString ComponentNameDialog::isValid() const
{
    if (!m_componentNameEdit->isValid())
        return m_componentNameEdit->errorMessage();

    QString compName = m_componentNameEdit->text();
    if (compName.isEmpty() || !compName[0].isUpper())
        return Tr::tr("Invalid component name.");

    if (!m_pathEdit->isValid())
        return Tr::tr("Invalid path.");

    if (m_pathEdit->filePath().pathAppended(compName + ".qml").exists())
        return Tr::tr("Component already exists.");

    return QString();
}