SeExpr
ExprColorSwatch.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <sstream>
3#include <algorithm>
4
5#include <QColorDialog>
6#include <QDoubleValidator>
7#include <QGraphicsSceneMouseEvent>
8#include <QHBoxLayout>
9#include <QVBoxLayout>
10#include <QGridLayout>
11#include <QResizeEvent>
12#include <QPushButton>
13#include <QDialogButtonBox>
14#include <QPainter>
15#include <QMenu>
16#include <QLabel>
17
19#ifdef SEEXPR_USE_QDGUI
20#include <qdgui/QdColorPickerDialog.h>
21#endif
22
23#include "ExprColorSwatch.h"
24
25// Simple color frame for swatch
26ExprColorFrame::ExprColorFrame(SeExpr2::Vec3d value, QWidget *parent) : QFrame(parent), _value(value) {
28 setFrameStyle(QFrame::Box | QFrame::Plain);
29 QPalette pal = palette();
30 pal.setColor(backgroundRole(), pal.highlight().color());
31 setPalette(pal);
32 setAutoFillBackground(true);
33}
34
36 _color = QColor(int(255 * value[0] + 0.5), int(255 * value[1] + 0.5), int(255 * value[2] + 0.5));
37 _value = value;
38 update();
39}
40
42
43void ExprColorFrame::paintEvent(QPaintEvent *event) {
44 Q_UNUSED(event);
45 QPainter p(this);
46 p.fillRect(contentsRect(), _color);
47}
48
49void ExprColorFrame::mouseReleaseEvent(QMouseEvent *event) {
50 if (event->button() == Qt::RightButton)
51 deleteSwatchMenu(event->pos());
52 else {
53
54#ifdef SEEXPR_USE_QDGUI
55 QColor color = QdColorPickerDialog::chooseColorFromDialog(_color, this);
56#else
57 QColor color = QColorDialog::getColor(_color);
58#endif
59
60 if (color.isValid()) {
61 _value[0] = color.red() / 255.0;
62 _value[1] = color.green() / 255.0;
63 _value[2] = color.blue() / 255.0;
64 update();
65 _color = color;
67 emit swatchChanged(color);
68 }
69 }
70}
71
72void ExprColorFrame::deleteSwatchMenu(const QPoint &pos) {
73 QMenu *menu = new QMenu(this);
74 QAction *deleteAction = menu->addAction("Delete Swatch");
75 menu->addAction("Cancel");
76 QAction *action = menu->exec(mapToGlobal(pos));
77 if (action == deleteAction) emit deleteSwatch(this);
78}
79
80// Simple color widget with or without index label
81ExprColorWidget::ExprColorWidget(SeExpr2::Vec3d value, int index, bool indexLabel, QWidget *parent) : QWidget(parent) {
83 _colorFrame->setFixedWidth(32);
84 _colorFrame->setFixedHeight(16);
85
86 QVBoxLayout *vbox = new QVBoxLayout();
87 vbox->setContentsMargins(0, 0, 0, 0);
88 vbox->setSpacing(0);
89 vbox->addWidget(_colorFrame);
90
91 if (indexLabel) {
92 std::stringstream indexSS;
93 indexSS << index;
94 QLabel *label = new QLabel(indexSS.str().c_str());
95 vbox->addWidget(label);
96 }
97
98 setLayout(vbox);
99 // emit swatchAdded(index, val);
100}
101
103
104// Grid layout of color swatches
105ExprColorSwatchWidget::ExprColorSwatchWidget(bool indexLabel, QWidget *parent)
106 : QWidget(parent), _columns(8), _indexLabel(indexLabel) {
107 QHBoxLayout *hboxLayout = new QHBoxLayout();
108 hboxLayout->setContentsMargins(0, 0, 0, 0);
109 setLayout(hboxLayout);
110
111 QPushButton *addBtn = new QPushButton("+");
112 addBtn->setFixedWidth(16);
113 addBtn->setFixedHeight(16);
114 QVBoxLayout *swatchControlLayout = new QVBoxLayout();
115 swatchControlLayout->setContentsMargins(0, 0, 0, 0);
116 QHBoxLayout *addRemoveBtnLayout = new QHBoxLayout();
117 addRemoveBtnLayout->setContentsMargins(0, 0, 0, 0);
118 addRemoveBtnLayout->setSpacing(0);
119 addRemoveBtnLayout->addWidget(addBtn);
120 swatchControlLayout->addLayout(addRemoveBtnLayout);
121 swatchControlLayout->addStretch();
122
123 QHBoxLayout *paletteLayout = new QHBoxLayout();
124 paletteLayout->setContentsMargins(0, 0, 0, 0);
125 QWidget *colorGrid = new QWidget();
126 colorGrid->setMinimumWidth(256);
127 _gridLayout = new QGridLayout();
128 _gridLayout->setContentsMargins(0, 0, 0, 0);
129 _gridLayout->setSpacing(0);
130 paletteLayout->addLayout(_gridLayout);
131 paletteLayout->addStretch();
132 colorGrid->setLayout(paletteLayout);
133
134 hboxLayout->addWidget(colorGrid);
135 hboxLayout->addLayout(swatchControlLayout);
136 hboxLayout->addStretch();
137
138 // SIGNALS
139 connect(addBtn, SIGNAL(clicked()), this, SLOT(addNewColor()));
140}
141
143 SeExpr2::Vec3d val(.5);
144 addSwatch(val, -1);
145}
146
148 if (index == -1 || index > _gridLayout->count()) index = _gridLayout->count();
149 ExprColorWidget *widget = new ExprColorWidget(val, index, _indexLabel, this);
150 ExprColorFrame *swatchFrame = widget->getColorFrame();
151 _gridLayout->addWidget(widget, index / _columns, index % _columns);
152 connect(swatchFrame, SIGNAL(swatchChanged(QColor)), this, SLOT(internalSwatchChanged(QColor)));
153 connect(swatchFrame, SIGNAL(deleteSwatch(ExprColorFrame *)), this, SLOT(removeSwatch(ExprColorFrame *)));
154 emit swatchAdded(index, val);
155}
156
158 Q_UNUSED(newcolor);
159 ExprColorFrame *swatchFrame = (ExprColorFrame *)sender();
160 SeExpr2::Vec3d value = swatchFrame->getValue();
161 int index = _gridLayout->indexOf(swatchFrame->parentWidget());
163}
164
166 QWidget *parentWidget = widget->parentWidget();
167 // Find given widget to remove from grid layout
168 for (int i = 0; i < _gridLayout->count(); i++) {
169 if (_gridLayout->itemAt(i)->widget() == parentWidget) {
170 _gridLayout->removeWidget(parentWidget);
171 parentWidget->deleteLater();
172 emit swatchRemoved(i);
173 break;
174 }
175 }
176}
177
179 if (index >= 0 && index < _gridLayout->count()) {
180 SeExpr2::Vec3d newColor(color.redF(), color.greenF(), color.blueF());
181 QLayoutItem *layoutItem = _gridLayout->itemAt(index);
182 if (layoutItem && layoutItem->widget()) {
183 QWidget *widget = layoutItem->widget();
184 ExprColorFrame *cFrame = ((ExprColorWidget *)widget)->getColorFrame();
185 cFrame->setValue(newColor);
186 }
187 }
188}
189
191 if (index >= 0 && index < _gridLayout->count()) {
192 QLayoutItem *layoutItem = _gridLayout->itemAt(index);
193 if (layoutItem && layoutItem->widget()) {
194 QWidget *widget = layoutItem->widget();
195 ExprColorFrame *cFrame = ((ExprColorWidget *)widget)->getColorFrame();
196 SeExpr2::Vec3d val = cFrame->getValue();
197 return QColor::fromRgbF(val[0], val[1], val[2], 1);
198 }
199 }
200 return QColor();
201}
static const int p[514]
Definition NoiseTables.h:20
void selValChangedSignal(SeExpr2::Vec3d value)
void deleteSwatchMenu(const QPoint &pos)
ExprColorFrame(SeExpr2::Vec3d value, QWidget *parent=0)
void swatchChanged(QColor color)
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void paintEvent(QPaintEvent *event)
SeExpr2::Vec3d _value
void setValue(const SeExpr2::Vec3d &value)
SeExpr2::Vec3d getValue() const
void deleteSwatch(ExprColorFrame *swatch)
QGridLayout * _gridLayout
void swatchAdded(int index, SeExpr2::Vec3d val)
void swatchRemoved(int index)
void setSwatchColor(int index, QColor color)
void swatchChanged(int index, SeExpr2::Vec3d val)
void removeSwatch(ExprColorFrame *)
ExprColorSwatchWidget(bool indexLabel, QWidget *parent=0)
void addSwatch(SeExpr2::Vec3d &val, int index=-1)
void internalSwatchChanged(QColor color)
QColor getSwatchColor(int index)
ExprColorFrame * getColorFrame()
ExprColorFrame * _colorFrame
ExprColorWidget(SeExpr2::Vec3d value, int index, bool indexLabel, QWidget *parent)
The result is computed int int< br >< div style="margin-left: 40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed). &nbsp
For any rgb or hsl value(except for negative s values)