RedPanda-CPP/RedPandaIDE/widgets/coloredit.cpp

136 lines
3.8 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-06-07 11:02:03 +08:00
#include "coloredit.h"
#include <QColorDialog>
#include <QPainter>
#include <QDebug>
2021-06-20 22:54:16 +08:00
#include <QApplication>
2021-06-07 11:02:03 +08:00
ColorEdit::ColorEdit(QWidget *parent):QFrame(parent)
{
setFrameStyle(QFrame::Panel);
setLineWidth(1);
2022-01-04 16:50:54 +08:00
mColor = Qt::black;
2021-06-07 11:02:03 +08:00
}
QColor ColorEdit::color()
{
return mColor;
}
void ColorEdit::setColor(const QColor &value)
{
if (mColor!=value) {
mColor=value;
emit colorChanged(value);
2021-06-20 22:54:16 +08:00
resize(sizeHint());
repaint();
2021-06-07 11:02:03 +08:00
}
}
QColor ColorEdit::contrast()
{
2021-06-20 22:54:16 +08:00
int crBg;
if (!mColor.isValid())
crBg = palette().color(QPalette::Base).rgb() & 0xFFFFFF;
else
crBg = mColor.rgb() & 0xFFFFFF;
2021-06-07 11:02:03 +08:00
int TOLERANCE = 30;
int result;
if (
abs(((crBg ) & 0xFF) - 0x80) <= TOLERANCE &&
abs(((crBg >> 8) & 0xFF) - 0x80) <= TOLERANCE &&
abs(((crBg >> 16) & 0xFF) - 0x80) <= TOLERANCE
)
result = (0x7F7F7F + crBg) & 0xFFFFFF;
else
result = crBg ^ 0xFFFFFF;
return QColor(result);
}
QSize ColorEdit::sizeHint() const
{
2021-06-20 22:54:16 +08:00
QRect rect;
if (mColor.isValid() )
rect = fontMetrics().boundingRect(mColor.name(QColor::HexArgb));
2021-06-20 22:54:16 +08:00
else
rect = fontMetrics().boundingRect(tr("NONE"));
2021-06-07 11:02:03 +08:00
return QSize{rect.width()+ 10,
2021-06-20 14:30:47 +08:00
rect.height()+ 6 };
2021-06-07 11:02:03 +08:00
}
2021-06-20 22:54:16 +08:00
void ColorEdit::paintEvent(QPaintEvent *)
2021-06-07 11:02:03 +08:00
{
QPainter painter(this);
QRect rect = QRect(lineWidth(),lineWidth(),width()-2*lineWidth(),height()-2*lineWidth());
if (mColor.isValid() ) {
2021-06-20 22:54:16 +08:00
//painter.fillRect(rect,mColor);
if (isEnabled()) {
painter.setPen(contrast());
painter.setBrush(mColor);
} else {
painter.setBrush(palette().color(QPalette::Disabled,QPalette::Text));
painter.setBrush(palette().color(QPalette::Disabled,QPalette::Base));
}
2021-06-20 22:54:16 +08:00
painter.drawRect(rect);
painter.drawText(rect,Qt::AlignCenter, mColor.name(QColor::HexArgb));
2021-06-20 22:54:16 +08:00
} else {
//painter.fillRect(rect,palette().color(QPalette::Base));
if (isEnabled()) {
painter.setBrush(palette().color(QPalette::Text));
painter.setBrush(palette().color(QPalette::Base));
} else {
painter.setBrush(palette().color(QPalette::Disabled,QPalette::Text));
painter.setBrush(palette().color(QPalette::Disabled,QPalette::Base));
}
2021-06-20 22:54:16 +08:00
painter.setPen(contrast());
painter.setBrush(palette().color(QPalette::Base));
painter.drawRect(rect);
painter.drawText(rect,Qt::AlignCenter, tr("NONE"));
}
2021-06-07 11:02:03 +08:00
}
void ColorEdit::mouseReleaseEvent(QMouseEvent *)
2021-06-07 11:02:03 +08:00
{
QColor c = QColorDialog::getColor(mColor,nullptr,tr("Color"),
QColorDialog::ShowAlphaChannel | QColorDialog::DontUseNativeDialog);
2021-06-07 11:02:03 +08:00
if (c.isValid()) {
setColor(c);
}
}
#if QT_VERSION_MAJOR >= 6
void ColorEdit::enterEvent(QEnterEvent *)
#else
void ColorEdit::enterEvent(QEvent *)
#endif
2021-06-07 11:02:03 +08:00
{
setCursor(Qt::PointingHandCursor);
}
void ColorEdit::leaveEvent(QEvent *)
2021-06-07 11:02:03 +08:00
{
setCursor(Qt::ArrowCursor);
}
2021-06-20 14:30:47 +08:00
QSize ColorEdit::minimumSizeHint() const
{
2021-06-20 22:54:16 +08:00
return sizeHint();
2021-06-20 14:30:47 +08:00
}