diff --git a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp index 3f08786114f..85c5d679c60 100644 --- a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp +++ b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp @@ -28,6 +28,7 @@ **************************************************************************/ #include "qmljscomponentfromobjectdef.h" +#include "qmljscomponentnamedialog.h" #include "qmljsrefactoringchanges.h" #include @@ -86,16 +87,30 @@ public: Q_ASSERT(m_objDef != 0); m_componentName = getIdProperty(m_objDef); - m_componentName[0] = m_componentName.at(0).toUpper(); - setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef", - "Move Component into '%1.qml'").arg(m_componentName)); + if (m_componentName.isEmpty()) { + setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef", + "Move Component into separate file'")); + } else { + m_componentName[0] = m_componentName.at(0).toUpper(); + setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef", + "Move Component into '%1.qml'").arg(m_componentName)); + } } virtual void performChanges(QmlJSRefactoringFile *currentFile, QmlJSRefactoringChanges *refactoring) { - const QString newFileName = QFileInfo(fileName()).path() - + QDir::separator() + m_componentName + QLatin1String(".qml"); + QString componentName = m_componentName; + QString path = QFileInfo(fileName()).path(); + if (componentName.isEmpty()) { + ComponentNameDialog::go(&componentName, &path, state().editor()); + } + + if (componentName.isEmpty() || path.isEmpty()) + return; + + const QString newFileName = path + QDir::separator() + componentName + + QLatin1String(".qml"); QString imports; UiProgram *prog = currentFile->qmljsDocument()->qmlProgram(); @@ -115,7 +130,7 @@ public: return; Utils::ChangeSet changes; - changes.replace(start, end, m_componentName + QLatin1String(" {\n")); + changes.replace(start, end, componentName + QLatin1String(" {\n")); currentFile->change(changes); currentFile->indent(Range(start, end + 1)); } @@ -134,10 +149,8 @@ QList ComponentFromObjectDef::match(const QmlJSQuic if (UiObjectDefinition *objDef = cast(node)) { // check that the node is not the root node if (i > 0 && !cast(path.at(i - 1))) { - if (!getIdProperty(objDef).isEmpty()) { - result.append(QmlJSQuickFixOperation::Ptr(new Operation(state, objDef))); - return result; - } + result.append(QmlJSQuickFixOperation::Ptr(new Operation(state, objDef))); + return result; } } } diff --git a/src/plugins/qmljseditor/qmljscomponentnamedialog.cpp b/src/plugins/qmljseditor/qmljscomponentnamedialog.cpp new file mode 100644 index 00000000000..54afa2a6a6a --- /dev/null +++ b/src/plugins/qmljseditor/qmljscomponentnamedialog.cpp @@ -0,0 +1,100 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "qmljscomponentnamedialog.h" +#include "ui_qmljscomponentnamedialog.h" + +#include +#include + +using namespace QmlJSEditor::Internal; + +ComponentNameDialog::ComponentNameDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ComponentNameDialog) +{ + ui->setupUi(this); + + connect(ui->choosePathButton, SIGNAL(clicked()), + this, SLOT(choosePath())); + connect(ui->pathEdit, SIGNAL(textChanged(QString)), + this, SLOT(validate())); + connect(ui->componentNameEdit, SIGNAL(textChanged(QString)), + this, SLOT(validate())); +} + +ComponentNameDialog::~ComponentNameDialog() +{ + delete ui; +} + +void ComponentNameDialog::go(QString *proposedName, + QString *proposedPath, + QWidget *parent) +{ + Q_ASSERT(proposedName); + Q_ASSERT(proposedPath); + + ComponentNameDialog d(parent); + d.ui->componentNameEdit->setText(*proposedName); + d.ui->pathEdit->setText(*proposedPath); + + if (QDialog::Accepted == d.exec()) { + *proposedName = d.ui->componentNameEdit->text(); + *proposedPath = d.ui->pathEdit->text(); + } +} + +void ComponentNameDialog::choosePath() +{ + QString dir = QFileDialog::getExistingDirectory(this, tr("Choose a path"), + ui->pathEdit->text()); + if (!dir.isEmpty()) + ui->pathEdit->setText(dir); +} + +void ComponentNameDialog::validate() +{ + const QString msg = isValid(); + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(msg.isEmpty()); + ui->messageLabel->setText(msg); +} + +QString ComponentNameDialog::isValid() const +{ + QString compName = ui->componentNameEdit->text(); + if (compName.isEmpty() || !compName[0].isUpper()) + return tr("Invalid component name"); + + QString path = ui->pathEdit->text(); + if (path.isEmpty() || !QFileInfo(path).isDir()) + return tr("Invalid path"); + + return QString::null; +} diff --git a/src/plugins/qmljseditor/qmljscomponentnamedialog.h b/src/plugins/qmljseditor/qmljscomponentnamedialog.h new file mode 100644 index 00000000000..d9523584d31 --- /dev/null +++ b/src/plugins/qmljseditor/qmljscomponentnamedialog.h @@ -0,0 +1,68 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef QMLJSCOMPONENTNAMEDIALOG_H +#define QMLJSCOMPONENTNAMEDIALOG_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { + class ComponentNameDialog; +} +QT_END_NAMESPACE + +namespace QmlJSEditor { +namespace Internal { + +class ComponentNameDialog : public QDialog +{ + Q_OBJECT + +public: + explicit ComponentNameDialog(QWidget *parent = 0); + ~ComponentNameDialog(); + + static void go(QString *proposedName, QString *proposedPath, QWidget *parent = 0); + +public slots: + void choosePath(); + void validate(); + +protected: + QString isValid() const; + +private: + Ui::ComponentNameDialog *ui; +}; + +} // namespace Internal +} // namespace QmlJSEditor + +#endif // QMLJSCOMPONENTNAMEDIALOG_H diff --git a/src/plugins/qmljseditor/qmljscomponentnamedialog.ui b/src/plugins/qmljseditor/qmljscomponentnamedialog.ui new file mode 100644 index 00000000000..3b9171ceec6 --- /dev/null +++ b/src/plugins/qmljseditor/qmljscomponentnamedialog.ui @@ -0,0 +1,124 @@ + + + ComponentNameDialog + + + + 0 + 0 + 495 + 130 + + + + Dialog + + + + 0 + + + + + 8 + + + 10 + + + + + Component name: + + + + + + + + + + Path: + + + + + + + + + + Choose... + + + + + + + + + + + + + + + + Qt::Vertical + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + ComponentNameDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ComponentNameDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/plugins/qmljseditor/qmljseditor.pro b/src/plugins/qmljseditor/qmljseditor.pro index f51e277e55b..976a8292819 100644 --- a/src/plugins/qmljseditor/qmljseditor.pro +++ b/src/plugins/qmljseditor/qmljseditor.pro @@ -30,7 +30,8 @@ HEADERS += \ qmljseditorcodeformatter.h \ qmljsoutlinetreeview.h \ quicktoolbarsettingspage.h \ - quicktoolbar.h + quicktoolbar.h \ + qmljscomponentnamedialog.h SOURCES += \ qmljscodecompletion.cpp \ @@ -54,10 +55,12 @@ SOURCES += \ qmljseditorcodeformatter.cpp \ qmljsoutlinetreeview.cpp \ quicktoolbarsettingspage.cpp \ - quicktoolbar.cpp + quicktoolbar.cpp \ + qmljscomponentnamedialog.cpp RESOURCES += qmljseditor.qrc OTHER_FILES += QmlJSEditor.pluginspec QmlJSEditor.mimetypes.xml FORMS += \ - quicktoolbarsettingspage.ui + quicktoolbarsettingspage.ui \ + qmljscomponentnamedialog.ui