aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmljseditor/qmljscomponentnamedialog.cpp
blob: 4917e8c59aceb3e00b9d5c8ed6d8c7d681bde528 (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
// 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 <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;

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().toString();

        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 (item->text() == QLatin1String("x")
                || item->text() == QLatin1String("y"))
            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();
}