量化交易之QT篇 - IController

// IController.h

#ifndef ICONTROLLER_H
#define ICONTROLLER_H

#include 


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 
#include 
#include 
#include 

#include 

#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() {

}

你可能感兴趣的:(QT,qt,ui,开发语言,linux)