mirror of
https://github.com/YACReader/yacreader
synced 2025-07-26 08:55:05 -04:00
fixed VolumesModel
VolumeComicsModel and LocalComicListModel finished SortVolumeComics tables are now synchronized
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
#include "local_comic_list_model.h"
|
||||
|
||||
LocalComicListModel::LocalComicListModel(QObject *parent) :
|
||||
QAbstractItemModel(parent)
|
||||
QAbstractItemModel(parent),numExtraRows(0)
|
||||
{
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ QModelIndex LocalComicListModel::parent(const QModelIndex &index) const
|
||||
int LocalComicListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
return _data.count();
|
||||
return _data.count() + numExtraRows;
|
||||
}
|
||||
|
||||
int LocalComicListModel::columnCount(const QModelIndex &parent) const
|
||||
@ -50,8 +50,11 @@ QVariant LocalComicListModel::data(const QModelIndex &index, int role) const
|
||||
return QVariant();
|
||||
|
||||
int row = index.row();
|
||||
int column = index.column();
|
||||
return _data[row].getFileName();
|
||||
|
||||
if(row < _data.count())
|
||||
return _data[row].getFileName();
|
||||
else
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
Qt::ItemFlags LocalComicListModel::flags(const QModelIndex &index) const
|
||||
@ -64,6 +67,9 @@ Qt::ItemFlags LocalComicListModel::flags(const QModelIndex &index) const
|
||||
QVariant LocalComicListModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
|
||||
if ( role == Qt::TextAlignmentRole)
|
||||
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
|
||||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
||||
{
|
||||
return QVariant(QString(tr("file name")));
|
||||
@ -80,3 +86,8 @@ QModelIndex LocalComicListModel::index(int row, int column, const QModelIndex &p
|
||||
return createIndex(row, column);
|
||||
}
|
||||
|
||||
void LocalComicListModel::addExtraRows(int numRows)
|
||||
{
|
||||
numExtraRows = numRows;
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,10 @@ signals:
|
||||
|
||||
public slots:
|
||||
|
||||
void addExtraRows(int numRows);
|
||||
|
||||
private:
|
||||
int numExtraRows;
|
||||
QList<ComicDB> _data;
|
||||
};
|
||||
|
||||
|
@ -1,11 +1,158 @@
|
||||
#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)
|
||||
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
|
||||
{
|
||||
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 issueNumber = resultsValue.property("issue_number").toString();
|
||||
QString name = resultsValue.property("name").toString();
|
||||
QString id = resultsValue.property("id").toString();
|
||||
QStringList l;
|
||||
l << issueNumber << name << id;
|
||||
//test = name.isEmpty() && year.isEmpty() && numIssues.isEmpty() && url.isEmpty();
|
||||
if(numResults > 0)
|
||||
_data.push_back(l);
|
||||
numResults--;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
return _data[index.row()][ID];
|
||||
}
|
||||
|
||||
void VolumeComicsModel::addExtraRows(int numRows)
|
||||
{
|
||||
numExtraRows = numRows;
|
||||
}
|
||||
|
||||
|
@ -9,10 +9,30 @@ class VolumeComicsModel : public JSONModel
|
||||
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;
|
||||
void addExtraRows(int numRows);
|
||||
|
||||
private:
|
||||
int numExtraRows;
|
||||
QList <QList <QString> > _data;
|
||||
|
||||
enum Column {
|
||||
ISSUE = 0,
|
||||
TITLE,
|
||||
ID
|
||||
};
|
||||
};
|
||||
|
||||
#endif // VOLUME_COMICS_MODEL_H
|
||||
|
@ -10,7 +10,7 @@ VolumesModel::VolumesModel(QObject *parent) :
|
||||
|
||||
VolumesModel::~VolumesModel()
|
||||
{
|
||||
std::for_each(_data.begin(), _data.end(), [](QList<QString> * ptr) { delete ptr; });
|
||||
//std::for_each(_data.begin(), _data.end(), [](QList<QString> * ptr) { delete ptr; });
|
||||
}
|
||||
|
||||
void VolumesModel::load(const QString &json)
|
||||
@ -39,11 +39,11 @@ void VolumesModel::load(const QString &json)
|
||||
QString url = resultsValue.property("image").property("medium_url").toString();
|
||||
QString deck = resultsValue.property("deck").toString();
|
||||
QString id = resultsValue.property("id").toString();
|
||||
QStringList & l = *(new QStringList);
|
||||
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);
|
||||
_data.push_back(l);
|
||||
numResults--;
|
||||
}
|
||||
}
|
||||
@ -89,7 +89,7 @@ QVariant VolumesModel::data(const QModelIndex &index, int role) const
|
||||
|
||||
int row = index.row();
|
||||
int column = index.column();
|
||||
return _data[row]->at(column);
|
||||
return _data[row][column];
|
||||
}
|
||||
|
||||
Qt::ItemFlags VolumesModel::flags(const QModelIndex &index) const
|
||||
@ -136,11 +136,16 @@ QModelIndex VolumesModel::index(int row, int column, const QModelIndex &parent)
|
||||
if (!hasIndex(row, column, parent))
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(row, column, _data[row]);
|
||||
return createIndex(row, column);
|
||||
}
|
||||
|
||||
QString VolumesModel::getVolumeId(const QModelIndex &index) const
|
||||
{
|
||||
return _data.at(index.row())->at(ID);
|
||||
return _data[index.row()][ID];
|
||||
}
|
||||
|
||||
QString VolumesModel::getCoverURL(const QModelIndex &index) const
|
||||
{
|
||||
return _data[index.row()][COVER_URL];
|
||||
}
|
||||
|
||||
|
@ -23,13 +23,14 @@ public:
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
|
||||
QString getVolumeId(const QModelIndex & index) const;
|
||||
QString getCoverURL(const QModelIndex & index) const;
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
QList <QList <QString> * > _data;
|
||||
QList <QList <QString> > _data;
|
||||
|
||||
public:
|
||||
enum Column {
|
||||
|
Reference in New Issue
Block a user