PeriDyno 1.0.0
Loading...
Searching...
No Matches
QColorWidget.cpp
Go to the documentation of this file.
1#include "QColorWidget.h"
2
3#include <QGridLayout>
4#include <QPainter>
5#include <QColorDialog>
6
7//RenderCore
8#include "Color.h"
9
10namespace dyno
11{
13
14 QColorButton::QColorButton(QWidget* pParent) :
15 QPushButton(pParent),
16 mMargin(5),
17 mRadius(4),
18 mColor(Qt::gray)
19 {
20 setText("");
21 }
22
23 void QColorButton::paintEvent(QPaintEvent* event)
24 {
25 setText("");
26
27 QPushButton::paintEvent(event);
28
29 QPainter Painter(this);
30
31 // Get button rectangle
32 QRect rect = event->rect();
33
34 // Deflate it
35 rect.adjust(mMargin, mMargin, -mMargin, -mMargin);
36
37 // Use anti aliasing
38 Painter.setRenderHint(QPainter::Antialiasing);
39
40 // Rectangle styling
41 Painter.setBrush(QBrush(isEnabled() ? mColor : Qt::lightGray));
42 Painter.setPen(QPen(isEnabled() ? QColor(25, 25, 25) : Qt::darkGray, 0.5));
43
44 // Draw
45 Painter.drawRoundedRect(rect, mRadius, Qt::AbsoluteSize);
46 }
47
48 void QColorButton::mousePressEvent(QMouseEvent* event)
49 {
50 QColorDialog colorDialog;
51
52 connect(&colorDialog, SIGNAL(currentColorChanged(const QColor&)), this, SLOT(onColorChanged(const QColor&)));
53
54 //ColorDialog.setWindowIcon(GetIcon("color--pencil"));
55 colorDialog.setCurrentColor(mColor);
56 colorDialog.adjustSize();
57 colorDialog.exec();
58
59 disconnect(&colorDialog, SIGNAL(currentColorChanged(const QColor&)), this, SLOT(onColorChanged(const QColor&)));
60 }
61
63 {
64 return mMargin;
65 }
66
67 void QColorButton::setMargin(const int& Margin)
68 {
70 update();
71 }
72
74 {
75 return mRadius;
76 }
77
78 void QColorButton::setRadius(const int& Radius)
79 {
81 update();
82 }
83
84 QColor QColorButton::getColor(void) const
85 {
86 return mColor;
87 }
88
89 void QColorButton::setColor(const QColor& Color, bool BlockSignals)
90 {
91 blockSignals(BlockSignals);
92
93 mColor = Color;
94 update();
95
96 blockSignals(false);
97 }
98
100 {
102
103 emit colorChanged(mColor);
104 }
105
108 {
109 QGridLayout* layout = new QGridLayout;
110 layout->setContentsMargins(0, 0, 0, 0);
111 layout->setHorizontalSpacing(3);
112
113 this->setLayout(layout);
114
115 QLabel* name = new QLabel();
116 QString str = FormatFieldWidgetName(field->getObjectName());
117 name->setFixedSize(100, 18);
118 QFontMetrics fontMetrics(name->font());
119 QString elide = fontMetrics.elidedText(str, Qt::ElideRight, 100);
120 name->setText(elide);
121 //Set label tips
122 name->setToolTip(str);
123
124 spinner1 = new QSpinBox;
125 spinner1->setMinimumWidth(30);
126 spinner1->setRange(0, 255);
127
128 spinner2 = new QSpinBox;
129 spinner2->setMinimumWidth(30);
130 spinner2->setRange(0, 255);
131
132 spinner3 = new QSpinBox;
133 spinner3->setMinimumWidth(30);
134 spinner3->setRange(0, 255);
135
136 layout->addWidget(name, 0, 0);
137 layout->addWidget(spinner1, 0, 1);
138 layout->addWidget(spinner2, 0, 2);
139 layout->addWidget(spinner3, 0, 3);
140
142 colorButton->setFixedSize(30, 30);
143
144 layout->addWidget(colorButton, 0, 4);
145
146 std::string template_name = field->getTemplateName();
147 int R = 0;
148 int G = 0;
149 int B = 0;
150
151 if (template_name == std::string(typeid(Color).name()))
152 {
154 auto v = f->getValue();
155
156 int r = int(v.r * 255) % 255;
157 int g = int(v.g * 255) % 255;
158 int b = int(v.b * 255) % 255;
159
160 spinner1->setValue(r);
161 spinner2->setValue(g);
162 spinner3->setValue(b);
163
164 colorButton->setColor(QColor(r, g, b), true);
165 }
166
167 QObject::connect(spinner1, SIGNAL(valueChanged(int)), this, SLOT(updateField(int)));
168 QObject::connect(spinner2, SIGNAL(valueChanged(int)), this, SLOT(updateField(int)));
169 QObject::connect(spinner3, SIGNAL(valueChanged(int)), this, SLOT(updateField(int)));
170
171 QObject::connect(colorButton, SIGNAL(colorChanged(const QColor&)), this, SLOT(updateColorWidget(const QColor&)));
172 }
173
177
179 {
180 int v1 = spinner1->value();
181 int v2 = spinner2->value();
182 int v3 = spinner3->value();
183
184 std::string template_name = field()->getTemplateName();
185
186 if (template_name == std::string(typeid(Color).name()))
187 {
189
190 float r = float(v1) / 255;
191 float g = float(v2) / 255;
192 float b = float(v3) / 255;
193
194 f->setValue(Color(r, g, b));
195 }
196
197 colorButton->setColor(QColor(v1, v2, v3), true);
198 }
199
200 void QColorWidget::updateColorWidget(const QColor& color)
201 {
202 spinner1->blockSignals(true);
203 spinner2->blockSignals(true);
204 spinner3->blockSignals(true);
205
206 spinner1->setValue(color.red());
207 spinner2->setValue(color.green());
208 spinner3->setValue(color.blue());
209
210 spinner1->blockSignals(false);
211 spinner2->blockSignals(false);
212 spinner3->blockSignals(false);
213
214 updateField(0);
215 }
216}
217
#define IMPL_FIELD_WIDGET(_data_type_, _type_)
virtual const std::string getTemplateName()
Definition FBase.h:51
T getValue()
Definition Field.h:130
void setValue(T val)
Definition Field.h:111
QColorButton(QWidget *pParent=NULL)
void setColor(const QColor &Color, bool BlockSignals=false)
virtual void mousePressEvent(QMouseEvent *event)
QColor getColor(void) const
virtual void paintEvent(QPaintEvent *event)
int getMargin(void) const
void colorChanged(const QColor &)
void setRadius(const int &Radius)
int getRadius(void) const
void onColorChanged(const QColor &Color)
void setMargin(const int &Margin)
DECLARE_FIELD_WIDGET QColorWidget(FBase *field)
QColorButton * colorButton
QSpinBox * spinner2
QSpinBox * spinner3
void updateColorWidget(const QColor &color)
QSpinBox * spinner1
QFieldWidget(FBase *field)
TA * cast(TB *b)
Definition Typedef.inl:286
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
QString FormatFieldWidgetName(std::string name)
Definition Format.cpp:9
void disconnect(Node *node, NodePort *port)
Definition NodePort.cpp:44