minor bug fix [skip ci]

This commit is contained in:
Hamed Masafi 2019-06-19 13:46:55 +04:30
parent f77e5e0794
commit 06d661e4de
6 changed files with 20 additions and 32 deletions

View File

@ -209,6 +209,11 @@ inline Row<T> create() {
return QSharedPointer<T>(new T);
}
template<class T>
inline Row<T> create(QObject *parent) {
return QSharedPointer<T>(new T(parent));
}
template<class T>
inline T *get(T *row) {
return row;

View File

@ -57,14 +57,6 @@ template <class T>
bool m_autoDelete;
public:
//#ifdef NUT_SHARED_POINTER
// typedef QList<QSharedPointer<T>> RowList;
// typedef QSharedPointer<T> Row;
//#else
// typedef QList<T*> RowList;
// typedef T* Row;
//#endif
explicit Query(Database *database, TableSetBase *tableSet, bool autoDelete);
~Query();

View File

@ -40,9 +40,8 @@ void SqlModel::setRenderer(const std::function<QVariant (int, QVariant)> &render
}
SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) :
QAbstractTableModel(parent), d_ptr(new SqlModelPrivate(this)), _renderer(nullptr)
QAbstractTableModel(parent), d(new SqlModelPrivate(this)), _renderer(nullptr)
{
Q_D(SqlModel);
d->model = database->model()
.tableByClassName(tableSet->childClassName());
d->tableName = d->model->name();
@ -54,20 +53,17 @@ SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent)
int SqlModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Q_D(const SqlModel);
return d->rows.count();
}
int SqlModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Q_D(const SqlModel);
return d->model->fields().count();
}
QVariant SqlModel::data(const QModelIndex &index, int role) const
{
Q_D(const SqlModel);
if (!index.isValid())
return QVariant();
@ -104,7 +100,7 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const
void SqlModel::setRows(RowList<Table> rows)
{
Q_D(SqlModel);
d.detach();
beginRemoveRows(QModelIndex(), 0, d->rows.count());
d->rows.clear();
endRemoveRows();
@ -115,7 +111,7 @@ void SqlModel::setRows(RowList<Table> rows)
void SqlModel::append(Row<Table> table)
{
Q_D(SqlModel);
d.detach();
beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count());
d->rows.append(table);
endInsertRows();
@ -129,7 +125,6 @@ void SqlModel::append(Row<Table> table)
QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
Q_D(const SqlModel);
return d->model->field(section)->displayName;
}
return QAbstractItemModel::headerData(section, orientation, role);
@ -137,11 +132,10 @@ QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role
Row<Table> SqlModel::at(const int &i) const
{
Q_D(const SqlModel);
return d->rows.at(i);
}
SqlModelPrivate::SqlModelPrivate(SqlModel *parent) : q_ptr(parent)
SqlModelPrivate::SqlModelPrivate(SqlModel *parent)
{
}

View File

@ -23,6 +23,8 @@
#include <QtCore/QAbstractTableModel>
#include "defines.h"
#include "sqlmodel_p.h"
#include <QExplicitlySharedDataPointer>
#include <QList>
#include <functional>
@ -30,7 +32,6 @@ NUT_BEGIN_NAMESPACE
class Database;
class TableSetBase;
class SqlModelPrivate;
class Table;
class TableModel;
@ -60,8 +61,7 @@ public:
void setRenderer(const std::function<QVariant (int, QVariant)> &renderer);
private:
SqlModelPrivate *d_ptr;
Q_DECLARE_PRIVATE(SqlModel)
QExplicitlySharedDataPointer<SqlModelPrivate> d;
signals:
void beforeShowText(int col, QVariant &value);
@ -70,8 +70,6 @@ signals:
template<class T>
Q_OUTOFLINE_TEMPLATE void SqlModel::setTable(RowList<T> rows)
{
Q_D(SqlModel);
RowList<Table> tab;
foreach (auto t, rows)
tab.append(t);

View File

@ -10,9 +10,7 @@ NUT_BEGIN_NAMESPACE
class SqlModel;
class Table;
class TableModel;
class SqlModelPrivate {
SqlModel *q_ptr;
Q_DECLARE_PUBLIC(SqlModel)
class SqlModelPrivate : public QSharedData {
public:
explicit SqlModelPrivate(SqlModel *parent);

View File

@ -54,8 +54,8 @@ public:
void append(Row<T> t);
void append(RowList<T> t);
void remove(T *t);
void remove(QList<T *> t);
void remove(Row<T> t);
void remove(RowList<T> t);
int length() const;
T *at(int i) const;
@ -135,18 +135,19 @@ Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(RowList<T> t)
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(T *t)
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(Row<T> t)
{
data.detach();
data->childs.removeOne(t.data());
data->tables.remove(t.data());
data->childs.removeOne(t);
data->tables.remove(t);
t->setStatus(Table::Deleted);
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(QList<T *> t)
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(RowList<T> t)
{
foreach (T* i, t)
foreach (Row<T> i, t)
remove(i);
}