add drag and drop onto app icon (dock etc.) support for MacOS platform.

This commit is contained in:
Daisuke Cato
2016-10-12 02:09:46 +09:00
commit 649c203759
987 changed files with 90423 additions and 0 deletions

View File

@ -0,0 +1,6 @@
#include "comics_model.h"
ComicsModel::ComicsModel(QObject *parent) :
JSONModel(parent)
{
}

View File

@ -0,0 +1,18 @@
#ifndef COMICS_MODEL_H
#define COMICS_MODEL_H
#include "json_model.h"
class ComicsModel : public JSONModel
{
Q_OBJECT
public:
explicit ComicsModel(QObject *parent = 0);
signals:
public slots:
};
#endif // COMICS_MODEL_H

View File

@ -0,0 +1,6 @@
#include "json_model.h"
JSONModel::JSONModel(QObject *parent) :
QAbstractItemModel(parent)
{
}

View File

@ -0,0 +1,19 @@
#ifndef JSON_MODEL_H
#define JSON_MODEL_H
#include <QAbstractItemModel>
class JSONModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit JSONModel(QObject *parent = 0);
virtual void load(const QString & json) = 0 ;
signals:
public slots:
};
#endif // JSON_MODEL_H

View File

@ -0,0 +1,184 @@
#include "local_comic_list_model.h"
LocalComicListModel::LocalComicListModel(QObject *parent) :
QAbstractItemModel(parent),numExtraRows(0)
{
}
void LocalComicListModel::load(QList<ComicDB> &comics)
{
_data = comics;
}
QModelIndex LocalComicListModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index)
return QModelIndex(); //no parent
}
int LocalComicListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return _data.count();
}
int LocalComicListModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if(_data.isEmpty())
return 0;
else
return 1;//_data.at(0)->count();
}
QVariant LocalComicListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DecorationRole)
{
return QVariant();
}
if (role == Qt::TextAlignmentRole)
{
//TODO
}
if(role != Qt::DisplayRole)
return QVariant();
int row = index.row();
//if(row < _data.count())
return _data[row].getFileName();
//else
//return QVariant();
}
Qt::ItemFlags LocalComicListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant LocalComicListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section);
if ( role == Qt::TextAlignmentRole)
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
return QVariant(QString(tr("file name")));
}
return QVariant();
}
QModelIndex LocalComicListModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
QList<ComicDB> LocalComicListModel::getData()
{
return _data;
}
void LocalComicListModel::removeComics(const QList<QModelIndex> &selectedIndexes)
{
QModelIndex mi = selectedIndexes.first();
QModelIndex lastMi = selectedIndexes.last();
int sourceRow = mi.row();
int sourceLastRow = lastMi.row();
beginRemoveRows(QModelIndex(),selectedIndexes.first().row(),selectedIndexes.last().row());
for(int i = sourceLastRow;i>=sourceRow;i--)
{
_removed.push_front(_data.at(i));
_data.removeAt(i);
}
endRemoveRows();
beginInsertRows(QModelIndex(),_data.count()-_removed.count(),_data.count()-1);
for(int i = 0; i<_removed.count(); i++)
_data.append(ComicDB());
endInsertRows();
}
void LocalComicListModel::restoreAll()
{
int numItemsToRemove = 0;
for(int i = 0;numItemsToRemove<_removed.count();i++)
{
if(_data.at(i).getFileName().isEmpty())
{
beginRemoveRows(QModelIndex(),i,i);
_data.removeAt(i);
endRemoveRows();
beginInsertRows(QModelIndex(),i,i);
_data.insert(i,_removed.at(numItemsToRemove));
endInsertRows();
numItemsToRemove++;
}
}
_removed.clear();
}
void LocalComicListModel::moveSelectionUp(const QList<QModelIndex> &selectedIndexes)
{
QModelIndex mi = selectedIndexes.first();
QModelIndex lastMi = selectedIndexes.last();
int sourceRow = mi.row();
int sourceLastRow = lastMi.row();
int destRow = sourceRow - 1;
if(destRow < 0)
return;
beginMoveRows(mi.parent(),sourceRow,sourceLastRow,mi.parent(),destRow);
for(int i = sourceRow; i <= sourceLastRow; i++)
_data.swap(i, i-1);
endMoveRows();
}
void LocalComicListModel::moveSelectionDown(const QList<QModelIndex> &selectedIndexes)
{
QModelIndex mi = selectedIndexes.first();
QModelIndex lastMi = selectedIndexes.last();
int sourceRow = mi.row();
int sourceLastRow = lastMi.row();
int destRow = sourceLastRow + 1;
if(destRow >= _data.count())
return;
beginMoveRows(mi.parent(),sourceRow,sourceLastRow,mi.parent(),destRow+1);
for(int i = sourceLastRow; i >= sourceRow; i--)
_data.swap(i, i+1);
endMoveRows();
}
void LocalComicListModel::addExtraRows(int numRows)
{
numExtraRows = numRows;
for(int i = 0; i<numExtraRows; i++)
_data.append(ComicDB());
}

