From eb06c571411b1d4da82dd8438a562c991296fd89 Mon Sep 17 00:00:00 2001 From: Eelke Klein Date: Wed, 18 Jan 2017 20:50:53 +0100 Subject: [PATCH] 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 --- PgsqlConn.cpp | 9 +++++ PgsqlConn.h | 13 +++++++ mainwindow.cpp | 2 + queryresultmodel.cpp | 88 +++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 106 insertions(+), 6 deletions(-) diff --git a/PgsqlConn.cpp b/PgsqlConn.cpp index e63dcaf..a3e99e2 100644 --- a/PgsqlConn.cpp +++ b/PgsqlConn.cpp @@ -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) diff --git a/PgsqlConn.h b/PgsqlConn.h index 3a96a9d..4d982a1 100644 --- a/PgsqlConn.h +++ b/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: diff --git a/mainwindow.cpp b/mainwindow.cpp index 74956a9..959f847 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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); }); diff --git a/queryresultmodel.cpp b/queryresultmodel.cpp index 45545e4..0b93d02 100644 --- a/queryresultmodel.cpp +++ b/queryresultmodel.cpp @@ -1,4 +1,6 @@ #include "queryresultmodel.h" +#include +#include QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr 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; }