6#include <c++utilities/io/path.h>
10#include <QFileSystemModel>
14#include <QStringBuilder>
15#ifndef QT_NO_CONTEXTMENU
16#include <QContextMenuEvent>
32QCompleter *PathSelection::s_completer =
nullptr;
40 , m_button(new QPushButton(this))
41 , m_customDialog(nullptr)
42 , m_customMode(QFileDialog::Directory)
45 s_completer =
new QCompleter;
46 s_completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
47 auto *
const fileSystemModel =
new QFileSystemModel(s_completer);
48 fileSystemModel->setRootPath(QString());
49 s_completer->setModel(fileSystemModel);
52 m_lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
53 m_lineEdit->installEventFilter(
this);
54 m_lineEdit->setCompleter(s_completer);
55 m_button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
58 auto *
const layout =
new QHBoxLayout(
this);
59 layout->setSpacing(3);
60 layout->setContentsMargins(0, 0, 0, 0);
61 layout->addWidget(m_lineEdit);
62 layout->addWidget(m_button);
65 connect(m_button, &QPushButton::clicked,
this, &PathSelection::showFileDialog);
70 switch (
event->type()) {
71 case QEvent::LanguageChange:
76 return QWidget::event(
event);
81#ifndef QT_NO_CONTEXTMENU
82 if (obj == m_lineEdit) {
83 switch (
event->type()) {
84 case QEvent::ContextMenu: {
85 unique_ptr<QMenu> menu(m_lineEdit->createStandardContextMenu());
87 connect(menu->addAction(QIcon::fromTheme(QStringLiteral(
"document-open")), tr(
"Select ...")), &QAction::triggered,
this,
88 &PathSelection::showFileDialog);
89 QFileInfo fileInfo(m_lineEdit->text());
90 if (fileInfo.exists()) {
91 if (fileInfo.isFile()) {
92 connect(menu->addAction(QIcon::fromTheme(QStringLiteral(
"system-run")), tr(
"Open")), &QAction::triggered,
94 }
else if (fileInfo.isDir()) {
95 connect(menu->addAction(QIcon::fromTheme(QStringLiteral(
"system-file-manager")), tr(
"Explore")), &QAction::triggered,
99 menu->exec(
static_cast<QContextMenuEvent *
>(
event)->globalPos());
106 return QWidget::eventFilter(obj,
event);
109void PathSelection::showFileDialog()
112 QFileInfo fileInfo(m_lineEdit->text());
113 if (fileInfo.exists()) {
114 if (fileInfo.isFile()) {
115 directory = fileInfo.absoluteDir().absolutePath();
117 directory = fileInfo.absolutePath();
120 if (m_customDialog) {
121 m_customDialog->setDirectory(directory);
122 if (m_customDialog->exec() == QFileDialog::Accepted) {
123 m_lineEdit->selectAll();
124 m_lineEdit->insert(m_customDialog->selectedFiles().join(SEARCH_PATH_SEP_CHAR));
127 QFileDialog dialog(
this);
128 dialog.setDirectory(directory);
129 dialog.setFileMode(m_customMode);
131 dialog.setWindowTitle(tr(
"Select path") % QStringLiteral(
" - ") % window()->windowTitle());
133 dialog.setWindowTitle(tr(
"Select path"));
135 if (dialog.exec() == QFileDialog::Accepted) {
136 m_lineEdit->selectAll();
137 m_lineEdit->insert(dialog.selectedFiles().join(SEARCH_PATH_SEP_CHAR));
142void PathSelection::setTexts()
144 m_button->setText(tr(
"Select ..."));
A QLineEdit with an embedded button for clearing its contents.
PathSelection(QWidget *parent=nullptr)
Constructs a path selection widget.
bool event(QEvent *event) override
bool eventFilter(QObject *obj, QEvent *event) override
QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path)
Shows the specified file or directory using the default file browser.