aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/common/colortoolbutton.cpp
blob: 5b3ebb553165efc11f476ecac6257f0f91772113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "colortoolbutton.h"
#include "colorpicker.h"
#include <QColorDialog>
#include <QPainter>

using namespace ScxmlEditor::Common;

ColorPickerAction::ColorPickerAction(const QString &key, QObject *parent)
    : QWidgetAction(parent)
    , m_key(key)
{
}

QWidget *ColorPickerAction::createWidget(QWidget *parent)
{
    auto picker = new ColorPicker(m_key, parent);
    connect(picker, &ColorPicker::colorSelected, this, &ColorPickerAction::colorSelected);
    connect(this, &ColorPickerAction::lastUsedColor, picker, &ColorPicker::setLastUsedColor);

    return picker;
}

ColorToolButton::ColorToolButton(const QString &key, const QString &iconName, const QString &tooltip, QWidget *parent)
    : QToolButton(parent)
{
    setIcon(QIcon(iconName));
    setToolTip(tooltip);
    setPopupMode(QToolButton::MenuButtonPopup);

    connect(this, &ColorToolButton::clicked, this, [this]() {
        setCurrentColor(m_color);
    });

    QPixmap p(15, 15);
    p.fill(Qt::black);

    m_colorPickerAction = new ColorPickerAction(key, this);
    connect(m_colorPickerAction, &ColorPickerAction::colorSelected, this, &ColorToolButton::setCurrentColor);
    connect(this, &ColorToolButton::colorSelected, m_colorPickerAction, &ColorPickerAction::lastUsedColor);

    m_menu = new QMenu(this);
    m_menu->addAction(QIcon(p), tr("Automatic Color"), this, &ColorToolButton::autoColorSelected);
    m_menu->addSeparator();
    m_menu->addAction(m_colorPickerAction);
    m_menu->addSeparator();
    m_menu->addAction(QIcon(QPixmap(":/scxmleditor/images/more_colors.png")), tr("More Colors..."), this, &ColorToolButton::showColorDialog);
    setMenu(m_menu);
}

ColorToolButton::~ColorToolButton()
{
    m_menu->deleteLater();
}

void ColorToolButton::autoColorSelected()
{
    setCurrentColor(QString());
}

void ColorToolButton::showColorDialog()
{
    QColor c = QColorDialog::getColor();
    if (c.isValid())
        setCurrentColor(c.name());
}

void ColorToolButton::setCurrentColor(const QString &currentColor)
{
    menu()->hide();
    m_color = currentColor;
    emit colorSelected(m_color);
    update();
}

void ColorToolButton::paintEvent(QPaintEvent *e)
{
    QToolButton::paintEvent(e);

    QPainter p(this);
    QRect r(2, height() - 7, width() - 17, 4);
    p.fillRect(r, QBrush(QColor(m_color)));

    if (!isEnabled()) {
        QColor c(Qt::gray);
        c.setAlpha(200);
        p.fillRect(r, QBrush(c));
    }
}