2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "pathchooser.h"
|
|
|
|
|
|
2013-03-18 14:47:33 +01:00
|
|
|
#include "fancylineedit.h"
|
2010-09-23 11:06:12 +02:00
|
|
|
#include "environment.h"
|
2008-12-09 15:25:01 +01:00
|
|
|
#include "qtcassert.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-02-22 12:25:19 +01:00
|
|
|
#include "synchronousprocess.h"
|
2012-12-20 17:14:18 +01:00
|
|
|
#include "hostosinfo.h"
|
2011-02-22 12:25:19 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QPushButton>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-03-02 17:13:33 +01:00
|
|
|
/*!
|
|
|
|
|
\class Utils::PathChooser
|
|
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The PathChooser class is a control that lets the user choose a path,
|
|
|
|
|
consisting of a QLineEdit and
|
2011-03-02 17:13:33 +01:00
|
|
|
a "Browse" button.
|
|
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
This class has some validation logic for embedding into QWizardPage.
|
2011-03-02 17:13:33 +01:00
|
|
|
*/
|
|
|
|
|
|
2009-04-17 09:03:32 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// ------------------ PathValidatingLineEdit
|
2010-09-23 11:06:12 +02:00
|
|
|
|
2014-02-13 16:13:54 +01:00
|
|
|
class PathValidatingLineEdit : public FancyLineEdit
|
2008-12-09 11:27:17 +01:00
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
public:
|
2008-12-05 18:51:07 +01:00
|
|
|
explicit PathValidatingLineEdit(PathChooser *chooser, QWidget *parent = 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual bool validate(const QString &value, QString *errorMessage) const;
|
2008-12-05 18:51:07 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PathChooser *m_chooser;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
PathValidatingLineEdit::PathValidatingLineEdit(PathChooser *chooser, QWidget *parent) :
|
2014-02-13 16:13:54 +01:00
|
|
|
FancyLineEdit(parent),
|
2008-12-05 18:51:07 +01:00
|
|
|
m_chooser(chooser)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2008-12-09 15:25:01 +01:00
|
|
|
QTC_ASSERT(chooser, return);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PathValidatingLineEdit::validate(const QString &value, QString *errorMessage) const
|
|
|
|
|
{
|
2008-12-05 18:51:07 +01:00
|
|
|
return m_chooser->validatePath(value, errorMessage);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-22 12:25:19 +01:00
|
|
|
// ------------------ BinaryVersionToolTipEventFilter
|
|
|
|
|
// Event filter to be installed on a lineedit used for entering
|
|
|
|
|
// executables, taking the arguments to print the version ('--version').
|
|
|
|
|
// On a tooltip event, the version is obtained by running the binary and
|
|
|
|
|
// setting its stdout as tooltip.
|
|
|
|
|
|
|
|
|
|
class BinaryVersionToolTipEventFilter : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BinaryVersionToolTipEventFilter(QLineEdit *le);
|
|
|
|
|
|
|
|
|
|
virtual bool eventFilter(QObject *, QEvent *);
|
|
|
|
|
|
|
|
|
|
QStringList arguments() const { return m_arguments; }
|
|
|
|
|
void setArguments(const QStringList &arguments) { m_arguments = arguments; }
|
|
|
|
|
|
|
|
|
|
static QString toolVersion(const QString &binary, const QStringList &arguments);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Extension point for concatenating existing tooltips.
|
|
|
|
|
virtual QString defaultToolTip() const { return QString(); }
|
|
|
|
|
|
|
|
|
|
QStringList m_arguments;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BinaryVersionToolTipEventFilter::BinaryVersionToolTipEventFilter(QLineEdit *le) :
|
|
|
|
|
QObject(le)
|
|
|
|
|
{
|
|
|
|
|
le->installEventFilter(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BinaryVersionToolTipEventFilter::eventFilter(QObject *o, QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (e->type() != QEvent::ToolTip)
|
|
|
|
|
return false;
|
|
|
|
|
QLineEdit *le = qobject_cast<QLineEdit *>(o);
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(le, return false);
|
2011-02-22 12:25:19 +01:00
|
|
|
|
|
|
|
|
const QString binary = le->text();
|
|
|
|
|
if (!binary.isEmpty()) {
|
|
|
|
|
const QString version = BinaryVersionToolTipEventFilter::toolVersion(QDir::cleanPath(binary), m_arguments);
|
|
|
|
|
if (!version.isEmpty()) {
|
|
|
|
|
// Concatenate tooltips.
|
|
|
|
|
QString tooltip = QLatin1String("<html><head/><body>");
|
|
|
|
|
const QString defaultValue = defaultToolTip();
|
|
|
|
|
if (!defaultValue.isEmpty()) {
|
|
|
|
|
tooltip += QLatin1String("<p>");
|
|
|
|
|
tooltip += defaultValue;
|
|
|
|
|
tooltip += QLatin1String("</p>");
|
|
|
|
|
}
|
|
|
|
|
tooltip += QLatin1String("<pre>");
|
|
|
|
|
tooltip += version;
|
|
|
|
|
tooltip += QLatin1String("</pre><body></html>");
|
|
|
|
|
le->setToolTip(tooltip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BinaryVersionToolTipEventFilter::toolVersion(const QString &binary, const QStringList &arguments)
|
|
|
|
|
{
|
|
|
|
|
if (binary.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
QProcess proc;
|
|
|
|
|
proc.start(binary, arguments);
|
|
|
|
|
if (!proc.waitForStarted())
|
|
|
|
|
return QString();
|
|
|
|
|
if (!proc.waitForFinished()) {
|
|
|
|
|
Utils::SynchronousProcess::stopProcess(proc);
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
2012-05-18 17:06:15 +02:00
|
|
|
return QString::fromLocal8Bit(QByteArray(proc.readAllStandardOutput()
|
|
|
|
|
+ proc.readAllStandardError()));
|
2011-02-22 12:25:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extends BinaryVersionToolTipEventFilter to prepend the existing pathchooser
|
|
|
|
|
// tooltip to display the full path.
|
|
|
|
|
class PathChooserBinaryVersionToolTipEventFilter : public BinaryVersionToolTipEventFilter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit PathChooserBinaryVersionToolTipEventFilter(PathChooser *pe) :
|
|
|
|
|
BinaryVersionToolTipEventFilter(pe->lineEdit()), m_pathChooser(pe) {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual QString defaultToolTip() const
|
|
|
|
|
{ return m_pathChooser->errorMessage(); }
|
|
|
|
|
|
|
|
|
|
const PathChooser *m_pathChooser;
|
|
|
|
|
};
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// ------------------ PathChooserPrivate
|
2010-09-23 11:06:12 +02:00
|
|
|
|
|
|
|
|
class PathChooserPrivate
|
2008-12-09 11:27:17 +01:00
|
|
|
{
|
2010-09-23 11:06:12 +02:00
|
|
|
public:
|
2008-12-05 18:51:07 +01:00
|
|
|
PathChooserPrivate(PathChooser *chooser);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-09-23 11:06:12 +02:00
|
|
|
QString expandedPath(const QString &path) const;
|
|
|
|
|
|
2009-04-17 09:03:32 +02:00
|
|
|
QHBoxLayout *m_hLayout;
|
2008-12-02 12:01:29 +01:00
|
|
|
PathValidatingLineEdit *m_lineEdit;
|
2011-02-21 12:42:20 +01:00
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
PathChooser::Kind m_acceptingKind;
|
|
|
|
|
QString m_dialogTitleOverride;
|
2009-06-29 14:47:04 +02:00
|
|
|
QString m_dialogFilter;
|
2008-12-19 18:25:20 +01:00
|
|
|
QString m_initialBrowsePathOverride;
|
2010-09-17 13:59:59 +02:00
|
|
|
QString m_baseDirectory;
|
2010-09-23 11:06:12 +02:00
|
|
|
Environment m_environment;
|
2011-02-22 12:25:19 +01:00
|
|
|
BinaryVersionToolTipEventFilter *m_binaryVersionToolTipEventFilter;
|
2012-06-21 16:05:30 +02:00
|
|
|
QList<QAbstractButton *> m_buttons;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
PathChooserPrivate::PathChooserPrivate(PathChooser *chooser) :
|
2009-04-17 09:03:32 +02:00
|
|
|
m_hLayout(new QHBoxLayout),
|
2008-12-05 18:51:07 +01:00
|
|
|
m_lineEdit(new PathValidatingLineEdit(chooser)),
|
2011-02-21 12:42:20 +01:00
|
|
|
m_acceptingKind(PathChooser::ExistingDirectory),
|
2011-02-22 12:25:19 +01:00
|
|
|
m_binaryVersionToolTipEventFilter(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 11:06:12 +02:00
|
|
|
QString PathChooserPrivate::expandedPath(const QString &input) const
|
|
|
|
|
{
|
2010-09-29 11:33:07 +02:00
|
|
|
if (input.isEmpty())
|
|
|
|
|
return input;
|
2011-04-29 13:39:43 +02:00
|
|
|
const QString path = QDir::cleanPath(m_environment.expandVariables(input));
|
2010-09-29 11:33:07 +02:00
|
|
|
if (path.isEmpty())
|
2010-09-23 11:06:12 +02:00
|
|
|
return path;
|
|
|
|
|
|
2010-09-29 11:33:07 +02:00
|
|
|
switch (m_acceptingKind) {
|
|
|
|
|
case PathChooser::Command:
|
|
|
|
|
case PathChooser::ExistingCommand: {
|
|
|
|
|
const QString expanded = m_environment.searchInPath(path, QStringList(m_baseDirectory));
|
2011-04-01 11:47:09 +02:00
|
|
|
return expanded.isEmpty() ? path : expanded;
|
2010-09-29 11:33:07 +02:00
|
|
|
}
|
|
|
|
|
case PathChooser::Any:
|
|
|
|
|
break;
|
|
|
|
|
case PathChooser::Directory:
|
2011-02-21 12:42:20 +01:00
|
|
|
case PathChooser::ExistingDirectory:
|
2010-09-29 11:33:07 +02:00
|
|
|
case PathChooser::File:
|
2013-04-05 09:46:05 -03:00
|
|
|
case PathChooser::SaveFile:
|
2010-09-29 11:33:07 +02:00
|
|
|
if (!m_baseDirectory.isEmpty() && QFileInfo(path).isRelative())
|
|
|
|
|
return QFileInfo(m_baseDirectory + QLatin1Char('/') + path).absoluteFilePath();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-09-23 11:06:12 +02:00
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
PathChooser::PathChooser(QWidget *parent) :
|
|
|
|
|
QWidget(parent),
|
2011-09-07 14:26:11 +02:00
|
|
|
d(new PathChooserPrivate(this))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_hLayout->setContentsMargins(0, 0, 0, 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
connect(d->m_lineEdit, SIGNAL(validReturnPressed()), this, SIGNAL(returnPressed()));
|
|
|
|
|
connect(d->m_lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(changed(QString)));
|
|
|
|
|
connect(d->m_lineEdit, SIGNAL(validChanged()), this, SIGNAL(validChanged()));
|
|
|
|
|
connect(d->m_lineEdit, SIGNAL(validChanged(bool)), this, SIGNAL(validChanged(bool)));
|
|
|
|
|
connect(d->m_lineEdit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
|
2013-07-13 14:44:26 +03:00
|
|
|
connect(d->m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged()));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-03-14 13:54:45 +01:00
|
|
|
d->m_lineEdit->setMinimumWidth(120);
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_hLayout->addWidget(d->m_lineEdit);
|
|
|
|
|
d->m_hLayout->setSizeConstraint(QLayout::SetMinimumSize);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-02-19 14:46:19 +01:00
|
|
|
addButton(browseButtonLabel(), this, SLOT(slotBrowse()));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
setLayout(d->m_hLayout);
|
|
|
|
|
setFocusProxy(d->m_lineEdit);
|
2011-09-13 13:12:34 +02:00
|
|
|
setFocusPolicy(d->m_lineEdit->focusPolicy());
|
2010-09-23 11:06:12 +02:00
|
|
|
setEnvironment(Environment::systemEnvironment());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PathChooser::~PathChooser()
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-04-17 09:03:32 +02:00
|
|
|
void PathChooser::addButton(const QString &text, QObject *receiver, const char *slotFunc)
|
2012-06-21 16:05:30 +02:00
|
|
|
{
|
|
|
|
|
insertButton(d->m_buttons.count(), text, receiver, slotFunc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::insertButton(int index, const QString &text, QObject *receiver, const char *slotFunc)
|
2009-04-17 09:03:32 +02:00
|
|
|
{
|
2011-02-28 15:29:24 +01:00
|
|
|
QPushButton *button = new QPushButton;
|
|
|
|
|
button->setText(text);
|
|
|
|
|
connect(button, SIGNAL(clicked()), receiver, slotFunc);
|
2012-06-21 16:05:30 +02:00
|
|
|
d->m_hLayout->insertWidget(index + 1/*line edit*/, button);
|
|
|
|
|
d->m_buttons.insert(index, button);
|
2009-04-17 09:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-19 14:46:19 +01:00
|
|
|
QString Utils::PathChooser::browseButtonLabel()
|
|
|
|
|
{
|
|
|
|
|
return HostOsInfo::isMacHost() ? tr("Choose...") : tr("Browse...");
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-22 15:18:42 +02:00
|
|
|
QAbstractButton *PathChooser::buttonAtIndex(int index) const
|
|
|
|
|
{
|
2012-06-21 16:05:30 +02:00
|
|
|
return d->m_buttons.at(index);
|
2009-07-22 15:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 13:59:59 +02:00
|
|
|
QString PathChooser::baseDirectory() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_baseDirectory;
|
2010-09-17 13:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::setBaseDirectory(const QString &directory)
|
|
|
|
|
{
|
2013-04-16 13:12:27 +02:00
|
|
|
if (d->m_baseDirectory == directory)
|
|
|
|
|
return;
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_baseDirectory = directory;
|
2014-03-05 12:37:03 +01:00
|
|
|
triggerChanged();
|
2010-09-17 13:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
2012-03-13 15:23:38 +01:00
|
|
|
FileName PathChooser::baseFileName() const
|
|
|
|
|
{
|
|
|
|
|
return Utils::FileName::fromString(d->m_baseDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::setBaseFileName(const FileName &base)
|
|
|
|
|
{
|
|
|
|
|
d->m_baseDirectory = base.toString();
|
2014-03-05 12:35:55 +01:00
|
|
|
triggerChanged();
|
2012-03-13 15:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
2010-09-23 11:06:12 +02:00
|
|
|
void PathChooser::setEnvironment(const Utils::Environment &env)
|
|
|
|
|
{
|
2010-10-06 13:38:16 +02:00
|
|
|
QString oldExpand = path();
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_environment = env;
|
2014-03-05 12:35:55 +01:00
|
|
|
if (path() != oldExpand) {
|
|
|
|
|
triggerChanged();
|
2010-10-06 13:38:16 +02:00
|
|
|
emit changed(rawPath());
|
2014-03-05 12:35:55 +01:00
|
|
|
}
|
2010-09-23 11:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QString PathChooser::path() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->expandedPath(QDir::fromNativeSeparators(d->m_lineEdit->text()));
|
2010-09-23 11:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathChooser::rawPath() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return QDir::fromNativeSeparators(d->m_lineEdit->text());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-13 16:35:53 +01:00
|
|
|
FileName PathChooser::fileName() const
|
|
|
|
|
{
|
|
|
|
|
return Utils::FileName::fromString(path());
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void PathChooser::setPath(const QString &path)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_lineEdit->setText(QDir::toNativeSeparators(path));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-13 16:35:53 +01:00
|
|
|
void PathChooser::setFileName(const Utils::FileName &fn)
|
|
|
|
|
{
|
|
|
|
|
d->m_lineEdit->setText(fn.toUserOutput());
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 12:45:21 +01:00
|
|
|
bool PathChooser::isReadOnly() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_lineEdit->isReadOnly();
|
2011-02-21 12:45:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::setReadOnly(bool b)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_lineEdit->setReadOnly(b);
|
2012-06-21 16:05:30 +02:00
|
|
|
foreach (QAbstractButton *button, d->m_buttons)
|
2011-02-28 15:29:24 +01:00
|
|
|
button->setEnabled(!b);
|
2011-02-21 12:45:21 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void PathChooser::slotBrowse()
|
|
|
|
|
{
|
2008-12-19 18:25:20 +01:00
|
|
|
emit beforeBrowsing();
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QString predefined = path();
|
2013-09-21 20:18:12 +03:00
|
|
|
QFileInfo fi(predefined);
|
|
|
|
|
|
|
|
|
|
if (!predefined.isEmpty() && !fi.isDir()) {
|
|
|
|
|
predefined = fi.path();
|
|
|
|
|
fi.setFile(predefined);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((predefined.isEmpty() || !fi.isDir())
|
2011-09-07 14:26:11 +02:00
|
|
|
&& !d->m_initialBrowsePathOverride.isNull()) {
|
|
|
|
|
predefined = d->m_initialBrowsePathOverride;
|
2013-09-21 20:18:12 +03:00
|
|
|
fi.setFile(predefined);
|
|
|
|
|
if (!fi.isDir()) {
|
2008-12-19 18:25:20 +01:00
|
|
|
predefined.clear();
|
2013-09-21 20:18:12 +03:00
|
|
|
fi.setFile(QString());
|
|
|
|
|
}
|
2008-12-19 18:25:20 +01:00
|
|
|
}
|
2008-12-05 18:51:07 +01:00
|
|
|
|
|
|
|
|
// Prompt for a file/dir
|
|
|
|
|
QString newPath;
|
2011-09-07 14:26:11 +02:00
|
|
|
switch (d->m_acceptingKind) {
|
2008-12-05 18:51:07 +01:00
|
|
|
case PathChooser::Directory:
|
2011-02-21 12:42:20 +01:00
|
|
|
case PathChooser::ExistingDirectory:
|
2008-12-05 18:51:07 +01:00
|
|
|
newPath = QFileDialog::getExistingDirectory(this,
|
2010-05-14 15:45:43 +02:00
|
|
|
makeDialogTitle(tr("Choose Directory")), predefined);
|
2008-12-05 18:51:07 +01:00
|
|
|
break;
|
2010-09-23 11:06:12 +02:00
|
|
|
case PathChooser::ExistingCommand:
|
2008-12-05 18:51:07 +01:00
|
|
|
case PathChooser::Command:
|
2010-09-23 11:06:12 +02:00
|
|
|
newPath = QFileDialog::getOpenFileName(this,
|
|
|
|
|
makeDialogTitle(tr("Choose Executable")), predefined,
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_dialogFilter);
|
2013-11-18 15:10:03 +01:00
|
|
|
if (HostOsInfo::hostOs() == OsTypeMac && newPath.endsWith(QLatin1String(".app"))) {
|
|
|
|
|
// possibly expand to Foo.app/Contents/MacOS/Foo
|
|
|
|
|
QFileInfo info(newPath);
|
|
|
|
|
if (info.isDir()) {
|
|
|
|
|
QString exePath = newPath + QLatin1String("/Contents/MacOS/") + info.completeBaseName();
|
|
|
|
|
if (QFileInfo(exePath).isExecutable())
|
|
|
|
|
newPath = exePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-23 11:06:12 +02:00
|
|
|
break;
|
|
|
|
|
case PathChooser::File: // fall through
|
2008-12-05 18:51:07 +01:00
|
|
|
newPath = QFileDialog::getOpenFileName(this,
|
2010-05-14 15:45:43 +02:00
|
|
|
makeDialogTitle(tr("Choose File")), predefined,
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_dialogFilter);
|
2008-12-05 18:51:07 +01:00
|
|
|
break;
|
2013-04-05 09:46:05 -03:00
|
|
|
case PathChooser::SaveFile:
|
|
|
|
|
newPath = QFileDialog::getSaveFileName(this,
|
|
|
|
|
makeDialogTitle(tr("Choose File")), predefined,
|
|
|
|
|
d->m_dialogFilter);
|
|
|
|
|
break;
|
2010-08-16 18:23:30 +02:00
|
|
|
case PathChooser::Any: {
|
|
|
|
|
QFileDialog dialog(this);
|
|
|
|
|
dialog.setFileMode(QFileDialog::AnyFile);
|
|
|
|
|
dialog.setWindowTitle(makeDialogTitle(tr("Choose File")));
|
|
|
|
|
if (fi.exists())
|
|
|
|
|
dialog.setDirectory(fi.absolutePath());
|
2010-09-16 12:50:18 +02:00
|
|
|
// FIXME: fix QFileDialog so that it filters properly: lib*.a
|
2011-09-07 14:26:11 +02:00
|
|
|
dialog.setNameFilter(d->m_dialogFilter);
|
2010-09-16 12:50:18 +02:00
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
|
|
|
// probably loop here until the *.framework dir match
|
2010-08-16 18:23:30 +02:00
|
|
|
QStringList paths = dialog.selectedFiles();
|
|
|
|
|
if (!paths.isEmpty())
|
|
|
|
|
newPath = paths.at(0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-12-05 18:51:07 +01:00
|
|
|
|
|
|
|
|
default:
|
2010-09-16 12:50:18 +02:00
|
|
|
break;
|
2008-12-05 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-19 15:32:21 +01:00
|
|
|
// Delete trailing slashes unless it is "/"|"\\", only
|
2008-12-09 11:27:17 +01:00
|
|
|
if (!newPath.isEmpty()) {
|
2008-12-19 15:32:21 +01:00
|
|
|
newPath = QDir::toNativeSeparators(newPath);
|
2008-12-09 11:27:17 +01:00
|
|
|
if (newPath.size() > 1 && newPath.endsWith(QDir::separator()))
|
|
|
|
|
newPath.truncate(newPath.size() - 1);
|
2008-12-02 12:01:29 +01:00
|
|
|
setPath(newPath);
|
|
|
|
|
}
|
2008-12-19 15:32:21 +01:00
|
|
|
|
|
|
|
|
emit browsingFinished();
|
2014-03-05 12:37:03 +01:00
|
|
|
triggerChanged();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-07-13 14:44:26 +03:00
|
|
|
void PathChooser::slotTextChanged()
|
|
|
|
|
{
|
|
|
|
|
emit pathChanged(path());
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool PathChooser::isValid() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_lineEdit->isValid();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathChooser::errorMessage() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_lineEdit->errorMessage();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-15 19:30:42 +01:00
|
|
|
void PathChooser::triggerChanged()
|
|
|
|
|
{
|
|
|
|
|
d->m_lineEdit->triggerChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool PathChooser::validatePath(const QString &path, QString *errorMessage)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
QString expandedPath = d->expandedPath(path);
|
2010-09-23 11:06:12 +02:00
|
|
|
|
2011-04-01 11:46:10 +02:00
|
|
|
if (path.isEmpty()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (errorMessage)
|
|
|
|
|
*errorMessage = tr("The path must not be empty.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-12-05 18:51:07 +01:00
|
|
|
|
2011-04-01 11:46:10 +02:00
|
|
|
if (expandedPath.isEmpty()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The path \"%1\" expanded to an empty string.").arg(QDir::toNativeSeparators(path));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-23 11:06:12 +02:00
|
|
|
const QFileInfo fi(expandedPath);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
// Check if existing
|
2011-09-07 14:26:11 +02:00
|
|
|
switch (d->m_acceptingKind) {
|
2011-02-21 12:42:20 +01:00
|
|
|
case PathChooser::ExistingDirectory: // fall through
|
2011-04-01 11:46:10 +02:00
|
|
|
if (!fi.exists()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The path \"%1\" does not exist.").arg(QDir::toNativeSeparators(expandedPath));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!fi.isDir()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The path \"%1\" is not a directory.").arg(QDir::toNativeSeparators(expandedPath));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2010-09-23 11:06:12 +02:00
|
|
|
case PathChooser::File: // fall through
|
2011-04-01 11:46:10 +02:00
|
|
|
if (!fi.exists()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The path \"%1\" does not exist.").arg(QDir::toNativeSeparators(expandedPath));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2013-04-05 09:46:05 -03:00
|
|
|
case PathChooser::SaveFile:
|
|
|
|
|
if (!fi.absoluteDir().exists()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The directory \"%1\" does not exist.").arg(QDir::toNativeSeparators(fi.absolutePath()));
|
2013-04-05 09:46:05 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2010-09-23 11:06:12 +02:00
|
|
|
case PathChooser::ExistingCommand:
|
2008-12-05 18:51:07 +01:00
|
|
|
if (!fi.exists()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The path \"%1\" does not exist.").arg(QDir::toNativeSeparators(expandedPath));
|
2008-12-05 18:51:07 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-04-01 11:46:10 +02:00
|
|
|
if (!fi.isExecutable()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("Cannot execute \"%1\".").arg(QDir::toNativeSeparators(expandedPath));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2008-12-05 18:51:07 +01:00
|
|
|
break;
|
2011-02-21 12:42:20 +01:00
|
|
|
case PathChooser::Directory:
|
2011-04-01 11:46:10 +02:00
|
|
|
if (fi.exists() && !fi.isDir()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("The path \"%1\" is not a directory.").arg(QDir::toNativeSeparators(expandedPath));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2008-12-05 18:51:07 +01:00
|
|
|
case PathChooser::Command: // fall through
|
2011-04-01 11:46:10 +02:00
|
|
|
if (fi.exists() && !fi.isExecutable()) {
|
|
|
|
|
if (errorMessage)
|
2014-04-17 14:09:47 +02:00
|
|
|
*errorMessage = tr("Cannot execute \"%1\".").arg(QDir::toNativeSeparators(expandedPath));
|
2011-04-01 11:46:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
default:
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check expected kind
|
2011-09-07 14:26:11 +02:00
|
|
|
switch (d->m_acceptingKind) {
|
2011-02-21 12:42:20 +01:00
|
|
|
case PathChooser::ExistingDirectory:
|
2010-03-15 16:15:36 +01:00
|
|
|
if (!fi.isDir()) {
|
2008-12-05 18:51:07 +01:00
|
|
|
if (errorMessage)
|
2010-09-23 11:06:12 +02:00
|
|
|
*errorMessage = tr("The path <b>%1</b> is not a directory.").arg(QDir::toNativeSeparators(expandedPath));
|
2008-12-05 18:51:07 +01:00
|
|
|
return false;
|
2008-12-09 11:27:17 +01:00
|
|
|
}
|
2008-12-05 18:51:07 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PathChooser::File:
|
2010-03-15 16:15:36 +01:00
|
|
|
if (!fi.isFile()) {
|
2008-12-05 18:51:07 +01:00
|
|
|
if (errorMessage)
|
2010-09-23 11:06:12 +02:00
|
|
|
*errorMessage = tr("The path <b>%1</b> is not a file.").arg(QDir::toNativeSeparators(expandedPath));
|
2008-12-05 18:51:07 +01:00
|
|
|
return false;
|
2008-12-09 11:27:17 +01:00
|
|
|
}
|
2008-12-05 18:51:07 +01:00
|
|
|
break;
|
|
|
|
|
|
2013-04-05 09:46:05 -03:00
|
|
|
case PathChooser::SaveFile:
|
|
|
|
|
if (fi.exists() && fi.isDir()) {
|
|
|
|
|
if (errorMessage)
|
|
|
|
|
*errorMessage = tr("The path <b>%1</b> is not a file.").arg(QDir::toNativeSeparators(fi.absolutePath()));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2010-09-23 11:06:12 +02:00
|
|
|
case PathChooser::ExistingCommand:
|
|
|
|
|
if (!fi.isFile() || !fi.isExecutable()) {
|
|
|
|
|
if (errorMessage)
|
2013-02-08 17:15:56 +01:00
|
|
|
*errorMessage = tr("The path <b>%1</b> is not an executable file.").arg(QDir::toNativeSeparators(expandedPath));
|
2010-09-23 11:06:12 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
case PathChooser::Command:
|
|
|
|
|
break;
|
|
|
|
|
|
2010-08-16 18:23:30 +02:00
|
|
|
case PathChooser::Any:
|
|
|
|
|
break;
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
default:
|
|
|
|
|
;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-09-29 12:03:14 +02:00
|
|
|
if (errorMessage)
|
|
|
|
|
*errorMessage = tr("Full path: <b>%1</b>").arg(QDir::toNativeSeparators(expandedPath));
|
2008-12-05 18:51:07 +01:00
|
|
|
return true;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathChooser::label()
|
|
|
|
|
{
|
|
|
|
|
return tr("Path:");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathChooser::homePath()
|
|
|
|
|
{
|
|
|
|
|
// Return 'users/<name>/Documents' on Windows, since Windows explorer
|
|
|
|
|
// does not let people actually display the contents of their home
|
|
|
|
|
// directory. Alternatively, create a QtCreator-specific directory?
|
2012-12-20 17:14:18 +01:00
|
|
|
if (HostOsInfo::isWindowsHost())
|
|
|
|
|
return QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
|
2008-12-02 12:01:29 +01:00
|
|
|
return QDir::homePath();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
void PathChooser::setExpectedKind(Kind expected)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
if (d->m_acceptingKind == expected)
|
2011-03-08 18:28:58 +01:00
|
|
|
return;
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_acceptingKind = expected;
|
|
|
|
|
d->m_lineEdit->triggerChanged();
|
2008-12-05 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
2009-06-29 14:47:04 +02:00
|
|
|
PathChooser::Kind PathChooser::expectedKind() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_acceptingKind;
|
2009-06-29 14:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
void PathChooser::setPromptDialogTitle(const QString &title)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_dialogTitleOverride = title;
|
2008-12-05 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
2009-06-29 14:47:04 +02:00
|
|
|
QString PathChooser::promptDialogTitle() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_dialogTitleOverride;
|
2009-06-29 14:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::setPromptDialogFilter(const QString &filter)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_dialogFilter = filter;
|
2009-06-29 14:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathChooser::promptDialogFilter() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_dialogFilter;
|
2009-06-29 14:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-19 18:25:20 +01:00
|
|
|
void PathChooser::setInitialBrowsePathBackup(const QString &path)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_initialBrowsePathOverride = path;
|
2008-12-19 18:25:20 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-05 18:51:07 +01:00
|
|
|
QString PathChooser::makeDialogTitle(const QString &title)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
if (d->m_dialogTitleOverride.isNull())
|
2008-12-05 18:51:07 +01:00
|
|
|
return title;
|
|
|
|
|
else
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_dialogTitleOverride;
|
2008-12-05 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
2012-08-23 13:55:11 +02:00
|
|
|
FancyLineEdit *PathChooser::lineEdit() const
|
2010-09-16 12:50:18 +02:00
|
|
|
{
|
|
|
|
|
// HACK: Make it work with HistoryCompleter.
|
2011-09-07 14:26:11 +02:00
|
|
|
if (d->m_lineEdit->objectName().isEmpty())
|
|
|
|
|
d->m_lineEdit->setObjectName(objectName() + QLatin1String("LineEdit"));
|
|
|
|
|
return d->m_lineEdit;
|
2010-09-16 12:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-22 12:25:19 +01:00
|
|
|
QString PathChooser::toolVersion(const QString &binary, const QStringList &arguments)
|
|
|
|
|
{
|
|
|
|
|
return BinaryVersionToolTipEventFilter::toolVersion(binary, arguments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::installLineEditVersionToolTip(QLineEdit *le, const QStringList &arguments)
|
|
|
|
|
{
|
|
|
|
|
BinaryVersionToolTipEventFilter *ef = new BinaryVersionToolTipEventFilter(le);
|
|
|
|
|
ef->setArguments(arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 18:18:03 +01:00
|
|
|
void PathChooser::setHistoryCompleter(const QString &historyKey)
|
|
|
|
|
{
|
|
|
|
|
d->m_lineEdit->setHistoryCompleter(historyKey);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-22 12:25:19 +01:00
|
|
|
QStringList PathChooser::commandVersionArguments() const
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
return d->m_binaryVersionToolTipEventFilter ?
|
|
|
|
|
d->m_binaryVersionToolTipEventFilter->arguments() :
|
2011-02-22 12:25:19 +01:00
|
|
|
QStringList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathChooser::setCommandVersionArguments(const QStringList &arguments)
|
|
|
|
|
{
|
|
|
|
|
if (arguments.isEmpty()) {
|
2011-09-07 14:26:11 +02:00
|
|
|
if (d->m_binaryVersionToolTipEventFilter) {
|
|
|
|
|
delete d->m_binaryVersionToolTipEventFilter;
|
|
|
|
|
d->m_binaryVersionToolTipEventFilter = 0;
|
2011-02-22 12:25:19 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2011-09-07 14:26:11 +02:00
|
|
|
if (!d->m_binaryVersionToolTipEventFilter)
|
|
|
|
|
d->m_binaryVersionToolTipEventFilter = new PathChooserBinaryVersionToolTipEventFilter(this);
|
|
|
|
|
d->m_binaryVersionToolTipEventFilter->setArguments(arguments);
|
2011-02-22 12:25:19 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
} // namespace Utils
|