1 /**
2  * Mandelbulber v2, a 3D fractal generator       ,=#MKNmMMKmmßMNWy,
3  *                                             ,B" ]L,,p%%%,,,§;, "K
4  * Copyright (C) 2016-21 Mandelbulber Team     §R-==%w["'~5]m%=L.=~5N
5  *                                        ,=mm=§M ]=4 yJKA"/-Nsaj  "Bw,==,,
6  * This file is part of Mandelbulber.    §R.r= jw",M  Km .mM  FW ",§=ß., ,TN
7  *                                     ,4R =%["w[N=7]J '"5=],""]]M,w,-; T=]M
8  * Mandelbulber is free software:     §R.ß~-Q/M=,=5"v"]=Qf,'§"M= =,M.§ Rz]M"Kw
9  * you can redistribute it and/or     §w "xDY.J ' -"m=====WeC=\ ""%""y=%"]"" §
10  * modify it under the terms of the    "§M=M =D=4"N #"%==A%p M§ M6  R' #"=~.4M
11  * GNU General Public License as        §W =, ][T"]C  §  § '§ e===~ U  !§[Z ]N
12  * published by the                    4M",,Jm=,"=e~  §  §  j]]""N  BmM"py=ßM
13  * Free Software Foundation,          ]§ T,M=& 'YmMMpM9MMM%=w=,,=MT]M m§;'§,
14  * either version 3 of the License,    TWw [.j"5=~N[=§%=%W,T ]R,"=="Y[LFT ]N
15  * or (at your option)                   TW=,-#"%=;[  =Q:["V""  ],,M.m == ]N
16  * any later version.                      J§"mr"] ,=,," =="""J]= M"M"]==ß"
17  *                                          §= "=C=4 §"eM "=B:m|4"]#F,§~
18  * Mandelbulber is distributed in            "9w=,,]w em%wJ '"~" ,=,,ß"
19  * the hope that it will be useful,                 . "K=  ,=RMMMßM"""
20  * but WITHOUT ANY WARRANTY;                            .'''
21  * without even the implied warranty
22  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  * See the GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with Mandelbulber. If not, see <http://www.gnu.org/licenses/>.
27  *
28  * ###########################################################################
29  *
30  * Authors: Sebastian Jennen (jenzebas@gmail.com)
31  *
32  * MyColorButton class - promoted QPushButton widget with context menu
33  */
34 
35 #include "my_color_button.h"
36 
37 #include <QtWidgets/QtWidgets>
38 
39 #include "src/animation_flight.hpp"
40 #include "src/system_data.hpp"
41 
MyColorButton(QWidget * parent)42 MyColorButton::MyColorButton(QWidget *parent) : QPushButton(parent), CommonMyWidgetWrapper(this)
43 {
44 	int size = systemData.GetPreferredThumbnailSize() * 0.6;
45 
46 	defaultValue = sRGB();
47 	currentValue = sRGB();
48 	setText("");
49 	w = size;
50 	h = size / 4;
51 	SetupColor();
52 }
53 
~MyColorButton()54 MyColorButton::~MyColorButton()
55 {
56 	// nothing to delete
57 }
58 
resetToDefault()59 void MyColorButton::resetToDefault()
60 {
61 	currentValue = defaultValue;
62 	UpdateColor();
63 }
64 
GetDefault()65 sRGB MyColorButton::GetDefault()
66 {
67 	if (parameterContainer && !gotDefault)
68 	{
69 		defaultValue = parameterContainer->GetDefault<sRGB>(parameterName);
70 		setToolTipText();
71 		gotDefault = true;
72 	}
73 	return defaultValue;
74 }
75 
getDefaultAsString()76 QString MyColorButton::getDefaultAsString()
77 {
78 	return QString("rgb(%1, %2, %3)")
79 		.arg(QString::number(defaultValue.R / 256), QString::number(defaultValue.G / 256),
80 			QString::number(defaultValue.B / 256));
81 }
82 
getFullParameterName()83 QString MyColorButton::getFullParameterName()
84 {
85 	return parameterName;
86 }
87 
GetColor() const88 sRGB MyColorButton::GetColor() const
89 {
90 	return currentValue;
91 }
92 
SetColor(sRGB newColor)93 void MyColorButton::SetColor(sRGB newColor)
94 {
95 	currentValue = newColor;
96 	UpdateColor();
97 	emit valueChanged();
98 }
99 
UpdateColor()100 void MyColorButton::UpdateColor()
101 {
102 	GetDefault();
103 	QColor color(currentValue.R / 256, currentValue.G / 256, currentValue.B / 256);
104 	painter->fillRect(QRect(0, 0, w, h), color);
105 	painter->drawRect(0, 0, w - 1, h - 1);
106 
107 	if (!(defaultValue.R / 256 == currentValue.R / 256 && defaultValue.G / 256 == currentValue.G / 256
108 				&& defaultValue.B / 256 == currentValue.B / 256))
109 	{
110 		// not (almost) default color -> make border thicker to indicate non default value
111 		painter->drawRect(1, 1, w - 3, h - 3);
112 	}
113 
114 	QIcon icon(*pix);
115 	setIcon(icon);
116 	setIconSize(QSize(w, h));
117 }
118 
SetupColor()119 void MyColorButton::SetupColor()
120 {
121 	pix.reset(new QPixmap(w, h));
122 	painter.reset(new QPainter(pix.get()));
123 }
124 
mousePressEvent(QMouseEvent * event)125 void MyColorButton::mousePressEvent(QMouseEvent *event)
126 {
127 	QPushButton::mousePressEvent(event);
128 
129 	Qt::MouseButton button = event->button();
130 
131 	if (button == Qt::LeftButton)
132 	{
133 		QColorDialog colorDialog(this);
134 		colorDialog.setOption(QColorDialog::DontUseNativeDialog);
135 		sRGB colorRGB = GetColor();
136 		QColor color(colorRGB.R / 256, colorRGB.G / 256, colorRGB.B / 256);
137 		colorDialog.setCurrentColor(color);
138 		colorDialog.setWindowTitle(tr("Edit color: %1").arg(parameterName));
139 		if (colorDialog.exec() == QDialog::Accepted)
140 		{
141 			color = colorDialog.currentColor();
142 			colorRGB = sRGB(color.red() * 256, color.green() * 256, color.blue() * 256);
143 			SetColor(colorRGB);
144 			emit valueChanged();
145 		}
146 	}
147 }
148 
contextMenuEvent(QContextMenuEvent * event)149 void MyColorButton::contextMenuEvent(QContextMenuEvent *event)
150 {
151 	CommonMyWidgetWrapper::contextMenuEvent(event);
152 }
153