#include "loqtitlebar.h"
#include "QDebug"
LoQTitleBar::LoQTitleBar(QWidget *parent) : QWidget(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);

    bn_Minimize = new QPushButton(this);
    bn_Maximize = new QPushButton(this);
    bn_Close    = new QPushButton(this);

    bn_Minimize->setObjectName("bnMinimize");
    bn_Maximize->setObjectName("bnMaximize");
    bn_Close->setObjectName("bnClose");

    bn_Minimize->setToolTip(tr("Minimize"));
    bn_Maximize->setToolTip(tr("Maximize"));
    bn_Close->setToolTip(tr("Close"));

    connect(bn_Minimize, SIGNAL(clicked(bool)), window(), SLOT(showMinimized()));
    connect(bn_Maximize, SIGNAL(clicked(bool)), this,     SLOT(onBnMaximizeClicked()));
    connect(bn_Close,    SIGNAL(clicked(bool)), window(), SLOT(close()));
}
void LoQTitleBar::hideMixmumButton()
{
    bn_Minimize->hide();

}
void LoQTitleBar::refreshLable()
{
    bn_Minimize->setToolTip(tr("Minimize"));
    bn_Maximize->setToolTip(tr("Maximize"));
    bn_Close->setToolTip(tr("Close"));

}
bool LoQTitleBar::eventFilter(QObject *obj, QEvent *event)
{
    switch (event->type()) {
        case QEvent::WindowStateChange:
        case QEvent::Resize:
            updateMaximize();
            return true;
//        case QEvent::MouseButtonPress: {
//            QMouseEvent *e = static_cast<QMouseEvent*>(event);
//            if(this->geometry().contains(e->pos())){
//                return true;
//            }
//            break;
//        }
        default: break;
    }
    return QWidget::eventFilter(obj, event);
}

void LoQTitleBar::updateMaximize()
{
    QWidget *pWindow = window();
    if (pWindow->isTopLevel()) {
        bool bMaximize = pWindow->isMaximized();
        if (bMaximize) {
            bn_Maximize->setToolTip(tr("Restore"));
        } else {
            bn_Maximize->setToolTip(tr("Maximize"));
        }
        bn_Maximize->setStyle(QApplication::style());
    }
    bn_Minimize->setToolTip(tr("Minimize"));
    bn_Close->setToolTip(tr("Close"));
}

void LoQTitleBar::onBnMaximizeClicked()
{
    QWidget *pWindow = window();
    if(pWindow->isMaximized()) {
        pWindow->showNormal();
        qDebug()<<"showNormal";
    } else {
        pWindow->showMaximized();
        qDebug()<<"showMaximized";
    }
}
