pgLab/pglablib/model/TypeSelectionItemModel.cpp

106 lines
2.2 KiB
C++
Raw Normal View History

#include "TypeSelectionItemModel.h"
#include "catalog/PgTypeContainer.h"
#include <algorithm>
TypeSelectionItemModel::TypeSelectionItemModel(QObject *parent)
: QAbstractListModel(parent)
{
}
2017-12-16 21:41:46 +01:00
int TypeSelectionItemModel::rowCount(const QModelIndex &/*parent*/) const
{
int result = static_cast<int>(m_types.size());
// if (!parent.isValid()) {
// }
return result;
}
2017-12-16 21:41:46 +01:00
int TypeSelectionItemModel::columnCount(const QModelIndex &/*parent*/) const
{
int result = 2;
// if (parent.isValid())
// result = 1;
return result;
}
QVariant TypeSelectionItemModel::data(const QModelIndex &index, int role) const
{
QVariant result;
if (index.isValid()) {
int row = index.row();
int column = index.column();
if (role == Qt::DisplayRole) {
//const PgType &tp = m_types->getByIdx(row);
if (column == 0) {
result = m_types[static_cast<size_t>(row)]; //tp.typname;
}
}
}
return result;
}
void TypeSelectionItemModel::setTypeList(std::shared_ptr<const PgTypeContainer> types)
{
beginResetModel();
m_types.clear();
if (types) {
for (const auto &e : *types) {
if (e.category != TypCategory::Array && e.type != "c") {
m_types.push_back(e.objectName());
}
}
std::sort(m_types.begin(), m_types.end());
}
//emit dataChanged(this->createIndex(0, 0), this->createIndex(types->count(), 0), QVector<int>() << Qt::DisplayRole);
endResetModel();
}
// ----------------
TypeModel::TypeModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int TypeModel::rowCount(const QModelIndex &/*parent*/) const
{
if (m_types)
return static_cast<int>(m_types->count());
return 0;
}
int TypeModel::columnCount(const QModelIndex &/*parent*/) const
{
return colCount;
}
QVariant TypeModel::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
int row = index.row();
int column = index.column();
if (role == Qt::DisplayRole) {
//const PgType &tp = m_types->getByIdx(row);
auto elem = m_types->getByIdx(row);
switch (column) {
case OidCol: return elem.oid();
case NameCol: return elem.objectName();
}
}
}
return QVariant();
}
void TypeModel::setTypeList(std::shared_ptr<const PgTypeContainer> types)
{
beginResetModel();
m_types = types;
endResetModel();
}