#include "logic.h"
#include "cwmlabelasix.h"
#include <QPainter>
#include <QResizeEvent>

CWMLabelAsix::CWMLabelAsix(QWidget* parent) :QLabel(parent)
{
    m_nAngle = 0;
    installEventFilter(this);
}


CWMLabelAsix::~CWMLabelAsix(void)
{
}

bool CWMLabelAsix::eventFilter(QObject* watched, QEvent* event)
{
    //if (watched == this && event->type() == QEvent::Resize)
    //{
    //    QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
    //    int newWidth = resizeEvent->size().width();
    //    int newHeight = resizeEvent->size().height();
    //    int step = 2; // 定义步长为2
    //    newWidth = (newWidth / step) * step;
    //    newHeight = (newHeight / step) * step;
    //    this->setFixedSize(newWidth, newHeight);
    //    return true; // 返回true表示事件已处理，不需要进一步传递
    //}
    return QWidget::eventFilter(watched, event);
}

void CWMLabelAsix::paintEvent(QPaintEvent* pEvent)
{
    UNUSED(pEvent);

    QPainter painter(this);
    painter.save();

    painter.setRenderHint(QPainter::Antialiasing, true);

    if (rect().width() % 2 != 0 || rect().height() % 2 != 0)
    {
        setFixedSize(rect().width() / 2 * 2, rect().height() / 2 * 2);
    }

    int size = 12;
    QRect rc = rect().adjusted(size, size, -size, -size);
    int r = qMin(rc.width(), rc.height()) / 2;
    rc.setRect(rect().center().x() - r, rect().center().y() - r, r * 2, r * 2);
    //MqDebug() << rect().width() << rect().height();

	QPoint psCenter = rect().center();
    QTransform tf;
    tf.translate(psCenter.x(), psCenter.y());
    tf.rotate(m_nAngle);
    tf.translate(-psCenter.x(), -psCenter.y());
    painter.setTransform(tf);

    /*painter.setPen(Qt::darkBlue);
    painter.setBrush(Qt::darkBlue);*/


    painter.setPen(QColor(108, 112, 124));
    painter.setBrush(QColor(108, 112, 124));
    painter.drawEllipse(psCenter, r, r);
    painter.fillRect(QRect(rc.left() - size, rc.center().y() - size / 2, size, size), Qt::gray);
    painter.fillRect(QRect(rc.center().x() - size / 2, rc.top() - size, size, size), Qt::gray);
    painter.fillRect(QRect(rc.right(), rc.center().y() - size / 2, size, size), Qt::gray);
    painter.fillRect(QRect(rc.center().x() - size / 2, rc.bottom(), size, size), Qt::gray);

    if (m_bHasWafer)
    {
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::darkGreen);
        painter.drawEllipse(psCenter, static_cast<int>(r * 0.8), static_cast<int>(r * 0.8));
    }

    painter.restore();
}

void CWMLabelAsix::setAngle(int nAngle)
{
    m_nAngle = nAngle;
    this->update();
}

void CWMLabelAsix::SetWafer(bool bHasWafer)
{
    m_bHasWafer = bHasWafer;
}
