DragAbleWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * 叶海辉
  3. * QQ群121376426
  4. * http://blog.yundiantech.com/
  5. */
  6. #include "DragAbleWidget.h"
  7. #include "ui_DragAbleWidget.h"
  8. #include <QDesktopWidget>
  9. #include <QMouseEvent>
  10. #include <QTimer>
  11. #include <QDebug>
  12. #define MARGINS 2 //窗体边框
  13. DragAbleWidget::DragAbleWidget(QWidget *parent) :
  14. QWidget(parent),
  15. ui(new Ui::DragAbleWidget)
  16. {
  17. ui->setupUi(this);
  18. ///改变窗体大小相关
  19. isMax = false;
  20. int w = this->width();
  21. int h = this->height();
  22. QRect screenRect = QApplication::desktop()->screenGeometry();//获取设备屏幕大小
  23. int x = (screenRect.width() - w) / 2;
  24. int y = (screenRect.height() - h) / 2;
  25. mLocation = this->geometry();
  26. // mLocation = QRect(x, y, w, h);
  27. // this->setGeometry(mLocation);
  28. isLeftPressDown = false;
  29. this->dir = NONE;
  30. this->setMouseTracking(true);// 追踪鼠标
  31. ui->widget_frame->setMouseTracking(true);
  32. ui->widget_back->setMouseTracking(true);
  33. ui->widget_container->setMouseTracking(true);
  34. // ui->widget_center->setMouseTracking(true);
  35. this->setFocusPolicy(Qt::ClickFocus);
  36. ui->widget_frame->setContentsMargins(MARGINS,MARGINS,MARGINS,MARGINS);
  37. showBorderRadius(true);
  38. // ui->widget_frame->setContentsMargins(1, 1, 1, 1);
  39. //安装事件监听器,让标题栏识别鼠标双击
  40. // ui->widget_beingClass_back->installEventFilter(this);
  41. }
  42. DragAbleWidget::~DragAbleWidget()
  43. {
  44. }
  45. QWidget *DragAbleWidget::getContainWidget()
  46. {
  47. return ui->widget_container;
  48. }
  49. void DragAbleWidget::setTitle(QString str)
  50. {
  51. ui->label_titleName->setText(str);
  52. this->setWindowTitle(str);
  53. }
  54. ////////////改变窗体大小相关
  55. void DragAbleWidget::mouseReleaseEvent(QMouseEvent *event)
  56. {
  57. if(event->button() == Qt::LeftButton)
  58. {
  59. isLeftPressDown = false;
  60. if(dir != NONE)
  61. {
  62. this->releaseMouse();
  63. this->setCursor(QCursor(Qt::ArrowCursor));
  64. }
  65. }
  66. }
  67. void DragAbleWidget::mousePressEvent(QMouseEvent *event)
  68. {
  69. // qDebug()<<__FUNCTION__;
  70. if (event->type() == QEvent::MouseButtonDblClick)
  71. {
  72. if (event->button() == Qt::LeftButton)
  73. {
  74. // if(QApplication::keyboardModifiers() == (Qt::ControlModifier|Qt::ShiftModifier|Qt::AltModifier))
  75. {
  76. doChangeFullScreen(); //ctrl + 左键
  77. }
  78. }
  79. }
  80. switch(event->button()) {
  81. case Qt::LeftButton:
  82. if (isMax || this->isFullScreen()) break;
  83. isLeftPressDown = true;
  84. checkCursorDirect(event->globalPos());
  85. if(dir != NONE) {
  86. this->mouseGrabber();
  87. } else {
  88. dragPosition = event->globalPos() - this->frameGeometry().topLeft();
  89. }
  90. break;
  91. // case Qt::RightButton:
  92. // if (!this->isFullScreen())
  93. // mAction_FullScreen->setText(tr("show fullscreen"));
  94. // else
  95. // mAction_FullScreen->setText(tr("quit fullscreen"));
  96. // mPopMenu->exec(QCursor::pos());
  97. // break;
  98. default:
  99. QWidget::mousePressEvent(event);
  100. }
  101. }
  102. void DragAbleWidget::mouseMoveEvent(QMouseEvent *event)
  103. {
  104. // qDebug()<<__FUNCTION__;
  105. if (isMax || this->isFullScreen()) return;
  106. QPoint gloPoint = event->globalPos();
  107. QRect rect = this->rect();
  108. QPoint tl = mapToGlobal(rect.topLeft());
  109. QPoint rb = mapToGlobal(rect.bottomRight());
  110. if(!isLeftPressDown) {
  111. checkCursorDirect(gloPoint);
  112. } else {
  113. if(dir != NONE) {
  114. QRect rMove(tl, rb);
  115. switch(dir) {
  116. case LEFT:
  117. if(rb.x() - gloPoint.x() <= this->minimumWidth())
  118. rMove.setX(tl.x());
  119. else
  120. rMove.setX(gloPoint.x());
  121. break;
  122. case RIGHT:
  123. rMove.setWidth(gloPoint.x() - tl.x());
  124. break;
  125. case UP:
  126. if(rb.y() - gloPoint.y() <= this->minimumHeight())
  127. rMove.setY(tl.y());
  128. else
  129. rMove.setY(gloPoint.y());
  130. break;
  131. case DOWN:
  132. rMove.setHeight(gloPoint.y() - tl.y());
  133. break;
  134. case LEFTTOP:
  135. if(rb.x() - gloPoint.x() <= this->minimumWidth())
  136. rMove.setX(tl.x());
  137. else
  138. rMove.setX(gloPoint.x());
  139. if(rb.y() - gloPoint.y() <= this->minimumHeight())
  140. rMove.setY(tl.y());
  141. else
  142. rMove.setY(gloPoint.y());
  143. break;
  144. case RIGHTTOP:
  145. rMove.setWidth(gloPoint.x() - tl.x());
  146. rMove.setY(gloPoint.y());
  147. break;
  148. case LEFTBOTTOM:
  149. rMove.setX(gloPoint.x());
  150. rMove.setHeight(gloPoint.y() - tl.y());
  151. break;
  152. case RIGHTBOTTOM:
  153. rMove.setWidth(gloPoint.x() - tl.x());
  154. rMove.setHeight(gloPoint.y() - tl.y());
  155. break;
  156. default:
  157. break;
  158. }
  159. this->setGeometry(rMove);
  160. // emit sig_WindowMoved(rMove);
  161. } else {
  162. checkCursorDirect(event->globalPos());
  163. if (dir == NONE && !isMax)
  164. {
  165. QPoint point = event->globalPos() - dragPosition;
  166. QRect mLimitRect = QApplication::desktop()->availableGeometry();
  167. if (point.x() < mLimitRect.x())
  168. point.setX(mLimitRect.x());
  169. if (point.x() > (mLimitRect.x()+mLimitRect.width()-this->width()))
  170. point.setX(mLimitRect.x()+mLimitRect.width()-this->width());
  171. if (point.y() < mLimitRect.y())
  172. point.setY(mLimitRect.y());
  173. if (point.y() > (mLimitRect.y()+mLimitRect.height()-this->height()))
  174. point.setY(mLimitRect.y()+mLimitRect.height()-this->height());
  175. move(point);
  176. }
  177. event->accept();
  178. }
  179. }
  180. // QWidget::mouseMoveEvent(event);、
  181. event->accept();
  182. }
  183. void DragAbleWidget::checkCursorDirect(const QPoint &cursorGlobalPoint)
  184. {
  185. // 获取窗体在屏幕上的位置区域,tl为topleft点,rb为rightbottom点
  186. QRect rect = this->rect();
  187. QPoint tl = mapToGlobal(rect.topLeft());
  188. QPoint rb = mapToGlobal(rect.bottomRight());
  189. int x = cursorGlobalPoint.x();
  190. int y = cursorGlobalPoint.y();
  191. if(tl.x() + PADDING >= x && tl.x() <= x && tl.y() + PADDING >= y && tl.y() <= y) {
  192. // 左上角
  193. dir = LEFTTOP;
  194. this->setCursor(QCursor(Qt::SizeFDiagCursor)); // 设置鼠标形状
  195. } else if(x >= rb.x() - PADDING && x <= rb.x() && y >= rb.y() - PADDING && y <= rb.y()) {
  196. // 右下角
  197. dir = RIGHTBOTTOM;
  198. this->setCursor(QCursor(Qt::SizeFDiagCursor));
  199. } else if(x <= tl.x() + PADDING && x >= tl.x() && y >= rb.y() - PADDING && y <= rb.y()) {
  200. //左下角
  201. dir = LEFTBOTTOM;
  202. this->setCursor(QCursor(Qt::SizeBDiagCursor));
  203. } else if(x <= rb.x() && x >= rb.x() - PADDING && y >= tl.y() && y <= tl.y() + PADDING) {
  204. // 右上角
  205. dir = RIGHTTOP;
  206. this->setCursor(QCursor(Qt::SizeBDiagCursor));
  207. } else if(x <= tl.x() + PADDING && x >= tl.x()) {
  208. // 左边
  209. dir = LEFT;
  210. this->setCursor(QCursor(Qt::SizeHorCursor));
  211. } else if( x <= rb.x() && x >= rb.x() - PADDING) {
  212. // 右边
  213. dir = RIGHT;
  214. this->setCursor(QCursor(Qt::SizeHorCursor));
  215. }else if(y >= tl.y() && y <= tl.y() + PADDING){
  216. // 上边
  217. dir = UP;
  218. this->setCursor(QCursor(Qt::SizeVerCursor));
  219. } else if(y <= rb.y() && y >= rb.y() - PADDING) {
  220. // 下边
  221. dir = DOWN;
  222. this->setCursor(QCursor(Qt::SizeVerCursor));
  223. }else {
  224. // 默认
  225. dir = NONE;
  226. this->setCursor(QCursor(Qt::ArrowCursor));
  227. }
  228. }
  229. void DragAbleWidget::doShowFullScreen()
  230. {
  231. this->show();
  232. this->showFullScreen();
  233. this->raise();
  234. ui->widget_frame->setContentsMargins(0,0,0,0); //隐藏边框
  235. showBorderRadius(false);
  236. ui->btnMenu_Max->setIcon(QIcon(":/image/shownormalbtn.png"));
  237. ui->widget_title->hide(); //隐藏标题栏
  238. // ui->verticalLayout_titleWidget_Back->removeWidget(ui->widget_title);
  239. // ui->widget_title->setParent(NULL);
  240. // ui->widget_title->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::Tool|Qt::X11BypassWindowManagerHint);
  241. // ui->widget_title->resize(QApplication::desktop()->screen()->width(), ui->widget_title->height());
  242. // ui->widget_title->move(0,0);
  243. // ui->widget_title->show();
  244. }
  245. void DragAbleWidget::doShowNormal()
  246. {
  247. qDebug()<<__FUNCTION__;
  248. this->show();
  249. this->showNormal();
  250. this->raise();
  251. if (!isMax)
  252. {
  253. ui->widget_frame->setContentsMargins(MARGINS,MARGINS,MARGINS,MARGINS);
  254. showBorderRadius(true);
  255. } else {
  256. ui->widget_frame->setContentsMargins(0,0,0,0);
  257. showBorderRadius(false);
  258. }
  259. ui->btnMenu_Max->setIcon(QIcon(":/image/showmaxsizebtn.png"));
  260. QTimer::singleShot(20,this,[&]()
  261. {
  262. ui->verticalLayout_titleWidget_Back->addWidget(ui->widget_title);
  263. ui->widget_title->show();
  264. });
  265. }
  266. void DragAbleWidget::showBorderRadius(bool isShow)
  267. {
  268. QString str;
  269. if (isShow)
  270. {
  271. str = QString("QWidget#widget_frame\
  272. {\
  273. border:3px solid rgb(46, 165, 255);\
  274. background-color: rgba(255, 255, 255, 0);\
  275. border-radius:5px;\
  276. }\
  277. QWidget#widget_back\
  278. {\
  279. border-radius:3px;\
  280. }\
  281. QWidget#widget_title\
  282. {\
  283. border-top-right-radius:5px;\
  284. border-top-left-radius:5px;\
  285. }\
  286. QWidget#widget_container\
  287. {\
  288. border-bottom-right-radius:5px;\
  289. border-bottom-left-radius:5px;\
  290. }\
  291. QStackedWidget\
  292. {\
  293. border-bottom-right-radius:5px;\
  294. border-bottom-left-radius:5px;\
  295. }\
  296. QWidget#page_courseList\
  297. {\
  298. border-bottom-right-radius:5px;\
  299. border-bottom-left-radius:5px;\
  300. }");
  301. }
  302. else
  303. {
  304. str = QString("QWidget#widget_frame\
  305. {\
  306. border:3px solid rgb(46, 165, 255);\
  307. background-color: rgba(255, 255, 255, 0);\
  308. border-radius:0px;\
  309. }\
  310. QWidget#widget_back\
  311. {\
  312. border-radius:0px;\
  313. }\
  314. QWidget#widget_title\
  315. {\
  316. border-top-right-radius:0px;\
  317. border-top-left-radius:0px;\
  318. }\
  319. QWidget#widget_container\
  320. {\
  321. border-bottom-right-radius:0px;\
  322. border-bottom-left-radius:0px;\
  323. }\
  324. QStackedWidget\
  325. {\
  326. border-bottom-right-radius:0px;\
  327. border-bottom-left-radius:0px;\
  328. }\
  329. QWidget#page_courseList\
  330. {\
  331. border-bottom-right-radius:0px;\
  332. border-bottom-left-radius:0px;\
  333. }");
  334. }
  335. ui->widget_frame->setStyleSheet(str);
  336. }
  337. void DragAbleWidget::doChangeFullScreen()
  338. {
  339. if (this->isFullScreen())
  340. {
  341. this->doShowNormal();
  342. // mAction_FullScreen->setText(tr("show fullscreen"));
  343. }
  344. else
  345. {
  346. this->doShowFullScreen();
  347. // mAction_FullScreen->setText(tr("quit fullscreen"));
  348. }
  349. }
  350. void DragAbleWidget::on_btnMenu_Close_clicked()
  351. {
  352. qDebug()<<__FUNCTION__;
  353. close();
  354. // QTimer::singleShot(500,this,[&]()
  355. // {
  356. // QPoint pt(0, 0);
  357. // QMouseEvent evt(QEvent::Leave, pt, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
  358. // qApp->sendEvent(ui->btnMenu_Close, &evt);
  359. // });
  360. }
  361. void DragAbleWidget::on_btnMenu_Max_clicked()
  362. {
  363. doChangeFullScreen();
  364. }
  365. void DragAbleWidget::on_btnMenu_Min_clicked()
  366. {
  367. if (this->isFullScreen())
  368. {
  369. // mAnimation->stop();
  370. // mTimer_CheckTitle->stop();
  371. ui->widget_title->move(ui->widget_title->x(), 0 - ui->widget_title->height());
  372. ui->widget_title->hide();
  373. }
  374. this->showMinimized();
  375. QTimer::singleShot(500,this,[&]()
  376. {
  377. QPoint pt(0, 0);
  378. QMouseEvent evt(QEvent::Leave, pt, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
  379. qApp->sendEvent(ui->btnMenu_Min, &evt);
  380. });
  381. }