Move color button into widgets lib

Wed, 22 Jun 2022 22:59:56 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 22 Jun 2022 22:59:56 +0300
changeset 256
c6f5de03dc0f
parent 255
5188f8a74a5c
child 257
afdab56e8210

Move color button into widgets lib

CMakeLists.txt file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.cpp file | annotate | diff | comparison | revisions
src/widgets/colorbutton.cpp file | annotate | diff | comparison | revisions
src/widgets/colorbutton.h file | annotate | diff | comparison | revisions
widgets/CMakeLists.txt file | annotate | diff | comparison | revisions
widgets/colorbutton.cpp file | annotate | diff | comparison | revisions
widgets/colorbutton.h file | annotate | diff | comparison | revisions
widgets/designerplugins.cpp file | annotate | diff | comparison | revisions
widgets/designerplugins.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Wed Jun 22 22:50:37 2022 +0300
+++ b/CMakeLists.txt	Wed Jun 22 22:59:56 2022 +0300
@@ -51,7 +51,6 @@
 	src/types/boundingbox.cpp
 	src/ui/circletooloptionswidget.cpp
 	src/ui/objecteditor.cpp
-	src/widgets/colorbutton.cpp
 	src/widgets/colorindexinput.cpp
 	src/widgets/colorselectdialog.cpp
 )
@@ -91,7 +90,6 @@
 	src/types/boundingbox.h
 	src/ui/circletooloptionswidget.h
 	src/ui/objecteditor.h
-	src/widgets/colorbutton.h
 	src/widgets/colorindexinput.h
 	src/widgets/colorselectdialog.h
 )
