OSDN Git Service

2f8f41bba89a672f43c7c2ce40929940dfd16c34
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmljscomponentnamedialog.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmljscomponentnamedialog.h"
35 #include "ui_qmljscomponentnamedialog.h"
36
37 #include <QtCore/QFileInfo>
38 #include <QtGui/QFileDialog>
39
40 using namespace QmlJSEditor::Internal;
41
42 ComponentNameDialog::ComponentNameDialog(QWidget *parent) :
43     QDialog(parent),
44     ui(new Ui::ComponentNameDialog)
45 {
46     ui->setupUi(this);
47
48     connect(ui->choosePathButton, SIGNAL(clicked()),
49             this, SLOT(choosePath()));
50     connect(ui->pathEdit, SIGNAL(textChanged(QString)),
51             this, SLOT(validate()));
52     connect(ui->componentNameEdit, SIGNAL(textChanged(QString)),
53             this, SLOT(validate()));
54 }
55
56 ComponentNameDialog::~ComponentNameDialog()
57 {
58     delete ui;
59 }
60
61 void ComponentNameDialog::go(QString *proposedName,
62                              QString *proposedPath,
63                              QWidget *parent)
64 {
65     Q_ASSERT(proposedName);
66     Q_ASSERT(proposedPath);
67
68     ComponentNameDialog d(parent);
69     d.ui->componentNameEdit->setText(*proposedName);
70     d.ui->pathEdit->setText(*proposedPath);
71
72     if (QDialog::Accepted == d.exec()) {
73         *proposedName = d.ui->componentNameEdit->text();
74         *proposedPath = d.ui->pathEdit->text();
75     }
76 }
77
78 void ComponentNameDialog::choosePath()
79 {
80     QString dir = QFileDialog::getExistingDirectory(this, tr("Choose a path"),
81                                                     ui->pathEdit->text());
82     if (!dir.isEmpty())
83         ui->pathEdit->setText(dir);
84 }
85
86 void ComponentNameDialog::validate()
87 {
88     const QString msg = isValid();
89     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(msg.isEmpty());
90     ui->messageLabel->setText(msg);
91 }
92
93 QString ComponentNameDialog::isValid() const
94 {
95     QString compName = ui->componentNameEdit->text();
96     if (compName.isEmpty() || !compName[0].isUpper())
97         return tr("Invalid component name");
98
99     QString path = ui->pathEdit->text();
100     if (path.isEmpty() || !QFileInfo(path).isDir())
101         return tr("Invalid path");
102
103     return QString();
104 }