Format code using clang-format

This commit is contained in:
Luis Ángel San Martín
2019-05-30 19:46:37 +02:00
parent e0eb94e3ae
commit e3ec56aa43
356 changed files with 19824 additions and 21874 deletions

View File

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

View File

@ -5,14 +5,13 @@
class ComicsModel : public JSONModel
{
Q_OBJECT
Q_OBJECT
public:
explicit ComicsModel(QObject *parent = 0);
explicit ComicsModel(QObject *parent = 0);
signals:
public slots:
};
#endif // COMICS_MODEL_H

View File

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

View File

@ -5,15 +5,14 @@
class JSONModel : public QAbstractItemModel
{
Q_OBJECT
Q_OBJECT
public:
explicit JSONModel(QObject *parent = 0);
virtual void load(const QString & json) = 0 ;
explicit JSONModel(QObject *parent = 0);
virtual void load(const QString &json) = 0;
signals:
public slots:
};
#endif // JSON_MODEL_H

View File

@ -1,90 +1,86 @@
#include "local_comic_list_model.h"
LocalComicListModel::LocalComicListModel(QObject *parent) :
QAbstractItemModel(parent),numExtraRows(0)
LocalComicListModel::LocalComicListModel(QObject *parent)
: QAbstractItemModel(parent), numExtraRows(0)
{
}
void LocalComicListModel::load(QList<ComicDB> &comics)
{
_data = comics;
_data = comics;
}
QModelIndex LocalComicListModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index)
return QModelIndex(); //no parent
Q_UNUSED(index)
return QModelIndex(); //no parent
}
int LocalComicListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return _data.count();
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();
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 (!index.isValid())
return QVariant();
if (role == Qt::DecorationRole)
{
return QVariant();
}
if (role == Qt::TextAlignmentRole)
{
//TODO
}
if (role == Qt::DecorationRole) {
return QVariant();
}
if (role == Qt::TextAlignmentRole) {
//TODO
}
if(role != Qt::DisplayRole)
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
int row = index.row();
int row = index.row();
//if(row < _data.count())
return _data[row].getFileName();
//else
//return QVariant();
//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;
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 (role == Qt::TextAlignmentRole)
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
return QVariant(QString(tr("file name")));
}
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return QVariant(QString(tr("file name")));
}
return QVariant();
return QVariant();
}
QModelIndex LocalComicListModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
return createIndex(row, column);
}
QList<ComicDB> LocalComicListModel::getData()
@ -99,18 +95,17 @@ void LocalComicListModel::removeComics(const QList<QModelIndex> &selectedIndexes
int sourceRow = mi.row();
int sourceLastRow = lastMi.row();
beginRemoveRows(QModelIndex(),selectedIndexes.first().row(),selectedIndexes.last().row());
beginRemoveRows(QModelIndex(), selectedIndexes.first().row(), selectedIndexes.last().row());
for(int i = sourceLastRow;i>=sourceRow;i--)
{
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++)
beginInsertRows(QModelIndex(), _data.count() - _removed.count(), _data.count() - 1);
for (int i = 0; i < _removed.count(); i++)
_data.append(ComicDB());
endInsertRows();
}
@ -118,16 +113,14 @@ void LocalComicListModel::removeComics(const QList<QModelIndex> &selectedIndexes
void LocalComicListModel::restoreAll()
{
int numItemsToRemove = 0;
for(int i = 0;numItemsToRemove<_removed.count();i++)
{
if(_data.at(i).getFileName().isEmpty())
{
beginRemoveRows(QModelIndex(),i,i);
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));
beginInsertRows(QModelIndex(), i, i);
_data.insert(i, _removed.at(numItemsToRemove));
endInsertRows();
numItemsToRemove++;
@ -139,46 +132,45 @@ void LocalComicListModel::restoreAll()
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;
QModelIndex mi = selectedIndexes.first();
QModelIndex lastMi = selectedIndexes.last();
int sourceRow = mi.row();
int sourceLastRow = lastMi.row();
int destRow = sourceRow - 1;
if(destRow < 0)
return;
if (destRow < 0)
return;
beginMoveRows(mi.parent(),sourceRow,sourceLastRow,mi.parent(),destRow);
beginMoveRows(mi.parent(), sourceRow, sourceLastRow, mi.parent(), destRow);
for(int i = sourceRow; i <= sourceLastRow; i++)
_data.swap(i, i-1);
for (int i = sourceRow; i <= sourceLastRow; i++)
_data.swap(i, i - 1);
endMoveRows();
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;
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;
if (destRow >= _data.count())
return;
beginMoveRows(mi.parent(),sourceRow,sourceLastRow,mi.parent(),destRow+1);
beginMoveRows(mi.parent(), sourceRow, sourceLastRow, mi.parent(), destRow + 1);
for(int i = sourceLastRow; i >= sourceRow; i--)
_data.swap(i, i+1);
for (int i = sourceLastRow; i >= sourceRow; i--)
_data.swap(i, i + 1);
endMoveRows();
endMoveRows();
}
void LocalComicListModel::addExtraRows(int numRows)
{
numExtraRows = numRows;
for(int i = 0; i<numExtraRows; i++)
_data.append(ComicDB());
numExtraRows = numRows;
for (int i = 0; i < numExtraRows; i++)
_data.append(ComicDB());
}

View File

@ -7,35 +7,35 @@
class LocalComicListModel : public QAbstractItemModel
{
Q_OBJECT
Q_OBJECT
public:
explicit LocalComicListModel(QObject *parent = 0);
explicit LocalComicListModel(QObject *parent = 0);
void load(QList<ComicDB> & comics);
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();
//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 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);
void moveSelectionUp(const QList<QModelIndex> &selectedIndexes);
void moveSelectionDown(const QList<QModelIndex> &selectedIndexes);
void addExtraRows(int numRows);
private:
int numExtraRows;
QList<ComicDB> _data;
int numExtraRows;
QList<ComicDB> _data;
QList<ComicDB> _removed;
};

View File

@ -3,8 +3,8 @@
#include <QtScript>
#include <QDebug>
ResponseParser::ResponseParser(QObject *parent) :
QObject(parent),error(false),errorTxt("None"),numResults(-1),currentPage(-1),totalPages(-1)
ResponseParser::ResponseParser(QObject *parent)
: QObject(parent), error(false), errorTxt("None"), numResults(-1), currentPage(-1), totalPages(-1)
{
}
@ -20,12 +20,12 @@ QString ResponseParser::errorDescription()
qint32 ResponseParser::getNumResults()
{
return numResults;
return numResults;
}
qint32 ResponseParser::getCurrentPage()
{
return currentPage;
return currentPage;
}
qint32 ResponseParser::getTotalPages()
@ -35,49 +35,43 @@ qint32 ResponseParser::getTotalPages()
bool ResponseParser::isError(qint32 error)
{
switch(error)
{
case 100:
return true;
switch (error) {
case 100:
return true;
default:
return false;
default:
return false;
}
}
void ResponseParser::loadJSONResponse(const QString &response)
{
QScriptEngine engine;
QScriptValue sc;
sc = engine.evaluate("(" + 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())
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();
} 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;
}
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

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

View File

@ -1,53 +1,49 @@
#include "volume_comics_model.h"
#include "qnaturalsorting.h"
#include <QtScript>
bool lessThan(const QList<QString> & left, const QList<QString> & right)
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;
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)
VolumeComicsModel::VolumeComicsModel(QObject *parent)
: JSONModel(parent), numExtraRows(0)
{
}
void VolumeComicsModel::load(const QString & json)
void VolumeComicsModel::load(const QString &json)
{
QScriptEngine engine;
QScriptValue sc;
sc = engine.evaluate("(" + 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)
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();
resultsValue = it.value();
QString issueNumber = resultsValue.property("issue_number").toString();
QScriptValue propertyName = resultsValue.property("name");
QString name = propertyName.isNull() ? "-" : propertyName.toString();
QString coverURL = resultsValue.property("image").property("medium_url").toString();
QString id = resultsValue.property("id").toString();
QStringList l;
l << issueNumber << name << coverURL << id;
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);
qSort(_data.begin(), _data.end(), lessThan);
}
}
@ -60,121 +56,116 @@ void VolumeComicsModel::load(const QString & json)
QModelIndex VolumeComicsModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index)
return QModelIndex(); //no parent
Q_UNUSED(index)
return QModelIndex(); //no parent
}
int VolumeComicsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return _data.count() + numExtraRows;
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;
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();
if (!index.isValid())
return QVariant();
int row = index.row();
int column = index.column();
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::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 (role != Qt::DisplayRole)
return QVariant();
if(row<_data.count())
return _data[row][column];
else
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;
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::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);
}
}
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();
return QVariant();
}
QModelIndex VolumeComicsModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
return createIndex(row, column);
}
QString VolumeComicsModel::getComicId(const QModelIndex &index) const
{
int row = index.row();
if(row >= _data.count())
return "";
return _data[row][ID];
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];
if (row >= _data.count())
return "";
return _data[row][ID];
}
QString VolumeComicsModel::getCoverURL(const QModelIndex &index) const
{
return _data[index.row()][COVER_URL];
return _data[index.row()][COVER_URL];
}
void VolumeComicsModel::addExtraRows(int numRows)
{
numExtraRows = numRows;
numExtraRows = numRows;
}

