widgets/colorbutton.cpp

Fri, 01 Jul 2022 23:51:16 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 23:51:16 +0300
changeset 314
4642ba1218e8
parent 264
76a025db4948
child 344
a24da8de2a3b
permissions
-rw-r--r--

Added rudimentary cylinder extrusion into circle tool.
I really had to think which way the vectors are in this one

#include <QColorDialog>
#include <QStyleFactory>
#include "widgets/colorbutton.h"

ColorButton::ColorButton(const QColor& color, QWidget* parent) :
	ColorButton{parent}
{
	this->setColor(color);
}

ColorButton::ColorButton(QWidget* parent) :
	QPushButton{parent}
{
	connect(this, &QPushButton::clicked, [&](){
		const QColor color = QColorDialog::getColor(this->storedSelectedColor, this->parentWidget());
		if (color.isValid()) {
			this->setColor(color);
		}
	});
	this->setColor(Qt::black);
}

const QColor& ColorButton::color() const
{
	return this->storedSelectedColor;
}

void ColorButton::setColor(const QColor& color)
{
	if (this->storedSelectedColor != color) {
		this->storedSelectedColor = color;
		this->setText(color.name());
		QPalette palette{color};
		const qreal lightness = color.lightnessF();
		const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
		palette.setColor(QPalette::ButtonText, foreground);
		this->setPalette(palette);
		this->setStyle(QStyleFactory::create("Fusion"));
		Q_EMIT this->colorChanged(this->storedSelectedColor);
	}
}

mercurial