mirror of
https://github.com/YACReader/yacreader
synced 2025-06-04 01:28:55 -04:00
fixed VolumesModel
VolumeComicsModel and LocalComicListModel finished SortVolumeComics tables are now synchronized
This commit is contained in:
parent
63636dd417
commit
5a08e109f4
@ -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 {
|
||||
|
@ -101,17 +101,17 @@ void SelectVolume::load(const QString & json)
|
||||
|
||||
tableVolumes->setFixedSize(619,341);
|
||||
|
||||
if(tempM->rowCount()>0)
|
||||
{
|
||||
tableVolumes->selectRow(0);
|
||||
loadVolumeInfo(tempM->index(0,0));
|
||||
}
|
||||
|
||||
if(model != 0)
|
||||
delete model;
|
||||
else
|
||||
model = tempM;
|
||||
|
||||
if(model->rowCount()>0)
|
||||
{
|
||||
tableVolumes->selectRow(0);
|
||||
loadVolumeInfo(model->index(0,0));
|
||||
}
|
||||
|
||||
tableVolumes->setColumnWidth(0,350);
|
||||
}
|
||||
|
||||
@ -119,10 +119,10 @@ SelectVolume::~SelectVolume() {}
|
||||
|
||||
void SelectVolume::loadVolumeInfo(const QModelIndex & mi)
|
||||
{
|
||||
QStringList * data = static_cast<QStringList *>(mi.internalPointer());
|
||||
QString coverURL = data->at(VolumesModel::COVER_URL);
|
||||
QString deck = data->at(VolumesModel::DECK);
|
||||
QString id = data->at(VolumesModel::ID);
|
||||
//QStringList * data = static_cast<QStringList *>(mi.internalPointer());
|
||||
QString coverURL = model->getCoverURL(mi);
|
||||
//QString deck = model->data(model->index(mi.row(),VolumesModel::DECK)).toString();
|
||||
QString id = model->getVolumeId(mi);
|
||||
|
||||
//cover->setText(coverURL);
|
||||
//detailLabel->setText(deck);
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include "scrapper_tableview.h"
|
||||
#include "local_comic_list_model.h"
|
||||
#include "volume_comics_model.h"
|
||||
|
||||
SortVolumeComics::SortVolumeComics(QWidget *parent) :
|
||||
QWidget(parent)
|
||||
@ -17,6 +18,7 @@ SortVolumeComics::SortVolumeComics(QWidget *parent) :
|
||||
label->setStyleSheet(labelStylesheet);
|
||||
|
||||
QLabel * sortLabel = new QLabel(tr("sort comic info to match your comic files"));
|
||||
sortLabel->setStyleSheet(labelStylesheet);
|
||||
moveUpButton = new QPushButton;
|
||||
moveDownButton = new QPushButton;
|
||||
|
||||
@ -33,7 +35,11 @@ SortVolumeComics::SortVolumeComics(QWidget *parent) :
|
||||
content->addWidget(tableVolumeComics,0,Qt::AlignRight|Qt::AlignTop);
|
||||
//content->addWidget(tableVolumes,0,Qt::AlignRight|Qt::AlignTop);
|
||||
|
||||
connect(tableVolumeComics->verticalScrollBar(), SIGNAL(valueChanged(int)), tableFiles->verticalScrollBar(), SLOT(setValue(int)));
|
||||
connect(tableVolumeComics->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(synchronizeScroll(int)));
|
||||
connect(tableFiles->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(synchronizeScroll(int)));
|
||||
|
||||
connect(tableVolumeComics, SIGNAL(pressed(QModelIndex)), tableFiles, SLOT(setCurrentIndex(QModelIndex)));
|
||||
connect(tableFiles, SIGNAL(pressed(QModelIndex)), tableVolumeComics, SLOT(setCurrentIndex(QModelIndex)));
|
||||
|
||||
sortButtonsLayout->addStretch();
|
||||
sortButtonsLayout->addWidget(sortLabel);
|
||||
@ -58,5 +64,43 @@ void SortVolumeComics::setData(QList<ComicDB> & comics, const QString &json)
|
||||
localComicsModel = new LocalComicListModel;
|
||||
localComicsModel->load(comics);
|
||||
|
||||
volumeComicsModel = new VolumeComicsModel;
|
||||
volumeComicsModel->load(json);
|
||||
|
||||
int numLocalComics = localComicsModel->rowCount();
|
||||
int numVolumeComics = volumeComicsModel->rowCount();
|
||||
|
||||
if(numLocalComics > numVolumeComics)
|
||||
volumeComicsModel->addExtraRows(numLocalComics - numVolumeComics);
|
||||
if(numLocalComics < numVolumeComics)
|
||||
localComicsModel->addExtraRows(numVolumeComics - numLocalComics);
|
||||
|
||||
tableFiles->setModel(localComicsModel);
|
||||
tableVolumeComics->setModel(volumeComicsModel);
|
||||
|
||||
tableVolumeComics->resizeColumnToContents(0);
|
||||
}
|
||||
|
||||
void SortVolumeComics::synchronizeScroll(int pos)
|
||||
{
|
||||
void * senderObject = sender();
|
||||
|
||||
if(senderObject == 0) //invalid call
|
||||
return;
|
||||
|
||||
QScrollBar * tableVolumeComicsScrollBar = tableVolumeComics->verticalScrollBar();
|
||||
QScrollBar * tableFilesScrollBar = tableFiles->verticalScrollBar();
|
||||
|
||||
if(senderObject == tableVolumeComicsScrollBar)
|
||||
{
|
||||
disconnect(tableFilesScrollBar,SIGNAL(valueChanged(int)),this,0);
|
||||
tableFilesScrollBar->setValue(pos);
|
||||
connect(tableFilesScrollBar, SIGNAL(valueChanged(int)), this, SLOT(synchronizeScroll(int)));
|
||||
}
|
||||
else
|
||||
{
|
||||
disconnect(tableVolumeComicsScrollBar,SIGNAL(valueChanged(int)),this,0);
|
||||
tableVolumeComicsScrollBar->setValue(pos);
|
||||
connect(tableVolumeComicsScrollBar, SIGNAL(valueChanged(int)), this, SLOT(synchronizeScroll(int)));
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define SORT_VOLUME_COMICS_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QModelIndex>
|
||||
|
||||
#include "comic_db.h"
|
||||
|
||||
@ -21,6 +22,9 @@ signals:
|
||||
public slots:
|
||||
void setData(QList<ComicDB> & comics, const QString & json);
|
||||
|
||||
protected slots:
|
||||
void synchronizeScroll(int pos);
|
||||
|
||||
private:
|
||||
ScrapperTableView * tableFiles;
|
||||
ScrapperTableView * tableVolumeComics;
|
||||
|
Loading…
x
Reference in New Issue
Block a user