Better result formatting

Cells holding a NULL value now display null in gray.
int, float and numeric columns are right aligned. Integers, floats and numerics are coloured.
Booleans, green or red for true or false. t, f is replaced by TRUE, FALSE
This commit is contained in:
Eelke Klein 2017-01-18 20:50:53 +01:00
parent 163bb1d513
commit eb06c57141
4 changed files with 106 additions and 6 deletions

View file

@ -1,4 +1,6 @@
#include "queryresultmodel.h"
#include <QBrush>
#include <QColor>
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r)
: QAbstractTableModel(parent)
@ -18,23 +20,97 @@ int QueryResultModel::columnCount(const QModelIndex &) const
return r;
}
//DisplayRole = 0,
//DecorationRole = 1,
//EditRole = 2,
//ToolTipRole = 3,
//StatusTipRole = 4,
//WhatsThisRole = 5,
//// Metadata
//FontRole = 6,
//TextAlignmentRole = 7,
//BackgroundColorRole = 8,
//BackgroundRole = 8,
//TextColorRole = 9,
//ForegroundRole = 9,
//CheckStateRole = 10,
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) {
r = QString(result->getVal(col, rij));
}
else if (role == Qt::TextAlignmentRole) {
if (col == 0) {
r = Qt::AlignRight + Qt::AlignVCenter;
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:
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;
}