ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
gradientcombobox.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "gradientcombobox.h"
9 
10 #include <QDebug>
11 #include <QHBoxLayout>
12 #include <QLabel>
13 #include <QListWidget>
14 #include <QPainter>
15 #include <QStylePainter>
16 
17 namespace Widgets {
18 
19 struct GradientData {
20  GradientData(const QString& name, const QColor& clr1, const QColor& clr2) {
21  this->name = name;
22  this->color1 = clr1;
23  this->color2 = clr2;
24  }
25 
26  GradientData(const GradientData& other) {
27  this->name = other.name;
28  this->color1 = other.color1;
29  this->color2 = other.color2;
30  }
31 
32  QString name;
33  QColor color1;
34  QColor color2;
35 };
36 
37 typedef QList<GradientData> GradientDataList;
38 
39 class GradientLabel : public QWidget {
40 public:
41  explicit GradientLabel(QWidget* parent = nullptr);
42  void setColors(const QColor& clr1, const QColor& clr2);
43 
44  QColor color1() const { return m_color1; }
45  QColor color2() const { return m_color2; }
46 
47 protected:
48  void paintEvent(QPaintEvent*);
49 
50 private:
51  QColor m_color1;
52  QColor m_color2;
53 };
54 
55 class GradientWidgetItem : public QWidget {
56 public:
57  GradientWidgetItem(const GradientData& gradient, QWidget* parent = nullptr);
58 
59  QString name() const { return m_gradientData.name; }
60  QColor color1() const { return m_gradientData.color1; }
61  QColor color2() const { return m_gradientData.color2; }
62 
63 private:
64  GradientData m_gradientData;
65 };
66 
68  QWidget* parent)
69  : QWidget(parent), m_gradientData(gradient) {
70  QHBoxLayout* layout = new QHBoxLayout;
71  // the label's text will overlap with qlistwidgetitem's text, so comment it
72  // out
73  QLabel* nameLabel = new QLabel(/*gradient.name, */ this);
74  nameLabel->setMinimumWidth(120);
75  nameLabel->setSizePolicy(
76  QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum));
77  GradientLabel* gLabel = new GradientLabel(this);
78  gLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
79  gLabel->setColors(gradient.color1, gradient.color2);
80  layout->addWidget(nameLabel);
81  layout->addWidget(gLabel);
82  layout->setSpacing(1);
83  layout->setContentsMargins(2, 2, 2, 2);
84  setLayout(layout);
85 }
86 
87 GradientLabel::GradientLabel(QWidget* parent) : QWidget(parent) {}
88 
89 void GradientLabel::setColors(const QColor& clr1, const QColor& clr2) {
90  m_color1 = clr1;
91  m_color2 = clr2;
92  update();
93 }
94 
95 void GradientLabel::paintEvent(QPaintEvent* event) {
96  Q_UNUSED(event)
97  QPainter painter(this);
98  painter.setPen(Qt::NoPen);
99  QLinearGradient lg(rect().topLeft(), rect().topRight());
100  lg.setColorAt(0.0, m_color1);
101  lg.setColorAt(1.0, m_color2);
102  painter.setBrush(QBrush(lg));
103  painter.drawRect(rect());
104 }
105 
106 class GradientComboBoxPrivate {
107 public:
108  GradientComboBoxPrivate(GradientComboBox* q);
109 
110  GradientComboBox* q_ptr;
111  static GradientDataList sGradientList;
112  QListWidget* gradientItemList;
113 
114  QMap<QListWidgetItem*, GradientWidgetItem*> listItem2GradientItemMap;
115 };
116 
117 #define USE_COLOR_SCHEME_NO1
118 #ifdef USE_COLOR_SCHEME_NO1
119 GradientDataList GradientComboBoxPrivate::sGradientList =
120  GradientDataList() << GradientData(QObject::tr("Fifty shades of grey"),
121  QColor(189, 195, 199),
122  QColor(44, 62, 80))
123  << GradientData(QObject::tr("Dusk"),
124  QColor(255, 216, 155),
125  QColor(25, 84, 123))
126  << GradientData(QObject::tr("Miami Dolphins"),
127  QColor(77, 160, 176),
128  QColor(211, 157, 56))
129  << GradientData(QObject::tr("Superman"),
130  QColor(0, 153, 247),
131  QColor(241, 23, 18))
132  << GradientData(QObject::tr("Timber"),
133  QColor(252, 0, 255),
134  QColor(0, 219, 222))
135  << GradientData(QObject::tr("Lizard"),
136  QColor(48, 67, 82),
137  QColor(215, 210, 204))
138  << GradientData(QObject::tr("Ukraine"),
139  QColor(0, 79, 249),
140  QColor(255, 249, 76))
141  << GradientData(QObject::tr("Starfall"),
142  QColor(240, 194, 123),
143  QColor(75, 18, 72))
144  << GradientData(QObject::tr("Kyoto"),
145  QColor(194, 21, 0),
146  QColor(255, 197, 0))
147  << GradientData(QObject::tr("Miaka"),
148  QColor(252, 53, 76),
149  QColor(10, 191, 188))
150  << GradientData(QObject::tr("Calm Darya"),
151  QColor(95, 44, 130),
152  QColor(73, 160, 157))
153  << GradientData(QObject::tr("Mantle"),
154  QColor(36, 198, 220),
155  QColor(81, 74, 157))
156  << GradientData(QObject::tr("Opa"),
157  QColor(61, 126, 170),
158  QColor(255, 228, 122))
159  << GradientData(QObject::tr("Horizon"),
160  QColor(0, 57, 115),
161  QColor(229, 229, 190))
162  << GradientData(QObject::tr("Rose Water"),
163  QColor(229, 93, 135),
164  QColor(95, 195, 228))
165  << GradientData(QObject::tr("Harmonic Energy"),
166  QColor(22, 160, 133),
167  QColor(244, 208, 63))
168  << GradientData(QObject::tr("Purple Paradise"),
169  QColor(29, 43, 100),
170  QColor(248, 205, 218))
171  << GradientData(QObject::tr("Aqua Marine"),
172  QColor(26, 41, 128),
173  QColor(38, 208, 206))
174  << GradientData(QObject::tr("Calm Darya"),
175  QColor(95, 44, 130),
176  QColor(73, 160, 157))
177  << GradientData(QObject::tr("Bora Bora"),
178  QColor(43, 192, 228),
179  QColor(234, 236, 198))
180  << GradientData(QObject::tr("Winter"),
181  QColor(230, 218, 218),
182  QColor(39, 64, 70))
183  << GradientData(QObject::tr("Bright Vault"),
184  QColor(0, 210, 255),
185  QColor(146, 141, 171))
186  << GradientData(QObject::tr("Sunset"),
187  QColor(11, 72, 107),
188  QColor(245, 98, 23))
189  << GradientData(QObject::tr("Sherbert"),
190  QColor(247, 157, 0),
191  QColor(100, 243, 140))
192  << GradientData(QObject::tr("Deep Sea Space"),
193  QColor(44, 62, 80),
194  QColor(76, 161, 175))
195  << GradientData(QObject::tr("Transfile"),
196  QColor(22, 191, 253),
197  QColor(203, 48, 102))
198  << GradientData(QObject::tr("Ali"),
199  QColor(255, 75, 31),
200  QColor(31, 221, 255))
201  << GradientData(QObject::tr("Alihossein"),
202  QColor(247, 255, 0),
203  QColor(219, 54, 164))
204  << GradientData(QObject::tr("Christmas"),
205  QColor(47, 115, 54),
206  QColor(170, 58, 56))
207  << GradientData(QObject::tr("Minnesota Vikings"),
208  QColor(86, 20, 176),
209  QColor(219, 214, 92))
210  << GradientData(QObject::tr("Pizelex"),
211  QColor(17, 67, 87),
212  QColor(242, 148, 146))
213  << GradientData(QObject::tr("Netflix"),
214  QColor(142, 14, 0),
215  QColor(31, 28, 24))
216  << GradientData(QObject::tr("Green and Blue"),
217  QColor(194, 229, 156),
218  QColor(100, 179, 244))
219  << GradientData(QObject::tr("Fresh Turboscent"),
220  QColor(241, 242, 181),
221  QColor(19, 80, 88))
222  << GradientData(QObject::tr("Purple Bliss"),
223  QColor(54, 0, 51),
224  QColor(11, 135, 147))
225  << GradientData(QObject::tr("Martini"),
226  QColor(253, 252, 71),
227  QColor(36, 254, 65))
228  << GradientData(QObject::tr("Shore"),
229  QColor(112, 225, 245),
230  QColor(255, 209, 148))
231  << GradientData(QObject::tr("Earthly"),
232  QColor(100, 145, 115),
233  QColor(219, 213, 164))
234  << GradientData(QObject::tr("Titanium"),
235  QColor(40, 48, 72),
236  QColor(133, 147, 152))
237  << GradientData(QObject::tr("Sun on the Horizon"),
238  QColor(252, 234, 187),
239  QColor(248, 181, 0))
240  << GradientData(QObject::tr("Grapefruit Sunset"),
241  QColor(233, 100, 67),
242  QColor(144, 78, 149))
243  << GradientData(QObject::tr("Politics"),
244  QColor(33, 150, 243),
245  QColor(244, 67, 54))
246  << GradientData(QObject::tr("Sweet Morning"),
247  QColor(255, 95, 109),
248  QColor(255, 195, 113))
249  << GradientData(QObject::tr("Forest"),
250  QColor(90, 63, 55),
251  QColor(44, 119, 68))
252  << GradientData(QObject::tr("Back to the Future"),
253  QColor(192, 36, 37),
254  QColor(240, 203, 53))
255  << GradientData(QObject::tr("Dark Knight"),
256  QColor(186, 139, 2),
257  QColor(24, 24, 24))
258  << GradientData(QObject::tr("Green to dark"),
259  QColor(106, 145, 19),
260  QColor(20, 21, 23))
261  << GradientData(QObject::tr("Virgin America"),
262  QColor(123, 67, 151),
263  QColor(220, 36, 48))
264  << GradientData(QObject::tr("Turquoise flow"),
265  QColor(19, 106, 138),
266  QColor(38, 120, 113))
267  << GradientData(QObject::tr("Portrait"),
268  QColor(142, 158, 171),
269  QColor(238, 242, 243))
270  << GradientData(QObject::tr("Flickr"),
271  QColor(255, 0, 132),
272  QColor(51, 0, 27))
273  << GradientData(QObject::tr("Predawn"),
274  QColor(255, 161, 127),
275  QColor(0, 34, 62))
276  << GradientData(QObject::tr("Crazy Orange I"),
277  QColor(211, 131, 18),
278  QColor(168, 50, 121))
279  << GradientData(QObject::tr("ServQuick"),
280  QColor(72, 85, 99),
281  QColor(41, 50, 60))
282  << GradientData(QObject::tr("Cheer Up Emo Kid"),
283  QColor(85, 98, 112),
284  QColor(255, 107, 107))
285  << GradientData(QObject::tr("Man of Steel"),
286  QColor(120, 2, 6),
287  QColor(6, 17, 97))
288  << GradientData(QObject::tr("Moor"),
289  QColor(97, 97, 97),
290  QColor(155, 197, 195))
291  << GradientData(QObject::tr("Forever Lost"),
292  QColor(93, 65, 87),
293  QColor(168, 202, 186))
294  << GradientData(QObject::tr("Dracula"),
295  QColor(220, 36, 36),
296  QColor(74, 86, 157))
297  << GradientData(QObject::tr("Moss"),
298  QColor(19, 78, 94),
299  QColor(113, 178, 128))
300  << GradientData(QObject::tr("Lemon Twist"),
301  QColor(60, 165, 92),
302  QColor(181, 172, 73))
303  << GradientData(QObject::tr("Frozen"),
304  QColor(64, 59, 74),
305  QColor(231, 233, 187))
306  << GradientData(QObject::tr("Emerald Water"),
307  QColor(52, 143, 80),
308  QColor(86, 180, 211))
309  << GradientData(QObject::tr("Mirage"),
310  QColor(22, 34, 42),
311  QColor(58, 96, 115))
312  << GradientData(QObject::tr("Shroom Haze"),
313  QColor(92, 37, 141),
314  QColor(67, 137, 162))
315  << GradientData(QObject::tr("Venice Blue"),
316  QColor(8, 80, 120),
317  QColor(133, 216, 206));
318 #else
319 GradientDataList GradientComboBoxPrivate::sGradientList =
320  GradientDataList() << GradientData(QObject::tr("Alpine"),
321  QColor(71, 92, 123),
322  QColor(250, 249, 228))
323  << GradientData(QObject::tr("Lake"),
324  QColor(85, 32, 150),
325  QColor(238, 231, 213))
326  << GradientData(QObject::tr("Army"),
327  QColor(117, 151, 123),
328  QColor(192, 190, 163))
329  << GradientData(QObject::tr("Mint"),
330  QColor(135, 248, 173),
331  QColor(233, 159, 201))
332  << GradientData(QObject::tr("Atlantic"),
333  QColor(41, 51, 52),
334  QColor(125, 130, 162))
335  << GradientData(QObject::tr("Neon"),
336  QColor(185, 222, 75),
337  QColor(205, 54, 193))
338  << GradientData(QObject::tr("Aurora"),
339  QColor(57, 60, 82),
340  QColor(212, 77, 243))
341  << GradientData(QObject::tr("Pearl"),
342  QColor(234, 220, 208),
343  QColor(239, 206, 250))
344  << GradientData(QObject::tr("Avocado"),
345  QColor(0, 18, 3),
346  QColor(251, 249, 57))
347  << GradientData(QObject::tr("Plum"),
348  QColor(24, 4, 6),
349  QColor(226, 220, 113))
350  << GradientData(QObject::tr("Beach"),
351  QColor(220, 137, 68),
352  QColor(236, 238, 252))
353  << GradientData(QObject::tr("Rose"),
354  QColor(58, 84, 33),
355  QColor(179, 38, 31))
356  << GradientData(QObject::tr("Candy"),
357  QColor(115, 53, 87),
358  QColor(169, 221, 225))
359  << GradientData(QObject::tr("Solar"),
360  QColor(132, 4, 3),
361  QColor(255, 205, 30))
362  << GradientData(QObject::tr("CMYK"),
363  QColor(90, 183, 235),
364  QColor(46, 43, 44))
365  << GradientData(QObject::tr("South West"),
366  QColor(119, 76, 47),
367  QColor(93, 153, 209))
368  << GradientData(QObject::tr("Deep Sea"),
369  QColor(49, 2, 91),
370  QColor(191, 233, 255))
371  << GradientData(QObject::tr("Starry Night"),
372  QColor(28, 47, 61),
373  QColor(244, 207, 96))
374  << GradientData(QObject::tr("Fall"),
375  QColor(67, 101, 98),
376  QColor(245, 200, 61))
377  << GradientData(QObject::tr("Sunset"),
378  QColor(31, 11, 42),
379  QColor(255, 245, 215))
380  << GradientData(QObject::tr("Fruit Punch"),
381  QColor(255, 130, 0),
382  QColor(243, 88, 137))
383  << GradientData(QObject::tr("Thermometer"),
384  QColor(53, 56, 217),
385  QColor(140, 26, 45))
386  << GradientData(QObject::tr("Island"),
387  QColor(184, 103, 69),
388  QColor(171, 202, 83))
389  << GradientData(QObject::tr("Watermelon"),
390  QColor(42, 48, 34),
391  QColor(227, 98, 98));
392 // << GradientData(QObject::tr(""), QColor(), QColor())
393 // << GradientData(QObject::tr(""), QColor(),
394 // QColor());
395 #endif
396 
397 GradientComboBoxPrivate::GradientComboBoxPrivate(GradientComboBox* q)
398  : q_ptr(q) {
399  gradientItemList = new QListWidget(q_ptr);
400  gradientItemList->setAlternatingRowColors(true);
401  q_ptr->connect(q_ptr, SIGNAL(activated(int)), q_ptr,
402  SLOT(onActivated(int)));
403 
404  for (int i = 0; i < sGradientList.size(); ++i) {
405  const GradientData& gd = sGradientList.at(i);
406  QListWidgetItem* listItem = new QListWidgetItem;
407  listItem->setText(gd.name);
408 
409  GradientWidgetItem* gwi = new GradientWidgetItem(gd, gradientItemList);
410  gradientItemList->addItem(listItem);
411  gradientItemList->setItemWidget(listItem, gwi);
412 
413  listItem2GradientItemMap.insert(listItem, gwi);
414  }
415 
416  // setModel & setView call order matters
417  q_ptr->setModel(gradientItemList->model());
418  q_ptr->setView(gradientItemList);
419 
420  // set default row
421  gradientItemList->setCurrentRow(0);
422 }
423 
424 GradientComboBox::GradientComboBox(QWidget* parent) : QComboBox(parent) {
425  d_ptr = new GradientComboBoxPrivate(this);
426 }
427 
429 
431  d_ptr->gradientItemList->setCurrentRow(index);
432  QListWidgetItem* currItem = d_ptr->gradientItemList->currentItem();
433  GradientWidgetItem* gwi = d_ptr->listItem2GradientItemMap.value(currItem);
434  if (gwi) setCurrentText(gwi->name());
435  emit activated(index);
436 }
437 
439  return d_ptr->gradientItemList->currentRow();
440 }
441 
443  QListWidgetItem* currItem = d_ptr->gradientItemList->currentItem();
444  if (!currItem) return QString();
445 
446  return d_ptr->listItem2GradientItemMap.value(currItem)->name();
447 }
448 
450  QListWidgetItem* currItem = d_ptr->gradientItemList->currentItem();
451  if (!currItem) return QColor();
452 
453  return d_ptr->listItem2GradientItemMap.value(currItem)->color1();
454 }
455 
457  QListWidgetItem* currItem = d_ptr->gradientItemList->currentItem();
458  if (!currItem) return QColor();
459 
460  return d_ptr->listItem2GradientItemMap.value(currItem)->color2();
461 }
462 
463 QPair<QColor, QColor> GradientComboBox::colorPair(int index) {
464  if (0 <= index && index < GradientComboBoxPrivate::sGradientList.size())
465  return qMakePair(
466  GradientComboBoxPrivate::sGradientList.at(index).color1,
467  GradientComboBoxPrivate::sGradientList.at(index).color2);
468  else
469  return qMakePair(QColor(), QColor());
470 }
471 
473  QComboBox::showPopup();
474  QRect qRect(view()->geometry());
475  int xPos = qRect.width() - d_ptr->gradientItemList->width();
476  int yPos = qRect.height() - d_ptr->gradientItemList->height();
477  d_ptr->gradientItemList->move(xPos, yPos);
478  d_ptr->gradientItemList->doItemsLayout();
479  d_ptr->gradientItemList->setVisible(true);
480 }
481 
483  QComboBox::hidePopup();
484  d_ptr->gradientItemList->setVisible(false);
485  int currRow = d_ptr->gradientItemList->currentRow();
486  emit currentIndexChanged(currRow);
487 }
488 
489 void GradientComboBox::paintEvent(QPaintEvent* e) {
490  QListWidgetItem* item = d_ptr->gradientItemList->currentItem();
491  if (!item) {
492  QComboBox::paintEvent(e);
493  return;
494  }
495 
496  QStylePainter painter(this);
497  painter.setPen(palette().color(QPalette::Text));
498 
499  // draw the combobox frame, focusrect and selected etc.
500  QStyleOptionComboBox opt;
501  initStyleOption(&opt);
502  painter.drawComplexControl(QStyle::CC_ComboBox, opt);
503 
504  GradientWidgetItem* gwi = d_ptr->listItem2GradientItemMap.value(item);
505 
506  QRect gradientRect(QPoint(125, 2), QPoint(width() - 20, height() - 4));
507  QLinearGradient lg(gradientRect.topLeft(), gradientRect.topRight());
508  lg.setColorAt(0.0, gwi->color1());
509  lg.setColorAt(1.0, gwi->color2());
510  painter.setBrush(QBrush(lg));
511  painter.drawRect(gradientRect);
512 
513  // draw the icon and text
514  painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
515 }
516 
517 void GradientComboBox::resizeEvent(QResizeEvent* e) {
518  QComboBox::resizeEvent(e);
519 
520  QRect qRect(view()->geometry());
521  int xPos = qRect.width() - d_ptr->gradientItemList->width();
522  int yPos = qRect.height() - d_ptr->gradientItemList->height();
523  d_ptr->gradientItemList->move(xPos, yPos);
524 }
525 
526 QSize GradientComboBox::minimumSizeHint() const { return QSize(240, 20); }
527 
528 void GradientComboBox::onActivated(int index) {
529  d_ptr->gradientItemList->setCurrentRow(index);
530  QListWidgetItem* currItem = d_ptr->gradientItemList->item(index);
531  GradientWidgetItem* gwi = d_ptr->listItem2GradientItemMap.value(currItem);
532  setCurrentText(gwi->name());
533  update();
534  emit colorsChanged(gwi->name(), gwi->color1(), gwi->color2());
535 }
536 
537 } // namespace Widgets
MouseEvent event
int width
int size
int height
math::float4 color
void resizeEvent(QResizeEvent *e)
static QPair< QColor, QColor > colorPair(int index)
void colorsChanged(const QString &name, const QColor &clr1, const QColor &clr2)
void paintEvent(QPaintEvent *e)
void setColors(const QColor &clr1, const QColor &clr2)
void paintEvent(QPaintEvent *)
GradientLabel(QWidget *parent=nullptr)
GradientWidgetItem(const GradientData &gradient, QWidget *parent=nullptr)
const double * e
QList< GradientData > GradientDataList
Tensor Minimum(const Tensor &input, const Tensor &other)
Computes the element-wise minimum of input and other. The tensors must have same data type and device...
Rgb at(size_t color_id)
GradientData(const QString &name, const QColor &clr1, const QColor &clr2)
GradientData(const GradientData &other)