Nut/src/sqlmodel.cpp

147 lines
3.8 KiB
C++
Raw Normal View History

2018-02-13 23:39:21 +08:00
/**************************************************************************
**
** This file is part of Nut project.
** https://github.com/HamedMasafi/Nut
**
** Nut is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Nut is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************/
#include "database.h"
#include "tablesetbase_p.h"
#include "databasemodel.h"
2019-06-07 16:19:20 +08:00
#include "tablemodel.h"
#include "table.h"
2018-02-13 23:39:21 +08:00
#include "sqlmodel_p.h"
#include "sqlmodel.h"
2019-06-19 00:11:14 +08:00
#include "query.h"
2018-02-13 23:39:21 +08:00
NUT_BEGIN_NAMESPACE
2019-06-19 00:11:14 +08:00
//SqlModel::SqlModel(Query *q) : QAbstractItemModel(q.)
//{
//}
void SqlModel::setRenderer(const std::function<QVariant (int, QVariant)> &renderer)
{
_renderer = renderer;
}
2018-02-13 23:39:21 +08:00
SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) :
2019-06-19 17:16:55 +08:00
QAbstractTableModel(parent), d(new SqlModelPrivate(this)), _renderer(nullptr)
2018-02-13 23:39:21 +08:00
{
d->model = database->model()
.tableByClassName(tableSet->childClassName());
d->tableName = d->model->name();
2019-06-19 00:11:14 +08:00
2018-02-13 23:39:21 +08:00
// setQuery("SELECT * FROM " + d->tableName, database->databaseName());
}
int SqlModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return d->rows.count();
}
int SqlModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return d->model->fields().count();
}
QVariant SqlModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= d->rows.count() || index.row() < 0)
return QVariant("-");
if (role == Qt::DisplayRole) {
2019-06-19 00:11:14 +08:00
Row<Table> t = d->rows.at(index.row());
QVariant v = t->property(d->model->field(index.column())->name.toLocal8Bit().data());
// emit beforeShowText(index.column(), v);
if (_renderer != nullptr)
v = _renderer(index.column(), v);
return v;
2018-02-13 23:39:21 +08:00
// LogData *d = dataList.at(index.row());
// switch (index.column()) {
// case COL_ID:
// return index.row() + 1;
// case COL_Type: {
// return typeText(d->type);
// }
// case COL_TITLE:
// return d->title;
// case COL_File:
// return d->file;
// case COL_Function:
// return d->function;
// case COL_Line:
// return d->line;
// }
}
return QVariant();
}
2019-06-19 00:11:14 +08:00
void SqlModel::setRows(RowList<Table> rows)
{
2019-06-19 17:16:55 +08:00
d.detach();
2019-06-23 22:07:06 +08:00
if (d->rows.count()) {
beginRemoveRows(QModelIndex(), 0, d->rows.count());
d->rows.clear();
endRemoveRows();
}
2019-06-19 00:11:14 +08:00
beginInsertRows(QModelIndex(), 0, rows.count());
d->rows = rows;
endInsertRows();
}
void SqlModel::append(Row<Table> table)
{
2019-06-19 17:16:55 +08:00
d.detach();
2019-06-19 00:11:14 +08:00
beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count());
d->rows.append(table);
endInsertRows();
}
//void SqlModel::append(Table *table)
//{
// append(TableType<Table>::Row(table));
//}
QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return d->model->field(section)->displayName;
}
return QAbstractItemModel::headerData(section, orientation, role);
}
Row<Table> SqlModel::at(const int &i) const
2019-06-19 00:11:14 +08:00
{
return d->rows.at(i);
2019-06-19 00:11:14 +08:00
}
2019-06-19 17:16:55 +08:00
SqlModelPrivate::SqlModelPrivate(SqlModel *parent)
2019-06-19 00:11:14 +08:00
{
}
2018-02-13 23:39:21 +08:00
NUT_END_NAMESPACE