Skip to content

Commit

Permalink
feat: Adjusting the color palette style
Browse files Browse the repository at this point in the history
Round to square
  • Loading branch information
XMuli committed Aug 21, 2023
1 parent 482c2dd commit 44c89c9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 84 deletions.
16 changes: 8 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ int main(int argc, char *argv[])
//#endif


// SelectBar* t1 = new SelectBar(Qt::Horizontal, t0);
// SelectBar* t1 = new SelectBar(Qt::Horizontal);
// t1->setWindowFlags(Qt::FramelessWindowHint);
//// t1 ->setBlurBackground(QPixmap("D:/projects/Flipped/src/p1.jpg"), 4);
// t1->move(200, 100);
// t1->show();

// ParameterBar* t2 = new ParameterBar(Qt::Horizontal, t0);
// ParameterBar* t2 = new ParameterBar(Qt::Horizontal);
//// t2->setBlurBackground(QPixmap("D:/projects/Flipped/src/p2.jpg"), 5);
// t2->move(200, 200);
// t2->show();
Expand All @@ -101,16 +101,16 @@ int main(int argc, char *argv[])
//// t3->resize(400, 300);
// t3->move(200, 300);
// t3->show();
//

// ColorParaBar* t3_1 = new ColorParaBar(ColorParaBarMode::CPB_HighLight, Qt::Horizontal);
// // t3->resize(400, 300);
// t3_1->move(200, 400);
// t3_1->show();
//
// ExWidget* t4 = new ExWidget();
// // t3->resize(400, 300);
// t4->move(100, 200);
// t4->show();

//// ExWidget* t4 = new ExWidget();
//// // t3->resize(400, 300);
//// t4->move(100, 200);
//// t4->show();

