ctkPopupWidget.cpp 17 KB

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