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