Implemented a custom delegate that does the samework as the BaseTableModel class did.
While this might seem to duplicate behaviour I believe having this code in a delegate is a better solution. - the logic in BaseTableModel is view logic not model logic - letting the custom delegate determine the formatting is much more efficient then letting the default delegate do many virtual calls to detemine how the model wants it formatted.
This commit is contained in:
parent
2ba27178a2
commit
3ac1d21728
2 changed files with 147 additions and 0 deletions
129
pglab/PgLabItemDelegate.cpp
Normal file
129
pglab/PgLabItemDelegate.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
#include "PgLabItemDelegate.h"
|
||||
#include <QApplication>
|
||||
|
||||
#include "Pgsql_oids.h"
|
||||
#include "ResultTableModelUtil.h"
|
||||
|
||||
PgLabItemDelegate::PgLabItemDelegate(QObject *parent)
|
||||
: QStyledItemDelegate(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
QVariant value;
|
||||
// value = index.data(Qt::FontRole);
|
||||
// if (value.isValid() && !value.isNull()) {
|
||||
// option->font = qvariant_cast<QFont>(value).resolve(option->font);
|
||||
// option->fontMetrics = QFontMetrics(option->font);
|
||||
// }
|
||||
|
||||
// value = index.data(Qt::TextAlignmentRole);
|
||||
// if (value.isValid() && !value.isNull())
|
||||
// option->displayAlignment = Qt::Alignment(value.toInt());
|
||||
|
||||
// value = index.data(Qt::ForegroundRole);
|
||||
// if (value.canConvert<QBrush>())
|
||||
// option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value));
|
||||
|
||||
|
||||
option->index = index;
|
||||
// value = index.data(Qt::CheckStateRole);
|
||||
// if (value.isValid() && !value.isNull()) {
|
||||
// option->features |= QStyleOptionViewItem::HasCheckIndicator;
|
||||
// option->checkState = static_cast<Qt::CheckState>(value.toInt());
|
||||
// }
|
||||
|
||||
// value = index.data(Qt::DecorationRole);
|
||||
// if (value.isValid() && !value.isNull()) {
|
||||
// option->features |= QStyleOptionViewItem::HasDecoration;
|
||||
// switch (value.type()) {
|
||||
// case QVariant::Icon: {
|
||||
// option->icon = qvariant_cast<QIcon>(value);
|
||||
// QIcon::Mode mode;
|
||||
// if (!(option->state & QStyle::State_Enabled))
|
||||
// mode = QIcon::Disabled;
|
||||
// else if (option->state & QStyle::State_Selected)
|
||||
// mode = QIcon::Selected;
|
||||
// else
|
||||
// mode = QIcon::Normal;
|
||||
// QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
|
||||
// QSize actualSize = option->icon.actualSize(option->decorationSize, mode, state);
|
||||
// // For highdpi icons actualSize might be larger than decorationSize, which we don't want. Clamp it to decorationSize.
|
||||
// option->decorationSize = QSize(qMin(option->decorationSize.width(), actualSize.width()),
|
||||
// qMin(option->decorationSize.height(), actualSize.height()));
|
||||
// break;
|
||||
// }
|
||||
// case QVariant::Color: {
|
||||
// QPixmap pixmap(option->decorationSize);
|
||||
// pixmap.fill(qvariant_cast<QColor>(value));
|
||||
// option->icon = QIcon(pixmap);
|
||||
// break;
|
||||
// }
|
||||
// case QVariant::Image: {
|
||||
// QImage image = qvariant_cast<QImage>(value);
|
||||
// option->icon = QIcon(QPixmap::fromImage(image));
|
||||
// option->decorationSize = image.size() / image.devicePixelRatio();
|
||||
// break;
|
||||
// }
|
||||
// case QVariant::Pixmap: {
|
||||
// QPixmap pixmap = qvariant_cast<QPixmap>(value);
|
||||
// option->icon = QIcon(pixmap);
|
||||
// option->decorationSize = pixmap.size() / pixmap.devicePixelRatio();
|
||||
// break;
|
||||
// }
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
Oid oid = InvalidOid;
|
||||
value = index.data(Qt::UserRole); // get OID
|
||||
if (value.isValid())
|
||||
oid = value.toInt(); //getType(index.column());
|
||||
|
||||
value = index.data(Qt::DisplayRole);
|
||||
|
||||
option->displayAlignment = GetDefaultAlignmentForType(oid);
|
||||
|
||||
|
||||
if (value.isValid() && ! value.isNull()) {
|
||||
QColor forground_color = oid == Pgsql::bool_oid
|
||||
? GetDefaultBoolColor(value.toBool())
|
||||
: GetDefaultColorForType(oid);
|
||||
option->palette.setBrush(QPalette::Text, QBrush(forground_color));
|
||||
|
||||
option->features |= QStyleOptionViewItem::HasDisplay;
|
||||
option->text = oid == Pgsql::bool_oid
|
||||
? FormatBoolForDisplay(value.toBool())
|
||||
: displayText(value, option->locale);
|
||||
}
|
||||
else {
|
||||
option->palette.setBrush(QPalette::Text, QBrush(GetDefaultNullColor()));
|
||||
|
||||
option->features |= QStyleOptionViewItem::HasDisplay;
|
||||
option->text = "null";
|
||||
}
|
||||
|
||||
|
||||
// option->backgroundBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
|
||||
|
||||
// disable style animations for checkboxes etc. within itemviews (QTBUG-30146)
|
||||
option->styleObject = 0;
|
||||
}
|
||||
|
||||
void PgLabItemDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
Q_ASSERT(index.isValid());
|
||||
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
|
||||
const QWidget *widget = option.widget; // QStyledItemDelegatePrivate::widget(option);
|
||||
QStyle *style = widget ? widget->style() : QApplication::style();
|
||||
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
|
||||
}
|
||||
18
pglab/PgLabItemDelegate.h
Normal file
18
pglab/PgLabItemDelegate.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef PGLABITEMDELEGATE_H
|
||||
#define PGLABITEMDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
class PgLabItemDelegate : public QStyledItemDelegate
|
||||
{
|
||||
public:
|
||||
PgLabItemDelegate(QObject *parent = nullptr);
|
||||
|
||||
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
protected:
|
||||
virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override;
|
||||
|
||||
};
|
||||
|
||||
#endif // PGLABITEMDELEGATE_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue