1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "coloredit.h"
18
19#include <QColorDialog>
20#include <QPainter>
21#include <QDebug>
22#include <QApplication>
23
24ColorEdit::ColorEdit(QWidget *parent):QFrame(parent)
25{
26 setFrameStyle(QFrame::Panel);
27 setLineWidth(1);
28 mColor = Qt::black;
29}
30
31QColor ColorEdit::color()
32{
33 return mColor;
34}
35
36void ColorEdit::setColor(const QColor &value)
37{
38 if (mColor!=value) {
39 mColor=value;
40 emit colorChanged(value);
41 resize(sizeHint());
42 repaint();
43 }
44}
45
46QColor ColorEdit::contrast()
47{
48 int crBg;
49 if (!mColor.isValid())
50 crBg = palette().color(QPalette::Base).rgb() & 0xFFFFFF;
51 else
52 crBg = mColor.rgb() & 0xFFFFFF;
53 int TOLERANCE = 30;
54 int result;
55
56 if (
57 abs(((crBg ) & 0xFF) - 0x80) <= TOLERANCE &&
58 abs(((crBg >> 8) & 0xFF) - 0x80) <= TOLERANCE &&
59 abs(((crBg >> 16) & 0xFF) - 0x80) <= TOLERANCE
60 )
61 result = (0x7F7F7F + crBg) & 0xFFFFFF;
62 else
63 result = crBg ^ 0xFFFFFF;
64 return QColor(result);
65}
66
67QSize ColorEdit::sizeHint() const
68{
69 QRect rect;
70 if (mColor.isValid() )
71 rect = fontMetrics().boundingRect(mColor.name(QColor::HexArgb));
72 else
73 rect = fontMetrics().boundingRect(tr("NONE"));
74 return QSize{rect.width()+ 10,
75 rect.height()+ 6 };
76}
77
78void ColorEdit::paintEvent(QPaintEvent *)
79{
80 QPainter painter(this);
81 QRect rect = QRect(lineWidth(),lineWidth(),width()-2*lineWidth(),height()-2*lineWidth());
82 if (mColor.isValid() ) {
83 //painter.fillRect(rect,mColor);
84 if (isEnabled()) {
85 painter.setPen(contrast());
86 painter.setBrush(mColor);
87 } else {
88 painter.setBrush(palette().color(QPalette::Disabled,QPalette::Text));
89 painter.setBrush(palette().color(QPalette::Disabled,QPalette::Base));
90 }
91 painter.drawRect(rect);
92 painter.drawText(rect,Qt::AlignCenter, mColor.name(QColor::HexArgb));
93 } else {
94 //painter.fillRect(rect,palette().color(QPalette::Base));
95 if (isEnabled()) {
96 painter.setBrush(palette().color(QPalette::Text));
97 painter.setBrush(palette().color(QPalette::Base));
98 } else {
99 painter.setBrush(palette().color(QPalette::Disabled,QPalette::Text));
100 painter.setBrush(palette().color(QPalette::Disabled,QPalette::Base));
101 }
102 painter.setPen(contrast());
103 painter.setBrush(palette().color(QPalette::Base));
104 painter.drawRect(rect);
105 painter.drawText(rect,Qt::AlignCenter, tr("NONE"));
106 }
107}
108
109void ColorEdit::mouseReleaseEvent(QMouseEvent *)
110{
111 QColor c = QColorDialog::getColor(mColor,nullptr,tr("Color"),
112 QColorDialog::ShowAlphaChannel | QColorDialog::DontUseNativeDialog);
113 if (c.isValid()) {
114 setColor(c);
115 }
116}
117
118void ColorEdit::enterEvent(QEvent *)
119{
120 setCursor(Qt::PointingHandCursor);
121}
122
123void ColorEdit::leaveEvent(QEvent *)
124{
125 setCursor(Qt::ArrowCursor);
126}
127
128QSize ColorEdit::minimumSizeHint() const
129{
130 return sizeHint();
131}
132