Compare commits

...

3 Commits

Author SHA1 Message Date
janrupf
f04502cd82 NOISSUE Fix outdated test 2019-06-22 02:05:53 +02:00
janrupf
4233e0c014 NOISSUE Try to enable dragging accross views 2019-06-22 02:05:53 +02:00
janrupf
cec5f7332e NOISSUE Prepare mod install for possible move 2019-06-22 02:05:53 +02:00
6 changed files with 61 additions and 15 deletions

View File

@@ -21,6 +21,7 @@
#include <QString> #include <QString>
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
#include <QDebug> #include <QDebug>
#include <QtCore/QDataStream>
SimpleModList::SimpleModList(const QString &dir) : QAbstractListModel(), m_dir(dir) SimpleModList::SimpleModList(const QString &dir) : QAbstractListModel(), m_dir(dir)
{ {
@@ -97,7 +98,7 @@ bool SimpleModList::isValid()
} }
// FIXME: this does not take disabled mod (with extra .disable extension) into account... // FIXME: this does not take disabled mod (with extra .disable extension) into account...
bool SimpleModList::installMod(const QString &filename) bool SimpleModList::installMod(const QString &filename, bool move)
{ {
// NOTE: fix for GH-1178: remove trailing slash to avoid issues with using the empty result of QFileInfo::fileName // NOTE: fix for GH-1178: remove trailing slash to avoid issues with using the empty result of QFileInfo::fileName
auto originalPath = FS::NormalizePath(filename); auto originalPath = FS::NormalizePath(filename);
@@ -143,7 +144,7 @@ bool SimpleModList::installMod(const QString &filename)
} }
qDebug() << newpath << "has been deleted."; qDebug() << newpath << "has been deleted.";
} }
if (!QFile::copy(fileinfo.filePath(), newpath)) if (move ? !QFile::rename(fileinfo.filePath(), newpath) : !QFile::copy(fileinfo.filePath(), newpath))
{ {
qWarning() << "Copy from" << originalPath << "to" << newpath << "has failed."; qWarning() << "Copy from" << originalPath << "to" << newpath << "has failed.";
// FIXME: report error in a user-visible way // FIXME: report error in a user-visible way
@@ -163,7 +164,7 @@ bool SimpleModList::installMod(const QString &filename)
return false; return false;
} }
if (!FS::copy(from, newpath)()) if (move ? !QDir().rename(from, newpath) : !FS::copy(from, newpath)())
{ {
qWarning() << "Copy of folder from" << originalPath << "to" << newpath << "has (potentially partially) failed."; qWarning() << "Copy of folder from" << originalPath << "to" << newpath << "has (potentially partially) failed.";
return false; return false;
@@ -307,15 +308,13 @@ QVariant SimpleModList::headerData(int section, Qt::Orientation orientation, int
default: default:
return QVariant(); return QVariant();
} }
return QVariant();
} }
Qt::ItemFlags SimpleModList::flags(const QModelIndex &index) const Qt::ItemFlags SimpleModList::flags(const QModelIndex &index) const
{ {
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index); Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
if (index.isValid()) if (index.isValid())
return Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled | return Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled | defaultFlags;
defaultFlags;
else else
return Qt::ItemIsDropEnabled | defaultFlags; return Qt::ItemIsDropEnabled | defaultFlags;
} }
@@ -326,6 +325,11 @@ Qt::DropActions SimpleModList::supportedDropActions() const
return Qt::CopyAction | Qt::MoveAction; return Qt::CopyAction | Qt::MoveAction;
} }
Qt::DropActions SimpleModList::supportedDragActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
QStringList SimpleModList::mimeTypes() const QStringList SimpleModList::mimeTypes() const
{ {
QStringList types; QStringList types;
@@ -333,6 +337,7 @@ QStringList SimpleModList::mimeTypes() const
return types; return types;
} }
bool SimpleModList::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&) bool SimpleModList::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&)
{ {
if (action == Qt::IgnoreAction) if (action == Qt::IgnoreAction)
@@ -357,11 +362,29 @@ bool SimpleModList::dropMimeData(const QMimeData* data, Qt::DropAction action, i
{ {
continue; continue;
} }
// TODO: implement not only copy, but also move
// FIXME: handle errors here // FIXME: handle errors here
installMod(url.toLocalFile()); installMod(url.toLocalFile(), action == Qt::DropAction::MoveAction);
} }
return true; return true;
} }
return false; return false;
} }
QMimeData *SimpleModList::mimeData(const QModelIndexList &indexes) const
{
auto *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
for(const auto &index : indexes)
{
if(index.isValid())
{
auto mod = mods[index.row()];
stream << "file://" << mod.filename().absoluteFilePath() << "\n";
}
}
mimeData->setData("text/uri-list", encodedData);
return mimeData;
}

View File

@@ -49,11 +49,13 @@ public:
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::DropActions supportedDropActions() const override; Qt::DropActions supportedDropActions() const override;
Qt::DropActions supportedDragActions() const override;
/// flags, mostly to support drag&drop /// flags, mostly to support drag&drop
virtual Qt::ItemFlags flags(const QModelIndex &index) const override; virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
QStringList mimeTypes() const override; QStringList mimeTypes() const override;
bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) override; bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
virtual int rowCount(const QModelIndex &) const override virtual int rowCount(const QModelIndex &) const override
{ {
@@ -83,7 +85,7 @@ public:
/** /**
* Adds the given mod to the list at the given index - if the list supports custom ordering * Adds the given mod to the list at the given index - if the list supports custom ordering
*/ */
bool installMod(const QString& filename); bool installMod(const QString& filename, bool move);
/// Deletes all the selected mods /// Deletes all the selected mods
virtual bool deleteMods(const QModelIndexList &indexes); virtual bool deleteMods(const QModelIndexList &indexes);

View File

@@ -33,7 +33,7 @@ slots:
QString folder = source; QString folder = source;
QTemporaryDir tempDir; QTemporaryDir tempDir;
SimpleModList m(tempDir.path()); SimpleModList m(tempDir.path());
m.installMod(folder); m.installMod(folder, false);
verify(tempDir.path()); verify(tempDir.path());
} }
@@ -42,7 +42,7 @@ slots:
QString folder = source + '/'; QString folder = source + '/';
QTemporaryDir tempDir; QTemporaryDir tempDir;
SimpleModList m(tempDir.path()); SimpleModList m(tempDir.path());
m.installMod(folder); m.installMod(folder, false);
verify(tempDir.path()); verify(tempDir.path());
} }
} }

View File

@@ -163,7 +163,7 @@ void ModFolderPage::on_addModBtn_clicked()
{ {
for (auto filename : list) for (auto filename : list)
{ {
m_mods->installMod(filename); m_mods->installMod(filename, false);
} }
} }
} }

View File

@@ -35,8 +35,10 @@ ModListView::ModListView ( QWidget* parent )
setHorizontalScrollBarPolicy ( Qt::ScrollBarAsNeeded ); setHorizontalScrollBarPolicy ( Qt::ScrollBarAsNeeded );
setDropIndicatorShown(true); setDropIndicatorShown(true);
setDragEnabled(true); setDragEnabled(true);
setDragDropMode(QAbstractItemView::DropOnly); setDragDropMode(QAbstractItemView::DragDrop);
viewport()->setAcceptDrops(true); viewport()->setAcceptDrops(true);
setAcceptDrops(true);
setDefaultDropAction(Qt::CopyAction);
} }
void ModListView::setModel ( QAbstractItemModel* model ) void ModListView::setModel ( QAbstractItemModel* model )
@@ -64,3 +66,18 @@ void ModListView::setModel ( QAbstractItemModel* model )
head->setSectionResizeMode(i, QHeaderView::ResizeToContents); head->setSectionResizeMode(i, QHeaderView::ResizeToContents);
} }
} }
void ModListView::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}
void ModListView::dragMoveEvent(QDragMoveEvent *event)
{
event->accept();
}
void ModListView::dropEvent(QDropEvent *event)
{
QAbstractItemView::dropEvent(event);
}

View File

@@ -22,6 +22,10 @@ class ModListView: public QTreeView
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ModListView ( QWidget* parent = 0 ); explicit ModListView ( QWidget* parent = nullptr );
virtual void setModel ( QAbstractItemModel* model ); void setModel ( QAbstractItemModel* model ) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dropEvent(QDropEvent *event) override;
}; };