HAL
color_selection.cpp
Go to the documentation of this file.
2 
3 #include <QPainter>
4 #include <QColor>
5 #include <QColorDialog>
6 
7 namespace hal {
8 
9  ColorSelection::ColorSelection(const QString& col, const QString &labl, bool bullet, QWidget* parent)
10  : QPushButton(" ", parent), mColorName(col), mLabel(labl), mBullet(bullet)
11  {
12  setMaximumSize(QSize(32,24));
14  connect(this,&QPushButton::clicked,this,&ColorSelection::execColorDialog);
15  }
16 
18  {
19  Q_UNUSED(evt);
20  QPainter painter(this);
21 
22  painter.setPen(QPen(Qt::black,0));
23  QRectF r = rect();
24  if (mBullet)
25  {
26  painter.setBrush(QBrush(QColor(mColorName)));
27  int delta = (r.width() - r.height()) / 2;
28  if (delta > 0)
29  {
30  r.setLeft(r.left() + delta);
31  r.setRight(r.right() - delta);
32  }
33  else if (delta < 0)
34  {
35  r.setTop(r.top() - delta);
36  r.setBottom(r.bottom() + delta);
37  }
38  painter.drawEllipse(r);
39  }
40  else
41  {
42  painter.drawRect(r);
43  painter.setPen(QPen(QColor(mColorName),5.));
44  int yc = r.top() + r.height()/2;
45  painter.drawLine(r.left(),yc,r.right(),yc);
46  }
47  }
48 
49  void ColorSelection::execColorDialog()
50  {
51  QColor currentColor = QColor(mColorName);
52  QColor selectedColor = QColorDialog::getColor(currentColor, this, "Select color for " + mLabel);
53  if (selectedColor.isValid() && selectedColor != currentColor)
54  {
55  mColorName = selectedColor.name();
56  Q_EMIT colorChanged(mColorName);
57  }
58  }
59 
60 }
void colorChanged(QString colorName)
void paintEvent(QPaintEvent *evt) override
ColorSelection(const QString &col, const QString &labl, bool bullet, QWidget *parent=nullptr)
void clicked(bool checked)
bool isValid() const const
QString name() const const
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QColorDialog::ColorDialogOptions options)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void drawEllipse(const QRectF &rectangle)
void drawLine(const QLineF &line)
void drawRect(const QRectF &rectangle)
void setBrush(const QBrush &brush)
void setPen(const QColor &color)
qreal bottom() const const
qreal height() const const
qreal left() const const
qreal right() const const
void setBottom(qreal y)
void setLeft(qreal x)
void setRight(qreal x)
void setTop(qreal y)
qreal top() const const
qreal width() const const
void setAutoFillBackground(bool enabled)
void setMaximumSize(const QSize &)