123 lines
2.3 KiB
C++
123 lines
2.3 KiB
C++
#include "queryresultmodel.h"
|
|
#include "Pgsql_declare.h"
|
|
#include <QBrush>
|
|
#include <QColor>
|
|
|
|
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r)
|
|
: QAbstractTableModel(parent)
|
|
, result(std::move(r))
|
|
{}
|
|
|
|
|
|
int QueryResultModel::rowCount(const QModelIndex &) const
|
|
{
|
|
int r = result->getRows();
|
|
return r;
|
|
}
|
|
|
|
int QueryResultModel::columnCount(const QModelIndex &) const
|
|
{
|
|
int r = result->getCols();
|
|
return r;
|
|
}
|
|
|
|
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
using namespace Pgsql;
|
|
|
|
QVariant r;
|
|
int rij = index.row();
|
|
int col = index.column();
|
|
if (role == Qt::DisplayRole) {
|
|
if (result->null(col, rij)) {
|
|
r = "null";
|
|
}
|
|
else {
|
|
Oid o = result->type(col);
|
|
QString s(result->getVal(col, rij));
|
|
switch (o) {
|
|
case oid_bool:
|
|
s = (s == "t") ? "TRUE" : "FALSE";
|
|
// intentional fall through
|
|
default:
|
|
if (s.length() > 256) {
|
|
s.truncate(256);
|
|
}
|
|
r = s;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
else if (role == Qt::TextAlignmentRole) {
|
|
Oid o = result->type(col);
|
|
switch (o) {
|
|
case oid_int2:
|
|
case oid_int4:
|
|
case oid_int8:
|
|
case oid_float4:
|
|
case oid_float8:
|
|
case oid_numeric:
|
|
case oid_oid:
|
|
r = Qt::AlignRight + Qt::AlignVCenter;
|
|
break;
|
|
case oid_bool:
|
|
r = Qt::AlignCenter;
|
|
break;
|
|
default:
|
|
r = Qt::AlignLeft + Qt::AlignVCenter;
|
|
}
|
|
}
|
|
else if (role == Qt::ForegroundRole) {
|
|
if (result->null(col, rij)) {
|
|
r = QBrush(Qt::gray);
|
|
}
|
|
else {
|
|
Oid o = result->type(col);
|
|
switch (o) {
|
|
case oid_int2:
|
|
case oid_int4:
|
|
case oid_int8:
|
|
r = QBrush(Qt::darkBlue);
|
|
break;
|
|
case oid_float4:
|
|
case oid_float8:
|
|
r = QBrush(Qt::darkCyan);
|
|
break;
|
|
case oid_numeric:
|
|
r = QBrush(Qt::darkGreen);
|
|
break;
|
|
case oid_bool:
|
|
if (strcmp(result->getVal(col, rij), "t") == 0) {
|
|
r = QBrush(Qt::darkGreen);
|
|
}
|
|
else {
|
|
r = QBrush(Qt::darkRed);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
QVariant r;
|
|
if (role == Qt::DisplayRole) {
|
|
if (orientation == Qt::Horizontal) {
|
|
r = QString(result->getColName(section));
|
|
}
|
|
else {
|
|
r = QString::number(section + 1);
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
//Qt::ItemFlags QueryResultModel::flags(const QModelIndex &) const
|
|
//{
|
|
// return Qt::ItemIsUserCheckable;
|
|
//}
|