ctkPopupWidget.cpp 16 KB

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