ctkPopupWidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QDebug>
  17. #include <QDesktopWidget>
  18. #include <QDir>
  19. #include <QEvent>
  20. #include <QLabel>
  21. #include <QLayout>
  22. #include <QMouseEvent>
  23. #include <QMoveEvent>
  24. #include <QPainter>
  25. #include <QPointer>
  26. #include <QPropertyAnimation>
  27. #include <QStyle>
  28. #include <QTimer>
  29. // CTK includes
  30. #include "ctkPopupWidget_p.h"
  31. #define LEAVE_CLOSING_DELAY 100 // we don't want to be too fast to close
  32. #define ENTER_OPENING_DELAY 20 // we want to be responsive but allow "errors"
  33. #define DEFAULT_FADING_DURATION 333 // fast enough without being too slow
  34. // -------------------------------------------------------------------------
  35. ctkPopupWidgetPrivate::ctkPopupWidgetPrivate(ctkPopupWidget& object)
  36. :Superclass(object)
  37. {
  38. this->Active = false;
  39. this->AutoShow = true;
  40. this->AutoHide = true;
  41. }
  42. // -------------------------------------------------------------------------
  43. ctkPopupWidgetPrivate::~ctkPopupWidgetPrivate()
  44. {
  45. }
  46. // -------------------------------------------------------------------------
  47. void ctkPopupWidgetPrivate::init()
  48. {
  49. Q_Q(ctkPopupWidget);
  50. this->setParent(q);
  51. this->Superclass::init();
  52. q->setActive(true);
  53. }
  54. // -------------------------------------------------------------------------
  55. QWidget* ctkPopupWidgetPrivate::mouseOver()
  56. {
  57. Q_Q(ctkPopupWidget);
  58. QWidget* widgetUnderCursor = this->Superclass::mouseOver();
  59. if (widgetUnderCursor &&
  60. !this->focusWidgets(true).contains(widgetUnderCursor))
  61. {
  62. widgetUnderCursor->installEventFilter(q);
  63. }
  64. return widgetUnderCursor;
  65. }
  66. // -------------------------------------------------------------------------
  67. bool ctkPopupWidgetPrivate::eventFilter(QObject* obj, QEvent* event)
  68. {
  69. Q_Q(ctkPopupWidget);
  70. QWidget* widget = qobject_cast<QWidget*>(obj);
  71. // Here are the application events, it's a lot of events, so we need to be
  72. // careful to be fast.
  73. if (event->type() == QEvent::ApplicationDeactivate)
  74. {
  75. // We wait to see if there is no other window being active
  76. QTimer::singleShot(0, this, SLOT(onApplicationDeactivate()));
  77. }
  78. else if (event->type() == QEvent::ApplicationActivate)
  79. {
  80. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  81. }
  82. if (!this->BaseWidget)
  83. {
  84. return false;
  85. }
  86. if (event->type() == QEvent::Move && widget != this->BaseWidget)
  87. {
  88. if (widget->isAncestorOf(this->BaseWidget))
  89. {
  90. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  91. QPoint topLeft = widget->parentWidget() ? widget->parentWidget()->mapToGlobal(moveEvent->pos()) : moveEvent->pos();
  92. topLeft += this->BaseWidget->mapTo(widget, QPoint(0,0));
  93. //q->move(q->pos() + moveEvent->pos() - moveEvent->oldPos());
  94. QRect newBaseGeometry = this->baseGeometry();
  95. newBaseGeometry.moveTopLeft(topLeft);
  96. QRect desiredGeometry = this->desiredOpenGeometry(newBaseGeometry);
  97. q->move(desiredGeometry.topLeft());
  98. }
  99. else if (widget->isWindow() &&
  100. widget->windowType() != Qt::ToolTip &&
  101. widget->windowType() != Qt::Popup)
  102. {
  103. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  104. }
  105. }
  106. else if (event->type() == QEvent::Resize)
  107. {
  108. if (widget->isWindow() &&
  109. widget != this->BaseWidget->window() &&
  110. widget->windowType() != Qt::ToolTip &&
  111. widget->windowType() != Qt::Popup)
  112. {
  113. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  114. }
  115. }
  116. else if (event->type() == QEvent::WindowStateChange &&
  117. widget != this->BaseWidget->window() &&
  118. widget->windowType() != Qt::ToolTip &&
  119. widget->windowType() != Qt::Popup)
  120. {
  121. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  122. }
  123. else if ((event->type() == QEvent::WindowActivate ||
  124. event->type() == QEvent::WindowDeactivate) &&
  125. widget == this->BaseWidget->window())
  126. {
  127. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  128. }
  129. return false;
  130. }
  131. // -------------------------------------------------------------------------
  132. void ctkPopupWidgetPrivate::onApplicationDeactivate()
  133. {
  134. // Still no active window, that means the user now is controlling another
  135. // application, we have no control over when the other app moves over the
  136. // popup, so we hide the popup as it would show on top of the other app.
  137. if (!qApp->activeWindow())
  138. {
  139. this->temporarilyHiddenOn();
  140. }
  141. }
  142. // -------------------------------------------------------------------------
  143. void ctkPopupWidgetPrivate::updateVisibility()
  144. {
  145. Q_Q(ctkPopupWidget);
  146. // If the BaseWidget window is active, then there is no reason to cover the
  147. // popup.
  148. if (!this->BaseWidget ||
  149. // the popupwidget active window is not active
  150. (!this->BaseWidget->window()->isActiveWindow() &&
  151. // and no other active window
  152. (!qApp->activeWindow() ||
  153. // or the active window is a popup/tooltip
  154. (qApp->activeWindow()->windowType() != Qt::ToolTip &&
  155. qApp->activeWindow()->windowType() != Qt::Popup))))
  156. {
  157. foreach(QWidget* topLevelWidget, qApp->topLevelWidgets())
  158. {
  159. // If there is at least 1 window (active or not) that covers the popup,
  160. // then we ensure the popup is hidden.
  161. // We have no way of knowing which toplevel is over (z-order) which one,
  162. // it is an OS specific information.
  163. // Of course, tooltips and popups don't count as covering windows.
  164. if (topLevelWidget->isVisible() &&
  165. !(topLevelWidget->windowState() & Qt::WindowMinimized) &&
  166. topLevelWidget->windowType() != Qt::ToolTip &&
  167. topLevelWidget->windowType() != Qt::Popup &&
  168. topLevelWidget != (this->BaseWidget ? this->BaseWidget->window() : 0) &&
  169. topLevelWidget->frameGeometry().intersects(q->geometry()))
  170. {
  171. //qDebug() << "hide" << q << "because of: " << topLevelWidget
  172. // << " with windowType: " << topLevelWidget->windowType()
  173. // << topLevelWidget->isVisible()
  174. // << (this->BaseWidget ? this->BaseWidget->window() : 0)
  175. // << topLevelWidget->frameGeometry();
  176. this->temporarilyHiddenOn();
  177. return;
  178. }
  179. }
  180. }
  181. // If the base widget is hidden or minimized, we don't want to restore the
  182. // popup.
  183. if (this->BaseWidget &&
  184. (!this->BaseWidget->isVisible() ||
  185. this->BaseWidget->window()->windowState() & Qt::WindowMinimized))
  186. {
  187. return;
  188. }
  189. // Restore the visibility of the popup if it was hidden
  190. this->temporarilyHiddenOff();
  191. }
  192. // -------------------------------------------------------------------------
  193. void ctkPopupWidgetPrivate::temporarilyHiddenOn()
  194. {
  195. Q_Q(ctkPopupWidget);
  196. if (!this->AutoHide &&
  197. (q->isVisible() || this->isOpening()) &&
  198. !(q->isHidden() || this->isClosing()))
  199. {
  200. this->setProperty("forcedClosed", this->isOpening() ? 2 : 1);
  201. }
  202. this->currentAnimation()->stop();
  203. this->hideAll();
  204. }
  205. // -------------------------------------------------------------------------
  206. void ctkPopupWidgetPrivate::temporarilyHiddenOff()
  207. {
  208. Q_Q(ctkPopupWidget);
  209. int forcedClosed = this->property("forcedClosed").toInt();
  210. if (forcedClosed > 0)
  211. {
  212. q->show();
  213. if (forcedClosed == 2)
  214. {
  215. emit q->popupOpened(true);
  216. }
  217. this->setProperty("forcedClosed", 0);
  218. }
  219. else
  220. {
  221. q->updatePopup();
  222. }
  223. }
  224. // -------------------------------------------------------------------------
  225. // Qt::FramelessWindowHint is required on Windows for Translucent background
  226. // Qt::Toolip is preferred to Qt::Popup as it would close itself at the first
  227. // click outside the widget (typically a click in the BaseWidget)
  228. ctkPopupWidget::ctkPopupWidget(QWidget* parentWidget)
  229. : Superclass(new ctkPopupWidgetPrivate(*this), parentWidget)
  230. {
  231. Q_D(ctkPopupWidget);
  232. d->init();
  233. }
  234. // -------------------------------------------------------------------------
  235. ctkPopupWidget::~ctkPopupWidget()
  236. {
  237. }
  238. // -------------------------------------------------------------------------
  239. bool ctkPopupWidget::isActive()const
  240. {
  241. Q_D(const ctkPopupWidget);
  242. return d->Active;
  243. }
  244. // -------------------------------------------------------------------------
  245. void ctkPopupWidget::setActive(bool active)
  246. {
  247. Q_D(ctkPopupWidget);
  248. if (active == d->Active)
  249. {
  250. return;
  251. }
  252. d->Active = active;
  253. if (d->Active)
  254. {
  255. if (d->BaseWidget)
  256. {
  257. d->BaseWidget->installEventFilter(this);
  258. }
  259. d->PopupPixmapWidget->installEventFilter(this);
  260. qApp->installEventFilter(d);
  261. }
  262. else // not active
  263. {
  264. if (d->BaseWidget)
  265. {
  266. d->BaseWidget->removeEventFilter(this);
  267. }
  268. d->PopupPixmapWidget->removeEventFilter(this);
  269. qApp->removeEventFilter(d);
  270. }
  271. }
  272. // -------------------------------------------------------------------------
  273. void ctkPopupWidget::setBaseWidget(QWidget* widget)
  274. {
  275. Q_D(ctkPopupWidget);
  276. if (d->BaseWidget)
  277. {
  278. d->BaseWidget->removeEventFilter(this);
  279. }
  280. this->Superclass::setBaseWidget(widget);
  281. if (d->BaseWidget && d->Active)
  282. {
  283. d->BaseWidget->installEventFilter(this);
  284. }
  285. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  286. }
  287. // -------------------------------------------------------------------------
  288. bool ctkPopupWidget::autoShow()const
  289. {
  290. Q_D(const ctkPopupWidget);
  291. return d->AutoShow;
  292. }
  293. // -------------------------------------------------------------------------
  294. void ctkPopupWidget::setAutoShow(bool mode)
  295. {
  296. Q_D(ctkPopupWidget);
  297. d->AutoShow = mode;
  298. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  299. }
  300. // -------------------------------------------------------------------------
  301. bool ctkPopupWidget::autoHide()const
  302. {
  303. Q_D(const ctkPopupWidget);
  304. return d->AutoHide;
  305. }
  306. // -------------------------------------------------------------------------
  307. void ctkPopupWidget::setAutoHide(bool mode)
  308. {
  309. Q_D(ctkPopupWidget);
  310. d->AutoHide = mode;
  311. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  312. }
  313. // -------------------------------------------------------------------------
  314. void ctkPopupWidget::onEffectFinished()
  315. {
  316. Q_D(ctkPopupWidget);
  317. bool wasClosing = d->wasClosing();
  318. this->Superclass::onEffectFinished();
  319. if (wasClosing)
  320. {
  321. /// restore the AutoShow if needed.
  322. if (!this->property("AutoShowOnClose").isNull())
  323. {
  324. d->AutoShow = this->property("AutoShowOnClose").toBool();
  325. this->setProperty("AutoShowOnClose", QVariant());
  326. }
  327. }
  328. }
  329. // --------------------------------------------------------------------------
  330. void ctkPopupWidget::leaveEvent(QEvent* event)
  331. {
  332. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  333. this->Superclass::leaveEvent(event);
  334. }
  335. // --------------------------------------------------------------------------
  336. void ctkPopupWidget::enterEvent(QEvent* event)
  337. {
  338. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  339. this->Superclass::enterEvent(event);
  340. }
  341. // --------------------------------------------------------------------------
  342. bool ctkPopupWidget::eventFilter(QObject* obj, QEvent* event)
  343. {
  344. Q_D(ctkPopupWidget);
  345. // Here we listen to PopupPixmapWidget, BaseWidget and ctkPopupWidget
  346. // children popups that were under the mouse
  347. switch(event->type())
  348. {
  349. case QEvent::Move:
  350. {
  351. if (obj != d->BaseWidget)
  352. {
  353. break;
  354. }
  355. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  356. QRect newBaseGeometry = d->baseGeometry();
  357. newBaseGeometry.moveTopLeft(d->mapToGlobal(moveEvent->pos()));
  358. QRect desiredGeometry = d->desiredOpenGeometry(newBaseGeometry);
  359. this->move(desiredGeometry.topLeft());
  360. //this->move(this->pos() + moveEvent->pos() - moveEvent->oldPos());
  361. this->update();
  362. break;
  363. }
  364. case QEvent::Hide:
  365. case QEvent::Close:
  366. // if the mouse was in a base widget child popup, then when we leave
  367. // the popup we want to check if it needs to be closed.
  368. if (obj != d->BaseWidget)
  369. {
  370. if (obj != d->PopupPixmapWidget &&
  371. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  372. {
  373. obj->removeEventFilter(this);
  374. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  375. }
  376. break;
  377. }
  378. d->temporarilyHiddenOn();
  379. break;
  380. case QEvent::Show:
  381. if (obj != d->BaseWidget)
  382. {
  383. break;
  384. }
  385. this->setGeometry(d->desiredOpenGeometry());
  386. d->temporarilyHiddenOff();
  387. break;
  388. case QEvent::Resize:
  389. if (obj != d->BaseWidget ||
  390. !(d->Alignment & Qt::AlignJustify ||
  391. (d->Alignment & Qt::AlignTop && d->Alignment & Qt::AlignBottom)) ||
  392. !(d->isOpening() || this->isVisible()))
  393. {
  394. break;
  395. }
  396. // TODO: bug when the effect is WindowOpacityFadeEffect
  397. this->setGeometry(d->desiredOpenGeometry());
  398. break;
  399. case QEvent::Enter:
  400. if ( d->currentAnimation()->state() == QAbstractAnimation::Stopped )
  401. {
  402. // Maybe the user moved the mouse on the widget by mistake, don't open
  403. // the popup instantly...
  404. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  405. }
  406. else
  407. {
  408. // ... except if the popup is closing, we want to reopen it as sooon as
  409. // possible.
  410. this->updatePopup();
  411. }
  412. break;
  413. case QEvent::Leave:
  414. // Don't listen to base widget children that are popups as what
  415. // matters here is their close event instead
  416. if (obj != d->BaseWidget &&
  417. obj != d->PopupPixmapWidget &&
  418. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  419. {
  420. break;
  421. }
  422. // The mouse might have left the area that keeps the popup open
  423. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  424. if (obj != d->BaseWidget &&
  425. obj != d->PopupPixmapWidget)
  426. {
  427. obj->removeEventFilter(this);
  428. }
  429. break;
  430. default:
  431. break;
  432. }
  433. return this->QObject::eventFilter(obj, event);
  434. }
  435. // --------------------------------------------------------------------------
  436. void ctkPopupWidget::updatePopup()
  437. {
  438. Q_D(ctkPopupWidget);
  439. // Querying mouseOver can be slow, don't do it if not needed.
  440. QWidget* mouseOver = (d->AutoShow || d->AutoHide) ? d->mouseOver() : 0;
  441. if (d->AutoShow && mouseOver)
  442. {
  443. this->showPopup();
  444. }
  445. else if (d->AutoHide && !mouseOver)
  446. {
  447. this->hidePopup();
  448. }
  449. }
  450. // --------------------------------------------------------------------------
  451. void ctkPopupWidget::hidePopup()
  452. {
  453. Q_D(ctkPopupWidget);
  454. // just in case it was set.
  455. this->setProperty("forcedClosed", 0);
  456. this->Superclass::hidePopup();
  457. }
  458. // --------------------------------------------------------------------------
  459. void ctkPopupWidget::pinPopup(bool pin)
  460. {
  461. Q_D(ctkPopupWidget);
  462. this->setAutoHide(!pin);
  463. if (pin)
  464. {
  465. this->showPopup();
  466. }
  467. else
  468. {
  469. // When closing, we don't want to inadvertently re-open the menu.
  470. this->setProperty("AutoShowOnClose", this->autoShow());
  471. d->AutoShow = false;
  472. this->hidePopup();
  473. }
  474. }