--- a/src/settingseditor/settingseditor.cpp	Wed Jun 22 22:50:37 2022 +0300
+++ b/src/settingseditor/settingseditor.cpp	Wed Jun 22 22:59:56 2022 +0300
@@ -40,9 +40,9 @@
 void SettingsEditor::handleAccepted()
 {
 	setSetting<Setting::Locale>(this->ui.language->currentData().toString());
-	setSetting<Setting::MainColor>(this->ui.mainColorButton->selectedColor());
-	setSetting<Setting::BackgroundColor>(this->ui.backgroundColorButton->selectedColor());
-	setSetting<Setting::SelectedColor>(this->ui.selectedColorButton->selectedColor());
+	setSetting<Setting::MainColor>(this->ui.mainColorButton->color());
+	setSetting<Setting::BackgroundColor>(this->ui.backgroundColorButton->color());
+	setSetting<Setting::SelectedColor>(this->ui.selectedColorButton->color());
 	setSetting<Setting::LineThickness>(static_cast<GLfloat>(this->ui.lineThickness->value()));
 	setSetting<Setting::LineAntiAliasing>(this->ui.lineAntiAliasing->isChecked());
 	setSetting<Setting::LogOpenGLDebugMessages>(this->ui.logOpenGLDebugMessages->isChecked());
@@ -78,9 +78,9 @@
 void SettingsEditor::setDefaults()
 {
 	this->setCurrentLanguage(setting<Setting::Locale>());
-	this->ui.mainColorButton->setSelectedColor(setting<Setting::MainColor>());
-	this->ui.backgroundColorButton->setSelectedColor(setting<Setting::BackgroundColor>());
-	this->ui.selectedColorButton->setSelectedColor(setting<Setting::SelectedColor>());
+	this->ui.mainColorButton->setColor(setting<Setting::MainColor>());
+	this->ui.backgroundColorButton->setColor(setting<Setting::BackgroundColor>());
+	this->ui.selectedColorButton->setColor(setting<Setting::SelectedColor>());
 	this->ui.lineThickness->setValue(double_cast(setting<Setting::LineThickness>()));
 	this->ui.lineAntiAliasing->setChecked(setting<Setting::LineAntiAliasing>());
 	this->ui.logOpenGLDebugMessages->setChecked(setting<Setting::LogOpenGLDebugMessages>());
--- a/src/widgets/colorbutton.cpp	Wed Jun 22 22:50:37 2022 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-#include <QColorDialog>
-#include "colorbutton.h"
-
-ColorButton::ColorButton(const QColor& color, QWidget* parent) :
-	ColorButton{parent}
-{
-	this->setSelectedColor(color);
-}
-
-ColorButton::ColorButton(QWidget* parent) :
-	QPushButton{parent}
-{
-	connect(this, &QPushButton::clicked, [&]()
-	{
-		const QColor color = QColorDialog::getColor(this->storedSelectedColor, this->parentWidget());
-		if (color.isValid())
-		{
-			this->setSelectedColor(color);
-		}
-	});
-}
-
-QColor ColorButton::selectedColor() const
-{
-	return this->storedSelectedColor;
-}
-
-void ColorButton::setSelectedColor(const QColor& newSelectedColor)
-{
-	this->storedSelectedColor = newSelectedColor;
-	this->setStyleSheet(QString{"background-color: %1"}.arg(newSelectedColor.name()));
-	this->setText(newSelectedColor.name());
-}
-
--- a/src/widgets/colorbutton.h	Wed Jun 22 22:50:37 2022 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-#pragma once
-#include <QPushButton>
-
-/**
- * @brief A button that can be used to select a color
- */
-class ColorButton : public QPushButton
-{
-public:
-	ColorButton(const QColor& color = {}, QWidget* parent = nullptr);
-	ColorButton(QWidget* parent = nullptr);
-	QColor selectedColor() const;
-	void setSelectedColor(const QColor& newSelectedColor);
-private:
-	QColor storedSelectedColor;
-};
--- a/widgets/CMakeLists.txt	Wed Jun 22 22:50:37 2022 +0300
+++ b/widgets/CMakeLists.txt	Wed Jun 22 22:59:56 2022 +0300
@@ -6,6 +6,8 @@
 )
 
 add_library(ldforgewidgets SHARED
+	colorbutton.cpp
+	colorbutton.h
 	designerplugins.cpp
 	designerplugins.h
 	doublespinbox.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/colorbutton.cpp	Wed Jun 22 22:59:56 2022 +0300
@@ -0,0 +1,36 @@
+#include <QColorDialog>
+#include "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->setStyleSheet(QString{"background-color: %1"}.arg(color.name()));
+		this->setText(color.name());
+		Q_EMIT this->colorChanged(this->storedSelectedColor);
+	}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/colorbutton.h	Wed Jun 22 22:59:56 2022 +0300
@@ -0,0 +1,19 @@
+#pragma once
+#include <QPushButton>
+
+/**
+ * @brief A button that can be used to select a color
+ */
+class ColorButton : public QPushButton
+{
+	Q_OBJECT
+	Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
+public:
+	ColorButton(const QColor& color = {}, QWidget* parent = nullptr);
+	ColorButton(QWidget* parent = nullptr);
+	const QColor& color() const;
+	Q_SLOT void setColor(const QColor& color);
+	Q_SIGNAL void colorChanged(const QColor& color);
+private:
+	QColor storedSelectedColor;
+};
--- a/widgets/designerplugins.cpp	Wed Jun 22 22:50:37 2022 +0300
+++ b/widgets/designerplugins.cpp	Wed Jun 22 22:59:56 2022 +0300
@@ -1,12 +1,14 @@
 #include "designerplugins.h"
 #include "vec3editor.h"
 #include "matrixeditor.h"
+#include "colorbutton.h"
 
 LDForgeWidgetCollection::LDForgeWidgetCollection(QObject* parent) :
 	QObject{parent}
 {
 	this->interfaces.append(new Vec3EditorPlugin{this});
 	this->interfaces.append(new MatrixEditorPlugin{this});
+	this->interfaces.append(new ColorButtonPlugin{this});
 }
 
 QList<QDesignerCustomWidgetInterface*> LDForgeWidgetCollection::customWidgets() const
@@ -93,3 +95,44 @@
 {
 	return new MatrixEditor{parent};
 }
+
+
+QString ColorButtonPlugin::name() const
+{
+	return "ColorButton";
+}
+
+QString ColorButtonPlugin::group() const
+{
+	return "LDForge";
+}
+
+QString ColorButtonPlugin::toolTip() const
+{
+	return "";
+}
+
+QString ColorButtonPlugin::whatsThis() const
+{
+	return "";
+}
+
+QString ColorButtonPlugin::includeFile() const
+{
+	return "colorbutton.h";
+}
+
+QIcon ColorButtonPlugin::icon() const
+{
+	return {};
+}
+
+bool ColorButtonPlugin::isContainer() const
+{
+	return false;
+}
+
+QWidget* ColorButtonPlugin::createWidget(QWidget* parent)
+{
+	return new ColorButton{parent};
+}
--- a/widgets/designerplugins.h	Wed Jun 22 22:50:37 2022 +0300
+++ b/widgets/designerplugins.h	Wed Jun 22 22:59:56 2022 +0300
@@ -47,3 +47,20 @@
 	bool isContainer() const override;
 	QWidget* createWidget(QWidget* parent) override;
 };
+
+class ColorButtonPlugin final : public QObject, public QDesignerCustomWidgetInterface
+{
+	Q_OBJECT
+	Q_INTERFACES(QDesignerCustomWidgetInterface)
+public:
+	ColorButtonPlugin(QObject* parent) : QObject{parent}{}
+	// QDesignerCustomWidgetInterface interface
+	QString name() const override;
+	QString group() const override;
+	QString toolTip() const override;
+	QString whatsThis() const override;
+	QString includeFile() const override;
+	QIcon icon() const override;
+	bool isContainer() const override;
+	QWidget* createWidget(QWidget* parent) override;
+};

mercurial