yacreader/YACReaderLibrary/db/treemodel.cpp
Luis Ángel San Martín 9dfa9c5f62 Modificaciones en pictureflow/goto_flow y comic_flow que aumentan la fuidez
Ordenaci?n naturalSorting a?adida a los modelos ?rbol y tabla

Primera versi?n de la actualizaci?n de librer?as basadas en BD (falta realizar bateria de pruebas)

Los di?logos deben ser mostrados antes de lanzar los hilos que los cerrar?n para evitar estados inconsistentes en la GUI
2012-05-27 22:32:46 +02:00

239 lines
7.0 KiB
C++
Raw Blame History

/****************************************************************************
**
** 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"
TreeModel::TreeModel(QObject *parent)
: QAbstractItemModel(parent)
{
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)
{
//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 = 0;
setupModelData(sqlquery, rootItem);
}
//! [0]
//! [1]
TreeModel::~TreeModel()
{
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)
return QVariant(QIcon(":/images/folder.png"));
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]
//! [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)
{
emit(beforeReset());
if(rootItem == 0)
delete rootItem; //TODO comprobar que se libera bien la memoria
//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 = 0;
//cargar la base de datos
if(_database.isOpen())
_database.close();
_database = DataBaseManagement::loadDatabase(path);
//crear la consulta
QSqlQuery selectQuery("select * from folder order by parentId,name",_database);
setupModelData(selectQuery,rootItem);
_database.close();
emit(reset());
}
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)
QMap<unsigned long long int, TreeItem *> items;
//se a<>ade el nodo 0
items.insert(parent->id,parent);
while (sqlquery.next()) {
QList<QVariant> data;
data << sqlquery.value(2).toString();
data << sqlquery.value(3).toString();
TreeItem * item = new TreeItem(data);
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
items.insert(item->id,item);
}
}
QSqlDatabase & TreeModel::getDatabase()
{
return _database;
}
QString TreeModel::getFolderPath(const QModelIndex &folder)
{
return static_cast<TreeItem*>(folder.internalPointer())->data(1).toString();
}