You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
5.0 KiB
C++
176 lines
5.0 KiB
C++
//
|
|
// Created by Josef Rokos on 03.05.2023.
|
|
//
|
|
|
|
#include "coloritemdelegate.h"
|
|
#include <QPainter>
|
|
#include <QApplication>
|
|
#include <QPaintEvent>
|
|
#include <QHBoxLayout>
|
|
#include <QColorDialog>
|
|
|
|
ColorLabel::ColorLabel(QWidget* parent) : QWidget(parent), m_color(Qt::white) {
|
|
setAttribute(Qt::WA_StaticContents);
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
setFocusPolicy(Qt::NoFocus);
|
|
}
|
|
|
|
QColor ColorLabel::color() const {
|
|
return m_color;
|
|
}
|
|
|
|
void ColorLabel::setColor(const QColor& color) {
|
|
m_color = color;
|
|
}
|
|
|
|
QSize ColorLabel::sizeHint() const {
|
|
return {20,20};
|
|
}
|
|
|
|
void ColorLabel::paintEvent(QPaintEvent* event) {
|
|
QPainter painter(this);
|
|
|
|
QStyle* style = QApplication::style();
|
|
painter.save();
|
|
painter.setBrush(m_color);
|
|
|
|
bool dark = false;
|
|
qreal darkness = 1-(0.299*m_color.red() + 0.587*m_color.green() + 0.114*m_color.blue())/255;
|
|
if(darkness >= 0.5){
|
|
dark = true;
|
|
}
|
|
|
|
QColor penColor = dark ? Qt::transparent : Qt::darkGray;
|
|
|
|
painter.setPen(penColor);
|
|
int border = (event->rect().height() - style->pixelMetric(QStyle::PM_IndicatorWidth))/2;
|
|
|
|
QRect rect(event->rect().x()+border, event->rect().y()+border,
|
|
style->pixelMetric(QStyle::PM_IndicatorWidth),
|
|
style->pixelMetric(QStyle::PM_IndicatorWidth));// = option.rect.adjusted(4,4,-4,-6);
|
|
|
|
painter.drawRect(rect);
|
|
painter.restore();
|
|
}
|
|
|
|
ColorItemEditor::ColorItemEditor(QWidget* parent) : QWidget(parent), m_buttonPressed(false)
|
|
{
|
|
m_colorIndicator = new ColorLabel(this);
|
|
m_colorIndicator->setColor(m_color);
|
|
m_button = new QToolButton(this);
|
|
m_button->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
|
|
m_button->setText("...");
|
|
m_button->installEventFilter(this);
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
layout->addWidget(m_colorIndicator);
|
|
layout->addWidget(m_button);
|
|
layout->setSpacing(0);
|
|
layout->setContentsMargins(1,1,1,1);
|
|
setFocusProxy(m_button);
|
|
setAutoFillBackground(true);
|
|
setLayout(layout);
|
|
setAutoFillBackground(true);
|
|
connect(m_button,SIGNAL(clicked()),this,SLOT(slotClicked()));
|
|
}
|
|
|
|
void ColorItemEditor::setColor(const QColor& value) {
|
|
m_color = value;
|
|
m_colorIndicator->setColor(value);
|
|
}
|
|
|
|
bool ColorItemEditor::eventFilter(QObject* obj, QEvent* event) {
|
|
if (obj == m_button){
|
|
if (event->type() == QEvent::FocusOut && !m_buttonPressed){
|
|
auto focusEvent = dynamic_cast<QFocusEvent*>(event);
|
|
if (focusEvent && focusEvent->reason()!=Qt::MouseFocusReason){
|
|
setFocusToParent();
|
|
emit(editingFinished());
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ColorItemEditor::setFocusToParent() {
|
|
if (parentWidget()) {
|
|
parentWidget()->setFocus();
|
|
}
|
|
}
|
|
|
|
void ColorItemEditor::slotClicked() {
|
|
m_buttonPressed = true;
|
|
auto dialog = new QColorDialog(this);
|
|
dialog->setCurrentColor(m_color);
|
|
|
|
if (dialog->exec()) {
|
|
setColor(dialog->currentColor());
|
|
}
|
|
|
|
delete dialog;
|
|
setFocusToParent();
|
|
emit(editingFinished());
|
|
}
|
|
|
|
ColorItemDelegate::ColorItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {
|
|
|
|
}
|
|
|
|
void ColorItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
|
QColor color(index.data().toString());
|
|
|
|
if (color.isValid()) {
|
|
painter->save();
|
|
painter->setBrush(color);
|
|
|
|
QRect rect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height());
|
|
painter->drawRect(rect);
|
|
painter->restore();
|
|
} else {
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
}
|
|
}
|
|
|
|
QSize ColorItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
|
return QStyledItemDelegate::sizeHint(option, index);
|
|
}
|
|
|
|
QWidget* ColorItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
|
QColor color(index.data().toString());
|
|
if (!color.isValid()) {
|
|
color = QColorConstants::Gray;
|
|
}
|
|
|
|
auto editor = new ColorItemEditor(parent);
|
|
editor->setColor(color);
|
|
connect(editor, &ColorItemEditor::editingFinished, this, &ColorItemDelegate::commitAndCloseEditor);
|
|
return editor;
|
|
}
|
|
|
|
void ColorItemDelegate::commitAndCloseEditor() {
|
|
auto editor = qobject_cast<ColorItemEditor*>(sender());
|
|
emit commitData(editor);
|
|
emit closeEditor(editor);
|
|
}
|
|
|
|
void ColorItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
|
|
auto edit = qobject_cast<ColorItemEditor*>(editor);
|
|
QColor color(index.data().toString());
|
|
|
|
if (edit && color.isValid()) {
|
|
edit->setColor(color);
|
|
} else {
|
|
QStyledItemDelegate::setEditorData(editor, index);
|
|
}
|
|
}
|
|
|
|
void ColorItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
|
|
auto edit = qobject_cast<ColorItemEditor*>(editor);
|
|
|
|
if (edit) {
|
|
model->setData(index, edit->color().name());
|
|
} else {
|
|
QStyledItemDelegate::setModelData(editor, model, index);
|
|
}
|
|
}
|