2015-09-06 20:20:00 +02:00
|
|
|
#include "./pathlineedit.h"
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2016-02-05 20:24:03 +01:00
|
|
|
#include <qtutilities/misc/desktoputils.h>
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
2018-03-07 01:18:01 +01:00
|
|
|
#include <QCompleter>
|
2015-04-22 19:33:53 +02:00
|
|
|
#include <QContextMenuEvent>
|
|
|
|
#include <QFileDialog>
|
2016-02-05 20:24:03 +01:00
|
|
|
#include <QFileInfo>
|
2018-03-07 01:18:01 +01:00
|
|
|
#include <QFileSystemModel>
|
2015-04-22 19:33:53 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
2016-02-05 20:24:03 +01:00
|
|
|
#include <functional>
|
2018-03-07 01:18:01 +01:00
|
|
|
#include <memory>
|
2016-02-05 20:24:03 +01:00
|
|
|
|
2015-04-22 19:33:53 +02:00
|
|
|
using namespace std;
|
2019-06-10 22:49:46 +02:00
|
|
|
using namespace QtUtilities;
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
PathLineEdit::PathLineEdit(QWidget *parent)
|
|
|
|
: ClearLineEdit(parent)
|
|
|
|
{
|
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
QAbstractItemModel *PathLineEdit::completionModel() const
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
if (QCompleter *c = completer()) {
|
2015-04-22 19:33:53 +02:00
|
|
|
return c->model();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathLineEdit::setCompletionModel(QAbstractItemModel *model)
|
|
|
|
{
|
|
|
|
QCompleter *c = completer();
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!c) {
|
2015-04-22 19:33:53 +02:00
|
|
|
c = new QCompleter(this);
|
|
|
|
c->setCompletionMode(QCompleter::PopupCompletion);
|
|
|
|
setCompleter(c);
|
|
|
|
}
|
|
|
|
c->setModel(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathLineEdit::editText(const QString &text)
|
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
emit textEdited(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathLineEdit::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
unique_ptr<QMenu> menu(createStandardContextMenu());
|
|
|
|
menu->addSeparator();
|
2016-04-09 02:38:36 +02:00
|
|
|
connect(menu->addAction(QIcon::fromTheme(QStringLiteral("document-open")), tr("Select directory ...")), &QAction::triggered, [this] {
|
2016-02-05 20:24:03 +01:00
|
|
|
const QString path = QFileDialog::getExistingDirectory(this);
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!path.isEmpty()) {
|
2015-04-22 19:33:53 +02:00
|
|
|
editText(path);
|
|
|
|
}
|
|
|
|
});
|
2016-02-05 20:24:03 +01:00
|
|
|
QFileInfo fileInfo(text());
|
2018-03-07 01:18:01 +01:00
|
|
|
if (fileInfo.exists()) {
|
|
|
|
if (fileInfo.isFile()) {
|
2019-06-12 20:47:44 +02:00
|
|
|
connect(
|
|
|
|
menu->addAction(QIcon::fromTheme(QStringLiteral("system-run")), tr("Open")), &QAction::triggered, bind(&openLocalFileOrDir, text()));
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (fileInfo.isDir()) {
|
|
|
|
connect(menu->addAction(QIcon::fromTheme(QStringLiteral("system-file-manager")), tr("Explore")), &QAction::triggered,
|
2019-06-10 22:49:46 +02:00
|
|
|
bind(&openLocalFileOrDir, text()));
|
2016-02-05 20:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
menu->exec(event->globalPos());
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
} // namespace QtGui
|