resizablepopup.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*****************************************************************************
  2. * resizablepopup.cpp - QStarDict, a StarDict clone written with using Qt *
  3. * Copyright (C) 2007 Alexander Rodin *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "resizablepopup.h"
  20. #include <QApplication>
  21. #include <QCursor>
  22. #include <QDesktopWidget>
  23. #include <QMouseEvent>
  24. #include <QTimerEvent>
  25. namespace
  26. {
  27. const int CornerSize = 10;
  28. }
  29. namespace QStarDict
  30. {
  31. ResizablePopup::ResizablePopup(QWidget *parent)
  32. : QFrame(parent, Qt::Popup)
  33. {
  34. m_isMoving = false;
  35. m_resizeDirection = None;
  36. m_timeoutBeforeHide = 0;
  37. m_timerCloseId = 0;
  38. m_timerResizeId = 0;
  39. m_isPopuped = false;
  40. setMouseTracking(true);
  41. setLineWidth(1);
  42. setMidLineWidth(2);
  43. setFrameStyle(QFrame::Box);
  44. setFrameShadow(QFrame::Raised);
  45. }
  46. void ResizablePopup::popup()
  47. {
  48. if (m_defaultSize != size())
  49. resize(m_defaultSize);
  50. QPoint newPosition = QCursor::pos() - QPoint(30, 30);
  51. if (newPosition.x() < 0)
  52. newPosition.setX(0);
  53. else if (newPosition.x() + width() > QApplication::desktop()->width())
  54. newPosition.setX(QApplication::desktop()->width() - width());
  55. if (newPosition.y() < 0)
  56. newPosition.setY(0);
  57. else if (newPosition.y() + height() > QApplication::desktop()->height())
  58. newPosition.setY(QApplication::desktop()->height() - height());
  59. move(newPosition);
  60. show();
  61. m_isPopuped = true;
  62. }
  63. void ResizablePopup::enterEvent(QEvent*)
  64. {
  65. if (m_timerCloseId)
  66. {
  67. killTimer(m_timerCloseId);
  68. m_timerCloseId = 0;
  69. }
  70. }
  71. void ResizablePopup::leaveEvent(QEvent*)
  72. {
  73. if (geometry().contains(QCursor::pos()))
  74. return;
  75. if (m_resizeDirection)
  76. return;
  77. if (m_timeoutBeforeHide < 0)
  78. return;
  79. if (m_timeoutBeforeHide == 0)
  80. {
  81. m_isPopuped = false;
  82. hide();
  83. }
  84. else if (! m_timerCloseId)
  85. m_timerCloseId = startTimer(m_timeoutBeforeHide);
  86. }
  87. void ResizablePopup::mouseMoveEvent(QMouseEvent *event)
  88. {
  89. Qt::CursorShape cursorShape = Qt::ArrowCursor;
  90. if ((event->x() >= 0 && event->x() < CornerSize &&
  91. event->y() >= 0 && event->y() < CornerSize) ||
  92. (event->x() < width() && event->x() >= width() - CornerSize &&
  93. event->y() < height() && event->y() >= height() - CornerSize))
  94. cursorShape = Qt::SizeFDiagCursor;
  95. else if ((event->x() < width() && event->x() >= width() - CornerSize &&
  96. event->y() >= 0 && event->y() < CornerSize) ||
  97. (event->x() >= 0 && event->x() < CornerSize &&
  98. event->y() < height() && event->y() >= height() - CornerSize))
  99. cursorShape = Qt::SizeBDiagCursor;
  100. else if ((event->x() >= 0 && event->x() < frameWidth()) ||
  101. (event->x() < width() && event->x() >= width() - frameWidth()))
  102. cursorShape = Qt::SizeHorCursor;
  103. else if ((event->y() >= 0 && event->y() < frameWidth()) ||
  104. (event->y() < height() && event->y() >= height() - frameWidth()))
  105. cursorShape = Qt::SizeVerCursor;
  106. if (cursor().shape() != cursorShape)
  107. setCursor(cursorShape);
  108. else
  109. if (event->buttons().testFlag(Qt::LeftButton))
  110. {
  111. if (m_isMoving)
  112. move(pos() + (event->globalPos() - m_oldCursorPos));
  113. m_oldCursorPos = event->globalPos();
  114. return;
  115. }
  116. m_isMoving = false;
  117. }
  118. void ResizablePopup::mousePressEvent(QMouseEvent *event)
  119. {
  120. if (! geometry().contains(event->globalPos()))
  121. {
  122. if (m_timerCloseId)
  123. {
  124. killTimer(m_timerCloseId);
  125. m_timerCloseId = 0;
  126. }
  127. m_isPopuped = false;
  128. hide();
  129. return;
  130. }
  131. if (event->buttons().testFlag(Qt::LeftButton))
  132. {
  133. if (event->x() < CornerSize && event->y() < CornerSize)
  134. m_resizeDirection = TopLeft;
  135. else if (event->x() >= width() - CornerSize && event->y() < CornerSize)
  136. m_resizeDirection = TopRight;
  137. else if (event->x() < CornerSize && event->y() >= height() - CornerSize)
  138. m_resizeDirection = BottomLeft;
  139. else if (event->x() >= width() - CornerSize && event->y() >= height() - CornerSize)
  140. m_resizeDirection = BottomRight;
  141. else if (event->x() < frameWidth())
  142. m_resizeDirection = Left;
  143. else if (event->x() >= width() - frameWidth())
  144. m_resizeDirection = Right;
  145. else if (event->y() < frameWidth())
  146. m_resizeDirection = Top;
  147. else if (event->y() >= height() - frameWidth())
  148. m_resizeDirection = Bottom;
  149. else
  150. m_resizeDirection = None;
  151. if (m_resizeDirection)
  152. m_timerResizeId = startTimer(8);
  153. }
  154. m_isMoving = true;
  155. m_oldCursorPos = event->globalPos();
  156. }
  157. void ResizablePopup::mouseReleaseEvent(QMouseEvent*)
  158. {
  159. stopResize();
  160. }
  161. void ResizablePopup::mouseDoubleClickEvent(QMouseEvent*)
  162. {
  163. m_isPopuped = false;
  164. if (m_timerCloseId)
  165. {
  166. killTimer(m_timerCloseId);
  167. m_timerCloseId = 0;
  168. }
  169. hide();
  170. }
  171. void ResizablePopup::timerEvent(QTimerEvent *event)
  172. {
  173. if (event->timerId() == m_timerResizeId)
  174. {
  175. doResize();
  176. }
  177. else if (event->timerId() == m_timerCloseId)
  178. {
  179. m_isPopuped = false;
  180. hide();
  181. killTimer(m_timerCloseId);
  182. m_timerCloseId = 0;
  183. stopResize();
  184. }
  185. }
  186. void ResizablePopup::doResize()
  187. {
  188. if (! QApplication::mouseButtons().testFlag(Qt::LeftButton))
  189. stopResize();
  190. if (m_resizeDirection)
  191. {
  192. QRect newGeometry = geometry();
  193. switch (m_resizeDirection)
  194. {
  195. case TopLeft:
  196. newGeometry.setTopLeft(QCursor::pos());
  197. break;
  198. case TopRight:
  199. newGeometry.setTopRight(QCursor::pos());
  200. break;
  201. case BottomLeft:
  202. newGeometry.setBottomLeft(QCursor::pos());
  203. break;
  204. case BottomRight:
  205. newGeometry.setBottomRight(QCursor::pos());
  206. break;
  207. case Left:
  208. newGeometry.setLeft(QCursor::pos().x());
  209. break;
  210. case Right:
  211. newGeometry.setRight(QCursor::pos().x());
  212. break;
  213. case Top:
  214. newGeometry.setTop(QCursor::pos().y());
  215. break;
  216. case Bottom:
  217. newGeometry.setBottom(QCursor::pos().y());
  218. break;
  219. default:
  220. ; // Nothing
  221. }
  222. if (newGeometry.width() < minimumSize().width())
  223. {
  224. newGeometry.setWidth(width());
  225. newGeometry.moveLeft(geometry().left());
  226. }
  227. if (newGeometry.height() < minimumSize().height())
  228. {
  229. newGeometry.setHeight(height());
  230. newGeometry.moveTop(geometry().top());
  231. }
  232. if (newGeometry != geometry())
  233. setGeometry(newGeometry);
  234. }
  235. }
  236. void ResizablePopup::stopResize()
  237. {
  238. if (m_resizeDirection)
  239. {
  240. m_resizeDirection = None;
  241. killTimer(m_timerResizeId);
  242. m_timerResizeId = 0;
  243. }
  244. }
  245. bool ResizablePopup::event(QEvent *event)
  246. {
  247. if (event->type() == QEvent::WindowUnblocked && m_isPopuped)
  248. {
  249. if (m_timerCloseId)
  250. {
  251. killTimer(m_timerCloseId);
  252. m_timerCloseId = 0;
  253. }
  254. show();
  255. return true;
  256. }
  257. else
  258. return QFrame::event(event);
  259. }
  260. }
  261. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc