OGS
ColorPickerPushButton.cpp
Go to the documentation of this file.
1 
15 // ** INCLUDES **
16 #include "ColorPickerPushButton.h"
17 
18 #include <QColorDialog>
19 
21  : QPushButton(parent)
22 {
23  setAutoFillBackground(true);
24  _color = QColor("white");
25 }
26 
28 {
29  Q_UNUSED(e);
30  QColor newColor = QColorDialog::getColor(_color, nullptr, "Choose a color");
31  if (!newColor.isValid())
32  {
33  return;
34  }
35 
36  setColor(newColor);
37 
38  emit colorPicked(_color);
39 }
40 
41 QString ColorPickerPushButton::colorToCss(QColor color)
42 {
43  QString colorStr = "rgb";
44  colorStr.append(colorToString(color));
45 
46  return colorStr;
47 }
48 
50 {
51  QString colorStr = "(";
52  colorStr.append(QString::number(color.red()));
53  colorStr.append(", ");
54  colorStr.append(QString::number(color.green()));
55  colorStr.append(", ");
56  colorStr.append(QString::number(color.blue()));
57  colorStr.append(")");
58 
59  return colorStr;
60 }
61 
63 {
64  _color = color;
65 
66  // Compute text color
67  QColor hsv = _color.toHsv();
68  QString textColorStr;
69  if (hsv.valueF() < 0.5f)
70  {
71  textColorStr = "color: rgb(255, 255, 255);";
72  }
73  else
74  {
75  textColorStr = "color: rgb(0, 0, 0);";
76  }
77 
78  QString stylesheetStr = "background-color: ";
79  stylesheetStr.append(colorToCss(_color));
80  stylesheetStr.append(";");
81  stylesheetStr.append(textColorStr);
82  this->setStyleSheet(stylesheetStr);
83 
84  this->setText(colorToString(_color));
85 }
86 
88 {
89  setColor(QColor::fromRgbF(color[0], color[1], color[2]));
90 }
Definition of the ColorPickerPushButton class.
void mouseReleaseEvent(QMouseEvent *e) override
Calls the QColorDialog.
void colorPicked(QColor)
Is emitted when a color was picked from the dialog.
ColorPickerPushButton(QWidget *parent=nullptr)
void setColor(QColor color)
Sets the color.
QString colorToString(QColor color)
QString colorToCss(QColor color)
Color getColor(const std::string &id, std::map< std::string, Color > &colors)
Definition: Color.cpp:36