pgLab/pglab/IndexModel.cpp
eelke 52011a9842 Updating of detail tabs besides list of tables works correctly again.
Had stopped working because the catalog now behaves a little different,
returning nullptr instead of an invalid element.
2018-11-18 20:24:27 +01:00

105 lines
2 KiB
C++

#include "IndexModel.h"
#include "PgDatabaseCatalog.h"
#include "PgIndexContainer.h"
#include "Pgsql_oids.h"
#include "ScopeGuard.h"
#include "CustomDataRole.h"
void IndexModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
{
beginResetModel();
SCOPE_EXIT { endResetModel(); };
m_catalog = cat;
m_table = table;
if (table)
m_indexes = cat->indexes()->getIndexesForTable(table->oid);
else
m_indexes.clear();
}
int IndexModel::rowCount(const QModelIndex &/*parent*/) const
{
return (int)m_indexes.size();
}
int IndexModel::columnCount(const QModelIndex &/*parent*/) const
{
return colCount;
}
Oid IndexModel::getType(int /*column*/) const
{
return Pgsql::varchar_oid;
}
QVariant IndexModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant v;
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
QString c;
switch (section) {
case TypeCol:
c = tr("Type");
break;
case NameCol:
c = tr("Name");
break;
case AmCol:
c = tr("AM");
break;
case ColumnsCol:
c = tr("On");
break;
case ConditionCol:
c = tr("Condition");
break;
// case DefinitionCol:
// c = tr("Definition");
// break;
}
v = c;
}
}
return v;
}
QVariant IndexModel::getData(const QModelIndex &index) const
{
QVariant v;
int rij = index.row();
const auto &dat = m_indexes[rij];
switch (index.column()) {
case TypeCol:
if (dat.isprimary) v = ":/icons/constraints/primarykey.png";
else if (dat.isunique) v = ":/icons/constraints/unique.png";
break;
case NameCol:
v = getIndexDisplayString(*m_catalog, dat.indexrelid);
break;
case AmCol:
v = dat.getAm();
break;
case ColumnsCol:
break;
case ConditionCol:
break;
}
return v;
}
QVariant IndexModel::data(const QModelIndex &index, int role) const
{
QVariant v;
if (role == Qt::DisplayRole)
v = getData(index);
else if (role == CustomDataTypeRole)
v = getType(index.column());
return v;
}