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 
18 #include <SeExpr2/ExprBuiltins.h>
19 #ifdef SEEXPR_USE_QDGUI
20 #include <qdgui/QdColorPickerDialog.h>
21 #endif
22 
23 #include "ExprColorSwatch.h"
24 
25 // Simple color frame for swatch
26 ExprColorFrame::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 
43 void ExprColorFrame::paintEvent(QPaintEvent *event) {
44  Q_UNUSED(event);
45  QPainter p(this);
46  p.fillRect(contentsRect(), _color);
47 }
48 
49 void 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 
72 void 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
81 ExprColorWidget::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
105 ExprColorSwatchWidget::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());
162  emit swatchChanged(index, value);
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 }
ExprColorSwatchWidget::_columns
int _columns
Definition: ExprColorSwatch.h:80
index
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
SeExpr2::Vec< double, 3, false >
ExprColorWidget::ExprColorWidget
ExprColorWidget(SeExpr2::Vec3d value, int index, bool indexLabel, QWidget *parent)
Definition: ExprColorSwatch.cpp:81
ExprColorSwatchWidget::internalSwatchChanged
void internalSwatchChanged(QColor color)
Definition: ExprColorSwatch.cpp:157
ExprColorFrame::swatchChanged
void swatchChanged(QColor color)
ExprColorFrame
Definition: ExprColorSwatch.h:12
ExprColorWidget::getColorFrame
ExprColorFrame * getColorFrame()
Definition: ExprColorSwatch.cpp:102
ExprColorSwatchWidget::addNewColor
void addNewColor()
Definition: ExprColorSwatch.cpp:142
ExprColorSwatchWidget::getSwatchColor
QColor getSwatchColor(int index)
Definition: ExprColorSwatch.cpp:190
ExprColorFrame::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: ExprColorSwatch.cpp:49
ExprColorSwatchWidget::setSwatchColor
void setSwatchColor(int index, QColor color)
Definition: ExprColorSwatch.cpp:178
ExprColorFrame::deleteSwatch
void deleteSwatch(ExprColorFrame *swatch)
ExprColorFrame::deleteSwatchMenu
void deleteSwatchMenu(const QPoint &pos)
Definition: ExprColorSwatch.cpp:72
ExprColorSwatchWidget::swatchChanged
void swatchChanged(int index, SeExpr2::Vec3d val)
ExprColorSwatchWidget::removeSwatch
void removeSwatch(ExprColorFrame *)
Definition: ExprColorSwatch.cpp:165
ExprColorFrame::_color
QColor _color
Definition: ExprColorSwatch.h:39
ExprColorSwatchWidget::_gridLayout
QGridLayout * _gridLayout
Definition: ExprColorSwatch.h:79
ExprColorSwatchWidget::swatchAdded
void swatchAdded(int index, SeExpr2::Vec3d val)
ExprColorWidget::_colorFrame
ExprColorFrame * _colorFrame
Definition: ExprColorSwatch.h:51
ExprBuiltins.h
ExprColorFrame::selValChangedSignal
void selValChangedSignal(SeExpr2::Vec3d value)
ExprColorFrame::_value
SeExpr2::Vec3d _value
Definition: ExprColorSwatch.h:38
value
For any rgb or hsl value(except for negative s values)
ExprColorFrame::getValue
SeExpr2::Vec3d getValue() const
Definition: ExprColorSwatch.cpp:41
ExprColorSwatchWidget::_indexLabel
bool _indexLabel
Definition: ExprColorSwatch.h:81
ExprColorSwatchWidget::addSwatch
void addSwatch(SeExpr2::Vec3d &val, int index=-1)
Definition: ExprColorSwatch.cpp:147
ExprColorWidget
Definition: ExprColorSwatch.h:44
ExprColorFrame::setValue
void setValue(const SeExpr2::Vec3d &value)
Definition: ExprColorSwatch.cpp:35
ExprColorSwatchWidget::swatchRemoved
void swatchRemoved(int index)
p
static const int p[514]
Definition: NoiseTables.h:20
ExprColorSwatch.h
ExprColorFrame::ExprColorFrame
ExprColorFrame(SeExpr2::Vec3d value, QWidget *parent=0)
Definition: ExprColorSwatch.cpp:26
ExprColorSwatchWidget::ExprColorSwatchWidget
ExprColorSwatchWidget(bool indexLabel, QWidget *parent=0)
Definition: ExprColorSwatch.cpp:105
ExprColorFrame::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition: ExprColorSwatch.cpp:43