// PinWidget* w5 = new PinWidget(QPixmap("C:/Users/xmuli/Desktop/Snipaste_2022-07-16_02-13-39.png"), QRect(100, 100, 400, 400));
// w5->show();
Expand Down
40 changes: 21 additions & 19 deletions src/tool/base/colorparabar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ColorParaBar::ColorParaBar(ColorParaBarMode mode, Qt::Orientations orien, QWidge
lab->installEventFilter(this);

if ((it + 1) == m_labMap.end()) // 最后一个渐变色
lab->setIsPickColor(true);
lab->setRainbow(true);
else
lab->setColor(it.value(), 1);

Expand Down Expand Up @@ -99,7 +99,7 @@ ColorParaBar::ColorParaBar(ColorParaBarMode mode, Qt::Orientations orien, QWidge
lab->installEventFilter(this);

if (it + 1 == m_labMap.cend()) // 最后一个渐变色
lab->setIsPickColor(true);
lab->setRainbow(true);
else
lab->setColor(it.value(), 1);

Expand Down Expand Up @@ -154,8 +154,10 @@ void ColorParaBar::onUpdateSel(const QColor& col)
bool ColorParaBar::eventFilter(QObject *watched, QEvent *event)
{
XLabel* lab = qobject_cast<XLabel *>(watched);
if (!lab)
return false;
if (!lab) return false;

for (auto& it : findChildren<XLabel*>())
it->setChecked(it == m_curXLab);

if (event->type() == QEvent::MouseButtonRelease) {
if (lab->objectName().compare(m_labMap.lastKey()) == 0) {
Expand All @@ -180,19 +182,19 @@ bool ColorParaBar::eventFilter(QObject *watched, QEvent *event)
void ColorParaBar::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
if (!m_curXLab)
return;

QPainter pa(this);
pa.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QPen pen(m_curCol);
pen.setWidth(CPB_WIDTH_SELECTED);
pa.setPen(pen);
pa.setBrush(Qt::NoBrush);

int margin = CPB_MARGIN_SELECTED;
auto topLeft = m_curXLab->mapToGlobal(QPoint(0, 0)); // 子控件的窗口的(左上角的)绝对坐标; QPoint(0, 0) 为子控件的左上角坐标,子窗口的总是(0, 0)
topLeft = mapFromGlobal(topLeft); // 切换为相对父窗口的绝对坐标
const QRect rt = QRect(topLeft, m_curXLab->size()).adjusted(-margin, -margin, margin, margin);
pa.drawEllipse(rt.center(), rt.width() / 2, rt.height() / 2);
// if (!m_curXLab)
// return;

// QPainter pa(this);
// pa.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
// QPen pen(m_curCol);
// pen.setWidth(CPB_WIDTH_SELECTED);
// pa.setPen(pen);
// pa.setBrush(Qt::NoBrush);

// int margin = CPB_MARGIN_SELECTED;
// auto topLeft = m_curXLab->mapToGlobal(QPoint(0, 0)); // 子控件的窗口的(左上角的)绝对坐标; QPoint(0, 0) 为子控件的左上角坐标,子窗口的总是(0, 0)
// topLeft = mapFromGlobal(topLeft); // 切换为相对父窗口的绝对坐标
// const QRect rt = QRect(topLeft, m_curXLab->size()).adjusted(-margin, -margin, margin, margin);
// pa.drawEllipse(rt.center(), rt.width() / 2, rt.height() / 2);
}
4 changes: 2 additions & 2 deletions src/tool/selectsize/selectsize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "../../xglobal.h"
#include "../../screen/datamaid.h"

SelectSize::SelectSize(QWidget* parent /*= nullptr*/, Qt::WindowFlags f /*= Qt::WindowFlags()*/)
SelectSize::SelectSize(QWidget* parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
initUI();
}

SelectSize::SelectSize(const QString& text, QWidget* parent /*= nullptr*/, Qt::WindowFlags f /*= Qt::WindowFlags()*/)
SelectSize::SelectSize(const QString& text, QWidget* parent, Qt::WindowFlags f)
: QLabel(text, parent, f)
{
initUI();
Expand Down
81 changes: 35 additions & 46 deletions src/widget/xlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,31 @@
#include <QPainter>
#include <QPainter>
#include <QPalette>
#include <QPen>
#include <QtGlobal>
#include <QMouseEvent>
#include "../core/xlog.h"
#include <QPainterPath>

XLabel::XLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
, m_color("#DB000F")
, m_alpha(1)
, m_bPickColor(false)
: QToolButton(parent)
{
init();
initUI();
}

XLabel::XLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
: QLabel(text, parent, f)
, m_color("#DB000F")
, m_alpha(1)
, m_bPickColor(false)
void XLabel::setChecked(const bool &checked)
{
init();
m_bChecked = checked;
}

XLabel::~XLabel()
{
}

void XLabel::setColor(QColor c, double alpha /*= 1*/)
void XLabel::setColor(QColor c, double alpha)
{
m_color = c;
m_alpha = alpha;
}

// 构造一个锥向渐变(center, angle) https://blog.csdn.net/weixin_37818081/article/details/118879134
void XLabel::setConicalGradientColor(QPainter& pa)
{
// 构造一个锥向渐变(center, angle) https://blog.csdn.net/weixin_37818081/article/details/118879134
QConicalGradient conicalGradient(QPointF(contentsRect().center()), 300);

conicalGradient.setColorAt(0, QColor("#EB004A"));
Expand All @@ -54,64 +44,63 @@ void XLabel::setConicalGradientColor(QPainter& pa)
conicalGradient.setColorAt(6 / 7.0, QColor("#FFDB34"));
conicalGradient.setColorAt(1, QColor("#EB004A"));

// 使用锥向渐变创建一个画刷,用来填充
pa.save();
pa.setPen(Qt::NoPen);
QColor colPen("#6B6B6B");
colPen.setAlphaF(0.5);
pa.setPen(colPen);
pa.setBrush(conicalGradient);
const QRect& rt = contentsRect().adjusted(1, 1, -1, -1);
pa.drawEllipse(rt.center(), rt.width() / 2, rt.height() / 2);
pa.drawRect(rect());
pa.restore();
}

void XLabel::setIsPickColor(bool enablem)
void XLabel::setRainbow(bool bRainbow)
{
m_bPickColor = enablem;
m_bRainbow = bRainbow;
}

void XLabel::init()
void XLabel::initUI()
{
m_color = "#DB000F";
m_alpha = 1;
m_bRainbow = false;
m_bChecked = false;

// 此两行也可以注释掉,亲测不影响,可能是由于后面有一个 ColorParaBar 作为父类
//setWindowFlags(Qt::FramelessWindowHint); // 去掉标题栏
setAttribute(Qt::WA_TranslucentBackground); // 设置透明,自绘画为圆角矩形
// setAutoFillBackground(false);

QColor col = this->palette().window().color();
m_bPickColor = false;
}


void XLabel::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter pa(this);
pa.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
// 矩形和内切圆此部分不用绘画 避免出现 拖曳时刻出现残影和交叉部分显示黑色

if (m_bPickColor) {
if (m_bRainbow) {
setConicalGradientColor(pa);
// TODO 2022.06.21 故意缺少最外层一单层圈、 #000000、 0.08; 但是加上就很显示外圈线条很黑
} else {
QColor colBrush(m_color);
colBrush.setAlphaF(m_alpha);
pa.setPen(Qt::NoPen);
QColor colPen("#6B6B6B");
colPen.setAlphaF(0.5);
pa.setPen(colPen);
pa.setBrush(colBrush);
const QRect& rt = contentsRect().adjusted(1, 1, -1, -1);
pa.drawEllipse(rt.center(), rt.width() / 2, rt.height() / 2);
pa.drawRect(rect());
}

// 此参数设计图写死
QColor colPen("#000000");
colPen.setAlphaF(0.08);
pa.setPen(colPen);
if (m_bChecked) {
int centerX = width() / 2;
int centerY = height() / 2;
const int side = rect().size().width() * 2 / 3;
const QRect rt(centerX - side / 2, centerY - side / 2, side, side);

pa.setPen(QPen(QColor("#FFFFFF"), 2));
pa.setBrush(Qt::NoBrush);
pa.drawEllipse(contentsRect().center(), contentsRect().width() / 2, contentsRect().height() / 2);
pa.drawRect(rt);
}
}

//void XLabel::mousePressEvent(QMouseEvent* ev)
//{
// ev->ignore(); //验证事件事件被忽略后会被传递给父对象
//}
//
//void XLabel::mouseReleaseEvent(QMouseEvent* ev)
//{
// ev->ignore();
//}
17 changes: 9 additions & 8 deletions src/widget/xlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@

#include <QLabel>
#include <QColor>
#include <QToolButton>

class QWidget;
class QString;
class QWidget;
class QPainter;
class QMouseEvent;

class XLabel : public QLabel
class XLabel : public QToolButton
{
Q_OBJECT
public:
explicit XLabel(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
explicit XLabel(const QString &text, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
virtual ~XLabel();
virtual ~XLabel() = default;

void setChecked(const bool& checked = false);
void setColor(QColor c, double alpha = 1);
void setIsPickColor(bool enablem = false);
void setRainbow(bool bRainbow = false);
private:
void init();
void initUI();
void setConicalGradientColor(QPainter &pa);

protected:
Expand All @@ -35,10 +36,10 @@ class XLabel : public QLabel
private:
QColor m_color;
double m_alpha;
bool m_bPickColor; // 默认为非渐变色
bool m_bChecked; // 当前被选中否?
bool m_bRainbow; // false:纯色 true:彩色
};

//Q_DECLARE_METATYPE(XLabel); // 另外自定义类型中一定要有默认构造函数
Q_DECLARE_METATYPE(XLabel*); // 提供给 QVariant 使用
Q_DECLARE_METATYPE(XLabel*); // 提供给 QVariant 使用; // 另外自定义类型中一定要有默认构造函数

#endif // XLABEL_H
2 changes: 1 addition & 1 deletion src/xglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#define CPB_PB_LABEL_WIDTH 12 // 圆形取色盘宽度
#define CPB_PB_MARGIN_HOR 10 // 取色盘, 圆形按钮的边框 左右 距离
#define CPB_PB_MARGIN_VER 6 // 取色盘, 圆形按钮的边框 上下 距离
#define CPB_PB_SPACING_HOR 7 // 取色盘, 圆形按钮之间的水平间距
#define CPB_PB_SPACING_HOR 4 // 取色盘, 圆形按钮之间的水平间距
#define CPB_PB_SPACING_VER 4 // 取色盘, 圆形按钮之间的竖直间距

// Mode: CPB_HighLight -> QHBoxLayout
Expand Down

0 comments on commit 44c89c9

Please sign in to comment.