xmainwindow.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "xmainwindow.h"
  2. XMainWindow::XMainWindow(QWidget *parent) :
  3. QWidget(parent)
  4. {
  5. createFrame();
  6. createWidget();
  7. createLayout();
  8. initVariables();
  9. initUI();
  10. }
  11. XMainWindow::~XMainWindow()
  12. {
  13. delete m_pTitleBar;
  14. delete m_pToolBar;
  15. delete m_pStatusBar;
  16. delete m_pLayout;
  17. }
  18. void XMainWindow::createFrame()
  19. {
  20. this->resize(500,300);
  21. this->setWindowFlags(Qt::FramelessWindowHint);
  22. this->setStyleSheet("background:red");
  23. this->setMouseTracking(true);
  24. }
  25. void XMainWindow::createWidget()
  26. {
  27. }
  28. void XMainWindow::createLayout()
  29. {
  30. }
  31. void XMainWindow::initUI()
  32. {
  33. }
  34. void XMainWindow::initVariables()
  35. {
  36. m_bMaxWin = false;
  37. m_mouseResizeWindowFlag = false;
  38. m_mouseMoveWindowFlag = false;
  39. m_rectRestoreWindow = geometry();
  40. m_pContentWidget = new QWidget(this);
  41. m_pTitleBar = new XTitleBar(this);
  42. m_pToolBar = new XToolBar(this);
  43. m_pStatusBar = new XStatusBar(this);
  44. connect(m_pTitleBar,SIGNAL(signalMoveOffset(QPoint)),this,SLOT(slotOnMoveOffset(QPoint)));
  45. connect(m_pTitleBar,SIGNAL(signalClose()),this,SLOT(slotOnClose()));
  46. connect(m_pTitleBar,SIGNAL(signalMin()),this,SLOT(slotOnMin()));
  47. connect(m_pTitleBar,SIGNAL(signalMaxRestore()),this,SLOT(slotOnMaxRestore()));
  48. m_pContentWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
  49. m_pContentWidget->setStyleSheet("background:white");
  50. m_pContentWidget->setMouseTracking(true);
  51. m_pLayout = new QVBoxLayout(this);
  52. m_pLayout->addWidget(m_pTitleBar);
  53. m_pLayout->addWidget(m_pToolBar);
  54. m_pLayout->addWidget(m_pContentWidget);
  55. m_pLayout->addWidget(m_pStatusBar);
  56. m_pLayout->setContentsMargins(1,1,1,1);
  57. m_pLayout->setSpacing(0);
  58. }
  59. void XMainWindow::slotOnMoveOffset(QPoint offset)
  60. {
  61. m_mouseResizeWindowFlag = false;
  62. if(m_bMaxWin)
  63. slotOnMaxRestore();
  64. move(this->pos()+offset);
  65. }
  66. void XMainWindow::slotOnMin()
  67. {
  68. setWindowState(Qt::WindowMinimized);
  69. }
  70. void XMainWindow::slotOnMaxRestore()
  71. {
  72. if(!m_bMaxWin)
  73. {
  74. m_rectRestoreWindow = geometry();
  75. setGeometry(qApp->desktop()->availableGeometry());
  76. m_bMaxWin = true;
  77. }
  78. else
  79. {
  80. setGeometry(m_rectRestoreWindow);
  81. m_bMaxWin = false;
  82. }
  83. }
  84. void XMainWindow::slotOnClose()
  85. {
  86. close();
  87. }
  88. void XMainWindow::paintEvent(QPaintEvent *event)
  89. {
  90. QBitmap bitmap(this->size());
  91. QPainter painter(&bitmap);
  92. painter.fillRect(bitmap.rect(), Qt::white);
  93. painter.setBrush(QColor(0, 0, 0));
  94. painter.drawRoundedRect(QRect(0, 0, this->width(), this->height()), 2, 2);
  95. setMask(bitmap);
  96. QPainter painter1(this);
  97. painter1.drawPixmap(rect(), QPixmap(":/skin/default"));
  98. }
  99. void XMainWindow::mouseDoubleClickEvent(QMouseEvent *event)
  100. {
  101. if (event->button() == Qt::LeftButton && event->y()<= m_pTitleBar->height())
  102. {
  103. slotOnMaxRestore();
  104. }
  105. }
  106. void XMainWindow::mouseMoveEvent(QMouseEvent *event)
  107. {
  108. if(!m_mouseResizeWindowFlag)
  109. {
  110. m_eDirection = getPointPos(event->x(),event->y());
  111. qDebug()<<m_mouseResizeWindowFlag<<event->pos()<<m_eDirection;
  112. setCursorStyle(m_eDirection);
  113. }
  114. else
  115. {
  116. if(!m_bMaxWin)
  117. {
  118. setDragSize(event->globalPos(),m_eDirection);
  119. m_mouseSrcPos = event->globalPos();
  120. }
  121. }
  122. // qDebug()<<event->button()<<m_mouseMoveWindowFlag;
  123. // if((event->button() == Qt::LeftButton) && m_mouseMoveWindowFlag)
  124. // {
  125. // static QWidget* parent_widget = this->parentWidget();
  126. // QPoint parent_point = this->pos();
  127. // parent_point.setX(parent_point.x() + event->x() - m_mouseSrcPos.x());
  128. // parent_point.setY(parent_point.y() + event->y() - m_mouseSrcPos.y());
  129. // this->move(parent_point);
  130. // }
  131. }
  132. void XMainWindow::mousePressEvent(QMouseEvent *event)
  133. {
  134. if (event->button() == Qt::LeftButton)
  135. {
  136. m_mouseSrcPos = event->globalPos();
  137. m_mouseResizeWindowFlag = true;
  138. // if(event->y()<= m_pTitleBar->height() + m_pToolBar->height() && event->y()>1)
  139. // {
  140. // m_mouseMoveWindowFlag = true;
  141. // }
  142. }
  143. }
  144. void XMainWindow::mouseReleaseEvent(QMouseEvent *event)
  145. {
  146. if (event->button() == Qt::LeftButton)
  147. {
  148. m_mouseResizeWindowFlag = false;
  149. m_mouseMoveWindowFlag = false;
  150. }
  151. }
  152. void XMainWindow::moveEvent(QMoveEvent *event)
  153. {
  154. //如果窗口最大化,移动时将窗口还原到正常状态
  155. if(m_bMaxWin)
  156. {
  157. slotOnMaxRestore();
  158. }
  159. }
  160. void XMainWindow::setCursorStyle(enum_Direction direction)
  161. {
  162. if(m_bMaxWin)
  163. return ;
  164. //设置上下左右以及右上、右下、左上、坐下的鼠标形状
  165. switch(direction)
  166. {
  167. case eTop:
  168. case eBottom:
  169. setCursor(Qt::SizeVerCursor);
  170. break;
  171. case eRight:
  172. case eLeft:
  173. setCursor(Qt::SizeHorCursor);
  174. break;
  175. case eTopRight:
  176. case eBottomLeft:
  177. setCursor(Qt::SizeBDiagCursor);
  178. break;
  179. case eRightBottom:
  180. case eLeftTop:
  181. setCursor(Qt::SizeFDiagCursor);
  182. break;
  183. default:
  184. setCursor(Qt::ArrowCursor);
  185. break;
  186. }
  187. }
  188. //确定点的位置方向
  189. XMainWindow::enum_Direction XMainWindow::getPointPos(int nXRelative,int nYRelative)
  190. {
  191. qDebug()<<nXRelative<<nYRelative<<VALUE_DIS;
  192. int nTop = qAbs(nYRelative)<VALUE_DIS ?eTop:eNone;
  193. int nRight = qAbs(nXRelative-rect().right())<VALUE_DIS ? eRight:eNone;
  194. int nBottom = qAbs(nYRelative-rect().bottom())<VALUE_DIS ? eBottom:eNone;
  195. int nLeft = qAbs(nXRelative)<VALUE_DIS ? eLeft:eNone;
  196. return enum_Direction(nTop + nRight + nBottom + nLeft);
  197. }
  198. void XMainWindow::setDragSize(QPoint destPos,enum_Direction direction)
  199. {
  200. //计算偏差
  201. int ndX = destPos.x() - m_mouseSrcPos.x();
  202. int ndY = destPos.y() - m_mouseSrcPos.y();
  203. //获得主窗口位置信息
  204. QRect rectWindow = geometry();
  205. //判别方向
  206. qDebug()<<direction<<ndX<<ndY;
  207. if(direction & eTop)
  208. {
  209. rectWindow.setTop(rectWindow.top()+ndY);
  210. }
  211. if(direction & eRight)
  212. {
  213. rectWindow.setRight(rectWindow.right()+ndX);
  214. }
  215. if(direction & eBottom)
  216. {
  217. rectWindow.setBottom(rectWindow.bottom()+ndY);
  218. }
  219. if(direction & eLeft)
  220. {
  221. rectWindow.setLeft(rectWindow.left()+ndX);
  222. }
  223. if(rectWindow.width()< minimumWidth() || rectWindow.height()<minimumHeight())
  224. {
  225. return;
  226. }
  227. //重新设置窗口位置为新位置信息
  228. setGeometry(rectWindow);
  229. }