1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "filewidget.h"
35 #include <QtGui/QLabel>
36 #include <QtGui/QToolButton>
37 #include <QtGui/QLineEdit>
38 #include <QtGui/QComboBox>
40 #include <QtGui/QHBoxLayout>
41 #include <QtGui/QFont>
42 #include <QtGui/QFileDialog>
43 #include <QtCore/QDirIterator>
44 #include <QtCore/QDebug>
46 namespace QmlEditorWidgets {
48 FileWidget::FileWidget(QWidget *parent) : QWidget(parent), m_filter("(*.*)"), m_showComboBox(false), m_lock(false)
50 m_pushButton = new QToolButton(this);
51 m_pushButton->setFixedWidth(32);
52 m_lineEdit = new QLineEdit(this);
53 m_comboBox = new QComboBox(this);
55 QHBoxLayout *layout = new QHBoxLayout(this);
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()));
68 FileWidget::~FileWidget()
72 void FileWidget::setShowComboBox(bool show)
74 m_showComboBox = show;
75 m_comboBox->setVisible(show);
76 m_lineEdit->setVisible(!show);
79 void FileWidget::lineEditChanged()
83 setFileNameStr(m_lineEdit->text());
86 void FileWidget::comboBoxChanged()
90 setFileNameStr(m_comboBox->currentText());
93 void FileWidget::onButtonReleased()
95 QString newFile = QFileDialog::getOpenFileName(0, tr("Open File"), m_path.toLocalFile(), m_filter);
96 if (!newFile.isEmpty())
97 setFileNameStr(newFile);
100 void FileWidget::setFileNameStr(const QString &fileName)
102 setFileName(QUrl(fileName));
104 void FileWidget::setFileName(const QUrl &fileName)
106 if (fileName == m_fileName)
109 m_fileName = fileName;
110 if (m_lineEdit->text() != fileName.toString()) {
111 m_lineEdit->setText(fileName.toString());
112 m_lineEdit->setToolTip(m_fileName.toString());
114 if (m_comboBox->currentText() != fileName.toString()) {
115 m_comboBox->setEditText(m_fileName.toString());
116 m_comboBox->setToolTip(m_fileName.toString());
118 emit fileNameChanged(fileName);
121 void FileWidget::setupComboBox()
129 if (m_path.isValid())
130 dir = QDir(m_path.toLocalFile());
132 QStringList filterList = m_filter.split(' ');
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));
139 m_comboBox->setEditText(m_fileName.toString());