#include "eobjectattr.h"
#include "ui_eobjectattr.h"
#include <QDirIterator>

eObjectAttr::eObjectAttr(const QRectF &data, const QRectF &rLimit, QWidget *parent) :
    QGroupBox(parent),
    ui(new Ui::eObjectAttr)
{
    m_rLimit.setX(rLimit.x());
    m_rLimit.setY(rLimit.y());
    m_rLimit.setWidth(rLimit.width());
    m_rLimit.setHeight(rLimit.height());

    ui->setupUi(this);
    ui->wX->setValue(static_cast<int>(data.x()));
    ui->wY->setValue(static_cast<int>(data.y()));
    ui->wW->setValue(static_cast<int>(data.width()));
    ui->wH->setValue(static_cast<int>(data.height()));
    connect(ui->wX, SIGNAL(valueChanged(int)), this, SLOT(onXChanged(int)));
    connect(ui->wY, SIGNAL(valueChanged(int)), this, SLOT(onYChanged(int)));
    connect(ui->wW, SIGNAL(valueChanged(int)), this, SLOT(onWChanged(int)));
    connect(ui->wH, SIGNAL(valueChanged(int)), this, SLOT(onHChanged(int)));

    QHBoxLayout *hBox = new QHBoxLayout();
    ui->verticalLayout->addLayout(hBox);
    hBox->addWidget(new QLabel("边框:"));
    borderFd = new QComboBox();
    borderFd->setStyleSheet("QComboBox{padding-left:4px; padding-right:4px;}");
    hBox->addWidget(borderFd);
    QDirIterator it(":res/borders/", QDirIterator::Subdirectories);
    int maxImgWidth = 0;
    while(it.hasNext()) {
        QString path = it.next();
        QPixmap img(path);
        if(img.width()>maxImgWidth) maxImgWidth = img.width();
        borderFd->addItem(QIcon(img), QString::number(img.height()));
    }
    borderFd->setIconSize(QSize(maxImgWidth, 24));
    connect(borderFd, SIGNAL(currentIndexChanged(int)), this, SLOT(onHChanged(int)));

    hBox->addWidget(new QLabel("特效:"));
    borderEffFd = new QComboBox();
    borderEffFd->setStyleSheet("QComboBox{padding-left:4px; padding-right:4px}");
    borderEffFd->addItem("静止");
    borderEffFd->addItem("旋转");
    borderEffFd->addItem("闪烁");
    hBox->addWidget(borderEffFd);

    hBox->addStretch();
    ui->verticalLayout->addStretch();

}

eObjectAttr::~eObjectAttr()
{
    delete ui;
}

void eObjectAttr::onXChanged(int n)
{
    qDebug()<<"eObjectAttr::onXChanged(int)";
    if(m_rLimit != INVALID_RECT) {
        int mx = n;
        int mw = ui->wW->value();
        if(mx + mw > m_rLimit.width()) {
            mx = static_cast<int>(m_rLimit.width()) - mw;
            ui->wX->blockSignals(true);
            ui->wX->setValue(mx);
            ui->wX->blockSignals(false);
        }
    }
    onAttrChanged();
}

void eObjectAttr::onYChanged(int n)
{
    qDebug()<<"eObjectAttr::onYChanged(int)";
    if(m_rLimit != INVALID_RECT) {
        int my = n;
        int mh = ui->wH->value();
        if(my + mh > m_rLimit.height()) {
            my = static_cast<int>(m_rLimit.height()) - mh;
            ui->wY->blockSignals(true);
            ui->wY->setValue(my);
            ui->wY->blockSignals(false);
        }
    }
    onAttrChanged();
}

void eObjectAttr::onWChanged(int n)
{
    qDebug()<<"eObjectAttr::onWChanged(int)";
    if(m_rLimit != INVALID_RECT) {
        int mx = ui->wX->value();
        int mw = n;
        if(mx + mw > m_rLimit.width()) {
            mw = static_cast<int>(m_rLimit.width()) - mx;
            ui->wW->blockSignals(true);
            ui->wW->setValue(mw);
            ui->wW->blockSignals(false);
        }
    }
    onAttrChanged();
}

void eObjectAttr::onHChanged(int n)
{
    qDebug()<<"eObjectAttr::onHChanged(int)";
    if(m_rLimit != INVALID_RECT) {
        int my = ui->wY->value();
        int mh = n;
        if(my + mh > m_rLimit.height()) {
            mh = static_cast<int>(m_rLimit.height()) - my;
            ui->wH->blockSignals(true);
            ui->wH->setValue(mh);
            ui->wH->blockSignals(false);
        }
    }
    onAttrChanged();
}

void eObjectAttr::onAttrChanged()
{
    qDebug()<<"eObjectAttr::onAttrChanged()";
    QRectF rect(ui->wX->value(), ui->wY->value(), ui->wW->value(), ui->wH->value());
    emit sAttrChanged(rect);
}

void eObjectAttr::onAttrSetting(const QRectF &rect)
{
    qDebug()<<"eObjectAttr::onAttrSetting(QRectF)";
    ui->wX->setValue(static_cast<int>(rect.x()));
    ui->wY->setValue(static_cast<int>(rect.y()));
    ui->wW->setValue(static_cast<int>(rect.width()));
    ui->wH->setValue(static_cast<int>(rect.height()));
}
