#include "wattrview.h"
#include "ui_wattrview.h"
#include <wProgramManager/wEditProgram/wpageitem.h>
#include <wProgramManager/wEditProgram/wpageattr.h>
#include <wProgramManager/wEditProgram/wElement/eobject.h>

wAttrView::wAttrView(QWidget *parent) :
    QTabWidget(parent),
    ui(new Ui::wAttrView),
    m_page(nullptr),
    m_element(nullptr)
{
    setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    ui->wPage->setWidget(wEmpty());
    ui->wElement->setWidget(wEmpty());
    setTabText(0, tr("widget properties"));
    setTabText(1, tr("Page properties"));
}

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

void wAttrView::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

QWidget* wAttrView::wEmpty()
{
    QWidget *w = new QWidget();
    w->setProperty("ssBackground", "dark");
    w->setAttribute(Qt::WA_DeleteOnClose);
    QVBoxLayout *playout = new QVBoxLayout();
    playout->addStretch(1);
    w->setLayout(playout);
    return w;
}
//左边幻灯片选中后端响应函数
void wAttrView::onSlotPageSelected(wPageItem *page)
{
    setCurrentIndex(1);
    if(m_page != page)//如果先前的属性页不是当前选中的幻灯片，切换ui的幻灯片属性页为新选中的属性
    {
        m_page = page;
        if(nullptr != m_page) {
            ui->wPage->setWidget(m_page->wAttr());
        } else {
            ui->wPage->setWidget(wEmpty());
        }
    }
}

void wAttrView::onSlotElementSelected(eObject *element)
{
    if(m_element != element) {
        m_element = element;
        if(nullptr != m_element)//如果先前的媒体元素不是当前选中的媒体元素，切换ui的媒体元素属性页为新选中的属性
        {
            setCurrentIndex(0);
            ui->wElement->takeWidget()->close();
            ui->wElement->setWidget(m_element->wAttr());//该widget赋值新的widget时，会析构掉上一个widget
        } else {
            setCurrentIndex(1);
            ui->wElement->setWidget(wEmpty());
        }
    }
}
