QueryResultModel relies on BaseTableModel for most formatting now.
This commit is contained in:
parent
dec52a3829
commit
5a199c9138
4 changed files with 40 additions and 63 deletions
|
|
@ -4,10 +4,6 @@
|
||||||
|
|
||||||
using namespace Pgsql;
|
using namespace Pgsql;
|
||||||
|
|
||||||
BaseTableModel::BaseTableModel(QObject *parent)
|
|
||||||
: QAbstractTableModel(parent)
|
|
||||||
{}
|
|
||||||
|
|
||||||
QVariant BaseTableModel::data(const QModelIndex &index, int role) const
|
QVariant BaseTableModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
QVariant v;
|
QVariant v;
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ class BaseTableModel : public QAbstractTableModel
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit BaseTableModel(QObject *parent);
|
using QAbstractTableModel::QAbstractTableModel;
|
||||||
|
|
||||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
|
|
||||||
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r)
|
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r)
|
||||||
: QAbstractTableModel(parent)
|
: BaseTableModel(parent)
|
||||||
, result(std::move(r))
|
, result(std::move(r))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
@ -22,72 +22,48 @@ int QueryResultModel::columnCount(const QModelIndex &) const
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
Oid QueryResultModel::getType(int column) const
|
||||||
{
|
{
|
||||||
using namespace Pgsql;
|
return result->type(column);
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant QueryResultModel::getData(const QModelIndex &index) const
|
||||||
|
{
|
||||||
QVariant r;
|
QVariant r;
|
||||||
int rij = index.row();
|
int rij = index.row();
|
||||||
int col = index.column();
|
int col = index.column();
|
||||||
if (role == Qt::DisplayRole) {
|
|
||||||
if (result->null(col, rij)) {
|
if (result->null(col, rij)) {
|
||||||
r = "null";
|
r = "null";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Oid o = result->type(col);
|
// Oid o = result->type(col);
|
||||||
QString s(result->val(col, rij));
|
QString s(result->val(col, rij));
|
||||||
switch (o) {
|
// switch (o) {
|
||||||
case BOOLOID:
|
// case BOOLOID:
|
||||||
s = (s == "t") ? "TRUE" : "FALSE";
|
// s = (s == "t") ? "TRUE" : "FALSE";
|
||||||
// intentional fall through
|
// // intentional fall through
|
||||||
default:
|
// default:
|
||||||
if (s.length() > 256) {
|
if (s.length() > 256) {
|
||||||
s.truncate(256);
|
s.truncate(256);
|
||||||
}
|
}
|
||||||
r = s;
|
r = s;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (role == Qt::TextAlignmentRole) {
|
|
||||||
r = GetDefaultAlignmentForType(result->type(col));
|
|
||||||
}
|
|
||||||
else if (role == Qt::ForegroundRole) {
|
|
||||||
if (result->null(col, rij)) {
|
|
||||||
r = QBrush(Qt::gray);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Oid o = result->type(col);
|
|
||||||
switch (o) {
|
|
||||||
case INT2OID:
|
|
||||||
case INT4OID:
|
|
||||||
case INT8OID:
|
|
||||||
r = QBrush(Qt::darkBlue);
|
|
||||||
break;
|
|
||||||
case FLOAT4OID:
|
|
||||||
case FLOAT8OID:
|
|
||||||
r = QBrush(Qt::darkCyan);
|
|
||||||
break;
|
|
||||||
case NUMERICOID:
|
|
||||||
r = QBrush(Qt::darkGreen);
|
|
||||||
break;
|
|
||||||
case BOOLOID:
|
|
||||||
if (strcmp(result->val(col, rij), "t") == 0) {
|
|
||||||
r = QBrush(Qt::darkGreen);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
r = QBrush(Qt::darkRed);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
if (role == Qt::ForegroundRole) {
|
||||||
|
int rij = index.row();
|
||||||
|
int col = index.column();
|
||||||
|
if (result->null(col, rij)) {
|
||||||
|
return QBrush(Qt::gray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return BaseTableModel::data(index, role);
|
||||||
|
}
|
||||||
|
|
||||||
QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
{
|
{
|
||||||
QVariant r;
|
QVariant r;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
#ifndef QUERYRESULTMODEL_H
|
#ifndef QUERYRESULTMODEL_H
|
||||||
#define QUERYRESULTMODEL_H
|
#define QUERYRESULTMODEL_H
|
||||||
|
|
||||||
#include <QAbstractTableModel>
|
//#include <QAbstractTableModel>
|
||||||
|
#include "BaseTableModel.h"
|
||||||
#include "Pgsql_Connection.h"
|
#include "Pgsql_Connection.h"
|
||||||
|
|
||||||
class QueryResultModel : public QAbstractTableModel
|
class QueryResultModel : public BaseTableModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
|
@ -16,6 +17,9 @@ public:
|
||||||
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||||
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual Oid getType(int column) const override;
|
||||||
|
virtual QVariant getData(const QModelIndex &index) const override;
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Pgsql::Result> result;
|
std::shared_ptr<Pgsql::Result> result;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue