PathChooser: Allow for chooser-only selection of files/directories

Makes the line edit disabled and read-only in that case.
We also need to change the custom context menu from the line edit to the
path chooser itself, because disabled widgets do not show a context
menu, but we still want to see the select & copy, and the special
items for opening explorer or terminal.

Task-number: QTCREATORBUG-23798
Change-Id: Ib653b4eaaedfbe54c614377795ddc52d21ac12c0
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Tobias Hunger
2020-03-16 10:24:35 +01:00
committed by Eike Ziller
parent 5838b40c6d
commit 69b0a2cafc
5 changed files with 66 additions and 18 deletions

View File

@@ -175,13 +175,15 @@ public:
Environment m_environment;
BinaryVersionToolTipEventFilter *m_binaryVersionToolTipEventFilter = nullptr;
QList<QAbstractButton *> m_buttons;
MacroExpander *m_macroExpander;
MacroExpander *m_macroExpander = globalMacroExpander();
bool m_isReadOnly = false;
bool m_isFileDialogOnly = false;
};
PathChooserPrivate::PathChooserPrivate() :
m_hLayout(new QHBoxLayout),
m_lineEdit(new FancyLineEdit),
m_macroExpander(globalMacroExpander())
PathChooserPrivate::PathChooserPrivate()
: m_hLayout(new QHBoxLayout)
, m_lineEdit(new FancyLineEdit)
{
}
@@ -223,9 +225,13 @@ PathChooser::PathChooser(QWidget *parent) :
{
d->m_hLayout->setContentsMargins(0, 0, 0, 0);
d->m_lineEdit->setContextMenuPolicy(Qt::CustomContextMenu);
setContextMenuPolicy(Qt::CustomContextMenu);
d->m_lineEdit->setContextMenuPolicy(Qt::NoContextMenu);
connect(d->m_lineEdit, &FancyLineEdit::customContextMenuRequested, this, &PathChooser::contextMenuRequested);
connect(this,
&FancyLineEdit::customContextMenuRequested,
this,
&PathChooser::contextMenuRequested);
connect(d->m_lineEdit, &FancyLineEdit::validReturnPressed, this, &PathChooser::returnPressed);
connect(d->m_lineEdit, &QLineEdit::textChanged, this,
[this] { emit rawPathChanged(rawPath()); });
@@ -268,6 +274,7 @@ void PathChooser::insertButton(int index, const QString &text, QObject *context,
connect(button, &QAbstractButton::clicked, context, callback);
d->m_hLayout->insertWidget(index + 1/*line edit*/, button);
d->m_buttons.insert(index, button);
updateReadOnlyStateOfSubwidgets();
}
QString PathChooser::browseButtonLabel()
@@ -344,15 +351,24 @@ void PathChooser::setFilePath(const FilePath &fn)
bool PathChooser::isReadOnly() const
{
return d->m_lineEdit->isReadOnly();
return d->m_isReadOnly;
}
void PathChooser::setReadOnly(bool b)
{
d->m_lineEdit->setReadOnly(b);
const auto buttons = d->m_buttons;
for (QAbstractButton *button : buttons)
button->setEnabled(!b);
d->m_isReadOnly = b;
updateReadOnlyStateOfSubwidgets();
}
bool PathChooser::isFileDialogOnly() const
{
return d->m_isFileDialogOnly;
}
void PathChooser::setFileDialogOnly(bool b)
{
d->m_isFileDialogOnly = b;
updateReadOnlyStateOfSubwidgets();
}
void PathChooser::slotBrowse()
@@ -442,16 +458,31 @@ void PathChooser::slotBrowse()
void PathChooser::contextMenuRequested(const QPoint &pos)
{
if (QMenu *menu = d->m_lineEdit->createStandardContextMenu()) {
if (!d->m_lineEdit->rect().contains(pos))
return;
QMenu *menu = d->m_lineEdit->createStandardContextMenu();
if (!menu)
menu = new QMenu;
if (s_aboutToShowContextMenuHandler)
s_aboutToShowContextMenuHandler(this, menu);
if (!menu->actions().isEmpty()) {
menu->setAttribute(Qt::WA_DeleteOnClose);
if (s_aboutToShowContextMenuHandler)
s_aboutToShowContextMenuHandler(this, menu);
menu->popup(d->m_lineEdit->mapToGlobal(pos));
menu->popup(mapToGlobal(pos));
} else {
delete menu;
}
}
void PathChooser::updateReadOnlyStateOfSubwidgets()
{
const bool readOnlyLineEdit = d->m_isReadOnly || d->m_isFileDialogOnly;
d->m_lineEdit->setEnabled(!readOnlyLineEdit);
d->m_lineEdit->setReadOnly(readOnlyLineEdit);
setFocusPolicy(d->m_lineEdit->focusPolicy());
for (QAbstractButton *button : qAsConst(d->m_buttons))
button->setEnabled(!d->m_isReadOnly);
}
bool PathChooser::isValid() const
{
return d->m_lineEdit->isValid();