Separaci?n en ficheros de las clases LibraryItem, Folder y Comic

A?adida la toolbar para la gesti?n de c?mics + im?genes para los iconos
Modificado el di?logo de informaci?n/propiedades de los c?mics
This commit is contained in:
Luis Ángel San Martín
2012-05-29 23:45:31 +02:00
parent 9dfa9c5f62
commit ed850ef374
23 changed files with 725 additions and 246 deletions

View File

@ -0,0 +1,101 @@
#include "comic.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QVariant>
Comic::Comic()
{
}
Comic::Comic(qulonglong cparentId, qulonglong ccomicInfoId, QString cname, QString cpath, QString chash)
:comicInfoId(ccomicInfoId),hash(chash)
{
parentId = cparentId;
name = cname;
path = cpath;
}
qulonglong Comic::insert(QSqlDatabase & db)
{
//TODO comprobar si ya hay comic info con ese hash
QSqlQuery comicInfoInsert(db);
comicInfoInsert.prepare("INSERT INTO comic_info (hash) "
"VALUES (:hash)");
comicInfoInsert.bindValue(":hash", hash);
comicInfoInsert.exec();
qulonglong comicInfoId =comicInfoInsert.lastInsertId().toLongLong();
QSqlQuery query(db);
query.prepare("INSERT INTO comic (parentId, comicInfoId, fileName, path) "
"VALUES (:parentId,:comicInfoId,:name, :path)");
query.bindValue(":parentId", parentId);
query.bindValue(":comicInfoId", comicInfoId);
query.bindValue(":name", name);
query.bindValue(":path", path);
query.exec();
return query.lastInsertId().toLongLong();
}
QList<LibraryItem *> Comic::getComicsFromParent(qulonglong parentId, QSqlDatabase & db)
{
QList<LibraryItem *> list;
QSqlQuery selectQuery(db); //TODO check
selectQuery.prepare("select c.id,c.parentId,c.fileName,c.path,ci.hash from comic c inner join comic_info ci on (c.comicInfoId = ci.id) where c.parentId = :parentId");
selectQuery.bindValue(":parentId", parentId);
selectQuery.exec();
Comic * currentItem;
while (selectQuery.next())
{
QList<QVariant> data;
QSqlRecord record = selectQuery.record();
for(int i=0;i<record.count();i++)
data << record.value(i);
//TODO sort by sort indicator and name
currentItem = new Comic();
currentItem->id = record.value(0).toLongLong();
currentItem->parentId = record.value(1).toLongLong();
currentItem->name = record.value(2).toString();
currentItem->hash = record.value(3).toString();
int lessThan = 0;
if(list.isEmpty())
list.append(currentItem);
else
{
Comic * last = static_cast<Comic *>(list.back());
QString nameLast = last->name;
QString nameCurrent = currentItem->name;
QList<LibraryItem *>::iterator i;
i = list.end();
i--;
while ((0 > (lessThan = nameCurrent.localeAwareCompare(nameLast))) && i != list.begin()) //se usa la misma ordenaci<63>n que en QDir
{
i--;
nameLast = (*i)->name;
}
if(lessThan>0) //si se ha encontrado un elemento menor que current, se inserta justo despu<70>s
list.insert(++i,currentItem);
else
list.insert(i,currentItem);
}
}
return list;
}
void Comic::removeFromDB(QSqlDatabase & db)
{
QSqlQuery query(db);
query.prepare("DELETE FROM comic WHERE id = :id");
query.bindValue(":id", id);
query.exec();
}
bool Comic::isDir()
{
return false;
}

View File

@ -0,0 +1,24 @@
#ifndef __COMIC_H
#define __COMIC_H
#include "library_item.h"
#include <QSqlDatabase>
#include <QList>
class Comic : public LibraryItem
{
public:
qulonglong comicInfoId;
QString hash;
Comic();
Comic(qulonglong cparentId, qulonglong ccomicInfoId, QString cname, QString cpath, QString chash);
//Comic(QString fn, QString fp):name(fn),path(fp),knownParent(false), knownId(false){};
qulonglong insert(QSqlDatabase & db);
static QList<LibraryItem *> getComicsFromParent(qulonglong parentId, QSqlDatabase & db);
bool isDir();
void removeFromDB(QSqlDatabase & db);
};
#endif

View File

@ -0,0 +1,71 @@
#include "folder.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QVariant>
qulonglong Folder::insert(QSqlDatabase & db)
{
QSqlQuery query(db);
query.prepare("INSERT INTO folder (parentId, name, path) "
"VALUES (:parentId, :name, :path)");
query.bindValue(":parentId", parentId);
query.bindValue(":name", name);
query.bindValue(":path", path);
query.exec();
return query.lastInsertId().toLongLong();
}
QList<LibraryItem *> Folder::getFoldersFromParent(qulonglong parentId, QSqlDatabase & db)
{
QList<LibraryItem *> list;
QSqlQuery selectQuery(db); //TODO check
selectQuery.prepare("SELECT * FROM folder WHERE parentId = :parentId");
selectQuery.bindValue(":parentId", parentId);
selectQuery.exec();
Folder * currentItem;
while (selectQuery.next())
{
QList<QVariant> data;
QSqlRecord record = selectQuery.record();
for(int i=0;i<record.count();i++)
data << record.value(i);
//TODO sort by sort indicator and name
currentItem = new Folder(record.value(0).toLongLong(),record.value(1).toLongLong(),record.value(2).toString(),record.value(2).toString());
int lessThan = 0;
if(list.isEmpty())
list.append(currentItem);
else
{
Folder * last = static_cast<Folder *>(list.back());
QString nameLast = last->name; //TODO usar info name si est<73> disponible, sino el nombre del fichero.....
QString nameCurrent = currentItem->name;
QList<LibraryItem *>::iterator i;
i = list.end();
i--;
while ((0 > (lessThan = nameCurrent.localeAwareCompare(nameLast))) && i != list.begin())
{
i--;
nameLast = (*i)->name;
}
if(lessThan>0) //si se ha encontrado un elemento menor que current, se inserta justo despu<70>s
list.insert(++i,currentItem);
else
list.insert(i,currentItem);
}
}
return list;
}
void Folder::removeFromDB(QSqlDatabase & db)
{
QSqlQuery query(db);
query.prepare("DELETE FROM folder WHERE id = :id");
query.bindValue(":id", id);
query.exec();
}

View File

@ -0,0 +1,26 @@
#ifndef __FOLDER_H
#define __FOLDER_H
#include "library_item.h"
#include <QSqlDatabase>
#include <QList>
class Folder : public LibraryItem
{
public:
bool knownParent;
bool knownId;
Folder():knownParent(false), knownId(false){};
Folder(qulonglong sid, qulonglong pid,QString fn, QString fp):knownParent(true), knownId(true){id = sid; parentId = pid;name = fn; path = fp;};
Folder(QString fn, QString fp):knownParent(false), knownId(false){name = fn; path = fp;};
void setId(qulonglong sid){id = sid;knownId = true;};
void setFather(qulonglong pid){parentId = pid;knownParent = true;};
static QList<LibraryItem *> getFoldersFromParent(qulonglong parentId, QSqlDatabase & db);
qulonglong insert(QSqlDatabase & db);
bool isDir(){return true;};
void removeFromDB(QSqlDatabase & db);
};
#endif

View File

View File

@ -0,0 +1,17 @@
#ifndef __LIBRARY_ITEM_H
#define __LIBRARY_ITEM_H
#include <QSqlDatabase>
class LibraryItem
{
public:
virtual bool isDir() = 0;
virtual void removeFromDB(QSqlDatabase & db) = 0;
QString name;
QString path;
qulonglong parentId;
qulonglong id;
};
#endif

View File

@ -0,0 +1,42 @@
#include <QStringList>
#include "tableitem.h"
//! [0]
TableItem::TableItem(const QList<QVariant> &data)
{
itemData = data;
}
//! [0]
//! [1]
TableItem::~TableItem()
{
}
//! [1]
//! [5]
int TableItem::columnCount() const
{
return itemData.count();
}
//! [5]
//! [6]
QVariant TableItem::data(int column) const
{
return itemData.value(column);
}
//! [6]
//! [8]
int TableItem::row() const
{
return 0;
}
//! [8]

View File

@ -0,0 +1,24 @@
#ifndef TABLEITEM_H
#define TABLEITEM_H
#include <QList>
#include <QVariant>
//! [0]
class TableItem
{
public:
TableItem(const QList<QVariant> &data);
~TableItem();
int columnCount() const;
QVariant data(int column) const;
int row() const;
unsigned long long int id; //TODO sustituir por una clase adecuada
private:
QList<QVariant> itemData;
};
//! [0]
#endif

View File

@ -0,0 +1,211 @@
#include <QtGui>
#include <QtDebug>
#include "tableitem.h"
#include "tablemodel.h"
#include "data_base_management.h"
#include "qnaturalsorting.h"
TableModel::TableModel(QObject *parent)
: QAbstractItemModel(parent)
{
connect(this,SIGNAL(beforeReset()),this,SIGNAL(modelAboutToBeReset()));
connect(this,SIGNAL(reset()),this,SIGNAL(modelReset()));
}
//! [0]
TableModel::TableModel( QSqlQuery &sqlquery, QObject *parent)
: QAbstractItemModel(parent)
{
setupModelData(sqlquery);
}
//! [0]
//! [1]
TableModel::~TableModel()
{
qDeleteAll(_data);
}
//! [1]
//! [2]
int TableModel::columnCount(const QModelIndex &parent) const
{
if(_data.isEmpty())
return 0;
return _data.first()->columnCount();
}
//! [2]
//! [3]
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DecorationRole)
{
return QVariant();
}
if (role != Qt::DisplayRole)
return QVariant();
TableItem *item = static_cast<TableItem*>(index.internalPointer());
return item->data(index.column());
}
//! [3]
//! [4]
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
//! [4]
//! [5]
QVariant TableModel::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 0:
return QVariant(QString("Id"));
case 1:
return QVariant(QString("ParentId"));
case 2:
return QVariant(QString("File Name"));
case 3:
return QVariant(QString("Path"));
}
}
if(orientation == Qt::Vertical && role == Qt::DecorationRole)
return QVariant(QIcon(":/images/icon.png"));
return QVariant();
}
//! [5]
//! [6]
QModelIndex TableModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column, _data.at(row));
}
//! [6]
//! [7]
QModelIndex TableModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
//! [7]
//! [8]
int TableModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
return _data.count();
return 0;
}
//! [8]
QStringList TableModel::getPaths(const QString & _source)
{
QStringList paths;
QString source = _source + "/.yacreaderlibrary/covers/";
QList<TableItem *>::ConstIterator itr;
for(itr = _data.constBegin();itr != _data.constEnd();itr++)
{
QString path = (*itr)->data(4).toString();
paths << source+ path +".jpg";
}
return paths;
}
void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & db)
{
//QFile f(QCoreApplication::applicationDirPath()+"/performance.txt");
//f.open(QIODevice::Append);
emit(beforeReset());
//QElapsedTimer timer;
//timer.start();
qDeleteAll(_data);
_data.clear();
//QTextStream txtS(&f);
//txtS << "TABLEMODEL: Tiempo de borrado: " << timer.elapsed() << "ms\r\n";
db.open();
//crear la consulta
//timer.restart();
QSqlQuery selectQuery(db); //TODO check
selectQuery.prepare("select c.id,c.parentId,c.fileName,c.path,ci.hash from comic c inner join comic_info ci on (c.comicInfoId = ci.id) where c.parentId = :parentId");
selectQuery.bindValue(":parentId", folderId);
selectQuery.exec();
//txtS << "TABLEMODEL: Tiempo de consulta: " << timer.elapsed() << "ms\r\n";
//timer.restart();
setupModelData(selectQuery);
//txtS << "TABLEMODEL: Tiempo de creaci<63>n del modelo: " << timer.elapsed() << "ms\r\n";
db.close();
emit(reset());
//f.close();
}
QString TableModel::getComicPath(QModelIndex & mi)
{
if(mi.isValid())
return _data.at(mi.row())->data(3).toString();
return "";
}
void TableModel::setupModelData(QSqlQuery &sqlquery)
{
TableItem * currentItem;
while (sqlquery.next())
{
QList<QVariant> data;
QSqlRecord record = sqlquery.record();
for(int i=0;i<record.count();i++)
data << record.value(i);
//TODO sort by sort indicator and name
currentItem = new TableItem(data);
bool lessThan = false;
if(_data.isEmpty())
_data.append(currentItem);
else
{
TableItem * last = _data.back();
QString nameLast = last->data(2).toString(); //TODO usar info name si est<73> disponible, sino el nombre del fichero.....
QString nameCurrent = currentItem->data(2).toString();
QList<TableItem *>::iterator i;
i = _data.end();
i--;
while ((lessThan = naturalSortLessThanCI(nameCurrent,nameLast)) && i != _data.begin())
{
i--;
nameLast = (*i)->data(2).toString();
}
if(!lessThan) //si se ha encontrado un elemento menor que current, se inserta justo despu<70>s
_data.insert(++i,currentItem);
else
_data.insert(i,currentItem);
}
}
}

View File

@ -0,0 +1,52 @@
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QSqlQuery>
#include <QSqlDatabase>
class TableItem;
//! [0]
class TableModel : public QAbstractItemModel
{
Q_OBJECT
public:
TableModel(QObject *parent = 0);
TableModel( QSqlQuery &sqlquery, QObject *parent = 0);
~TableModel();
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 = QModelIndex()) const;
void setupModelData(unsigned long long int parentFolder,QSqlDatabase & db);
//M<>todos de conveniencia
QStringList getPaths(const QString & _source);
QString getComicPath(QModelIndex & mi);
//getComicInfo(QModelIndex & mi); --> para la edici<63>n
//getComicsInfo(QList<QModelIndex> list); --> recupera la informaci<63>n com<6F>n a los comics seleccionados
//setcomicInfo(QModelIndex & mi); --> inserta en la base datos
//setComicInfoForAllComics(); --> inserta la informaci<63>n com<6F>n a todos los c<>mics de una sola vez.
//setComicInfoForSelectedComis(QList<QModelIndex> list); -->inserta la informaci<63>n com<6F>n para los comics seleccionados
private:
void setupModelData( QSqlQuery &sqlquery);
QList<TableItem *> _data;
signals:
void beforeReset();
void reset();
};
//! [0]
#endif

View File

@ -61,9 +61,11 @@ public:
TreeItem *parent();
TreeItem *parentItem;
unsigned long long int id;
QList<QString> comicNames;
private:
QList<TreeItem*> childItems;
QList<QVariant> itemData;
};

View File

@ -221,6 +221,15 @@ void TreeModel::setupModelData(QSqlQuery &sqlquery, TreeItem *parent)
data << sqlquery.value(2).toString();
data << sqlquery.value(3).toString();
TreeItem * item = new TreeItem(data);
/*QSqlQuery selectQuery(_database); //TODO check
selectQuery.prepare("select fileName from comic where parentId = :parentId");
selectQuery.bindValue(":parentId", sqlquery.value(0).toLongLong());
selectQuery.exec();
while (selectQuery.next()) {
item->comicNames.append(sqlquery.value(0).toString());
}*/
item->id = sqlquery.value(0).toLongLong();
items.value(sqlquery.value(1).toLongLong())->appendChild(item);
//se a<>ade el item al map, de forma que se pueda encontrar como padre en siguientes iteraciones