mirror of
https://github.com/YACReader/yacreader
synced 2025-07-26 00:44:59 -04:00
added drag&drop support for sorting comics in lists
This commit is contained in:
@ -40,6 +40,118 @@ int ComicModel::columnCount(const QModelIndex &parent) const
|
||||
return _data.first()->columnCount();
|
||||
}
|
||||
|
||||
bool ComicModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if(!enableResorting)
|
||||
return false;
|
||||
return data->formats().contains(YACReader::YACReaderLibrarComiscSelectionMimeDataFormat);
|
||||
}
|
||||
|
||||
//TODO: optimize this method
|
||||
bool ComicModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
QAbstractItemModel::dropMimeData(data,action,row,column,parent);
|
||||
QLOG_INFO() << ">>>>>>>>>>>>>>dropMimeData ComicModel<<<<<<<<<<<<<<<<<"<< parent << row << "," << column;
|
||||
|
||||
if(!data->formats().contains(YACReader::YACReaderLibrarComiscSelectionMimeDataFormat))
|
||||
return false;
|
||||
|
||||
QList<qulonglong> comicIds = YACReader::mimeDataToComicsIds(data);
|
||||
QList<int> currentIndexes;
|
||||
int i;
|
||||
foreach(qulonglong id, comicIds)
|
||||
{
|
||||
i = 0;
|
||||
foreach (ComicItem *item, _data) {
|
||||
if(item->data(Id)==id)
|
||||
{
|
||||
currentIndexes << i;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(currentIndexes.begin(), currentIndexes.end());
|
||||
QList<ComicItem *> resortedData;
|
||||
|
||||
if(currentIndexes.contains(row))//no resorting
|
||||
return false;
|
||||
|
||||
ComicItem * destinationItem;
|
||||
if(row == -1 || row >= _data.length())
|
||||
destinationItem = 0;
|
||||
else
|
||||
destinationItem = _data.at(row);
|
||||
|
||||
QList<int> newSorting;
|
||||
|
||||
i = 0;
|
||||
foreach (ComicItem *item, _data) {
|
||||
if(!currentIndexes.contains(i))
|
||||
{
|
||||
|
||||
if(item == destinationItem) {
|
||||
foreach(int index, currentIndexes)
|
||||
{
|
||||
resortedData << _data.at(index);
|
||||
newSorting << index;
|
||||
}
|
||||
}
|
||||
|
||||
resortedData << item;
|
||||
newSorting << i;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if(destinationItem == 0)
|
||||
{
|
||||
foreach(int index, currentIndexes)
|
||||
{
|
||||
resortedData << _data.at(index);
|
||||
newSorting << index;
|
||||
}
|
||||
}
|
||||
|
||||
QLOG_INFO() << newSorting;
|
||||
|
||||
_data = resortedData;
|
||||
|
||||
|
||||
//TODO emit signals
|
||||
//TODO fix selection
|
||||
QList<qulonglong> allComicIds;
|
||||
foreach (ComicItem *item, _data) {
|
||||
allComicIds << item->data(Id).toULongLong();
|
||||
}
|
||||
|
||||
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
|
||||
switch (mode) {
|
||||
case Favorites:
|
||||
DBHelper::reasignOrderToComicsInFavorites(allComicIds,db);
|
||||
break;
|
||||
case Label:
|
||||
DBHelper::reasignOrderToComicsInLabel(sourceId,allComicIds,db);
|
||||
break;
|
||||
case ReadingList:
|
||||
DBHelper::reasignOrderToComicsInReadingList(sourceId,allComicIds,db);
|
||||
break;
|
||||
}
|
||||
|
||||
QSqlDatabase::removeDatabase(_databasePath);
|
||||
emit resortedIndexes(newSorting);
|
||||
int destSelectedIndex = row<0?_data.length():row;
|
||||
|
||||
if(destSelectedIndex>currentIndexes.at(0))
|
||||
emit newSelectedIndex(index(qMax(0,destSelectedIndex-1),0,parent));
|
||||
else
|
||||
emit newSelectedIndex(index(qMax(0,destSelectedIndex),0,parent));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QMimeData *ComicModel::mimeData(const QModelIndexList &indexes) const
|
||||
{
|
||||
//custom model data
|
||||
@ -62,6 +174,14 @@ QMimeData *ComicModel::mimeData(const QModelIndexList &indexes) const
|
||||
return mimeData;
|
||||
}
|
||||
|
||||
QStringList ComicModel::mimeTypes() const
|
||||
{
|
||||
QLOG_DEBUG() << "mimeTypes";
|
||||
QStringList list;
|
||||
list << YACReader::YACReaderLibrarComiscSelectionMimeDataFormat;
|
||||
return list;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ComicModel::roleNames() const {
|
||||
QHash<int, QByteArray> roles;
|
||||
|
||||
@ -162,10 +282,10 @@ QVariant ComicModel::data(const QModelIndex &index, int role) const
|
||||
Qt::ItemFlags ComicModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return 0;
|
||||
return 0;
|
||||
if(index.column() == ComicModel::Rating)
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled ;
|
||||
}
|
||||
|
||||
QVariant ComicModel::headerData(int section, Qt::Orientation orientation,
|
||||
@ -284,6 +404,10 @@ QStringList ComicModel::getPaths(const QString & _source)
|
||||
|
||||
void ComicModel::setupFolderModelData(unsigned long long int folderId,const QString & databasePath)
|
||||
{
|
||||
enableResorting = false;
|
||||
mode = Folder;
|
||||
sourceId=folderId;
|
||||
|
||||
beginResetModel();
|
||||
qDeleteAll(_data);
|
||||
_data.clear();
|
||||
@ -309,6 +433,10 @@ void ComicModel::setupFolderModelData(unsigned long long int folderId,const QStr
|
||||
|
||||
void ComicModel::setupLabelModelData(unsigned long long parentLabel, const QString &databasePath)
|
||||
{
|
||||
enableResorting = true;
|
||||
mode = Label;
|
||||
sourceId = parentLabel;
|
||||
|
||||
beginResetModel();
|
||||
qDeleteAll(_data);
|
||||
_data.clear();
|
||||
@ -336,6 +464,9 @@ void ComicModel::setupLabelModelData(unsigned long long parentLabel, const QStri
|
||||
|
||||
void ComicModel::setupReadingListModelData(unsigned long long parentReadingList, const QString &databasePath)
|
||||
{
|
||||
mode = ReadingList;
|
||||
sourceId = parentReadingList;
|
||||
|
||||
beginResetModel();
|
||||
qDeleteAll(_data);
|
||||
_data.clear();
|
||||
@ -356,6 +487,9 @@ void ComicModel::setupReadingListModelData(unsigned long long parentReadingList,
|
||||
while(subfolders.next())
|
||||
ids << subfolders.record().value(0).toULongLong();
|
||||
|
||||
enableResorting = ids.length()==1;//only resorting if no sublists exist
|
||||
|
||||
|
||||
foreach(qulonglong id, ids)
|
||||
{
|
||||
QSqlQuery selectQuery(db);
|
||||
@ -371,7 +505,7 @@ void ComicModel::setupReadingListModelData(unsigned long long parentReadingList,
|
||||
QList<ComicItem *> tempData = _data;
|
||||
_data.clear();
|
||||
|
||||
setupModelData(selectQuery);
|
||||
setupModelDataForList(selectQuery);
|
||||
|
||||
_data = tempData << _data;
|
||||
}
|
||||
@ -384,6 +518,9 @@ void ComicModel::setupReadingListModelData(unsigned long long parentReadingList,
|
||||
|
||||
void ComicModel::setupFavoritesModelData(const QString &databasePath)
|
||||
{
|
||||
enableResorting = true;
|
||||
mode = Favorites;
|
||||
|
||||
beginResetModel();
|
||||
qDeleteAll(_data);
|
||||
_data.clear();
|
||||
@ -411,6 +548,9 @@ void ComicModel::setupFavoritesModelData(const QString &databasePath)
|
||||
|
||||
void ComicModel::setupReadingModelData(const QString &databasePath)
|
||||
{
|
||||
enableResorting = false;
|
||||
mode = Reading;
|
||||
|
||||
beginResetModel();
|
||||
qDeleteAll(_data);
|
||||
_data.clear();
|
||||
|
@ -34,7 +34,10 @@ public:
|
||||
QModelIndex parent(const QModelIndex &index) const;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const;
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
|
||||
QMimeData * mimeData(const QModelIndexList &indexes) const;
|
||||
QStringList mimeTypes() const;
|
||||
|
||||
void setupFolderModelData(unsigned long long int parentFolder,const QString & databasePath);
|
||||
void setupLabelModelData(unsigned long long int parentLabel, const QString & databasePath);
|
||||
@ -113,6 +116,16 @@ public:
|
||||
|
||||
};
|
||||
|
||||
enum Mode {
|
||||
Folder,
|
||||
Favorites,
|
||||
Reading,
|
||||
Label,
|
||||
ReadingList
|
||||
};
|
||||
|
||||
|
||||
|
||||
public slots:
|
||||
void remove(int row);
|
||||
void startTransaction();
|
||||
@ -135,11 +148,17 @@ private:
|
||||
|
||||
QSqlDatabase dbTransaction;
|
||||
|
||||
bool enableResorting;
|
||||
Mode mode;
|
||||
qulonglong sourceId;
|
||||
|
||||
signals:
|
||||
void beforeReset();
|
||||
void reset();
|
||||
void isEmpty();
|
||||
void searchNumResults(int);
|
||||
void resortedIndexes(QList<int>);
|
||||
void newSelectedIndex(const QModelIndex &);
|
||||
};
|
||||
//! [0]
|
||||
|
||||
|
@ -220,7 +220,7 @@ bool DataBaseManagement::createTables(QSqlDatabase & database)
|
||||
|
||||
return success;
|
||||
}
|
||||
#include "QsLog.h"
|
||||
|
||||
bool DataBaseManagement::createV8Tables(QSqlDatabase &database)
|
||||
{
|
||||
bool success = true;
|
||||
@ -304,7 +304,6 @@ bool DataBaseManagement::createV8Tables(QSqlDatabase &database)
|
||||
//1 Favorites
|
||||
//queryInsertDefaultReadingList.bindValue(":name", "Favorites");
|
||||
success = success && queryInsertDefaultReadingList.exec("INSERT INTO default_reading_list (name) VALUES (\"Favorites\")");
|
||||
QLOG_ERROR() << success;
|
||||
|
||||
//Reading doesn't need its onw list
|
||||
|
||||
|
@ -244,10 +244,7 @@ bool ReadingListModel::dropMimeData(const QMimeData *data, Qt::DropAction action
|
||||
|
||||
bool ReadingListModel::dropComics(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
QList<qulonglong> comicIds;
|
||||
QByteArray rawData = data->data(YACReader::YACReaderLibrarComiscSelectionMimeDataFormat);
|
||||
QDataStream in(&rawData,QIODevice::ReadOnly);
|
||||
in >> comicIds; //deserialize the list of indentifiers
|
||||
QList<qulonglong> comicIds = YACReader::mimeDataToComicsIds(data);
|
||||
|
||||
QLOG_DEBUG() << "dropped : " << comicIds;
|
||||
|
||||
|
Reference in New Issue
Block a user