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 28 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 20 of file ColorPickerPushButton.cpp.

21 : QPushButton(parent)
22{
23 setAutoFillBackground(true);
24 _color = QColor("white");
25}

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 41 of file ColorPickerPushButton.cpp.

42{
43 QString colorStr = "rgb";
44 colorStr.append(colorToString(color));
45
46 return colorStr;
47}
QString colorToString(QColor color)

References colorToString().

Referenced by setColor().

◆ colorToString()

QString ColorPickerPushButton::colorToString ( QColor color)
private

Definition at line 49 of file ColorPickerPushButton.cpp.

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}

Referenced by colorToCss(), and setColor().

◆ mouseReleaseEvent

void ColorPickerPushButton::mouseReleaseEvent ( QMouseEvent * e)
overrideslot

Calls the QColorDialog.

Definition at line 27 of file ColorPickerPushButton.cpp.

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}
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 87 of file ColorPickerPushButton.cpp.

88{
89 setColor(QColor::fromRgbF(color[0], color[1], color[2]));
90}

References setColor().

◆ setColor [2/2]

void ColorPickerPushButton::setColor ( QColor color)
slot

Sets the color.

Definition at line 62 of file ColorPickerPushButton.cpp.

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}
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 47 of file ColorPickerPushButton.h.

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


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