Files
qt-creator/src/libs/utils/pathchooser.cpp

616 lines
18 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2011-01-11 16:28:15 +01:00
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
2011-04-13 08:42:33 +02:00
** Contact: Nokia Corporation (info@qt.nokia.com)
2008-12-02 12:01:29 +01:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** 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.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include "pathchooser.h"
2008-12-09 15:25:01 +01:00
#include "basevalidatinglineedit.h"
#include "environment.h"
2008-12-09 15:25:01 +01:00
#include "qtcassert.h"
2008-12-02 12:01:29 +01:00
#include "synchronousprocess.h"
2008-12-09 15:25:01 +01:00
#include <QtCore/QDebug>
2008-12-02 12:01:29 +01:00
#include <QtCore/QDir>
2008-12-09 15:25:01 +01:00
#include <QtCore/QFileInfo>
2008-12-02 12:01:29 +01:00
#include <QtCore/QSettings>
#include <QtCore/QProcess>
2008-12-09 15:25:01 +01:00
#include <QtGui/qevent.h>
2008-12-09 15:25:01 +01:00
#include <QtGui/QDesktopServices>
#include <QtGui/QFileDialog>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
2008-12-02 12:01:29 +01:00
/*!
\class Utils::PathChooser
\brief A control that let's the user choose a path, consisting of a QLineEdit and
a "Browse" button.
Has some validation logic for embedding into QWizardPage.
*/
/*static*/ const char * const Utils::PathChooser::browseButtonLabel =
#ifdef Q_WS_MAC
QT_TRANSLATE_NOOP("Utils::PathChooser", "Choose...");
#else
QT_TRANSLATE_NOOP("Utils::PathChooser", "Browse...");
#endif
namespace Utils {
2008-12-02 12:01:29 +01:00
// ------------------ PathValidatingLineEdit
2008-12-09 11:27:17 +01:00
class PathValidatingLineEdit : public BaseValidatingLineEdit
{
2008-12-02 12:01:29 +01:00
public:
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;
private:
PathChooser *m_chooser;
2008-12-02 12:01:29 +01:00
};
PathValidatingLineEdit::PathValidatingLineEdit(PathChooser *chooser, QWidget *parent) :
BaseValidatingLineEdit(parent),
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
{
return m_chooser->validatePath(value, errorMessage);
2008-12-02 12:01:29 +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);
QTC_ASSERT(le, return false; )
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();
}
return QString::fromLocal8Bit(proc.readAllStandardOutput());
}
// 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
class PathChooserPrivate
2008-12-09 11:27:17 +01:00
{
public:
PathChooserPrivate(PathChooser *chooser);
2008-12-02 12:01:29 +01:00
QString expandedPath(const QString &path) const;
QHBoxLayout *m_hLayout;
2008-12-02 12:01:29 +01:00
PathValidatingLineEdit *m_lineEdit;
PathChooser::Kind m_acceptingKind;
QString m_dialogTitleOverride;
QString m_dialogFilter;
QString m_initialBrowsePathOverride;
QString m_baseDirectory;
Environment m_environment;
BinaryVersionToolTipEventFilter *m_binaryVersionToolTipEventFilter;
2008-12-02 12:01:29 +01:00
};
PathChooserPrivate::PathChooserPrivate(PathChooser *chooser) :
m_hLayout(new QHBoxLayout),
m_lineEdit(new PathValidatingLineEdit(chooser)),
m_acceptingKind(PathChooser::ExistingDirectory),
m_binaryVersionToolTipEventFilter(0)
2008-12-02 12:01:29 +01:00
{
}
QString PathChooserPrivate::expandedPath(const QString &input) const
{
if (input.isEmpty())
return input;
const QString path = QDir::cleanPath(m_environment.expandVariables(input));
if (path.isEmpty())
return path;
switch (m_acceptingKind) {
case PathChooser::Command:
case PathChooser::ExistingCommand: {
const QString expanded = m_environment.searchInPath(path, QStringList(m_baseDirectory));
return expanded.isEmpty() ? path : expanded;
}
case PathChooser::Any:
break;
case PathChooser::Directory:
case PathChooser::ExistingDirectory:
case PathChooser::File:
if (!m_baseDirectory.isEmpty() && QFileInfo(path).isRelative())
return QFileInfo(m_baseDirectory + QLatin1Char('/') + path).absoluteFilePath();
break;
}
return path;
}
2008-12-02 12:01:29 +01:00
PathChooser::PathChooser(QWidget *parent) :
QWidget(parent),
m_d(new PathChooserPrivate(this))
2008-12-02 12:01:29 +01:00
{
m_d->m_hLayout->setContentsMargins(0, 0, 0, 0);
2008-12-02 12:01:29 +01:00
connect(m_d->m_lineEdit, SIGNAL(validReturnPressed()), this, SIGNAL(returnPressed()));
connect(m_d->m_lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(changed(QString)));
2008-12-02 12:01:29 +01:00
connect(m_d->m_lineEdit, SIGNAL(validChanged()), this, SIGNAL(validChanged()));
connect(m_d->m_lineEdit, SIGNAL(validChanged(bool)), this, SIGNAL(validChanged(bool)));
connect(m_d->m_lineEdit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
2008-12-02 12:01:29 +01:00
2009-01-28 17:38:41 +01:00
m_d->m_lineEdit->setMinimumWidth(200);
m_d->m_hLayout->addWidget(m_d->m_lineEdit);
m_d->m_hLayout->setSizeConstraint(QLayout::SetMinimumSize);
2008-12-02 12:01:29 +01:00
addButton(tr(browseButtonLabel), this, SLOT(slotBrowse()));
2008-12-02 12:01:29 +01:00
setLayout(m_d->m_hLayout);
2008-12-02 12:01:29 +01:00
setFocusProxy(m_d->m_lineEdit);
setEnvironment(Environment::systemEnvironment());
2008-12-02 12:01:29 +01:00
}
PathChooser::~PathChooser()
{
delete m_d;
}
void PathChooser::addButton(const QString &text, QObject *receiver, const char *slotFunc)
{
QPushButton *button = new QPushButton;
button->setText(text);
connect(button, SIGNAL(clicked()), receiver, slotFunc);
m_d->m_hLayout->addWidget(button);
}
2009-07-22 15:18:42 +02:00
QAbstractButton *PathChooser::buttonAtIndex(int index) const
{
return findChildren<QAbstractButton*>().at(index);
}
QString PathChooser::baseDirectory() const
{
return m_d->m_baseDirectory;
}
void PathChooser::setBaseDirectory(const QString &directory)
{
m_d->m_baseDirectory = directory;
}
void PathChooser::setEnvironment(const Utils::Environment &env)
{
QString oldExpand = path();
m_d->m_environment = env;
if (path() != oldExpand)
emit changed(rawPath());
}
2008-12-02 12:01:29 +01:00
QString PathChooser::path() const
{
return m_d->expandedPath(QDir::fromNativeSeparators(m_d->m_lineEdit->text()));
}
QString PathChooser::rawPath() const
{
return QDir::fromNativeSeparators(m_d->m_lineEdit->text());
2008-12-02 12:01:29 +01:00
}
void PathChooser::setPath(const QString &path)
{
m_d->m_lineEdit->setText(QDir::toNativeSeparators(path));
2008-12-02 12:01:29 +01:00
}
bool PathChooser::isReadOnly() const
{
return m_d->m_lineEdit->isReadOnly();
}
void PathChooser::setReadOnly(bool b)
{
m_d->m_lineEdit->setReadOnly(b);
const QList<QAbstractButton *> &allButtons = findChildren<QAbstractButton *>();
foreach (QAbstractButton *button, allButtons)
button->setEnabled(!b);
}
2008-12-02 12:01:29 +01:00
void PathChooser::slotBrowse()
{
emit beforeBrowsing();
2008-12-02 12:01:29 +01:00
QString predefined = path();
if ((predefined.isEmpty() || !QFileInfo(predefined).isDir())
&& !m_d->m_initialBrowsePathOverride.isNull()) {
predefined = m_d->m_initialBrowsePathOverride;
if (!QFileInfo(predefined).isDir())
predefined.clear();
}
// Prompt for a file/dir
QString newPath;
switch (m_d->m_acceptingKind) {
case PathChooser::Directory:
case PathChooser::ExistingDirectory:
newPath = QFileDialog::getExistingDirectory(this,
makeDialogTitle(tr("Choose Directory")), predefined);
break;
case PathChooser::ExistingCommand:
case PathChooser::Command:
newPath = QFileDialog::getOpenFileName(this,
makeDialogTitle(tr("Choose Executable")), predefined,
m_d->m_dialogFilter);
break;
case PathChooser::File: // fall through
newPath = QFileDialog::getOpenFileName(this,
makeDialogTitle(tr("Choose File")), predefined,
m_d->m_dialogFilter);
break;
case PathChooser::Any: {
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setWindowTitle(makeDialogTitle(tr("Choose File")));
QFileInfo fi(predefined);
if (fi.exists())
dialog.setDirectory(fi.absolutePath());
// FIXME: fix QFileDialog so that it filters properly: lib*.a
dialog.setNameFilter(m_d->m_dialogFilter);
if (dialog.exec() == QDialog::Accepted) {
// probably loop here until the *.framework dir match
QStringList paths = dialog.selectedFiles();
if (!paths.isEmpty())
newPath = paths.at(0);
}
break;
}
default:
break;
}
// Delete trailing slashes unless it is "/"|"\\", only
2008-12-09 11:27:17 +01:00
if (!newPath.isEmpty()) {
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);
}
emit browsingFinished();
m_d->m_lineEdit->triggerChanged();
2008-12-02 12:01:29 +01:00
}
bool PathChooser::isValid() const
{
return m_d->m_lineEdit->isValid();
}
QString PathChooser::errorMessage() const
{
2008-12-09 11:27:17 +01:00
return m_d->m_lineEdit->errorMessage();
2008-12-02 12:01:29 +01:00
}
bool PathChooser::validatePath(const QString &path, QString *errorMessage)
{
QString expandedPath = m_d->expandedPath(path);
if (path.isEmpty()) {
2008-12-02 12:01:29 +01:00
if (errorMessage)
*errorMessage = tr("The path must not be empty.");
return false;
}
if (expandedPath.isEmpty()) {
if (errorMessage)
*errorMessage = tr("The path '%1' expanded to an empty string.").arg(QDir::toNativeSeparators(path));
return false;
}
const QFileInfo fi(expandedPath);
2008-12-02 12:01:29 +01:00
// Check if existing
switch (m_d->m_acceptingKind) {
case PathChooser::ExistingDirectory: // fall through
if (!fi.exists()) {
if (errorMessage)
*errorMessage = tr("The path '%1' does not exist.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
if (!fi.isDir()) {
if (errorMessage)
*errorMessage = tr("The path '%1' is not a directory.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
break;
case PathChooser::File: // fall through
if (!fi.exists()) {
if (errorMessage)
*errorMessage = tr("The path '%1' does not exist.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
break;
case PathChooser::ExistingCommand:
if (!fi.exists()) {
if (errorMessage)
*errorMessage = tr("The path '%1' does not exist.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
if (!fi.isExecutable()) {
if (errorMessage)
*errorMessage = tr("Cannot execute '%1'.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
break;
case PathChooser::Directory:
if (fi.exists() && !fi.isDir()) {
if (errorMessage)
*errorMessage = tr("The path '%1' is not a directory.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
break;
case PathChooser::Command: // fall through
if (fi.exists() && !fi.isExecutable()) {
if (errorMessage)
*errorMessage = tr("Cannot execute '%1'.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
break;
default:
;
}
// Check expected kind
switch (m_d->m_acceptingKind) {
case PathChooser::ExistingDirectory:
if (!fi.isDir()) {
if (errorMessage)
*errorMessage = tr("The path <b>%1</b> is not a directory.").arg(QDir::toNativeSeparators(expandedPath));
return false;
2008-12-09 11:27:17 +01:00
}
break;
case PathChooser::File:
if (!fi.isFile()) {
if (errorMessage)
*errorMessage = tr("The path <b>%1</b> is not a file.").arg(QDir::toNativeSeparators(expandedPath));
return false;
2008-12-09 11:27:17 +01:00
}
break;
case PathChooser::ExistingCommand:
if (!fi.isFile() || !fi.isExecutable()) {
if (errorMessage)
*errorMessage = tr("The path <b>%1</b> is not a executable file.").arg(QDir::toNativeSeparators(expandedPath));
return false;
}
case PathChooser::Command:
break;
case PathChooser::Any:
break;
default:
;
2008-12-02 12:01:29 +01:00
}
if (errorMessage)
*errorMessage = tr("Full path: <b>%1</b>").arg(QDir::toNativeSeparators(expandedPath));
return true;
2008-12-02 12:01:29 +01:00
}
QString PathChooser::label()
{
return tr("Path:");
}
QString PathChooser::homePath()
{
#ifdef Q_OS_WIN
// 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?
return QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
#else
return QDir::homePath();
#endif
}
void PathChooser::setExpectedKind(Kind expected)
{
if (m_d->m_acceptingKind == expected)
return;
m_d->m_acceptingKind = expected;
m_d->m_lineEdit->triggerChanged();
}
PathChooser::Kind PathChooser::expectedKind() const
{
return m_d->m_acceptingKind;
}
void PathChooser::setPromptDialogTitle(const QString &title)
{
m_d->m_dialogTitleOverride = title;
}
QString PathChooser::promptDialogTitle() const
{
return m_d->m_dialogTitleOverride;
}
void PathChooser::setPromptDialogFilter(const QString &filter)
{
m_d->m_dialogFilter = filter;
}
QString PathChooser::promptDialogFilter() const
{
return m_d->m_dialogFilter;
}
void PathChooser::setInitialBrowsePathBackup(const QString &path)
{
m_d->m_initialBrowsePathOverride = path;
}
QString PathChooser::makeDialogTitle(const QString &title)
{
if (m_d->m_dialogTitleOverride.isNull())
return title;
else
return m_d->m_dialogTitleOverride;
}
QLineEdit *PathChooser::lineEdit() const
{
// HACK: Make it work with HistoryCompleter.
if (m_d->m_lineEdit->objectName().isEmpty())
m_d->m_lineEdit->setObjectName(objectName() + QLatin1String("LineEdit"));
return m_d->m_lineEdit;
}
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);
}
QStringList PathChooser::commandVersionArguments() const
{
return m_d->m_binaryVersionToolTipEventFilter ?
m_d->m_binaryVersionToolTipEventFilter->arguments() :
QStringList();
}
void PathChooser::setCommandVersionArguments(const QStringList &arguments)
{
if (arguments.isEmpty()) {
if (m_d->m_binaryVersionToolTipEventFilter) {
delete m_d->m_binaryVersionToolTipEventFilter;
m_d->m_binaryVersionToolTipEventFilter = 0;
}
} else {
if (!m_d->m_binaryVersionToolTipEventFilter)
m_d->m_binaryVersionToolTipEventFilter = new PathChooserBinaryVersionToolTipEventFilter(this);
m_d->m_binaryVersionToolTipEventFilter->setArguments(arguments);
}
}
2008-12-02 14:09:21 +01:00
} // namespace Utils