OGS
ColorPickerPushButton Class Reference

Detailed Description

ColorPickerPushButton calls a QColorDialog on clicking and then sends a colorPicked(QColor) signal. It also saves the last color and sets its background color accordingly.

Definition at line 17 of file ColorPickerPushButton.h.

#include <ColorPickerPushButton.h>

Inheritance diagram for ColorPickerPushButton:
[legend]
Collaboration diagram for ColorPickerPushButton:
[legend]

Public Slots

void mouseReleaseEvent (QMouseEvent *e) override
 Calls the QColorDialog.
void setColor (QColor color)
 Sets the color.
void setColor (double *color)

Signals

void colorPicked (QColor)
 Is emitted when a color was picked from the dialog.

Public Member Functions

 ColorPickerPushButton (QWidget *parent=nullptr)

Private Member Functions

QString colorToCss (QColor color)
QString colorToString (QColor color)

Private Attributes

QColor _color

Constructor & Destructor Documentation

◆ ColorPickerPushButton()

ColorPickerPushButton::ColorPickerPushButton ( QWidget * parent = nullptr)
explicit

Definition at line 9 of file ColorPickerPushButton.cpp.

10 : QPushButton(parent)
11{
12 setAutoFillBackground(true);
13 _color = QColor("white");
14}

References _color.

Member Function Documentation

◆ colorPicked

void ColorPickerPushButton::colorPicked ( QColor )
signal

Is emitted when a color was picked from the dialog.

Referenced by mouseReleaseEvent().

◆ colorToCss()

QString ColorPickerPushButton::colorToCss ( QColor color)
private

Definition at line 30 of file ColorPickerPushButton.cpp.

31{
32 QString colorStr = "rgb";
33 colorStr.append(colorToString(color));
34
35 return colorStr;
36}
QString colorToString(QColor color)

References colorToString().

Referenced by setColor().

◆ colorToString()

QString ColorPickerPushButton::colorToString ( QColor color)
private

Definition at line 38 of file ColorPickerPushButton.cpp.

39{
40 QString colorStr = "(";
41 colorStr.append(QString::number(color.red()));
42 colorStr.append(", ");
43 colorStr.append(QString::number(color.green()));
44 colorStr.append(", ");
45 colorStr.append(QString::number(color.blue()));
46 colorStr.append(")");
47
48 return colorStr;
49}

Referenced by colorToCss(), and setColor().

◆ mouseReleaseEvent

void ColorPickerPushButton::mouseReleaseEvent ( QMouseEvent * e)
overrideslot

Calls the QColorDialog.

Definition at line 16 of file ColorPickerPushButton.cpp.

17{
18 Q_UNUSED(e);
19 QColor newColor = QColorDialog::getColor(_color, nullptr, "Choose a color");
20 if (!newColor.isValid())
21 {
22 return;
23 }
24
25 setColor(newColor);
26
27 emit colorPicked(_color);
28}
void colorPicked(QColor)
Is emitted when a color was picked from the dialog.
void setColor(QColor color)
Sets the color.

References _color, colorPicked(), and setColor().

◆ setColor [1/2]

void ColorPickerPushButton::setColor ( double * color)
slot

Definition at line 76 of file ColorPickerPushButton.cpp.

77{
78 setColor(QColor::fromRgbF(color[0], color[1], color[2]));
79}

References setColor().

◆ setColor [2/2]

void ColorPickerPushButton::setColor ( QColor color)
slot

Sets the color.

Definition at line 51 of file ColorPickerPushButton.cpp.

52{
53 _color = color;
54
55 // Compute text color
56 QColor hsv = _color.toHsv();
57 QString textColorStr;
58 if (hsv.valueF() < 0.5f)
59 {
60 textColorStr = "color: rgb(255, 255, 255);";
61 }
62 else
63 {
64 textColorStr = "color: rgb(0, 0, 0);";
65 }
66
67 QString stylesheetStr = "background-color: ";
68 stylesheetStr.append(colorToCss(_color));
69 stylesheetStr.append(";");
70 stylesheetStr.append(textColorStr);
71 this->setStyleSheet(stylesheetStr);
72
73 this->setText(colorToString(_color));
74}
QString colorToCss(QColor color)

References _color, colorToCss(), and colorToString().

Referenced by mouseReleaseEvent(), and setColor().

Member Data Documentation

◆ _color

QColor ColorPickerPushButton::_color
private

Definition at line 36 of file ColorPickerPushButton.h.

Referenced by ColorPickerPushButton(), mouseReleaseEvent(), and setColor().


The documentation for this class was generated from the following files: