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 <QCompleter>
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QContextMenuEvent>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileSystemModel>
|
2016-02-05 20:24:03 +01:00
|
|
|
#include <QFileInfo>
|
2015-04-22 19:33:53 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
2016-02-05 20:24:03 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
2015-04-22 19:33:53 +02:00
|
|
|
using namespace std;
|
|
|
|
using namespace Widgets;
|
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
|
|
|
PathLineEdit::PathLineEdit(QWidget *parent) :
|
|
|
|
ClearLineEdit(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
QAbstractItemModel *PathLineEdit::completionModel() const
|
|
|
|
{
|
|
|
|
if(QCompleter *c = completer()) {
|
|
|
|
return c->model();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathLineEdit::setCompletionModel(QAbstractItemModel *model)
|
|
|
|
{
|
|
|
|
QCompleter *c = completer();
|
|
|
|
if(!c) {
|
|
|
|
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);
|
2015-04-22 19:33:53 +02:00
|
|
|
if(!path.isEmpty()) {
|
|
|
|
editText(path);
|
|
|
|
}
|
|
|
|
});
|
2016-02-05 20:24:03 +01:00
|
|
|
QFileInfo fileInfo(text());
|
|
|
|
if(fileInfo.exists()) {
|
|
|
|
if(fileInfo.isFile()) {
|
2016-04-09 02:38:36 +02:00
|
|
|
connect(menu->addAction(QIcon::fromTheme(QStringLiteral("system-run")), tr("Open")), &QAction::triggered, bind(&DesktopUtils::openLocalFileOrDir, text()));
|
2016-02-05 20:24:03 +01:00
|
|
|
} else if(fileInfo.isDir()) {
|
2016-04-09 02:38:36 +02:00
|
|
|
connect(menu->addAction(QIcon::fromTheme(QStringLiteral("system-file-manager")), tr("Explore")), &QAction::triggered, bind(&DesktopUtils::openLocalFileOrDir, text()));
|
2016-02-05 20:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
menu->exec(event->globalPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|