#include "circlelabel.h"

#include <QPainter>
#include <QMouseEvent>


CircleLabel::CircleLabel(QWidget *parent)
    : QLabel(parent)
{
}

CircleLabel::CircleLabel(QPixmap pixmap, QWidget *parent)
    : QLabel(parent)
{
    setPixmap(pixmap);
}

CircleLabel::~CircleLabel()
{
}

void CircleLabel::setPixmap(QPixmap pixmap)
{
    mPixmap = pixmap;
    update();
}

void CircleLabel::paintEvent(QPaintEvent *event)
{
   QPainter palinter(this);

   palinter.setRenderHint(QPainter::Antialiasing, true);
   palinter.setRenderHint(QPainter::SmoothPixmapTransform);

   int radius = qMin(width(), height()) / 2;
   QPoint center(width() / 2, height() / 2);

   palinter.setPen(QPen(Qt::transparent, -1, Qt::SolidLine));
   palinter.setBrush(Qt::white);
   palinter.drawEllipse(center, radius, radius);

   QPixmap pixmapa(mPixmap);

   palinter.drawPixmap(0, 0, radius * 2, radius * 2, pixmapa);
}

void CircleLabel::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
    }
}

void CircleLabel::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        QPushButton *button = qobject_cast<QPushButton *>(this->parent());
        if (button != nullptr) {
            button->setChecked(true);
            Q_EMIT button->clicked();
        }
    }

}
