closes #4: Favorite buttons has been extended with category buttons.
							parent
							
								
									5b5f31f64d
								
							
						
					
					
						commit
						5b77337b3f
					
				@ -0,0 +1,175 @@
 | 
				
			|||||||
 | 
					//
 | 
				
			||||||
 | 
					// 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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,64 @@
 | 
				
			|||||||
 | 
					//
 | 
				
			||||||
 | 
					// Created by Josef Rokos on 03.05.2023.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef COLORITEMDELEGATE_H
 | 
				
			||||||
 | 
					#define COLORITEMDELEGATE_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QStyledItemDelegate>
 | 
				
			||||||
 | 
					#include <QToolButton>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ColorItemDelegate : public QStyledItemDelegate {
 | 
				
			||||||
 | 
					    Q_OBJECT
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    explicit ColorItemDelegate(QObject* parent = nullptr);
 | 
				
			||||||
 | 
					    ~ColorItemDelegate() override = default;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
 | 
				
			||||||
 | 
					    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
 | 
				
			||||||
 | 
					    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
 | 
				
			||||||
 | 
					    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
 | 
				
			||||||
 | 
					    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private slots:
 | 
				
			||||||
 | 
					    void commitAndCloseEditor();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ColorLabel : public QWidget{
 | 
				
			||||||
 | 
					Q_OBJECT
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    explicit ColorLabel(QWidget* parent = nullptr);
 | 
				
			||||||
 | 
					    QColor color() const;
 | 
				
			||||||
 | 
					    void setColor(const QColor &color);
 | 
				
			||||||
 | 
					    QSize sizeHint() const override;
 | 
				
			||||||
 | 
					protected:
 | 
				
			||||||
 | 
					    void paintEvent(QPaintEvent *event) override;
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    QColor m_color;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ColorItemEditor : public QWidget
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					Q_OBJECT
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    explicit ColorItemEditor(QWidget *parent = nullptr);
 | 
				
			||||||
 | 
					    QColor color(){return m_color;}
 | 
				
			||||||
 | 
					    void setColor(const QColor& value);
 | 
				
			||||||
 | 
					protected:
 | 
				
			||||||
 | 
					    bool eventFilter(QObject *obj, QEvent *event) override;
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    void setFocusToParent();
 | 
				
			||||||
 | 
					signals:
 | 
				
			||||||
 | 
					    void editingFinished();
 | 
				
			||||||
 | 
					private slots:
 | 
				
			||||||
 | 
					    void slotClicked();
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    QColor m_color;
 | 
				
			||||||
 | 
					    QToolButton* m_button;
 | 
				
			||||||
 | 
					    ColorLabel* m_colorIndicator;
 | 
				
			||||||
 | 
					    bool m_buttonPressed;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif //COLORITEMDELEGATE_H
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue