34 #include <QtCore/QDebug>
35 #include <QtCore/QDir>
36 #include <QtCore/QFileInfo>
37 #include <QtCore/QSettings>
39 #include <QDesktopServices>
40 #include <QStandardPaths>
41 #include <QFileDialog>
42 #include <QHBoxLayout>
44 #include <QToolButton>
45 #include <QPushButton>
47 const char *
const Utils::PathChooser::browseButtonLabel =
63 virtual bool validate(
const QString &value, QString *errorMessage)
const;
73 QTC_ASSERT(chooser,
return);
78 return m_chooser->validatePath(value, errorMessage);
95 m_hLayout(new QHBoxLayout),
101 PathChooser::PathChooser(QWidget *parent) :
106 m_d->m_hLayout->setContentsMargins(0, 0, 0, 0);
108 connect(m_d->m_lineEdit, SIGNAL(validReturnPressed()),
this, SIGNAL(returnPressed()));
109 connect(m_d->m_lineEdit, SIGNAL(textChanged(QString)),
this, SIGNAL(changed(QString)));
110 connect(m_d->m_lineEdit, SIGNAL(validChanged()),
this, SIGNAL(validChanged()));
111 connect(m_d->m_lineEdit, SIGNAL(validChanged(
bool)),
this, SIGNAL(validChanged(
bool)));
112 connect(m_d->m_lineEdit, SIGNAL(editingFinished()),
this, SIGNAL(editingFinished()));
114 m_d->m_lineEdit->setMinimumWidth(50);
115 m_d->m_hLayout->addWidget(m_d->m_lineEdit);
116 m_d->m_hLayout->setSizeConstraint(QLayout::SetMinimumSize);
118 addButton(tr(browseButtonLabel),
this, SLOT(slotBrowse()));
120 setLayout(m_d->m_hLayout);
121 setFocusProxy(m_d->m_lineEdit);
124 PathChooser::~PathChooser()
129 void PathChooser::addButton(
const QString &text, QObject *receiver,
const char *slotFunc)
132 QPushButton *button =
new QPushButton;
134 QToolButton *button =
new QToolButton;
136 button->setText(text);
137 connect(button, SIGNAL(clicked()), receiver, slotFunc);
141 QAbstractButton *PathChooser::buttonAtIndex(
int index)
const
143 return findChildren<QAbstractButton*>().at(index);
146 QString PathChooser::path()
const
153 m_d->
m_lineEdit->setText(QDir::toNativeSeparators(path));
156 void PathChooser::slotBrowse()
160 QString predefined = path();
161 if ((predefined.isEmpty() || !QFileInfo(predefined).isDir())
164 if (!QFileInfo(predefined).isDir())
168 if (predefined.startsWith(
":"))
175 case PathChooser::Directory:
176 newPath = QFileDialog::getExistingDirectory(
this,
177 makeDialogTitle(tr(
"Choose a directory")), predefined);
180 case PathChooser::File:
181 case PathChooser::Command:
182 newPath = QFileDialog::getOpenFileName(
this,
183 makeDialogTitle(tr(
"Choose a file")), predefined,
192 if (!newPath.isEmpty()) {
193 newPath = QDir::toNativeSeparators(newPath);
194 if (newPath.size() > 1 && newPath.endsWith(QDir::separator()))
195 newPath.truncate(newPath.size() - 1);
202 bool PathChooser::isValid()
const
207 QString PathChooser::errorMessage()
const
212 bool PathChooser::validatePath(
const QString &path, QString *errorMessage)
214 if (path.isEmpty()) {
216 *errorMessage = tr(
"The path must not be empty.");
220 const QFileInfo fi(path);
221 const bool isDir = fi.isDir();
225 case PathChooser::Directory:
226 case PathChooser::File:
229 *errorMessage = tr(
"The path '%1' does not exist.").arg(path);
234 case PathChooser::Command:
241 case PathChooser::Directory:
244 *errorMessage = tr(
"The path '%1' is not a directory.").arg(path);
249 case PathChooser::File:
252 *errorMessage = tr(
"The path '%1' is not a file.").arg(path);
257 case PathChooser::Command:
269 QString PathChooser::label()
274 QString PathChooser::homePath()
280 return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
281 return QDir::homePath();
284 void PathChooser::setExpectedKind(Kind expected)
289 PathChooser::Kind PathChooser::expectedKind()
const
294 void PathChooser::setPromptDialogTitle(
const QString &title)
299 QString PathChooser::promptDialogTitle()
const
304 void PathChooser::setPromptDialogFilter(
const QString &filter)
309 QString PathChooser::promptDialogFilter()
const
314 void PathChooser::setInitialBrowsePathBackup(
const QString &path)
319 QString PathChooser::makeDialogTitle(
const QString &title)
PathValidatingLineEdit(PathChooser *chooser, QWidget *parent=nullptr)
virtual bool validate(const QString &value, QString *errorMessage) const
PathChooser::Kind m_acceptingKind
QString m_initialBrowsePathOverride
QT_TRANSLATE_NOOP("Utils::PathChooser","Browse...")
QString m_dialogTitleOverride
PathValidatingLineEdit * m_lineEdit
PathChooserPrivate(PathChooser *chooser)
void setPath(const QString &)
static bool isWindowsHost()