Improve sorting
This commit is contained in:
parent
06504ecc1f
commit
2c5a42f45d
4 changed files with 76 additions and 30 deletions
|
|
@ -23,6 +23,7 @@ enum CustomDataRole {
|
|||
CustomDataTypeRole = Qt::UserRole, ///< Requist the basic type of the value
|
||||
CustomReferencedTypeRole, ///<
|
||||
CustomDataMeaningRole,
|
||||
CustomSortRole, // return data in a form that will sort correctly
|
||||
// Add other enum before this one as we might want to have multiple hidden values
|
||||
FirstHiddenValue, ///< Used to request value from a model which is not handed to the view
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include "Pgsql_declare.h"
|
||||
#include "Pgsql_oids.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include "CustomDataRole.h"
|
||||
#include <QBrush>
|
||||
#include <QColor>
|
||||
|
||||
|
|
@ -52,7 +53,7 @@ QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (role == Qt::UserRole) {
|
||||
else if (role == CustomDataTypeRole) {
|
||||
v = oid;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include "catalog/PgConstraintContainer.h"
|
||||
#include "SqlFormattingUtils.h"
|
||||
#include "Pgsql_oids.h"
|
||||
#include "CustomDataRole.h"
|
||||
#include <QtConcurrent>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
|
|
@ -124,37 +125,79 @@ CrudModel::Value CrudModel::getSavedData(const RowMapping &row_mapping, int colu
|
|||
return val.value_or(Value());
|
||||
}
|
||||
|
||||
QVariant CrudModel::data(const QModelIndex &index, int role) const
|
||||
QVariant StringToVariant(const std::string &str, Oid oid)
|
||||
{
|
||||
QVariant v;
|
||||
if (role == Qt::EditRole) {
|
||||
auto value = getLatestData(index);
|
||||
if (value) {
|
||||
QString s = QString::fromUtf8(value->c_str());
|
||||
v = s;
|
||||
Pgsql::Value v(str.c_str(), oid);
|
||||
switch (oid) {
|
||||
default:
|
||||
return QString::fromStdString(str);
|
||||
|
||||
case Pgsql::timestamp_oid:
|
||||
case Pgsql::timestamptz_oid:
|
||||
return v.operator QDateTime();
|
||||
|
||||
case Pgsql::date_oid:
|
||||
return v.operator QDate();
|
||||
|
||||
case Pgsql::time_oid:
|
||||
case Pgsql::timetz_oid:
|
||||
return v.operator QTime();
|
||||
|
||||
case Pgsql::int2_oid:
|
||||
return v.operator int16_t();
|
||||
|
||||
case Pgsql::int4_oid:
|
||||
return v.operator int32_t();
|
||||
|
||||
case Pgsql::oid_oid:
|
||||
case Pgsql::int8_oid:
|
||||
return v.operator int64_t();
|
||||
|
||||
case Pgsql::bool_oid:
|
||||
return v.operator bool();
|
||||
|
||||
case Pgsql::float4_oid:
|
||||
return v.operator float();
|
||||
|
||||
case Pgsql::float8_oid:
|
||||
return v.operator double();
|
||||
}
|
||||
|
||||
}
|
||||
else if (role == Qt::DisplayRole) {
|
||||
}
|
||||
|
||||
QVariant CrudModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
Oid typ = getType(index.column());
|
||||
if (role == Qt::EditRole || role == Qt::DisplayRole)
|
||||
{
|
||||
auto value = getLatestData(index);
|
||||
if (value) {
|
||||
Oid o = m_roData->type(index.column());
|
||||
if (o == Pgsql::bool_oid) {
|
||||
v = *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
|
||||
}
|
||||
else {
|
||||
if (role == Qt::EditRole)
|
||||
return QString::fromUtf8(value->c_str());
|
||||
else
|
||||
{
|
||||
if (typ == Pgsql::bool_oid)
|
||||
return *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
|
||||
else
|
||||
{
|
||||
QString s = QString::fromUtf8(value->c_str());
|
||||
if (s.length() > 256) {
|
||||
s.truncate(256);
|
||||
}
|
||||
v = s;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (role == Qt::UserRole) {
|
||||
v = getType(index.column());
|
||||
}
|
||||
return v;
|
||||
else if (role == CustomDataTypeRole)
|
||||
return typ;
|
||||
else if (role == CustomSortRole)
|
||||
{
|
||||
auto value = getLatestData(index);
|
||||
if (value) {
|
||||
return StringToVariant(value->c_str(), typ);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void CrudModel::loadData()
|
||||
|
|
@ -367,7 +410,7 @@ std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow
|
|||
for (size_t p = 2; p <= data.size(); ++p)
|
||||
q << ",$" << p;
|
||||
q << ") RETURNING *";
|
||||
for (auto e : data) {
|
||||
for (auto& e : data) {
|
||||
params.add(e.second, getType(e.first));
|
||||
}
|
||||
q.flush();
|
||||
|
|
@ -480,7 +523,6 @@ std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>
|
|||
// First delete rows in table
|
||||
QString delete_statement = createDeleteStatement();
|
||||
{
|
||||
db_update_conn.query("BEGIN;");
|
||||
auto tx = Pgsql::Transaction::startTransaction(db_update_conn);
|
||||
for (auto range : row_ranges) {
|
||||
for (int current_row = range.start(); current_row < range.end(); ++current_row) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "CrudTab.h"
|
||||
#include "ui_CrudTab.h"
|
||||
#include "CrudModel.h"
|
||||
#include "CustomDataRole.h"
|
||||
#include "ResultTableModelUtil.h"
|
||||
#include "PgLabItemDelegate.h"
|
||||
#include "IntegerRange.h"
|
||||
|
|
@ -32,6 +33,7 @@ CrudTab::CrudTab(IDatabaseWindow *context, QWidget *parent)
|
|||
|
||||
m_SortFilterProxy = new QSortFilterProxyModel(this);
|
||||
m_SortFilterProxy->setSourceModel(m_crudModel);
|
||||
m_SortFilterProxy->setSortRole(CustomSortRole);
|
||||
ui->tableView->setModel(m_SortFilterProxy);
|
||||
ui->tableView->setSortingEnabled(true);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue