basemainwidget.cpp 1.53 KB
#include "basemainwidget.h"
#include <QPainter>
#include <qmath.h>
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopWidget>

BaseMainWidget::BaseMainWidget(QWidget *parent) :
    QFrame(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    this->setAutoFillBackground(true);
}
void BaseMainWidget::paintEvent(QPaintEvent *)
{
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
    int cut = 1;
    path.addRect(cut, cut, this->width()-cut*2, this->height()-cut*2);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillPath(path, QBrush(Qt::white));

    QColor color("#ffffff");
    for(int i=0; i<cut; i++)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);
        path.addRect(cut-i, cut-i, this->width()-(cut-i)*2, this->height()-(cut-i)*2);
        color.setAlpha(150 - qSqrt(i)*50);
        painter.setPen(color);
        painter.drawPath(path);
    }

}

void BaseMainWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void BaseMainWidget::mouseReleaseEvent(QMouseEvent *)
{
    if (geometry().y() < QApplication::desktop()->geometry().top()) {
        move(geometry().x(), QApplication::desktop()->geometry().top());
    }
}

void BaseMainWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() &Qt::LeftButton) {
        move(event->globalPos() - dragPosition);
        event->accept();
    }
}