#include "TitleBar.h"

#include <QPainter>
#include <QDebug>

#include "ui_TitleBar.h"

TitleBar::TitleBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TitleBar)
{
    ui->setupUi(this);

    background_ = ":/res/title.png";
    qDebug() << "load default background:" << backgroundPixmap_.load(background_);
}

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

void TitleBar::setOpacity(float opacity) {
    opacity_ = opacity;
    update();
}

void TitleBar::setBackground(const QString& background) {
    if (background.isEmpty() || background == background_) {
        return;
    }

    if (!backgroundPixmap_.load(background)) {
        qDebug() << "load background failed:" << background;
        return;
    }

    background_ = background;
    update();
}

void TitleBar::paintEvent(QPaintEvent* event) {
    if (backgroundPixmap_.isNull()) {
        return;
    }

    QPainter painter(this);

    painter.setOpacity(0.5);
    const QRect& r = rect();
    painter.drawPixmap(r, backgroundPixmap_, backgroundPixmap_.rect());
}