View File

@ -0,0 +1,42 @@
#ifndef LOCAL_COMIC_LIST_MODEL_H
#define LOCAL_COMIC_LIST_MODEL_H
#include <QAbstractItemModel>
#include "comic_db.h"
class LocalComicListModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit LocalComicListModel(QObject *parent = 0);
void load(QList<ComicDB> & comics);
//QAbstractItemModel methods
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QList<ComicDB> getData();
void removeComics(const QList<QModelIndex> & selectedIndexes);
void restoreAll();
signals:
public slots:
void moveSelectionUp(const QList<QModelIndex> & selectedIndexes);
void moveSelectionDown(const QList<QModelIndex> & selectedIndexes);
void addExtraRows(int numRows);
private:
int numExtraRows;
QList<ComicDB> _data;
QList<ComicDB> _removed;
};
#endif // LOCAL_COMIC_LIST_MODEL_H

View File

@ -0,0 +1,83 @@
#include "response_parser.h"
#include <QtScript>
#include <QDebug>
ResponseParser::ResponseParser(QObject *parent) :
QObject(parent),error(false),numResults(-1),currentPage(-1),totalPages(-1),errorTxt("None")
{
}
bool ResponseParser::responseError()
{
return error;
}
QString ResponseParser::errorDescription()
{
return errorTxt;
}
qint32 ResponseParser::getNumResults()
{
return numResults;
}
qint32 ResponseParser::getCurrentPage()
{
return currentPage;
}
qint32 ResponseParser::getTotalPages()
{
return totalPages;
}
bool ResponseParser::isError(qint32 error)
{
switch(error)
{
case 100:
return true;
default:
return false;
}
}
void ResponseParser::loadJSONResponse(const QString &response)
{
QScriptEngine engine;
QScriptValue sc;
sc = engine.evaluate("(" + response + ")");
errorTxt = "None";
if (!sc.property("status_code").isValid() || isError(sc.property("status_code").toInt32()))
{
error = true;
if(sc.property("error").isValid())
errorTxt = sc.property("error").toString();
else
errorTxt = "Unknown error";
}
else
{
error = false;
if(sc.property("number_of_total_results").isValid())
numResults = sc.property("number_of_total_results").toString().toInt();// sc.property("number_of_total_results").toInt32();
else
qDebug() << sc.property("oops").toString();
int limit = sc.property("limit").toInt32();
int offset = sc.property("offset").toInt32();
int total = sc.property("number_of_total_results").toInt32();
if(limit > 0)
{
totalPages = (total / limit) + (total%limit>0?1:0);
currentPage = (offset / limit) + 1;
}
else
totalPages = currentPage = 1;
}
}

View File

@ -0,0 +1,30 @@
#ifndef RESPONSE_PARSER_H
#define RESPONSE_PARSER_H
#include <QObject>
class ResponseParser : public QObject
{
Q_OBJECT
public:
explicit ResponseParser(QObject *parent = 0);
bool responseError();
QString errorDescription();
qint32 getNumResults();
qint32 getCurrentPage();
qint32 getTotalPages();
bool isError(qint32 error);
signals:
public slots:
void loadJSONResponse(const QString & response);
protected:
bool error;
QString errorTxt;
qint32 numResults;
qint32 currentPage;
qint32 totalPages;
};
#endif // RESPONSE_PARSER_H

