#include "mainwindow.h" #include "ui_mainwindow.h" #include "resultview.h" #include "titlebar.h" #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setMouseTracking(true);//鼠标追踪 initWidget(); signalAndSlot(); } MainWindow::~MainWindow() { if (m_title) { delete m_title; m_title = nullptr; } if (m_result) { delete m_result; m_result = nullptr; } delete ui; qDebug() << __FUNCTION__ << "line:" << __LINE__ << " ~MainWindow success!"; } void MainWindow::initWidget() { qDebug() << __FUNCTION__ << __LINE__ << "start"; QDir dir = QApplication::applicationDirPath(); currPath = dir.path(); setWindowFlags(Qt::Window|Qt::FramelessWindowHint|Qt::WindowSystemMenuHint|Qt::MSWindowsFixedSizeDialogHint |Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint); setStyleSheet("QWidget{background-color: rgb(175, 175, 175);}" "QToolTip{border-style: none;color:black; font-size:12px;}"); m_title = new TitleBar(this); m_title->setMouseTracking(true); m_title->setPath(currPath); m_title->setFixedHeight(38); m_title->setStyleSheet("TitleBar{background-color: rgb(180, 165, 130);border:0px;}"); m_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); // 加载界面 m_result = new ResultView(this); QFont m_Font; m_Font.setFamily("Microsoft YaHei"); m_Font.setPixelSize(15); m_result->my_SetFont(m_Font); m_result->setMouseTracking(true); m_result->setPath(currPath); m_result->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); QVBoxLayout* h_layout = new QVBoxLayout(); h_layout->addWidget(m_title); h_layout->addWidget(m_result); h_layout->setStretchFactor(m_title, 1); h_layout->setStretchFactor(m_result, 9); h_layout->setContentsMargins(5, 5, 5, 5); QWidget *m_widget = new QWidget; m_widget->setMouseTracking(true); delete m_widget->layout(); m_widget->setLayout(h_layout); this->setCentralWidget(m_widget); m_result->LoadSavepath(currPath + QString::fromUtf8("/config/软件加载配置.csv")); connect(m_title, &TitleBar::mouseRecover, this, [=]() { setCursor(Qt::ArrowCursor); }); connect(m_title, &TitleBar::OpenProjectSignal, this, [=](QString path) { m_result->Load_Param_Config(path); setCursor(Qt::ArrowCursor); }); connect(m_title, &TitleBar::SaveProjectSignal, this, [=](QString path) { m_result->Save_Param_Config(path); setCursor(Qt::ArrowCursor); }); connect(m_title, &TitleBar::showStatisticalTableSignal, this, [=]() { setCursor(Qt::ArrowCursor); // 统计报表 m_result->getResultRepot(); }); } void MainWindow::signalAndSlot() { } void MainWindow::mousePressEvent(QMouseEvent *event) { if (Qt::LeftButton == event->button() && m_bCanDragResize) { m_start_drag_point = event->globalPos(); } return QWidget::mousePressEvent(event); } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (Qt::LeftButton & event->buttons()) { if (m_bCanDragResize) { if( m_emHorizontal == DRAG_LOCK_RIGHT && m_emVertical == DRAG_LOCK_TOP ) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl,rb); if(rb.x() - gloPoint.x() <= minimumWidth()) rMove.setX(tl.x()); else rMove.setX(gloPoint.x()); if(rb.y() - gloPoint.y() <= minimumHeight() ) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y()-tl.y()); } else rMove.setY(gloPoint.y()); setGeometry(rMove); } else if( m_emHorizontal == DRAG_LOCK_RIGHT && m_emVertical == DRAG_LOCK_BOTTOM ) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl,rb); if(rb.x() - gloPoint.x() <= minimumWidth()) rMove.setX(tl.x()); else rMove.setX(gloPoint.x()); if(rb.y() - gloPoint.y() <= minimumHeight() ) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y()-tl.y()); } else rMove.setY(gloPoint.y()); setGeometry(rMove); } else if( m_emHorizontal == DRAG_LOCK_LEFT && m_emVertical == DRAG_LOCK_BOTTOM ) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl,rb); if(rb.x() - gloPoint.x() <= minimumWidth()) { rMove.setX(tl.x()); rMove.setWidth(gloPoint.x()-tl.x()); } else rMove.setX(gloPoint.x()); if(rb.y() - gloPoint.y() <= minimumHeight() ) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y()-tl.y()); } else rMove.setY(gloPoint.y()); setGeometry(rMove); } else if(m_emHorizontal == DRAG_LOCK_RIGHT) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl,rb); if(rb.x() - gloPoint.x() <= minimumWidth()) rMove.setX(tl.x()); else rMove.setX(gloPoint.x()); this->setGeometry(rMove); } else if ( m_emVertical == DRAG_LOCK_BOTTOM ) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl,rb); if(rb.y() - gloPoint.y() <= minimumHeight() ) rMove.setY(tl.y()); else rMove.setY(gloPoint.y()); this->setGeometry(rMove); } else if( m_emVertical == DRAG_LOCK_TOP && m_emHorizontal != DRAG_LOCK_LEFT) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl,rb); qreal y_val = rb.y() - gloPoint.y(); if(y_val <= minimumHeight() ) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y()-tl.y()); } else rMove.setY(gloPoint.y()); qreal screen_h = qApp->desktop()->screenGeometry(this).height(); if(rMove.height()+rMove.y() > screen_h-60) { rMove.setHeight(rMove.height()-30); } this->setGeometry(rMove); } else { const QPoint point_offset = event->globalPos() - m_start_drag_point; m_start_drag_point = event->globalPos(); int nOffsetX1 = DRAG_LOCK_RIGHT == m_emHorizontal ? point_offset.x() : 0; int nOffsetY1 = DRAG_LOCK_BOTTOM == m_emVertical ? point_offset.y() : 0; int nOffsetX2 = DRAG_LOCK_LEFT == m_emHorizontal ? point_offset.x() : 0; int nOffsetY2 = DRAG_LOCK_TOP == m_emVertical ?point_offset.y() : 0; setGeometry(geometry().adjusted(nOffsetX1, nOffsetY1, nOffsetX2, nOffsetY2)); } } } else if (Qt::NoButton == event->button()) { const QPoint& globalPos = event->globalPos(); const QPoint& pos = event->pos(); bool bHorLeft = qAbs(globalPos.x()-this->geometry().x()) < g_nBorder; bool bHorRight = (pos.x() > this->rect().right() - g_nBorder); bool bVertTop = pos.y() < g_nBorder-2; bool bVertBottom = pos.y() > this->rect().bottom() - g_nBorder; if (bHorLeft || bHorRight || bVertTop || bVertBottom) { m_bCanDragResize = true; if (bHorLeft && bVertTop) //左上角拖拽 { setCursor(Qt::SizeFDiagCursor); m_emHorizontal = DRAG_LOCK_RIGHT; m_emVertical = DRAG_LOCK_BOTTOM; } else if (bHorLeft && bVertBottom) //左下角拖拽 { setCursor(Qt::SizeBDiagCursor); m_emHorizontal = DRAG_LOCK_RIGHT; m_emVertical = DRAG_LOCK_TOP; } else if (bHorRight && bVertTop) //右上角拖拽 { setCursor(Qt::SizeBDiagCursor); m_emHorizontal = DRAG_LOCK_LEFT; m_emVertical = DRAG_LOCK_BOTTOM; } else if ( bHorRight && bVertBottom ) //右下角拖拽 { setCursor(Qt::SizeFDiagCursor); m_emHorizontal = DRAG_LOCK_LEFT; m_emVertical = DRAG_LOCK_TOP; } else if (bHorLeft) //左边框拖拽 { setCursor(Qt::SizeHorCursor); m_emHorizontal = DRAG_LOCK_RIGHT; m_emVertical = DRAG_KEEP; } else if (bHorRight) //右边框拖拽 { setCursor(Qt::SizeHorCursor); m_emHorizontal = DRAG_LOCK_LEFT; m_emVertical = DRAG_KEEP; } else if (bVertTop) //上边框拖拽 { setCursor(Qt::SizeVerCursor); m_emHorizontal = DRAG_KEEP; m_emVertical = DRAG_LOCK_BOTTOM; } else if (bVertBottom) //下边框拖拽 { setCursor(Qt::SizeVerCursor); m_emHorizontal = DRAG_KEEP; m_emVertical = DRAG_LOCK_TOP; } } else if (m_bCanDragResize) { m_bCanDragResize = false; setCursor(Qt::ArrowCursor); } } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { return QWidget::mouseReleaseEvent(event); } void MainWindow::resizeEvent(QResizeEvent *event) { Q_UNUSED(event); // ui->View->fitInView(retPixmap,Qt::KeepAspectRatio); }