ctkPopupWidget.cpp 17 KB

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