View File

@ -0,0 +1,172 @@
#include "volume_comics_model.h"
#include "qnaturalsorting.h"
#include <QtScript>
bool lessThan(const QList<QString> & left, const QList<QString> & right)
{
if ((left.count() > 0) && (right.count() > 0))
return naturalSortLessThanCI(left.at(0),right.at(0));
else
return true;
}
VolumeComicsModel::VolumeComicsModel(QObject * parent) :
JSONModel(parent),numExtraRows(0)
{
}
void VolumeComicsModel::load(const QString & json)
{
QScriptEngine engine;
QScriptValue sc;
sc = engine.evaluate("(" + json + ")");
if (!sc.property("error").isValid() && sc.property("error").toString() != "OK")
{
qDebug("Error detected");
}
else
{
QScriptValueIterator it(sc.property("results"));
//bool test;
QScriptValue resultsValue;
while (it.hasNext()) {
it.next();
if(it.flags() & QScriptValue::SkipInEnumeration)
continue;
resultsValue = it.value();
QString issueNumber = resultsValue.property("issue_number").toString();
QString name = resultsValue.property("name").toString();
QString coverURL = resultsValue.property("image").property("medium_url").toString();
QString id = resultsValue.property("id").toString();
QStringList l;
l << issueNumber << name << coverURL << id;
_data.push_back(l);
}
qSort(_data.begin(),_data.end(),lessThan);
}
}
QModelIndex VolumeComicsModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index)
return QModelIndex(); //no parent
}
int VolumeComicsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return _data.count() + numExtraRows;
}
int VolumeComicsModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if(_data.isEmpty())
return 0;
else
return 2;
}
QVariant VolumeComicsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
int column = index.column();
if (role == Qt::DecorationRole)
{
return QVariant();
}
if (role == Qt::TextAlignmentRole)
{
switch(column)//TODO obtener esto de la query
{
case ISSUE:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
case TITLE:
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
}
}
if(role != Qt::DisplayRole)
return QVariant();
if(row<_data.count())
return _data[row][column];
else
return QVariant();
}
Qt::ItemFlags VolumeComicsModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant VolumeComicsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch(section)//TODO obtener esto de la query
{
case ISSUE:
return QVariant(QString("issue"));
case TITLE:
return QVariant(QString(tr("title")));
}
}
if (orientation == Qt::Horizontal && role == Qt::TextAlignmentRole)
{
switch(section)//TODO obtener esto de la query
{
case ISSUE:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
case TITLE:
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
}
}
return QVariant();
}
QModelIndex VolumeComicsModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
QString VolumeComicsModel::getComicId(const QModelIndex &index) const
{
int row = index.row();
if(row >= _data.count())
return "";
return _data[row][ID];
}
QString VolumeComicsModel::getComicId(int row) const
{
if(row >= _data.count())
return "";
return _data[row][ID];
}
QString VolumeComicsModel::getCoverURL(const QModelIndex &index) const
{
return _data[index.row()][COVER_URL];
}
void VolumeComicsModel::addExtraRows(int numRows)
{
numExtraRows = numRows;
}

View File

@ -0,0 +1,41 @@
#ifndef VOLUME_COMICS_MODEL_H
#define VOLUME_COMICS_MODEL_H
#include "json_model.h"
class VolumeComicsModel : public JSONModel
{
Q_OBJECT
public:
explicit VolumeComicsModel(QObject *parent = 0);
void load(const QString & json);
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
signals:
public slots:
QString getComicId(const QModelIndex &index) const;
QString getComicId(int row) const;
QString getCoverURL(const QModelIndex &index) const;
void addExtraRows(int numRows);
private:
int numExtraRows;
QList <QList <QString> > _data;
enum Column {
ISSUE = 0,
TITLE,
COVER_URL,
ID
};
};
#endif // VOLUME_COMICS_MODEL_H

