KSeExpr  4.0.4.0
ExprColorSwatch.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #include <QColorDialog>
7 #include <QDialogButtonBox>
8 #include <QDoubleValidator>
9 #include <QGraphicsSceneMouseEvent>
10 #include <QGridLayout>
11 #include <QHBoxLayout>
12 #include <QLabel>
13 #include <QMenu>
14 #include <QPainter>
15 #include <QPushButton>
16 #include <QResizeEvent>
17 #include <QToolButton>
18 #include <QVBoxLayout>
19 
20 #include <KSeExpr/ExprBuiltins.h>
21 
22 #include "ExprColorSwatch.h"
23 
24 // Simple color frame for swatch
26  : QFrame(parent)
27  , _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), int(255 * value[1] + 0.5), int(255 * value[2] + 0.5));
40  _value = value;
41  update();
42 }
43 
45 {
46  return _value;
47 }
48 
49 void ExprColorFrame::paintEvent(QPaintEvent *)
50 {
51  QPainter p(this);
52  p.fillRect(contentsRect(), _color);
53 }
54 
55 void ExprColorFrame::mouseReleaseEvent(QMouseEvent *event)
56 {
57  if (event->button() == Qt::RightButton)
58  deleteSwatchMenu(event->pos());
59  else {
60  QColor color = QColorDialog::getColor(_color);
61 
62  if (color.isValid()) {
63  _value[0] = color.red() / 255.0;
64  _value[1] = color.green() / 255.0;
65  _value[2] = color.blue() / 255.0;
66  update();
67  _color = color;
69  emit swatchChanged(color);
70  }
71  }
72 }
73 
74 void ExprColorFrame::deleteSwatchMenu(const QPoint &pos)
75 {
76  auto *menu = new QMenu(this);
77  QAction *deleteAction = menu->addAction(tr("Delete Swatch"));
78  menu->addAction(tr("Cancel"));
79  QAction *action = menu->exec(mapToGlobal(pos));
80  if (action == deleteAction)
81  emit deleteSwatch(this);
82 }
83 
84 // Simple color widget with or without index label
85 ExprColorWidget::ExprColorWidget(KSeExpr::Vec3d value, int index, bool indexLabel, QWidget *parent)
86  : QWidget(parent)
87 {
88  _colorFrame = new ExprColorFrame(value);
89  _colorFrame->setFixedWidth(32);
90  _colorFrame->setFixedHeight(16);
91 
92  auto *vbox = new QVBoxLayout();
93  vbox->setContentsMargins(0, 0, 0, 0);
94  vbox->setSpacing(0);
95  vbox->addWidget(_colorFrame);
96 
97  if (indexLabel) {
98  auto *label = new QLabel(tr("%1").arg(index));
99  vbox->addWidget(label);
100  }
101 
102  setLayout(vbox);
103  // emit swatchAdded(index, val);
104 }
105 
107 {
108  return _colorFrame;
109 }
110 
111 // Grid layout of color swatches
112 ExprColorSwatchWidget::ExprColorSwatchWidget(bool indexLabel, QWidget *parent)
113  : QWidget(parent)
114  , _columns(8)
115  , _indexLabel(indexLabel)
116 {
117  auto *hboxLayout = new QHBoxLayout();
118  hboxLayout->setContentsMargins(0, 0, 0, 0);
119  setLayout(hboxLayout);
120 
121  auto *addBtn = new QToolButton;
122  addBtn->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
123  auto *detailAction = new QAction(tr("&Add..."));
124  QIcon detailIcon = QIcon::fromTheme("addlayer", QIcon::fromTheme("list-add"));
125  detailAction->setIcon(detailIcon);
126  addBtn->setDefaultAction(detailAction);
127  addBtn->setFixedHeight(16);
128  auto *swatchControlLayout = new QVBoxLayout();
129  swatchControlLayout->setContentsMargins(0, 0, 0, 0);
130  swatchControlLayout->setAlignment(Qt::AlignLeft | Qt::AlignCenter);
131  auto *addRemoveBtnLayout = new QHBoxLayout();
132  addRemoveBtnLayout->setContentsMargins(0, 0, 0, 0);
133  addRemoveBtnLayout->setSpacing(0);
134  addRemoveBtnLayout->addWidget(addBtn);
135  swatchControlLayout->addLayout(addRemoveBtnLayout);
136  swatchControlLayout->addStretch();
137 
138  auto *paletteLayout = new QHBoxLayout();
139  paletteLayout->setContentsMargins(0, 0, 0, 0);
140  auto *colorGrid = new QWidget();
141  colorGrid->setMinimumWidth(256);
142  _gridLayout = new QGridLayout();
143  _gridLayout->setContentsMargins(0, 0, 0, 0);
144  _gridLayout->setSpacing(0);
145  paletteLayout->addLayout(_gridLayout);
146  colorGrid->setLayout(paletteLayout);
147 
148  hboxLayout->addWidget(colorGrid);
149  hboxLayout->addLayout(swatchControlLayout);
150  hboxLayout->addStretch();
151 
152  // SIGNALS
153  connect(addBtn, SIGNAL(triggered(QAction *)), this, SLOT(addNewColor()));
154 }
155 
157 {
158  KSeExpr::Vec3d val(.5);
159  addSwatch(val, -1);
160 }
161 
163 {
164  if (index == -1 || index > _gridLayout->count())
165  index = _gridLayout->count();
166  auto *widget = new ExprColorWidget(val, index, _indexLabel, this);
167  ExprColorFrame *swatchFrame = widget->getColorFrame();
168  _gridLayout->addWidget(widget, index / _columns, index % _columns);
169  connect(swatchFrame, SIGNAL(swatchChanged(QColor)), this, SLOT(internalSwatchChanged(QColor)));
170  connect(swatchFrame, SIGNAL(deleteSwatch(ExprColorFrame *)), this, SLOT(removeSwatch(ExprColorFrame *)));
171  emit swatchAdded(index, val);
172 }
173 
175 {
176  auto *swatchFrame = dynamic_cast<ExprColorFrame *>(sender());
177  KSeExpr::Vec3d value = swatchFrame->getValue();
178  int index = _gridLayout->indexOf(swatchFrame->parentWidget());
179  emit swatchChanged(index, value);
180 }
181 
183 {
184  QWidget *parentWidget = widget->parentWidget();
185  // Find given widget to remove from grid layout
186  for (int i = 0; i < _gridLayout->count(); i++) {
187  if (_gridLayout->itemAt(i)->widget() == parentWidget) {
188  _gridLayout->removeWidget(parentWidget);
189  parentWidget->deleteLater();
190  emit swatchRemoved(i);
191  break;
192  }
193  }
194 }
195 
196 void ExprColorSwatchWidget::setSwatchColor(int index, QColor color)
197 {
198  if (index >= 0 && index < _gridLayout->count()) {
199  KSeExpr::Vec3d newColor(color.redF(), color.greenF(), color.blueF());
200  QLayoutItem *layoutItem = _gridLayout->itemAt(index);
201  if (layoutItem && layoutItem->widget()) {
202  QWidget *widget = layoutItem->widget();
203  auto *cFrame = (dynamic_cast<ExprColorWidget *>(widget))->getColorFrame();
204  cFrame->setValue(newColor);
205  }
206  }
207 }
208 
210 {
211  if (index >= 0 && index < _gridLayout->count()) {
212  QLayoutItem *layoutItem = _gridLayout->itemAt(index);
213  if (layoutItem && layoutItem->widget()) {
214  QWidget *widget = layoutItem->widget();
215  ExprColorFrame *cFrame = (dynamic_cast<ExprColorWidget *>(widget))->getColorFrame();
216  KSeExpr::Vec3d val = cFrame->getValue();
217  return QColor::fromRgbF(val[0], val[1], val[2], 1);
218  }
219  }
220  return {};
221 }
static constexpr std::array< int, 514 > p
Definition: NoiseTables.h:10
void setValue(const KSeExpr::Vec3d &value)
void deleteSwatchMenu(const QPoint &pos)
void swatchChanged(QColor color)
void mouseReleaseEvent(QMouseEvent *event) override
void selValChangedSignal(KSeExpr::Vec3d value)
ExprColorFrame(KSeExpr::Vec3d value, QWidget *parent=nullptr)
KSeExpr::Vec3d getValue() const
KSeExpr::Vec3d _value
void paintEvent(QPaintEvent *event) override
void deleteSwatch(ExprColorFrame *swatch)
QGridLayout * _gridLayout
void swatchRemoved(int index)
void setSwatchColor(int index, QColor color)
void removeSwatch(ExprColorFrame *)
void internalSwatchChanged(QColor color)
ExprColorSwatchWidget(bool indexLabel, QWidget *parent=nullptr)
void swatchAdded(int index, KSeExpr::Vec3d val)
void addSwatch(KSeExpr::Vec3d &val, int index=-1)
void swatchChanged(int index, KSeExpr::Vec3d val)
QColor getSwatchColor(int index)
ExprColorWidget(KSeExpr::Vec3d value, int index, bool indexLabel, QWidget *parent)
ExprColorFrame * getColorFrame()
ExprColorFrame * _colorFrame