| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #include "xmainwindow.h"
- XMainWindow::XMainWindow(QWidget *parent) :
- QWidget(parent)
- {
- createFrame();
- createWidget();
- createLayout();
- initVariables();
- initUI();
- }
- XMainWindow::~XMainWindow()
- {
- delete m_pTitleBar;
- delete m_pToolBar;
- delete m_pStatusBar;
- delete m_pLayout;
- }
- void XMainWindow::createFrame()
- {
- this->resize(500,300);
- this->setWindowFlags(Qt::FramelessWindowHint);
- this->setStyleSheet("background:red");
- this->setMouseTracking(true);
- }
- void XMainWindow::createWidget()
- {
- }
- void XMainWindow::createLayout()
- {
- }
- void XMainWindow::initUI()
- {
- }
- void XMainWindow::initVariables()
- {
- m_bMaxWin = false;
- m_mouseResizeWindowFlag = false;
- m_mouseMoveWindowFlag = false;
- m_rectRestoreWindow = geometry();
- m_pContentWidget = new QWidget(this);
- m_pTitleBar = new XTitleBar(this);
- m_pToolBar = new XToolBar(this);
- m_pStatusBar = new XStatusBar(this);
- connect(m_pTitleBar,SIGNAL(signalMoveOffset(QPoint)),this,SLOT(slotOnMoveOffset(QPoint)));
- connect(m_pTitleBar,SIGNAL(signalClose()),this,SLOT(slotOnClose()));
- connect(m_pTitleBar,SIGNAL(signalMin()),this,SLOT(slotOnMin()));
- connect(m_pTitleBar,SIGNAL(signalMaxRestore()),this,SLOT(slotOnMaxRestore()));
- m_pContentWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
- m_pContentWidget->setStyleSheet("background:white");
- m_pContentWidget->setMouseTracking(true);
- m_pLayout = new QVBoxLayout(this);
- m_pLayout->addWidget(m_pTitleBar);
- m_pLayout->addWidget(m_pToolBar);
- m_pLayout->addWidget(m_pContentWidget);
- m_pLayout->addWidget(m_pStatusBar);
- m_pLayout->setContentsMargins(1,1,1,1);
- m_pLayout->setSpacing(0);
- }
- void XMainWindow::slotOnMoveOffset(QPoint offset)
- {
- m_mouseResizeWindowFlag = false;
- if(m_bMaxWin)
- slotOnMaxRestore();
- move(this->pos()+offset);
- }
- void XMainWindow::slotOnMin()
- {
- setWindowState(Qt::WindowMinimized);
- }
- void XMainWindow::slotOnMaxRestore()
- {
- if(!m_bMaxWin)
- {
- m_rectRestoreWindow = geometry();
- setGeometry(qApp->desktop()->availableGeometry());
- m_bMaxWin = true;
- }
- else
- {
- setGeometry(m_rectRestoreWindow);
- m_bMaxWin = false;
- }
- }
- void XMainWindow::slotOnClose()
- {
- close();
- }
- void XMainWindow::paintEvent(QPaintEvent *event)
- {
- QBitmap bitmap(this->size());
- QPainter painter(&bitmap);
- painter.fillRect(bitmap.rect(), Qt::white);
- painter.setBrush(QColor(0, 0, 0));
- painter.drawRoundedRect(QRect(0, 0, this->width(), this->height()), 2, 2);
- setMask(bitmap);
- QPainter painter1(this);
- painter1.drawPixmap(rect(), QPixmap(":/skin/default"));
- }
- void XMainWindow::mouseDoubleClickEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton && event->y()<= m_pTitleBar->height())
- {
- slotOnMaxRestore();
- }
- }
- void XMainWindow::mouseMoveEvent(QMouseEvent *event)
- {
- if(!m_mouseResizeWindowFlag)
- {
- m_eDirection = getPointPos(event->x(),event->y());
- qDebug()<<m_mouseResizeWindowFlag<<event->pos()<<m_eDirection;
- setCursorStyle(m_eDirection);
- }
- else
- {
- if(!m_bMaxWin)
- {
- setDragSize(event->globalPos(),m_eDirection);
- m_mouseSrcPos = event->globalPos();
- }
- }
- // qDebug()<<event->button()<<m_mouseMoveWindowFlag;
- // if((event->button() == Qt::LeftButton) && m_mouseMoveWindowFlag)
- // {
- // static QWidget* parent_widget = this->parentWidget();
- // QPoint parent_point = this->pos();
- // parent_point.setX(parent_point.x() + event->x() - m_mouseSrcPos.x());
- // parent_point.setY(parent_point.y() + event->y() - m_mouseSrcPos.y());
- // this->move(parent_point);
- // }
- }
- void XMainWindow::mousePressEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton)
- {
- m_mouseSrcPos = event->globalPos();
- m_mouseResizeWindowFlag = true;
- // if(event->y()<= m_pTitleBar->height() + m_pToolBar->height() && event->y()>1)
- // {
- // m_mouseMoveWindowFlag = true;
- // }
- }
- }
- void XMainWindow::mouseReleaseEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton)
- {
- m_mouseResizeWindowFlag = false;
- m_mouseMoveWindowFlag = false;
- }
- }
- void XMainWindow::moveEvent(QMoveEvent *event)
- {
- //如果窗口最大化,移动时将窗口还原到正常状态
- if(m_bMaxWin)
- {
- slotOnMaxRestore();
- }
- }
- void XMainWindow::setCursorStyle(enum_Direction direction)
- {
- if(m_bMaxWin)
- return ;
- //设置上下左右以及右上、右下、左上、坐下的鼠标形状
- switch(direction)
- {
- case eTop:
- case eBottom:
- setCursor(Qt::SizeVerCursor);
- break;
- case eRight:
- case eLeft:
- setCursor(Qt::SizeHorCursor);
- break;
- case eTopRight:
- case eBottomLeft:
- setCursor(Qt::SizeBDiagCursor);
- break;
- case eRightBottom:
- case eLeftTop:
- setCursor(Qt::SizeFDiagCursor);
- break;
- default:
- setCursor(Qt::ArrowCursor);
- break;
- }
- }
- //确定点的位置方向
- XMainWindow::enum_Direction XMainWindow::getPointPos(int nXRelative,int nYRelative)
- {
- qDebug()<<nXRelative<<nYRelative<<VALUE_DIS;
- int nTop = qAbs(nYRelative)<VALUE_DIS ?eTop:eNone;
- int nRight = qAbs(nXRelative-rect().right())<VALUE_DIS ? eRight:eNone;
- int nBottom = qAbs(nYRelative-rect().bottom())<VALUE_DIS ? eBottom:eNone;
- int nLeft = qAbs(nXRelative)<VALUE_DIS ? eLeft:eNone;
- return enum_Direction(nTop + nRight + nBottom + nLeft);
- }
- void XMainWindow::setDragSize(QPoint destPos,enum_Direction direction)
- {
- //计算偏差
- int ndX = destPos.x() - m_mouseSrcPos.x();
- int ndY = destPos.y() - m_mouseSrcPos.y();
- //获得主窗口位置信息
- QRect rectWindow = geometry();
- //判别方向
- qDebug()<<direction<<ndX<<ndY;
- if(direction & eTop)
- {
- rectWindow.setTop(rectWindow.top()+ndY);
- }
- if(direction & eRight)
- {
- rectWindow.setRight(rectWindow.right()+ndX);
- }
- if(direction & eBottom)
- {
- rectWindow.setBottom(rectWindow.bottom()+ndY);
- }
- if(direction & eLeft)
- {
- rectWindow.setLeft(rectWindow.left()+ndX);
- }
- if(rectWindow.width()< minimumWidth() || rectWindow.height()<minimumHeight())
- {
- return;
- }
- //重新设置窗口位置为新位置信息
- setGeometry(rectWindow);
- }
|