量化交易之QT篇 - IController

// IController.h

#ifndef ICONTROLLER_H
#define ICONTROLLER_H

#include <QMainWindow>


class IController : public QMainWindow
{
    Q_OBJECT
public:
    explicit IController(QWidget *parent = nullptr);
    virtual ~IController();

protected:
    virtual void ResetWindow(double widthScale, double heightScale);

private:
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    virtual void mouseDoubleClickEvent(QMouseEvent *event);

    virtual void paintEvent(QPaintEvent *event);


private:
    double originHeightScale;
    double originWidthScale;
    QPoint z;
    bool isMove;

signals:

};

#endif // ICONTROLLER_H
// IController.cpp

#include "IController.h"
#include "ui_mainwindow.h"

#include <QScreen>
#include <QMouseEvent>
#include <QDebug>
#include <QPainter>

#include <QWidget>

#include "IController.h"

// ワンピースは実在する
IController::IController(QWidget *parent) :
    QMainWindow(parent),
    originHeightScale(0),
    originWidthScale(0),
    isMove(false) {

}


void IController::ResetWindow(double widthScale, double heightScale) {
    widthScale = (widthScale > 1) ? 1 : widthScale;
    heightScale = (heightScale > 1) ? 1 : heightScale;

    QScreen *screen = qApp->primaryScreen();

    int screenWidth = screen->size().width();
    int screenHeight = screen->size().height();
    this->resize(screenWidth * widthScale, screenHeight * heightScale);
    this->move(screenWidth * (1 - widthScale) * 0.5, screenHeight * (1 - heightScale) * 0.5);

    this->setAttribute(Qt::WA_TranslucentBackground);


    this->originHeightScale = !this->originHeightScale ? heightScale : this->originHeightScale;
    this->originWidthScale = !this->originWidthScale ? widthScale : this->originWidthScale;
}

void IController::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QBrush(QColor(50, 50, 50, 238)));
    painter.setPen(Qt::transparent);

    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    painter.drawRoundedRect(rect, 6, 6);

    QWidget::paintEvent(event);
}

void IController::mousePressEvent(QMouseEvent *event) {
    if (event->button() != Qt::LeftButton)
        return;

    QWidget::mousePressEvent(event);

    QPoint y = event->globalPos();
    QPoint x = this->geometry().topLeft();
    this->z = y - x;

    this->isMove = true;
}


void IController::mouseMoveEvent(QMouseEvent *event) {
    if (!this->isMove)
        return;

    QWidget::mouseMoveEvent(event);

    QPoint y = event->globalPos();
    QPoint x = y - this->z;

    this->move(x);
}

void IController::mouseReleaseEvent(QMouseEvent *event) {
    if (event->button() != Qt::LeftButton)
        return;

    QWidget::mouseReleaseEvent(event);

    this->isMove = false;
}

void IController::mouseDoubleClickEvent(QMouseEvent *event) {
    if (event->button() != Qt::LeftButton)
        return;

    QWidget::mouseDoubleClickEvent(event);

    this->ResetWindow(this->originWidthScale, this->originHeightScale);
}


IController::~IController() {

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值