Skip to content

Commit

Permalink
Typed PathListWidget FILE vs FOLDER
Browse files Browse the repository at this point in the history
Make sure folders are folders

Make sure files are executable(s)

Issue: With an "Add" dialog open, dragging and dropping a file or folder
to where the dialog does not accept them will make the underlying widget
accept it.
  • Loading branch information
Kissaki committed Apr 6, 2017
1 parent 4a1663f commit e18de15
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/mumble/OverlayConfig.cpp
Expand Up @@ -149,6 +149,8 @@ OverlayConfig::OverlayConfig(Settings &st) :
fViewScale(1.0f) {
setupUi(this);

qlwPaths->setPathType(PathListWidget::FOLDER);

qcbOverlayExclusionMode->insertItem(static_cast<int>(OverlaySettings::LauncherFilterExclusionMode), tr("Launcher Filter"));
qcbOverlayExclusionMode->insertItem(static_cast<int>(OverlaySettings::WhitelistExclusionMode), tr("Whitelist"));
qcbOverlayExclusionMode->insertItem(static_cast<int>(OverlaySettings::BlacklistExclusionMode), tr("Blacklist"));
Expand Down
63 changes: 45 additions & 18 deletions src/mumble/PathListWidget.cpp
Expand Up @@ -3,17 +3,27 @@
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "mumble_pch.hpp"

#include "PathListWidget.h"

#include <QGraphicsSceneDragDropEvent>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QFile>
#include <QDir>
#include "Overlay.h"

PathListWidget::PathListWidget(QWidget* parent)
: QListWidget(parent) {
: QListWidget(parent)
, pathType(FILE) {
setAcceptDrops(true);
}

void PathListWidget::setPathType(PathType type) {
pathType = type;
}

void PathListWidget::addFilePath(const QString & path) {
QString qsAppIdentifier = OverlayAppInfo::applicationIdentifierForPath(path);
QStringList qslIdentifiers;
Expand All @@ -27,35 +37,52 @@ void PathListWidget::addFilePath(const QString & path) {
}
}

void PathListWidget::checkAcceptDragEvent(QDropEvent *event) {
void PathListWidget::addFolderPath(const QString &path) {
QString dir = QDir::toNativeSeparators(path);
QStringList qslIdentifiers;
for (int i = 0; i < count(); i++)
qslIdentifiers << item(i)->data(Qt::UserRole).toString();
if (! dir.isEmpty() && ! qslIdentifiers.contains(dir)) {
QListWidgetItem *qlwiPath = new QListWidgetItem(QIcon(), QDir(dir).path(), this);
qlwiPath->setData(Qt::UserRole, QVariant(dir));
setCurrentItem(qlwiPath);
}
}

void PathListWidget::checkAcceptDragEvent(QDropEvent *event, bool addItem) {
if (event->mimeData()->hasUrls()) {
foreach (auto url, event->mimeData()->urls()) {
if (url.isLocalFile()) {
event->setDropAction(Qt::CopyAction);
// event->acceptProposedAction();
event->accept();
break;
auto info = QFileInfo(url.toLocalFile());
switch (pathType) {
case FILE:
if (info.isFile() && info.isExecutable()) {
addFilePath(info.filePath());
event->setDropAction(Qt::LinkAction);
event->accept();
}
break;
case FOLDER:
if (info.isDir()) {
addFolderPath(url.toLocalFile());
event->setDropAction(Qt::LinkAction);
event->accept();
}
break;
}
}
}
}
}

void PathListWidget::dragEnterEvent(QDragEnterEvent *event) {
checkAcceptDragEvent(event);
checkAcceptDragEvent(event, false);
}

void PathListWidget::dragMoveEvent(QDragMoveEvent *event) {
checkAcceptDragEvent(event);
checkAcceptDragEvent(event, false);
}

void PathListWidget::dropEvent(QDropEvent *event) {
if (event->mimeData()->hasUrls()) {
foreach (auto url, event->mimeData()->urls()) {
if (url.isLocalFile()) {
addFilePath(url.toLocalFile());
// event->acceptProposedAction();
event->accept();
}
}
}
checkAcceptDragEvent(event, true);
}
18 changes: 12 additions & 6 deletions src/mumble/PathListWidget.h
Expand Up @@ -12,17 +12,23 @@ class QGraphicsSceneDragDropEvent;
class QDropEvent;

class PathListWidget : public QListWidget {
private:
Q_OBJECT
Q_DISABLE_COPY(PathListWidget)

void addFilePath(const QString & path);
void checkAcceptDragEvent(QDropEvent *event);
public:
enum PathType { FILE, FOLDER };

PathListWidget(QWidget* parent=nullptr);
void setPathType(PathType type);
virtual void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
virtual void dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
virtual void dropEvent(QDropEvent *event) Q_DECL_OVERRIDE;
private:
Q_OBJECT
Q_DISABLE_COPY(PathListWidget)

PathType pathType;

void addFilePath(const QString &path);
void addFolderPath(const QString &path);
void checkAcceptDragEvent(QDropEvent *event, bool addItem);
};

#endif

0 comments on commit e18de15

Please sign in to comment.