/*
* Copyright (C) 2020, Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/
#include "commontooltip.h"
#include <QBitmap>
#include <QPainter>
#include "theme.h"
#include <QVBoxLayout>

CommonToolTip::CommonToolTip(QWidget *parent) : QDialog(parent)
{

    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::ToolTip); //设置无边框窗口
    setRoundStyle();
    setMsgLabelStyle();

}

void CommonToolTip::setMsgText(QString msg)
{
    m_msgLabel->setText(msg);
}

void CommonToolTip::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    //底色
    painter.setBrush(theme::backcolcr);
    painter.setPen(theme::boderColor);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    //圆角
    painter.drawRoundedRect(rect, 6, 6);
    QWidget::paintEvent(event);
}

void CommonToolTip::setRoundStyle()
{
    //圆角
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    p.setPen(Qt::NoPen);
    p.setBrush(palette().color(QPalette::Base));
    p.drawRoundedRect(bmp.rect(),6,6);
    setMask(bmp);
}

void CommonToolTip::setMsgLabelStyle()
{
    QSize size  = QSize(50,32);
    this->setMinimumSize(size);
    QVBoxLayout *mainvbox=new QVBoxLayout(this);
    this->setLayout(mainvbox);
    m_msgLabel = new QLabel(this);
    m_msgLabel->setMinimumSize(size);
    mainvbox->addWidget(m_msgLabel);

}
