2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-09-23 12:37:12 +02:00
|
|
|
|
|
|
|
#include "qmljscomponentnamedialog.h"
|
2022-08-25 12:46:47 +02:00
|
|
|
#include "qmljseditortr.h"
|
2010-09-23 12:37:12 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
#include <utils/classnamevalidatinglineedit.h>
|
|
|
|
#include <utils/layoutbuilder.h>
|
|
|
|
#include <utils/pathchooser.h>
|
|
|
|
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QDialogButtonBox>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileDialog>
|
2022-08-17 17:13:21 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QListWidget>
|
|
|
|
#include <QPlainTextEdit>
|
2012-04-11 17:58:59 +02:00
|
|
|
#include <QPushButton>
|
2010-09-23 12:37:12 +02:00
|
|
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
|
|
|
|
|
|
|
ComponentNameDialog::ComponentNameDialog(QWidget *parent) :
|
2022-08-17 17:13:21 +02:00
|
|
|
QDialog(parent)
|
2010-09-23 12:37:12 +02:00
|
|
|
{
|
2022-08-25 12:46:47 +02:00
|
|
|
setWindowTitle(Tr::tr("Move Component into Separate File"));
|
2022-08-17 17:13:21 +02:00
|
|
|
m_componentNameEdit = new Utils::ClassNameValidatingLineEdit;
|
2022-12-14 22:24:56 +01:00
|
|
|
m_componentNameEdit->setObjectName("componentNameEdit");
|
2022-08-25 12:46:47 +02:00
|
|
|
m_componentNameEdit->setPlaceholderText(Tr::tr("Component Name"));
|
2022-08-17 17:13:21 +02:00
|
|
|
m_messageLabel = new QLabel;
|
|
|
|
m_pathEdit = new Utils::PathChooser;
|
|
|
|
m_label = new QLabel;
|
|
|
|
m_listWidget = new QListWidget;
|
|
|
|
m_plainTextEdit = new QPlainTextEdit;
|
2022-08-25 12:46:47 +02:00
|
|
|
m_checkBox = new QCheckBox(Tr::tr("ui.qml file"));
|
2022-08-17 17:13:21 +02:00
|
|
|
m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
|
|
|
|
|
|
|
using namespace Utils::Layouting;
|
|
|
|
Column {
|
|
|
|
Form {
|
2022-08-25 12:46:47 +02:00
|
|
|
Tr::tr("Component name:"), m_componentNameEdit, br,
|
2022-08-17 17:13:21 +02:00
|
|
|
empty, m_messageLabel, br,
|
2022-08-25 12:46:47 +02:00
|
|
|
Tr::tr("Path:"), m_pathEdit, br,
|
2022-08-17 17:13:21 +02:00
|
|
|
},
|
|
|
|
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,
|
2016-06-27 22:25:11 +03:00
|
|
|
this, &ComponentNameDialog::validate);
|
2022-08-17 17:13:21 +02:00
|
|
|
connect(m_componentNameEdit, &QLineEdit::textChanged,
|
2016-06-27 22:25:11 +03:00
|
|
|
this, &ComponentNameDialog::validate);
|
2010-09-23 12:37:12 +02:00
|
|
|
}
|
|
|
|
|
2015-03-23 15:53:23 +01:00
|
|
|
bool ComponentNameDialog::go(QString *proposedName,
|
2010-09-23 12:37:12 +02:00
|
|
|
QString *proposedPath,
|
2016-09-29 15:38:29 +02:00
|
|
|
QString *proposedSuffix,
|
2016-04-29 10:31:21 +02:00
|
|
|
const QStringList &properties,
|
|
|
|
const QStringList &sourcePreview,
|
|
|
|
const QString &oldFileName,
|
|
|
|
QStringList *result,
|
2010-09-23 12:37:12 +02:00
|
|
|
QWidget *parent)
|
|
|
|
{
|
|
|
|
Q_ASSERT(proposedName);
|
|
|
|
Q_ASSERT(proposedPath);
|
|
|
|
|
2016-09-29 15:38:29 +02:00
|
|
|
const bool isUiFile = QFileInfo(oldFileName).completeSuffix() == "ui.qml";
|
|
|
|
|
2010-09-23 12:37:12 +02:00
|
|
|
ComponentNameDialog d(parent);
|
2022-08-17 17:13:21 +02:00
|
|
|
d.m_componentNameEdit->setNamespacesEnabled(false);
|
|
|
|
d.m_componentNameEdit->setLowerCaseFileName(false);
|
|
|
|
d.m_componentNameEdit->setForceFirstCapitalLetter(true);
|
2016-04-29 10:31:21 +02:00
|
|
|
if (proposedName->isEmpty())
|
|
|
|
*proposedName = QLatin1String("MyComponent");
|
2022-08-17 17:13:21 +02:00
|
|
|
d.m_componentNameEdit->setText(*proposedName);
|
|
|
|
d.m_pathEdit->setExpectedKind(Utils::PathChooser::ExistingDirectory);
|
|
|
|
d.m_pathEdit->setHistoryCompleter(QLatin1String("QmlJs.Component.History"));
|
|
|
|
d.m_pathEdit->setPath(*proposedPath);
|
2022-08-25 12:46:47 +02:00
|
|
|
d.m_label->setText(Tr::tr("Property assignments for %1:").arg(oldFileName));
|
2022-08-17 17:13:21 +02:00
|
|
|
d.m_checkBox->setChecked(isUiFile);
|
|
|
|
d.m_checkBox->setVisible(isUiFile);
|
2016-04-29 10:31:21 +02:00
|
|
|
d.m_sourcePreview = sourcePreview;
|
|
|
|
|
|
|
|
d.setProperties(properties);
|
|
|
|
|
|
|
|
d.generateCodePreview();
|
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
d.connect(d.m_listWidget, &QListWidget::itemChanged, &d, &ComponentNameDialog::generateCodePreview);
|
|
|
|
d.connect(d.m_componentNameEdit, &QLineEdit::textChanged, &d, &ComponentNameDialog::generateCodePreview);
|
2010-09-23 12:37:12 +02:00
|
|
|
|
|
|
|
if (QDialog::Accepted == d.exec()) {
|
2022-08-17 17:13:21 +02:00
|
|
|
*proposedName = d.m_componentNameEdit->text();
|
|
|
|
*proposedPath = d.m_pathEdit->filePath().toString();
|
2016-04-29 10:31:21 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
if (d.m_checkBox->isChecked())
|
2016-09-29 15:38:29 +02:00
|
|
|
*proposedSuffix = "ui.qml";
|
|
|
|
else
|
|
|
|
*proposedSuffix = "qml";
|
|
|
|
|
2016-04-29 10:31:21 +02:00
|
|
|
if (result)
|
|
|
|
*result = d.propertiesToKeep();
|
2015-03-23 15:53:23 +01:00
|
|
|
return true;
|
2010-09-23 12:37:12 +02:00
|
|
|
}
|
2015-03-23 15:53:23 +01:00
|
|
|
|
|
|
|
return false;
|
2010-09-23 12:37:12 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 10:31:21 +02:00
|
|
|
void ComponentNameDialog::setProperties(const QStringList &properties)
|
|
|
|
{
|
2022-08-17 17:13:21 +02:00
|
|
|
m_listWidget->addItems(properties);
|
2016-04-29 10:31:21 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
for (int i = 0; i < m_listWidget->count(); ++i) {
|
|
|
|
QListWidgetItem *item = m_listWidget->item(i);
|
2016-04-29 10:31:21 +02:00
|
|
|
item->setFlags(Qt::ItemIsUserCheckable | Qt:: ItemIsEnabled);
|
|
|
|
if (item->text() == QLatin1String("x")
|
|
|
|
|| item->text() == QLatin1String("y"))
|
2022-08-17 17:13:21 +02:00
|
|
|
m_listWidget->item(i)->setCheckState(Qt::Checked);
|
2016-04-29 10:31:21 +02:00
|
|
|
else
|
2022-08-17 17:13:21 +02:00
|
|
|
m_listWidget->item(i)->setCheckState(Qt::Unchecked);
|
2016-04-29 10:31:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ComponentNameDialog::propertiesToKeep() const
|
|
|
|
{
|
|
|
|
QStringList result;
|
2022-08-17 17:13:21 +02:00
|
|
|
for (int i = 0; i < m_listWidget->count(); ++i) {
|
|
|
|
QListWidgetItem *item = m_listWidget->item(i);
|
2016-04-29 10:31:21 +02:00
|
|
|
|
|
|
|
if (item->checkState() == Qt::Checked)
|
|
|
|
result.append(item->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentNameDialog::generateCodePreview()
|
|
|
|
{
|
2022-08-17 17:13:21 +02:00
|
|
|
const QString componentName = m_componentNameEdit->text();
|
2016-04-29 10:31:21 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
m_plainTextEdit->clear();
|
|
|
|
m_plainTextEdit->appendPlainText(componentName + QLatin1String(" {"));
|
2016-04-29 10:31:21 +02:00
|
|
|
if (!m_sourcePreview.first().isEmpty())
|
2022-08-17 17:13:21 +02:00
|
|
|
m_plainTextEdit->appendPlainText(m_sourcePreview.first());
|
2016-04-29 10:31:21 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
for (int i = 0; i < m_listWidget->count(); ++i) {
|
|
|
|
QListWidgetItem *item = m_listWidget->item(i);
|
2016-04-29 10:31:21 +02:00
|
|
|
|
|
|
|
if (item->checkState() == Qt::Checked)
|
2022-08-17 17:13:21 +02:00
|
|
|
m_plainTextEdit->appendPlainText(m_sourcePreview.at(i + 1));
|
2016-04-29 10:31:21 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
m_plainTextEdit->appendPlainText(QLatin1String("}"));
|
2016-04-29 10:31:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-23 12:37:12 +02:00
|
|
|
void ComponentNameDialog::validate()
|
|
|
|
{
|
2012-04-11 17:58:59 +02:00
|
|
|
const QString message = isValid();
|
2022-08-17 17:13:21 +02:00
|
|
|
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(message.isEmpty());
|
|
|
|
m_messageLabel->setText(message);
|
2010-09-23 12:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ComponentNameDialog::isValid() const
|
|
|
|
{
|
2022-08-17 17:13:21 +02:00
|
|
|
if (!m_componentNameEdit->isValid())
|
|
|
|
return m_componentNameEdit->errorMessage();
|
2012-04-11 17:58:59 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
QString compName = m_componentNameEdit->text();
|
2010-09-23 12:37:12 +02:00
|
|
|
if (compName.isEmpty() || !compName[0].isUpper())
|
2022-08-25 12:46:47 +02:00
|
|
|
return Tr::tr("Invalid component name.");
|
2010-09-23 12:37:12 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
if (!m_pathEdit->isValid())
|
2022-08-25 12:46:47 +02:00
|
|
|
return Tr::tr("Invalid path.");
|
2010-09-23 12:37:12 +02:00
|
|
|
|
2022-08-17 17:13:21 +02:00
|
|
|
if (m_pathEdit->filePath().pathAppended(compName + ".qml").exists())
|
2022-08-25 12:46:47 +02:00
|
|
|
return Tr::tr("Component already exists.");
|
2020-08-01 02:20:32 +02:00
|
|
|
|
2011-02-25 15:27:13 +01:00
|
|
|
return QString();
|
2010-09-23 12:37:12 +02:00
|
|
|
}
|