ctkPopupWidget.cpp 17 KB

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