OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmleditorwidgets / filewidget.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "filewidget.h"
34
35 #include <QtGui/QLabel>
36 #include <QtGui/QToolButton>
37 #include <QtGui/QLineEdit>
38 #include <QtGui/QComboBox>
39
40 #include <QtGui/QHBoxLayout>
41 #include <QtGui/QFont>
42 #include <QtGui/QFileDialog>
43 #include <QtCore/QDirIterator>
44 #include <QtCore/QDebug>
45
46 namespace QmlEditorWidgets {
47
48 FileWidget::FileWidget(QWidget *parent) : QWidget(parent), m_filter("(*.*)"), m_showComboBox(false), m_lock(false)
49 {
50     m_pushButton = new QToolButton(this);
51     m_pushButton->setFixedWidth(32);
52     m_lineEdit = new QLineEdit(this);
53     m_comboBox = new QComboBox(this);
54     m_comboBox->hide();
55     QHBoxLayout *layout = new QHBoxLayout(this);
56     setLayout(layout);
57     layout->setContentsMargins(0, 0, 0, 0);
58     layout->addWidget(m_lineEdit);
59     layout->addWidget(m_comboBox);
60     m_comboBox->setEditable(true);
61     layout->addWidget(m_pushButton);
62     m_pushButton->setText("...");
63     connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(lineEditChanged()));
64     connect(m_pushButton, SIGNAL(released()), this, SLOT(onButtonReleased()));
65     connect(m_comboBox, SIGNAL(editTextChanged(const QString &)), this, SLOT(comboBoxChanged()));
66 }
67
68 FileWidget::~FileWidget()
69 {
70 }
71
72 void FileWidget::setShowComboBox(bool show)
73 {
74     m_showComboBox = show;
75     m_comboBox->setVisible(show);
76     m_lineEdit->setVisible(!show);
77 }
78
79 void FileWidget::lineEditChanged()
80 {
81     if (m_lock)
82         return;
83     setFileNameStr(m_lineEdit->text());
84 }
85
86 void FileWidget::comboBoxChanged()
87 {
88     if (m_lock)
89         return;
90     setFileNameStr(m_comboBox->currentText());
91 }
92
93 void FileWidget::onButtonReleased()
94 {
95     QString newFile = QFileDialog::getOpenFileName(0, tr("Open File"), m_path.toLocalFile(), m_filter);
96     if (!newFile.isEmpty())
97         setFileNameStr(newFile);
98 }
99
100 void FileWidget::setFileNameStr(const QString &fileName)
101 {
102     setFileName(QUrl(fileName));
103 }
104 void FileWidget::setFileName(const QUrl &fileName)
105 {
106     if (fileName == m_fileName)
107         return;
108
109     m_fileName = fileName;
110     if (m_lineEdit->text() != fileName.toString()) {
111         m_lineEdit->setText(fileName.toString());
112         m_lineEdit->setToolTip(m_fileName.toString());
113     }
114     if (m_comboBox->currentText() != fileName.toString()) {
115         m_comboBox->setEditText(m_fileName.toString());
116         m_comboBox->setToolTip(m_fileName.toString());
117     }
118     emit fileNameChanged(fileName);
119 }
120
121 void FileWidget::setupComboBox()
122 {
123     m_lock = true;
124     m_comboBox->clear();
125
126     QDir dir;
127
128
129     if (m_path.isValid())
130         dir = QDir(m_path.toLocalFile());
131
132     QStringList filterList = m_filter.split(' ');
133
134     QDirIterator it(dir.absolutePath(), filterList, QDir::Files, QDirIterator::Subdirectories);
135     while (it.hasNext()) {
136         QString absolutePath = it.next();
137         m_comboBox->addItem(dir.relativeFilePath(absolutePath));
138     }
139     m_comboBox->setEditText(m_fileName.toString());
140
141     m_lock = false;
142 }
143
144 } //QmlEditorWidgets
145
146