量化交易之QT篇 - IController - 接口类(stable版)

// IController.h

#ifndef ICONTROLLER_H
#define ICONTROLLER_H

#include <QMainWindow>
#include <QSettings>

#include "TQZModel/AccountModel.h"

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);


protected:
    QMap<QString, AccountModel>* accountModels(QString settingPath="./config/iniTest.ini");


private:
    bool m_moveable;
    QPoint z;

    QMap<QString, AccountModel>* m_accountModels;

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"
#include "TQZModel/AccountModel.h"

#include <QMap>


IController::IController(QWidget *parent) :
    QMainWindow(parent),
    m_moveable(false),
    m_accountModels(nullptr)
{

}


QMap<QString, AccountModel>* IController::accountModels(QString settingPath) {
    if (this->m_accountModels == nullptr) {
        this->m_accountModels = new QMap<QString, AccountModel>();

        QSettings setting(settingPath, QSettings::IniFormat);

        QStringList groupList = setting.childGroups();
        foreach (const QString &group, groupList) {
            QMap<QString, QString> accountMap;

            setting.beginGroup(group);
            foreach (const QString &key, setting.allKeys()) {
                accountMap.insert(key, setting.value(key).toString());
            }
            setting.endGroup();

            this->m_accountModels->insert(group, AccountModel(accountMap));
        }
    }

    return this->m_accountModels;
}


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);
}

void IController::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QBrush(QColor(50, 50, 50, 248)));
    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->m_moveable = true;
}

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

    QWidget::mouseMoveEvent(event);

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


    if (this->isMaximized())
        return;

    this->move(x);
}

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

    QWidget::mouseReleaseEvent(event);

    this->m_moveable = false;
}

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

    QWidget::mouseDoubleClickEvent(event);
}


IController::~IController() {
    if (this->m_accountModels != nullptr) {
        delete m_accountModels;
        this->m_accountModels = nullptr;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值