View File

@ -0,0 +1,161 @@
#include "volumes_model.h"
#include <QtScript>
VolumesModel::VolumesModel(QObject *parent) :
JSONModel(parent)
{
}
VolumesModel::~VolumesModel()
{
//std::for_each(_data.begin(), _data.end(), [](QList<QString> * ptr) { delete ptr; });
}
void VolumesModel::load(const QString &json)
{
QScriptEngine engine;
QScriptValue sc;
sc = engine.evaluate("(" + json + ")");
if (!sc.property("error").isValid() && sc.property("error").toString() != "OK")
{
qDebug("Error detected");
}
else
{
int numResults = sc.property("number_of_total_results").toString().toInt(); //fix to weird behaviour using hasNext
QScriptValueIterator it(sc.property("results"));
bool test;
QScriptValue resultsValue;
while (it.hasNext()) {
it.next();
resultsValue = it.value();
QString numIssues = resultsValue.property("count_of_issues").toString();
QString year = resultsValue.property("start_year").toString();
QString name = resultsValue.property("name").toString();
QString publisher = resultsValue.property("publisher").property("name").toString();
QString url = resultsValue.property("image").property("medium_url").toString();
QString deck = resultsValue.property("deck").toString();
QString id = resultsValue.property("id").toString();
QStringList l;
l << name << year << numIssues << publisher << url << deck << id;
test = name.isEmpty() && year.isEmpty() && numIssues.isEmpty() && url.isEmpty();
if(numResults>0 && !test)
_data.push_back(l);
numResults--;
}
}
}
QModelIndex VolumesModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index)
return QModelIndex(); //no parent
}
int VolumesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return _data.count();
}
int VolumesModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if(_data.isEmpty())
return 0;
else
return 4;//_data.at(0)->count();
}
QVariant VolumesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DecorationRole)
{
return QVariant();
}
if (role == Qt::TextAlignmentRole)
{
//TODO
}
if(role != Qt::DisplayRole)
return QVariant();
int row = index.row();
int column = index.column();
return _data[row][column];
}
Qt::ItemFlags VolumesModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant VolumesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch(section)//TODO obtener esto de la query
{
case SERIES:
return QVariant(QString("series"));
case YEAR:
return QVariant(QString(tr("year")));
case ISSUES:
return QVariant(QString(tr("issues")));
case PUBLISHER:
return QVariant(QString(tr("publisher")));
}
}
if (orientation == Qt::Horizontal && role == Qt::TextAlignmentRole)
{
switch(section)//TODO obtener esto de la query
{
case YEAR:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
case ISSUES:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
}
}
return QVariant();
}
QModelIndex VolumesModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
QString VolumesModel::getVolumeId(const QModelIndex &index) const
{
return _data[index.row()][ID];
}
int VolumesModel::getNumIssues(const QModelIndex &index) const
{
return _data[index.row()][ISSUES].toInt();
}
QString VolumesModel::getPublisher(const QModelIndex &index) const
{
return _data[index.row()][PUBLISHER];
}
QString VolumesModel::getCoverURL(const QModelIndex &index) const
{
return _data[index.row()][COVER_URL];
}

View File

@ -0,0 +1,50 @@
#ifndef VOLUMES_MODEL_H
#define VOLUMES_MODEL_H
#include "json_model.h"
class VolumesModel : public JSONModel
{
Q_OBJECT
public:
explicit VolumesModel(QObject *parent = 0);
virtual ~VolumesModel();
//receive a valid json with a list of volumes
void load(const QString & json);
//QAbstractItemModel methods
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QString getVolumeId(const QModelIndex & index) const;
int getNumIssues(const QModelIndex & index) const;
QString getPublisher(const QModelIndex & index) const;
QString getCoverURL(const QModelIndex & index) const;
signals:
public slots:
private:
QList <QList <QString> > _data;
public:
enum Column {
SERIES = 0,
YEAR,
ISSUES,
PUBLISHER,
COVER_URL,
DECK,
ID
};
};
#endif // VOLUMES_MODEL_H