View File

@ -5,38 +5,38 @@
class VolumeComicsModel : public JSONModel
{
Q_OBJECT
Q_OBJECT
public:
explicit VolumeComicsModel(QObject *parent = 0);
void load(const QString & json);
explicit VolumeComicsModel(QObject *parent = 0);
void load(const QString &json);
//void load(const QStringList & jsonList);
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;
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);
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;
int numExtraRows;
QList<QList<QString>> _data;
enum Column {
ISSUE = 0,
TITLE,
COVER_URL,
ID
};
enum Column {
ISSUE = 0,
TITLE,
COVER_URL,
ID
};
};
#endif // VOLUME_COMICS_MODEL_H

View File

@ -2,90 +2,129 @@
#include <QtScript>
VolumesModel::VolumesModel(QObject *parent) :
JSONModel(parent)
VolumesModel::VolumesModel(QObject *parent)
: JSONModel(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)
{
QScriptEngine engine;
QScriptValue sc;
sc = engine.evaluate("(" + 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--;
}
}
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
Q_UNUSED(index)
return QModelIndex(); //no parent
}
int VolumesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return _data.count();
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();
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)
{
if (!index.isValid())
return QVariant();
}
if (role == Qt::DecorationRole) {
return QVariant();
}
int row = index.row();
int column = index.column();
if (role == Qt::TextAlignmentRole)
{
switch(column)
if (role == Qt::TextAlignmentRole) {
switch (column) {
case YEAR:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
case ISSUES:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
default:
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
}
}
if (role != Qt::DisplayRole)
return QVariant();
if (column == YEAR || column == ISSUES) {
return _data[row][column].toInt();
} else {
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);
@ -94,68 +133,17 @@ QVariant VolumesModel::data(const QModelIndex &index, int role) const
default:
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
}
}
if(role != Qt::DisplayRole)
return QVariant();
if (column == YEAR || column == ISSUES)
{
return _data[row][column].toInt();
}
else
{
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);
default:
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
}
}
return QVariant();
return QVariant();
}
QModelIndex VolumesModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
return createIndex(row, column);
}
QString VolumesModel::getVolumeId(const QModelIndex &index) const
@ -175,6 +163,5 @@ QString VolumesModel::getPublisher(const QModelIndex &index) const
QString VolumesModel::getCoverURL(const QModelIndex &index) const
{
return _data[index.row()][COVER_URL];
return _data[index.row()][COVER_URL];
}

View File

@ -5,49 +5,49 @@
class VolumesModel : public JSONModel
{
Q_OBJECT
Q_OBJECT
public:
explicit VolumesModel(QObject *parent = 0);
virtual ~VolumesModel();
//receive a valid json with a list of volumes
void load(const QString & json);
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;
//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;
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;
QList<QList<QString>> _data;
public:
enum Column {
SERIES = 0,
YEAR,
ISSUES,
PUBLISHER,
COVER_URL,
DECK,
ID
};
enum Column {
SERIES = 0,
YEAR,
ISSUES,
PUBLISHER,
COVER_URL,
DECK,
ID
};
enum Role {
SORT_ROLE = Qt::UserRole
};
SORT_ROLE = Qt::UserRole
};
};
#endif // VOLUMES_MODEL_H