OGS
ColorTableView.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "ColorTableView.h"
5
6#include <QHeaderView>
7#include <QPainter>
8
9ColorTableView::ColorTableView(QWidget* parent /*= 0*/) : QTableView(parent)
10{
11 this->verticalHeader()->hide();
12 this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
13 this->resizeColumnsToContents();
14 this->resizeRowsToContents();
15}
16
17void ColorTableViewDelegate::paint(QPainter* painter,
18 const QStyleOptionViewItem& option,
19 const QModelIndex& index) const
20{
21 QColor val;
22 if (index.column() == 1)
23 {
24 if (index.data().canConvert(QMetaType::QColor))
25 {
26 val = index.data().value<QColor>();
27 QBrush brush(val);
28 painter->fillRect(option.rect, brush);
29 }
30 }
31 else
32 {
33 QItemDelegate::paint(painter, option, index);
34 }
35}
36
37QSize ColorTableViewDelegate::sizeHint(const QStyleOptionViewItem& option,
38 const QModelIndex& index) const
39{
40 QSize s = QItemDelegate::sizeHint(option, index);
41 if (s.isValid())
42 {
43 s.setHeight(static_cast<int>(0.5 * s.height()));
44 }
45 return s;
46}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Overwrites the paint-method to set user-defined properties instead of the default properties.
ColorTableView(QWidget *parent=nullptr)
Constructor.