modificados los iconos del input de b?squeda

modificado el estilo de las titled toolbars
This commit is contained in:
Luis Ángel San Martín
2013-06-15 11:35:36 +02:00
commit efd004d2dd
522 changed files with 44592 additions and 0 deletions

View File

@ -0,0 +1,591 @@
#include "data_base_management.h"
#include <QtCore>
#include "library_creator.h"
#include "check_new_version.h"
static QString fields = "title ,"
"coverPage,"
"numPages,"
"number,"
"isBis,"
"count,"
"volume,"
"storyArc,"
"arcNumber,"
"arcCount,"
"genere,"
"writer,"
"penciller,"
"inker,"
"colorist,"
"letterer,"
"coverArtist,"
"date,"
"publisher,"
"format,"
"color,"
"ageRating,"
"synopsis,"
"characters,"
"notes,"
"hash";
DataBaseManagement::DataBaseManagement()
:QObject(),dataBasesList()
{
}
/*TreeModel * DataBaseManagement::newTreeModel(QString path)
{
//la consulta se ejecuta...
QSqlQuery selectQuery(loadDatabase(path));
selectQuery.setForwardOnly(true);
selectQuery.exec("select * from folder order by parentId,name");
//selectQuery.finish();
return new TreeModel(selectQuery);
}*/
QSqlDatabase DataBaseManagement::createDatabase(QString name, QString path)
{
return createDatabase(QDir::cleanPath(path) + "/" + name + ".ydb");
}
QSqlDatabase DataBaseManagement::createDatabase(QString dest)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",dest);
db.setDatabaseName(dest);
if (!db.open())
qDebug() << db.lastError();
else {
qDebug() << db.tables();
}
{
QSqlQuery pragma("PRAGMA foreign_keys = ON",db);
//pragma.finish();
DataBaseManagement::createTables(db);
QSqlQuery query("INSERT INTO folder (parentId, name, path) "
"VALUES (1,'root', '/')",db);
}
//query.finish();
//db.close();
return db;
}
QSqlDatabase DataBaseManagement::loadDatabase(QString path)
{
//TODO check path
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",path);
db.setDatabaseName(path+"/library.ydb");
if (!db.open()) {
//se devuelve una base de datos vac<61>a e inv<6E>lida
return QSqlDatabase();
}
QSqlQuery pragma("PRAGMA foreign_keys = ON",db);
//pragma.finish();
//devuelve la base de datos
return db;
}
QSqlDatabase DataBaseManagement::loadDatabaseFromFile(QString filePath)
{
//TODO check path
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",filePath);
db.setDatabaseName(filePath);
if (!db.open()) {
//se devuelve una base de datos vac<61>a e inv<6E>lida
return QSqlDatabase();
}
{
QSqlQuery pragma("PRAGMA foreign_keys = ON",db);
}
//pragma.finish();
//devuelve la base de datos
return db;
}
bool DataBaseManagement::createTables(QSqlDatabase & database)
{
bool success = true;
//FOLDER (representa una carpeta en disco)
{
QSqlQuery queryFolder(database);
queryFolder.prepare("CREATE TABLE folder (id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL, FOREIGN KEY(parentId) REFERENCES folder(id) ON DELETE CASCADE)");
success = success && queryFolder.exec();
//queryFolder.finish();
//COMIC INFO (representa la informaci<63>n de un c<>mic, cada c<>mic tendr<64> un id<69>ntificador <20>nico formado por un hash sha1'de los primeros 512kb' + su tama<6D>o en bytes)
QSqlQuery queryComicInfo(database);
queryComicInfo.prepare("CREATE TABLE comic_info ("
"id INTEGER PRIMARY KEY,"
"title TEXT,"
"coverPage INTEGER DEFAULT 1,"
"numPages INTEGER,"
"number INTEGER,"
"isBis BOOLEAN,"
"count INTEGER,"
"volume TEXT,"
"storyArc TEXT,"
"arcNumber INTEGER,"
"arcCount INTEGER,"
"genere TEXT,"
"writer TEXT,"
"penciller TEXT,"
"inker TEXT,"
"colorist TEXT,"
"letterer TEXT,"
"coverArtist TEXT,"
"date TEXT," //dd/mm/yyyy --> se mostrar<61> en 3 campos diferentes
"publisher TEXT,"
"format TEXT,"
"color BOOLEAN,"
"ageRating BOOLEAN,"
"synopsis TEXT,"
"characters TEXT,"
"notes TEXT,"
"hash TEXT UNIQUE NOT NULL,"
"edited BOOLEAN DEFAULT 0,"
"read BOOLEAN DEFAULT 0)");
success = success && queryComicInfo.exec();
//queryComicInfo.finish();
//COMIC (representa un c<>mic en disco, contiene el nombre de fichero)
QSqlQuery queryComic(database);
queryComic.prepare("CREATE TABLE comic (id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, comicInfoId INTEGER NOT NULL, fileName TEXT NOT NULL, path TEXT, FOREIGN KEY(parentId) REFERENCES folder(id) ON DELETE CASCADE, FOREIGN KEY(comicInfoId) REFERENCES comic_info(id))");
success = success && queryComic.exec();
//queryComic.finish();
//DB INFO
QSqlQuery queryDBInfo(database);
queryDBInfo.prepare("CREATE TABLE db_info (version TEXT NOT NULL)");
success = success && queryDBInfo.exec();
//queryDBInfo.finish();
QSqlQuery query("INSERT INTO db_info (version) "
"VALUES ('"VERSION"')",database);
//query.finish();
}
return success;
}
#include <qmessagebox.h>
void DataBaseManagement::exportComicsInfo(QString source, QString dest)
{
//QSqlDatabase sourceDB = loadDatabase(source);
QSqlDatabase destDB = loadDatabaseFromFile(dest);
//sourceDB.open();
{
QSqlQuery attach(destDB);
attach.prepare("ATTACH DATABASE '"+QDir().toNativeSeparators(dest) +"' AS dest;");
//attach.bindValue(":dest",QDir().toNativeSeparators(dest));
attach.exec();
//attach.finish();
QSqlQuery attach2(destDB);
attach2.prepare("ATTACH DATABASE '"+QDir().toNativeSeparators(source) +"' AS source;");
attach2.exec();
//attach2.finish();
//sourceDB.close();
QSqlQuery queryDBInfo(destDB);
queryDBInfo.prepare("CREATE TABLE dest.db_info (version TEXT NOT NULL)");
queryDBInfo.exec();
//queryDBInfo.finish();
/*QSqlQuery queryComicsInfo(sourceDB);
queryComicsInfo.prepare("CREATE TABLE dest.comic_info (id INTEGER PRIMARY KEY, hash TEXT NOT NULL, edited BOOLEAN DEFAULT FALSE, title TEXT, read BOOLEAN)");
queryComicsInfo.exec();*/
QSqlQuery query("INSERT INTO dest.db_info (version) "
"VALUES ('"VERSION"')",destDB);
//query.finish();
QSqlQuery exportData(destDB);
exportData.prepare("create table dest.comic_info as select " + fields +
" from source.comic_info where source.comic_info.edited = 1");
exportData.exec();
//exportData.finish();
}
//sourceDB.close();
destDB.close();
QSqlDatabase::removeDatabase(dest);
}
bool DataBaseManagement::importComicsInfo(QString source, QString dest)
{
QString error;
QString driver;
QStringList hashes;
bool b = false;
QSqlDatabase sourceDB = loadDatabaseFromFile(source);
QSqlDatabase destDB = loadDatabaseFromFile(dest);
{
QSqlQuery pragma("PRAGMA synchronous=OFF",destDB);
QSqlQuery newInfo(sourceDB);
newInfo.prepare("SELECT * FROM comic_info");
newInfo.exec();
destDB.transaction();
int cp;
while (newInfo.next()) //cada tupla deber<65> ser insertada o actualizada
{
QSqlQuery update(destDB);
update.prepare("UPDATE comic_info SET "
"title = :title,"
"coverPage = :coverPage,"
"numPages = :numPages,"
"number = :number,"
"isBis = :isBis,"
"count = :count,"
"volume = :volume,"
"storyArc = :storyArc,"
"arcNumber = :arcNumber,"
"arcCount = :arcCount,"
"genere = :genere,"
"writer = :writer,"
"penciller = :penciller,"
"inker = :inker,"
"colorist = :colorist,"
"letterer = :letterer,"
"coverArtist = :coverArtist,"
"date = :date,"
"publisher = :publisher,"
"format = :format,"
"color = :color,"
"ageRating = :ageRating,"
"synopsis = :synopsis,"
"characters = :characters,"
"notes = :notes,"
"edited = :edited"
" WHERE hash = :hash ");
QSqlQuery insert(destDB);
insert.prepare("INSERT INTO comic_info "
"(title,"
"coverPage,"
"numPages,"
"number,"
"isBis,"
"count,"
"volume,"
"storyArc,"
"arcNumber,"
"arcCount,"
"genere,"
"writer,"
"penciller,"
"inker,"
"colorist,"
"letterer,"
"coverArtist,"
"date,"
"publisher,"
"format,"
"color,"
"ageRating,"
"synopsis,"
"characters,"
"notes,"
"read,"
"edited,"
"hash)"
"VALUES (:title,"
":coverPage,"
":numPages,"
":number,"
":isBis,"
":count,"
":volume,"
":storyArc,"
":arcNumber,"
":arcCount,"
":genere,"
":writer,"
":penciller,"
":inker,"
":colorist,"
":letterer,"
":coverArtist,"
":date,"
":publisher,"
":format,"
":color,"
":ageRating,"
":synopsis,"
":characters,"
":notes,"
":read,"
":edited,"
":hash )");
QSqlRecord record = newInfo.record();
cp = record.value("coverPage").toInt();
if(cp>1)
{
QSqlQuery checkCoverPage(destDB);
checkCoverPage.prepare("SELECT coverPage FROM comic_info where hash = :hash");
checkCoverPage.bindValue(":hash",record.value("hash").toString());
checkCoverPage.exec();
bool extract = false;
if(checkCoverPage.next())
{
extract = checkCoverPage.record().value("coverPage").toInt() != cp;
}
if(extract)
hashes.append(record.value("hash").toString());
}
bindValuesFromRecord(record,update);
update.bindValue(":edited",1);
update.exec();
if(update.numRowsAffected() == 0)
{
bindValuesFromRecord(record,insert);
insert.bindValue(":edited",1);
insert.bindValue(":read",0);
insert.exec();
QString error1 = insert.lastError().databaseText();
QString error2 = insert.lastError().driverText();
//QMessageBox::critical(NULL,"db",error1);
//QMessageBox::critical(NULL,"driver",error2);
}
//update.finish();
//insert.finish();
}
}
destDB.commit();
QString hash;
foreach(hash, hashes)
{
QSqlQuery getComic(destDB);
getComic.prepare("SELECT c.path,ci.coverPage FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) where ci.hash = :hash");
getComic.bindValue(":hash",hash);
getComic.exec();
if(getComic.next())
{
QString basePath = QString(dest).remove("/.yacreaderlibrary/library.ydb");
QString path = basePath + getComic.record().value("path").toString();
int coverPage = getComic.record().value("coverPage").toInt();
ThumbnailCreator tc(path,basePath+"/.yacreaderlibrary/covers/"+hash+".jpg",coverPage);
tc.create();
}
}
destDB.close();
sourceDB.close();
QSqlDatabase::removeDatabase(source);
QSqlDatabase::removeDatabase(dest);
return b;
}
void DataBaseManagement::bindValuesFromRecord(const QSqlRecord & record, QSqlQuery & query)
{
bindString("title",record,query);
bindInt("coverPage",record,query);
bindInt("numPages",record,query);
bindInt("number",record,query);
bindInt("isBis",record,query);
bindInt("count",record,query);
bindString("volume",record,query);
bindString("storyArc",record,query);
bindInt("arcNumber",record,query);
bindInt("arcCount",record,query);
bindString("genere",record,query);
bindString("writer",record,query);
bindString("penciller",record,query);
bindString("inker",record,query);
bindString("colorist",record,query);
bindString("letterer",record,query);
bindString("coverArtist",record,query);
bindString("date",record,query);
bindString("publisher",record,query);
bindString("format",record,query);
bindInt("color",record,query);
bindString("ageRating",record,query);
bindString("synopsis",record,query);
bindString("characters",record,query);
bindString("notes",record,query);
bindString("hash",record,query);
}
void DataBaseManagement::bindString(const QString & name, const QSqlRecord & record, QSqlQuery & query)
{
if(!record.value(name).isNull())
{
query.bindValue(":"+name,record.value(name).toString());
}
}
void DataBaseManagement::bindInt(const QString & name, const QSqlRecord & record, QSqlQuery & query)
{
if(!record.value(name).isNull())
{
query.bindValue(":"+name,record.value(name).toInt());
}
}
QString DataBaseManagement::checkValidDB(const QString & fullPath)
{
QSqlDatabase db = loadDatabaseFromFile(fullPath);
QString versionString = "";
if(db.isValid() && db.isOpen())
{
QSqlQuery version(db);
version.prepare("SELECT * FROM db_info");
version.exec();
if(version.next())
versionString = version.record().value("version").toString();
}
db.close();
QSqlDatabase::removeDatabase(fullPath);
return versionString;
}
int DataBaseManagement::compareVersions(const QString & v1, const QString v2)
{
QStringList v1l = v1.split('.');
QStringList v2l = v2.split('.');
QList<int> v1il;
QList<int> v2il;
foreach(QString s, v1l)
v1il.append(s.toInt());
foreach(QString s,v2l)
v2il.append(s.toInt());
for(int i=0;i<qMin(v1il.length(),v2il.length());i++)
{
if(v1il[i]<v2il[i])
return -1;
if(v1il[i]>v2il[i])
return 1;
}
if(v1il.length() < v2il.length())
return -1;
if(v1il.length() == v2il.length())
return 0;
if(v1il.length() > v2il.length())
return 1;
return 0;
}
bool DataBaseManagement::updateToCurrentVersion(const QString & fullPath)
{
QSqlDatabase db = loadDatabaseFromFile(fullPath);
bool returnValue = false;
if(db.isValid() && db.isOpen())
{
QSqlQuery updateVersion(db);
updateVersion.prepare("UPDATE db_info SET "
"version = :version");
updateVersion.bindValue(":version",VERSION);
updateVersion.exec();
if(updateVersion.numRowsAffected() > 0)
returnValue = true;
}
db.close();
QSqlDatabase::removeDatabase(fullPath);
return returnValue;
}
//COMICS_INFO_EXPORTER
ComicsInfoExporter::ComicsInfoExporter()
:QThread()
{
}
void ComicsInfoExporter::exportComicsInfo(QSqlDatabase & source, QSqlDatabase & dest)
{
}
void ComicsInfoExporter::run()
{
}
//COMICS_INFO_IMPORTER
ComicsInfoImporter::ComicsInfoImporter()
:QThread()
{
}
void ComicsInfoImporter::importComicsInfo(QSqlDatabase & source, QSqlDatabase & dest)
{
}
void ComicsInfoImporter::run()
{
}

View File

@ -0,0 +1,59 @@
#ifndef __DATA_BASE_MANAGEMENT_H
#define __DATA_BASE_MANAGEMENT_H
#include <QtCore>
#include <QtSql>
#include <QSqlDatabase>
#include "treemodel.h"
class ComicsInfoExporter : public QThread
{
Q_OBJECT
public:
ComicsInfoExporter();
void exportComicsInfo(QSqlDatabase & source, QSqlDatabase & dest);
private:
void run();
};
class ComicsInfoImporter : public QThread
{
Q_OBJECT
public:
ComicsInfoImporter();
void importComicsInfo(QSqlDatabase & source, QSqlDatabase & dest);
private:
void run();
};
class DataBaseManagement : public QObject
{
Q_OBJECT
private:
QList<QString> dataBasesList;
static void bindString(const QString & name, const QSqlRecord & record, QSqlQuery & query);
static void bindInt(const QString & name, const QSqlRecord & record, QSqlQuery & query);
static void bindValuesFromRecord(const QSqlRecord & record, QSqlQuery & query);
public:
DataBaseManagement();
//TreeModel * newTreeModel(QString path);
//crea una base de datos y todas sus tablas
static QSqlDatabase createDatabase(QString name, QString path);
static QSqlDatabase createDatabase(QString dest);
//carga una base de datos desde la ruta path
static QSqlDatabase loadDatabase(QString path);
static QSqlDatabase loadDatabaseFromFile(QString path);
static bool createTables(QSqlDatabase & database);
static void exportComicsInfo(QString source, QString dest);
static bool importComicsInfo(QString source, QString dest);
static QString checkValidDB(const QString & fullPath); //retorna "" si la DB es inv<6E>lida <20> la versi<73>n si es v<>lida.
static int compareVersions(const QString & v1, const QString v2); //retorna <0 si v1 < v2, 0 si v1 = v2 y >0 si v1 > v2
static bool updateToCurrentVersion(const QString & path);
};
#endif

View File

@ -0,0 +1,47 @@
#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]
void TableItem::setData(int column,const QVariant & value)
{
itemData[column] = value;
}
//! [8]
int TableItem::row() const
{
return 0;
}
//! [8]

View File

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

View File

@ -0,0 +1,491 @@
#include <QtGui>
#include <QtDebug>
#include "tableitem.h"
#include "tablemodel.h"
#include "data_base_management.h"
#include "qnaturalsorting.h"
#include "comic_db.h"
#include "db_helper.h"
//ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read
#define NUMBER 0
#define TITLE 1
#define FILE_NAME 2
#define NUM_PAGES 3
#define ID 4
#define PARENT_ID 5
#define PATH 6
#define HASH 7
#define READ 8
#define IS_BIS 9
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());
if(index.column() == HASH)
return QString::number(item->data(index.column()).toString().right(item->data(index.column()).toString().length()-40).toInt()/1024.0/1024.0,'f',2)+"Mb";
if(index.column() == READ)
return item->data(index.column()).toBool()?QVariant(tr("yes")):QVariant(tr("no"));
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("#"));
case 1:
return QVariant(QString(tr("Title")));
case 2:
return QVariant(QString(tr("File Name")));
case 3:
return QVariant(QString(tr("Pages")));
case 7:
return QVariant(QString(tr("Size")));
case 8:
return QVariant(QString(tr("Read")));
}
}
if(orientation == Qt::Vertical && role == Qt::DecorationRole)
{
QString fileName = _data.value(section)->data(FILE_NAME).toString();
QFileInfo fi(fileName);
QString ext = fi.suffix();
if (ext.compare("cbr",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/comicRar.png"));
else if (ext.compare("cbz",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/comicZip.png"));
else if(ext.compare("pdf",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/pdf.png"));
else if (ext.compare("tar",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/tar.png"));
else if(ext.compare("zip",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/zip.png"));
else if(ext.compare("rar",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/rar.png"));
else if (ext.compare("7z",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/7z.png"));
else if (ext.compare("cb7",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/comic7z.png"));
else if (ext.compare("cb7",Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/comicTar.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 hash = (*itr)->data(HASH).toString();
paths << source+ hash +".jpg";
}
return paths;
}
void TableModel::setupModelData(unsigned long long int folderId,const QString & databasePath)
{
//QFile f(QCoreApplication::applicationDirPath()+"/performance.txt");
//f.open(QIODevice::Append);
beginResetModel();
//QElapsedTimer timer;
//timer.start();
qDeleteAll(_data);
_data.clear();
//QTextStream txtS(&f);
//txtS << "TABLEMODEL: Tiempo de borrado: " << timer.elapsed() << "ms\r\n";
_databasePath = databasePath;
QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath);
{
//crear la consulta
//timer.restart();
QSqlQuery selectQuery(db); //TODO check
selectQuery.prepare("select ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis 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";
//selectQuery.finish();
}
db.close();
QSqlDatabase::removeDatabase(_databasePath);
endResetModel();
//f.close();
}
QString TableModel::getComicPath(QModelIndex mi)
{
if(mi.isValid())
return _data.at(mi.row())->data(PATH).toString();
return "";
}
#define NUMBER_MAX 99999999
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);
currentItem = new TableItem(data);
bool lessThan = false;
if(_data.isEmpty())
_data.append(currentItem);
else
{
TableItem * last = _data.back();
QString nameLast = last->data(FILE_NAME).toString();
QString nameCurrent = currentItem->data(FILE_NAME).toString();
int numberLast,numberCurrent;
numberLast = numberCurrent = NUMBER_MAX; //TODO change by std limit
if(!last->data(NUMBER).isNull())
numberLast = last->data(NUMBER).toInt();
if(!currentItem->data(NUMBER).isNull())
numberCurrent = currentItem->data(NUMBER).toInt();
QList<TableItem *>::iterator i;
i = _data.end();
i--;
if(numberCurrent != NUMBER_MAX)
{
while ((lessThan =numberCurrent < numberLast) && i != _data.begin())
{
i--;
numberLast = NUMBER_MAX; //TODO change by std limit
if(!(*i)->data(NUMBER).isNull())
numberLast = (*i)->data(NUMBER).toInt();
}
}
else
{
while ((lessThan = naturalSortLessThanCI(nameCurrent,nameLast)) && i != _data.begin() && numberLast == 99999999)
{
i--;
nameLast = (*i)->data(FILE_NAME).toString();
numberLast = NUMBER_MAX; //TODO change by std limit
if(!(*i)->data(NUMBER).isNull())
numberLast = (*i)->data(NUMBER).toInt();
}
}
if(!lessThan) //si se ha encontrado un elemento menor que current, se inserta justo despu<70>s
{
if(numberCurrent != NUMBER_MAX)
{
if(numberCurrent == numberLast)
if(currentItem->data(IS_BIS).toBool())
{
_data.insert(++i,currentItem);
}
else
_data.insert(i,currentItem);
else
_data.insert(++i,currentItem);
}
else
_data.insert(++i,currentItem);
}
else
{
_data.insert(i,currentItem);
}
}
}
}
ComicDB TableModel::getComic(const QModelIndex & mi)
{
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
ComicDB c = DBHelper::loadComic(_data.at(mi.row())->data(ID).toULongLong(),db);
db.close();
QSqlDatabase::removeDatabase(_databasePath);
return c;
}
ComicDB TableModel::_getComic(const QModelIndex & mi)
{
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
ComicDB c = DBHelper::loadComic(_data.at(mi.row())->data(ID).toULongLong(),db);
db.close();
QSqlDatabase::removeDatabase(_databasePath);
return c;
}
QVector<bool> TableModel::getReadList()
{
int numComics = _data.count();
QVector<bool> readList(numComics);
for(int i=0;i<numComics;i++)
{
readList[i] = _data.value(i)->data(READ).toBool();
}
return readList;
}
QVector<bool> TableModel::setAllComicsRead(bool read)
{
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
db.transaction();
int numComics = _data.count();
QVector<bool> readList(numComics);
for(int i=0;i<numComics;i++)
{
readList[i] = read;
_data.value(i)->setData(READ,QVariant(read));
ComicDB c = DBHelper::loadComic(_data.value(i)->data(ID).toULongLong(),db);
c.info.read = read;
DBHelper::update(&(c.info),db);
}
db.commit();
db.close();
QSqlDatabase::removeDatabase(_databasePath);
emit dataChanged(index(0,READ),index(numComics-1,READ));
return readList;
}
QList<ComicDB> TableModel::getComics(QList<QModelIndex> list)
{
QList<ComicDB> comics;
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
db.transaction();
QList<QModelIndex>::const_iterator itr;
for(itr = list.constBegin(); itr!= list.constEnd();itr++)
{
comics.append(_getComic(*itr));
}
db.commit();
db.close();
QSqlDatabase::removeDatabase(_databasePath);
return comics;
}
QVector<bool> TableModel::setComicsRead(QList<QModelIndex> list,bool read)
{
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
db.transaction();
foreach (QModelIndex mi, list)
{
_data.value(mi.row())->setData(READ, QVariant(read));
ComicDB c = DBHelper::loadComic(_data.value(mi.row())->data(ID).toULongLong(),db);
c.info.read = read;
DBHelper::update(&(c.info),db);
}
db.commit();
db.close();
QSqlDatabase::removeDatabase(_databasePath);
emit dataChanged(index(list.first().row(),READ),index(list.last().row(),READ));
return getReadList();
}
qint64 TableModel::asignNumbers(QList<QModelIndex> list,int startingNumber)
{
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
db.transaction();
qint64 idFirst = _data.value(list[0].row())->data(ID).toULongLong();
int i = 0;
foreach (QModelIndex mi, list)
{
ComicDB c = DBHelper::loadComic(_data.value(mi.row())->data(ID).toULongLong(),db);
c.info.setNumber(startingNumber+i);
c.info.edited = true;
DBHelper::update(&(c.info),db);
i++;
}
db.commit();
db.close();
QSqlDatabase::removeDatabase(_databasePath);
//emit dataChanged(index(list.first().row(),READ),index(list.last().row(),READ));
return idFirst;
}
QModelIndex TableModel::getIndexFromId(quint64 id)
{
QList<TableItem *>::ConstIterator itr;
int i=0;
for(itr = _data.constBegin();itr != _data.constEnd();itr++)
{
if((*itr)->data(ID).toULongLong() == id)
break;
i++;
}
return index(i,0);
}
void TableModel::startTransaction()
{
dbTransaction = DataBaseManagement::loadDatabase(_databasePath);
dbTransaction.transaction();
}
void TableModel::finishTransaction()
{
dbTransaction.commit();
dbTransaction.close();
QSqlDatabase::removeDatabase(_databasePath);
}
void TableModel::removeInTransaction(int row)
{
ComicDB c = DBHelper::loadComic(_data.at(row)->data(ID).toULongLong(),dbTransaction);
DBHelper::removeFromDB(&c,dbTransaction);
beginRemoveRows(QModelIndex(),row,row);
removeRow(row);
delete _data.at(row);
_data.removeAt(row);
endRemoveRows();
}
void TableModel::remove(ComicDB * comic, int row)
{
beginRemoveRows(QModelIndex(),row,row);
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
DBHelper::removeFromDB(comic,db);
removeRow(row);
delete _data.at(row);
_data.removeAt(row);
db.close();
QSqlDatabase::removeDatabase(_databasePath);
endRemoveRows();
}
ComicDB TableModel::getComic(int row)
{
return getComic(index(row,0));
}
void TableModel::remove(int row)
{
removeInTransaction(row);
}

View File

@ -0,0 +1,73 @@
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QSqlQuery>
#include <QSqlDatabase>
class ComicDB;
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,const QString & databasePath);
//M<>todos de conveniencia
QStringList getPaths(const QString & _source);
QString getComicPath(QModelIndex mi);
ComicDB getComic(const QModelIndex & mi); //--> para la edici<63>n
ComicDB getComic(int row);
QVector<bool> getReadList();
QVector<bool> setAllComicsRead(bool read);
QList<ComicDB> getComics(QList<QModelIndex> list); //--> recupera la informaci<63>n com<6F>n a los comics seleccionados
QModelIndex getIndexFromId(quint64 id);
//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
QVector<bool> setComicsRead(QList<QModelIndex> list,bool read);
qint64 asignNumbers(QList<QModelIndex> list,int startingNumber);
void remove(ComicDB * comic, int row);
void removeInTransaction(int row);
public slots:
void remove(int row);
void startTransaction();
void finishTransaction();
private:
void setupModelData( QSqlQuery &sqlquery);
ComicDB _getComic(const QModelIndex & mi);
QList<TableItem *> _data;
QString _databasePath;
QSqlDatabase dbTransaction;
signals:
void beforeReset();
void reset();
};
//! [0]
#endif

View File

@ -0,0 +1,147 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
/*
treeitem.cpp
A container for items of data supplied by the simple tree model.
*/
#include <QStringList>
#include "treeitem.h"
#include "qnaturalsorting.h"
//! [0]
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
{
parentItem = parent;
itemData = data;
}
//! [0]
//! [1]
TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}
//! [1]
//! [2]
void TreeItem::appendChild(TreeItem *item)
{
item->parentItem = this;
//TODO insertar odernado
if(childItems.isEmpty())
childItems.append(item);
else
{
TreeItem * last = childItems.back();
QString nameLast = last->data(1).toString(); //TODO usar info name si est<73> disponible, sino el nombre del fichero.....
QString nameCurrent = item->data(1).toString();
QList<TreeItem *>::iterator i;
i = childItems.end();
i--;
while (naturalSortLessThanCI(nameCurrent,nameLast) && i != childItems.begin())
{
i--;
nameLast = (*i)->data(1).toString();
}
if(!naturalSortLessThanCI(nameCurrent,nameLast)) //si se ha encontrado un elemento menor que current, se inserta justo despu<70>s
childItems.insert(++i,item);
else
childItems.insert(i,item);
}
//childItems.append(item);
}
//! [2]
//! [3]
TreeItem *TreeItem::child(int row)
{
return childItems.value(row);
}
//! [3]
//! [4]
int TreeItem::childCount() const
{
return childItems.count();
}
//! [4]
//! [5]
int TreeItem::columnCount() const
{
return itemData.count();
}
//! [5]
//! [6]
QVariant TreeItem::data(int column) const
{
return itemData.value(column);
}
//! [6]
//! [7]
TreeItem *TreeItem::parent()
{
return parentItem;
}
//! [7]
//! [8]
int TreeItem::row() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
return 0;
}
//! [8]
QList<QVariant> TreeItem::getData() const
{
return itemData;
}

View File

@ -0,0 +1,78 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef TREEITEM_H
#define TREEITEM_H
#include <QList>
#include <QVariant>
#include <QModelIndex>
//! [0]
class TreeItem
{
public:
TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
~TreeItem();
void appendChild(TreeItem *child);
TreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
QList<QVariant> getData() const;
int row() const;
TreeItem *parent();
TreeItem *parentItem;
unsigned long long int id;
QList<QString> comicNames;
TreeItem * originalItem;
private:
QList<TreeItem*> childItems;
QList<QVariant> itemData;
};
//! [0]
#endif

View File

@ -0,0 +1,417 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
/*
treemodel.cpp
Provides a simple tree model to show how to create and use hierarchical
models.
*/
#include <QtGui>
#include "treeitem.h"
#include "treemodel.h"
#include "data_base_management.h"
#define ROOT 1
TreeModel::TreeModel(QObject *parent)
: QAbstractItemModel(parent),rootItem(0),rootBeforeFilter(0),filterEnabled(false),includeComics(false)
{
connect(this,SIGNAL(beforeReset()),this,SIGNAL(modelAboutToBeReset()));
connect(this,SIGNAL(reset()),this,SIGNAL(modelReset()));
}
//! [0]
TreeModel::TreeModel( QSqlQuery &sqlquery, QObject *parent)
: QAbstractItemModel(parent),rootItem(0),rootBeforeFilter(0),filterEnabled(false),includeComics(false)
{
//lo m<>s probable es que el nodo ra<72>z no necesite tener informaci<63>n
QList<QVariant> rootData;
rootData << "root"; //id 0, padre 0, title "root" (el id, y el id del padre van a ir en la clase TreeItem)
rootItem = new TreeItem(rootData);
rootItem->id = ROOT;
rootItem->parentItem = 0;
setupModelData(sqlquery, rootItem);
//sqlquery.finish();
}
//! [0]
//! [1]
TreeModel::~TreeModel()
{
if(rootItem != 0)
delete rootItem;
}
//! [1]
//! [2]
int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
//! [2]
//! [3]
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DecorationRole)
#ifdef Q_OS_MAC
return QVariant(QIcon(":/images/folder_macosx.png"));
#else
return QVariant(QIcon(":/images/folder.png"));
#endif
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
//! [3]
//! [4]
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
//! [4]
//! [5]
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
//! [5]
//! [6]
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
//! [6]
//! [7]
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
//! [7]
QModelIndex TreeModel::indexFromItem(TreeItem * item,int column)
{
//if(item->parent() != 0)
// return index(item->row(),column,parent(indexFromItem(item->parent(),column-1)));
//else
// return index(item->row(),0,QModelIndex());
return createIndex(item->row(), column, item);
}
//! [8]
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
//! [8]
void TreeModel::setupModelData(QString path)
{
beginResetModel();
if(rootItem != 0)
delete rootItem; //TODO comprobar que se libera bien la memoria
filterEnabled = false;
rootItem = 0;
rootBeforeFilter = 0;
//inicializar el nodo ra<72>z
QList<QVariant> rootData;
rootData << "root"; //id 0, padre 0, title "root" (el id, y el id del padre van a ir en la clase TreeItem)
rootItem = new TreeItem(rootData);
rootItem->id = ROOT;
rootItem->parentItem = 0;
//cargar la base de datos
_databasePath = path;
QSqlDatabase db = DataBaseManagement::loadDatabase(path);
//crear la consulta
{
QSqlQuery selectQuery("select * from folder where id <> 1 order by parentId,name",db);
setupModelData(selectQuery,rootItem);
}
//selectQuery.finish();
db.close();
QSqlDatabase::removeDatabase(path);
endResetModel();
}
void TreeModel::setupModelData(QSqlQuery &sqlquery, TreeItem *parent)
{
//64 bits para la primary key, es decir la misma precisi<73>n que soporta sqlit 2^64
//el diccionario permitir<69> encontrar cualquier nodo del <20>rbol r<>pidamente, de forma que a<>adir un hijo a un padre sea O(1)
items.clear();
//se a<>ade el nodo 0
items.insert(parent->id,parent);
while (sqlquery.next()) {
QList<QVariant> data;
QSqlRecord record = sqlquery.record();
data << record.value("name").toString();
data << record.value("path").toString();
TreeItem * item = new TreeItem(data);
item->id = record.value("id").toULongLong();
//la inserci<63>n de hijos se hace de forma ordenada
items.value(record.value("parentId").toULongLong())->appendChild(item);
//se a<>ade el item al map, de forma que se pueda encontrar como padre en siguientes iteraciones
items.insert(item->id,item);
}
}
void TreeModel::setupFilteredModelData()
{
beginResetModel();
//TODO hay que liberar memoria de anteriores filtrados
//inicializar el nodo ra<72>z
if(rootBeforeFilter == 0)
rootBeforeFilter = rootItem;
else
delete rootItem;//los resultados de la b<>squeda anterior deben ser borrados
QList<QVariant> rootData;
rootData << "root"; //id 1, padre 1, title "root" (el id, y el id del padre van a ir en la clase TreeItem)
rootItem = new TreeItem(rootData);
rootItem->id = ROOT;
rootItem->parentItem = 0;
//cargar la base de datos
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
//crear la consulta
{
QSqlQuery selectQuery(db); //TODO check
if(!includeComics)
{
selectQuery.prepare("select * from folder where id <> 1 and upper(name) like upper(:filter) order by parentId,name ");
selectQuery.bindValue(":filter", "%%"+filter+"%%");
}
else
{
selectQuery.prepare("SELECT DISTINCT f.id, f.parentId, f.name, f.path FROM folder f INNER JOIN comic c ON (f.id = c.parentId) WHERE f.id <> 1 AND ((UPPER(c.fileName) like UPPER(:filter)) OR (UPPER(f.name) like UPPER(:filter2))) ORDER BY f.parentId,f.name");
selectQuery.bindValue(":filter", "%%"+filter+"%%");
selectQuery.bindValue(":filter2", "%%"+filter+"%%");
}
selectQuery.exec();
setupFilteredModelData(selectQuery,rootItem);
}
//selectQuery.finish();
db.close();
QSqlDatabase::removeDatabase(_databasePath);
endResetModel();
}
void TreeModel::setupFilteredModelData(QSqlQuery &sqlquery, TreeItem *parent)
{
//64 bits para la primary key, es decir la misma precisi<73>n que soporta sqlit 2^64
filteredItems.clear();
//se a<>ade el nodo 0 al modelo que representa el arbol de elementos que cumplen con el filtro
filteredItems.insert(parent->id,parent);
while (sqlquery.next()) { //se procesan todos los folders que cumplen con el filtro
//datos de la base de datos
QList<QVariant> data;
QSqlRecord record = sqlquery.record();
data << record.value("name").toString();
data << record.value("path").toString();
TreeItem * item = new TreeItem(data);
item->id = sqlquery.value(0).toULongLong();
//id del padre
quint64 parentId = record.value("parentId").toULongLong();
//se a<>ade el item al map, de forma que se pueda encontrar como padre en siguientes iteraciones
if(!filteredItems.contains(item->id))
filteredItems.insert(item->id,item);
//es necesario conocer las coordenadas de origen para poder realizar scroll autom<6F>tico en la vista
item->originalItem = items.value(item->id);
//si el padre ya existe en el modelo, el item se a<>ade como hijo
if(filteredItems.contains(parentId))
filteredItems.value(parentId)->appendChild(item);
else//si el padre a<>n no se ha a<>adido, hay que a<>adirlo a <20>l y todos los padres hasta el nodo ra<72>z
{
//comprobamos con esta variable si el <20>ltimo de los padres (antes del nodo ra<72>z) ya exist<73>a en el modelo
bool parentPreviousInserted = false;
//mientras no se alcance el nodo ra<72>z se procesan todos los padres (de abajo a arriba)
while(parentId != ROOT )
{
//el padre no estaba en el modelo filtrado, as<61> que se rescata del modelo original
TreeItem * parentItem = items.value(parentId);
//se debe crear un nuevo nodo (para no compartir los hijos con el nodo original)
TreeItem * newparentItem = new TreeItem(parentItem->getData()); //padre que se a<>adir<69> a la estructura de directorios filtrados
newparentItem->id = parentId;
newparentItem->originalItem = parentItem;
//si el modelo contiene al padre, se a<>ade el item actual como hijo
if(filteredItems.contains(parentId))
{
filteredItems.value(parentId)->appendChild(item);
parentPreviousInserted = true;
}
//sino se registra el nodo para poder encontrarlo con posterioridad y se a<>ade el item actual como hijo
else
{
newparentItem->appendChild(item);
filteredItems.insert(newparentItem->id,newparentItem);
parentPreviousInserted = false;
}
//variables de control del bucle, se avanza hacia el nodo padre
item = newparentItem;
parentId = parentItem->parentItem->id;
}
//si el nodo es hijo de 1 y no hab<61>a sido previamente insertado como hijo, se a<>ade como tal
if(!parentPreviousInserted)
filteredItems.value(ROOT)->appendChild(item);
}
}
}
QString TreeModel::getDatabase()
{
return _databasePath;
}
QString TreeModel::getFolderPath(const QModelIndex &folder)
{
return static_cast<TreeItem*>(folder.internalPointer())->data(1).toString();
}
void TreeModel::setFilter(QString filter, bool includeComics)
{
this->filter = filter;
this->includeComics = includeComics;
filterEnabled = true;
setupFilteredModelData();
}
void TreeModel::resetFilter()
{
beginResetModel();
filter = "";
includeComics = false;
//TODO hay que liberar la memoria reservada para el filtrado
//items.clear();
filteredItems.clear();
TreeItem * root = rootItem;
rootItem = rootBeforeFilter; //TODO si no se aplica el filtro previamente, esto invalidar<61>a en modelo
if(root !=0)
delete root;
rootBeforeFilter = 0;
filterEnabled = false;
endResetModel();
}

View File

@ -0,0 +1,106 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QSqlQuery>
#include <QSqlDatabase>
class TreeItem;
//! [0]
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel(QObject *parent = 0);
TreeModel( QSqlQuery &sqlquery, QObject *parent = 0);
~TreeModel();
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;
QModelIndex indexFromItem(TreeItem * item, int column);
/*QModelIndex _indexFromItem(TreeItem * item, int column);
int column;*/
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
void setupModelData(QString path);
QString getDatabase();
//M<>todos de conveniencia
QString getFolderPath(const QModelIndex &folder);
void setFilter(QString filter, bool includeComics);
void resetFilter();
bool isFilterEnabled(){return filterEnabled;};
private:
void setupModelData( QSqlQuery &sqlquery, TreeItem *parent);
void setupFilteredModelData( QSqlQuery &sqlquery, TreeItem *parent);
void setupFilteredModelData();
TreeItem *rootItem; //el <20>rbol
QMap<unsigned long long int, TreeItem *> items; //relaci<63>n entre folders
TreeItem *rootBeforeFilter;
QMap<unsigned long long int, TreeItem *> filteredItems; //relaci<63>n entre folders
QString _databasePath;
bool includeComics;
QString filter;
bool filterEnabled;
signals:
void beforeReset();
void reset();
};
//! [0]
#endif