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:
parent
163bb1d513
commit
eb06c57141
4 changed files with 106 additions and 6 deletions
|
|
@ -163,6 +163,15 @@ const char * Result::getVal(int col, int row) const
|
|||
return PQgetvalue(result, row, col);
|
||||
}
|
||||
|
||||
Oid Result::type(int col) const
|
||||
{
|
||||
return PQftype(result, col);
|
||||
}
|
||||
|
||||
bool Result::null(int col, int row) const
|
||||
{
|
||||
return PQgetisnull(result, row, col);
|
||||
}
|
||||
|
||||
Canceller::Canceller(PGcancel *c)
|
||||
: m_cancel(c)
|
||||
|
|
|
|||
13
PgsqlConn.h
13
PgsqlConn.h
|
|
@ -10,6 +10,16 @@
|
|||
|
||||
namespace Pgsql {
|
||||
|
||||
const Oid oid_bool = 16;
|
||||
const Oid oid_int2 = 21;
|
||||
const Oid oid_int4 = 23;
|
||||
const Oid oid_int8 = 20;
|
||||
const Oid oid_float4 = 700;
|
||||
const Oid oid_float8 = 701;
|
||||
const Oid oid_numeric = 1700;
|
||||
const Oid oid_oid = 26;
|
||||
|
||||
|
||||
class Connection;
|
||||
/*
|
||||
This library has multiple layers.
|
||||
|
|
@ -120,6 +130,9 @@ namespace Pgsql {
|
|||
const char* const getColName(int idx) const;
|
||||
|
||||
const char * getVal(int col, int row) const;
|
||||
Oid type(int col) const;
|
||||
bool null(int col, int row) const;
|
||||
|
||||
|
||||
// iterator begin();
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -109,6 +109,8 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
// action = ui->mainToolBar->addAction("cancel");
|
||||
// connect(action, &QAction::triggered, this, &MainWindow::cancel_query);
|
||||
|
||||
//ui->ResultView->setItemDelegate();
|
||||
|
||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||
{
|
||||
QueueTask([this, st]() { connectionStateChanged(st); });
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue