ctkPopupWidget.cpp 17 KB

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