2014-09-10 17:27:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vfilepropertyeditor.cpp
|
|
|
|
** @author hedgeware <internal(at)hedgeware.net>
|
|
|
|
** @date
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** All rights reserved. This program and the accompanying materials
|
|
|
|
** are made available under the terms of the GNU Lesser General Public License
|
|
|
|
** (LGPL) version 2.1 which accompanies this distribution, and is available at
|
|
|
|
** http://www.gnu.org/licenses/lgpl-2.1.html
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vfilepropertyeditor.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QDragLeaveEvent>
|
|
|
|
#include <QDragMoveEvent>
|
|
|
|
#include <QDropEvent>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QFile>
|
2014-09-10 17:27:45 +02:00
|
|
|
#include <QFileDialog>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QSizePolicy>
|
|
|
|
#include <QToolButton>
|
2014-09-10 17:27:45 +02:00
|
|
|
#include <QUrl>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <Qt>
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VFileEditWidget::VFileEditWidget(QWidget *parent, bool is_directory)
|
2014-09-20 18:05:21 +02:00
|
|
|
: QWidget(parent), CurrentFilePath(), ToolButton(nullptr), FileLineEdit(nullptr), FileDialogFilter(), FilterList(),
|
|
|
|
Directory(is_directory)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Create the tool button,ToolButton = new QToolButton(this);
|
|
|
|
ToolButton = new QToolButton(this);
|
|
|
|
ToolButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
|
2014-12-04 12:34:48 +01:00
|
|
|
ToolButton->setText("...");
|
2014-09-10 17:27:45 +02:00
|
|
|
ToolButton->setFixedWidth(20);
|
|
|
|
ToolButton->installEventFilter(this);
|
2014-09-10 19:57:08 +02:00
|
|
|
setFocusProxy(ToolButton); // Make the ToolButton the focus proxy
|
2014-09-10 17:27:45 +02:00
|
|
|
setFocusPolicy(ToolButton->focusPolicy());
|
2016-03-13 14:02:11 +01:00
|
|
|
connect(ToolButton, &QToolButton::clicked, this, &VFileEditWidget::onToolButtonClicked);
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
// Create the line edit widget
|
|
|
|
FileLineEdit = new QLineEdit(this);
|
|
|
|
FileLineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
FileLineEdit->setText(CurrentFilePath);
|
|
|
|
FileLineEdit->installEventFilter(this);
|
|
|
|
|
|
|
|
// The layout (a horizontal layout)
|
|
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->setMargin(0);
|
|
|
|
layout->addWidget(FileLineEdit);
|
|
|
|
layout->addWidget(ToolButton);
|
|
|
|
|
|
|
|
// Accept drops
|
|
|
|
setAcceptDrops(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VFileEditWidget::~VFileEditWidget()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// nothing needs to be done here
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::setFile(const QString &value, bool emit_signal)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
if (CurrentFilePath != value)
|
|
|
|
{
|
|
|
|
CurrentFilePath = value;
|
|
|
|
FileLineEdit->setText(CurrentFilePath);
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (emit_signal)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
emit dataChangedByUser(CurrentFilePath, this);
|
|
|
|
emit commitData(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::setFilter(const QString &dialog_filter, const QStringList& filter_list)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
FileDialogFilter = dialog_filter;
|
|
|
|
FilterList = filter_list;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::setDirectory(bool dir)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
Directory = dir;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
QString VPE::VFileEditWidget::getFile() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return CurrentFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::onToolButtonClicked()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2017-07-05 18:35:34 +02:00
|
|
|
QString filepath = (Directory ? QFileDialog::getExistingDirectory(nullptr, tr("Directory"), CurrentFilePath,
|
2017-06-19 15:11:44 +02:00
|
|
|
QFileDialog::ShowDirsOnly
|
2018-04-04 07:34:48 +02:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
| QFileDialog::DontUseNativeDialog
|
|
|
|
#endif
|
|
|
|
)
|
2017-07-05 18:35:34 +02:00
|
|
|
: QFileDialog::getOpenFileName(nullptr, tr("Open File"), CurrentFilePath,
|
2018-04-04 07:34:48 +02:00
|
|
|
FileDialogFilter, nullptr
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
, QFileDialog::DontUseNativeDialog
|
|
|
|
#endif
|
|
|
|
));
|
2014-09-10 19:57:08 +02:00
|
|
|
if (filepath.isNull() == false)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
setFile(filepath, true);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VFileEditWidget::eventFilter(QObject *obj, QEvent *ev)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (ev->type() == QEvent::DragEnter || ev->type() == QEvent::Drop)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
ev->ignore();
|
2014-09-10 19:57:08 +02:00
|
|
|
if (ev->type() == QEvent::DragEnter)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
dragEnterEvent(static_cast<QDragEnterEvent*>(ev));
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (ev->type() == QEvent::Drop)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
dropEvent(static_cast<QDropEvent*>(ev));
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (ev->isAccepted())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return true;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
else
|
|
|
|
return QWidget::eventFilter(obj, ev);
|
|
|
|
}
|
2014-09-11 19:15:07 +02:00
|
|
|
else if (obj == ToolButton && ev->type() == QEvent::KeyPress)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Ignore the event, so that eventually the delegate gets the event.
|
|
|
|
ev->ignore();
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-10 19:57:08 +02:00
|
|
|
else if (obj == FileLineEdit)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (ev->type() == QEvent::FocusOut)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
setFile(FileLineEdit->text(), true);
|
|
|
|
// We don't return true here because we still want the line edit to catch the event as well
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forward the signal to the parent class
|
|
|
|
return QWidget::eventFilter(obj, ev);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VFileEditWidget::isDirectory()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return Directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::dragEnterEvent(QDragEnterEvent* event)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
QString tmpFileName;
|
2014-09-10 19:57:08 +02:00
|
|
|
if (checkMimeData(event->mimeData(), tmpFileName))
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::dragMoveEvent(QDragMoveEvent* event)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::dragLeaveEvent(QDragLeaveEvent* event)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VFileEditWidget::dropEvent(QDropEvent* event)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
QString tmpFileName;
|
2014-09-10 19:57:08 +02:00
|
|
|
if (checkMimeData(event->mimeData(), tmpFileName))
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
setFile(tmpFileName);
|
|
|
|
emit dataChangedByUser(getFile(), this);
|
|
|
|
emit commitData(this);
|
|
|
|
event->accept();
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VFileEditWidget::checkMimeData(const QMimeData* data, QString& file) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
if (data->hasUrls())
|
|
|
|
{
|
|
|
|
QList<QUrl> tmpUrlList = data->urls();
|
|
|
|
QFileInfo tmpFileInfo;
|
|
|
|
|
2018-03-14 14:39:15 +01:00
|
|
|
for(const QUrl &tmpUrl : tmpUrlList)
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (QFile::exists(tmpUrl.toLocalFile()))
|
|
|
|
{
|
|
|
|
tmpFileInfo = QFileInfo(tmpUrl.toLocalFile()); break;
|
|
|
|
}
|
2018-03-14 14:39:15 +01:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (checkFileFilter(tmpFileInfo.fileName()))
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
file = tmpFileInfo.absoluteFilePath();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VFileEditWidget::checkFileFilter(const QString& file) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (FilterList.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return true;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
QFileInfo tmpFileInfo(file);
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if ((Directory && !tmpFileInfo.isDir()) || (!Directory && !tmpFileInfo.isFile()))
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2018-04-03 10:15:58 +02:00
|
|
|
for (auto &tmpFilter : FilterList)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
QRegExp tmpRegExpFilter(tmpFilter, Qt::CaseInsensitive, QRegExp::Wildcard);
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpRegExpFilter.exactMatch(file))
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return true;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|