ctkPopupWidget.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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.commontk.org/LICENSE
  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. #define LEAVE_CLOSING_DELAY 100 // we don't want to be too fast to close
  32. #define ENTER_OPENING_DELAY 20 // we want to be responsive but allow "errors"
  33. #define DEFAULT_FADING_DURATION 333 // fast enough without being too slow
  34. // -------------------------------------------------------------------------
  35. QGradient* duplicateGradient(const QGradient* gradient)
  36. {
  37. QGradient* newGradient = 0;
  38. switch (gradient->type())
  39. {
  40. case QGradient::LinearGradient:
  41. {
  42. const QLinearGradient* linearGradient = static_cast<const QLinearGradient*>(gradient);
  43. newGradient = new QLinearGradient(linearGradient->start(), linearGradient->finalStop());
  44. break;
  45. }
  46. case QGradient::RadialGradient:
  47. {
  48. const QRadialGradient* radialGradient = static_cast<const QRadialGradient*>(gradient);
  49. newGradient = new QRadialGradient(radialGradient->center(), radialGradient->radius());
  50. break;
  51. }
  52. case QGradient::ConicalGradient:
  53. {
  54. const QConicalGradient* conicalGradient = static_cast<const QConicalGradient*>(gradient);
  55. newGradient = new QConicalGradient(conicalGradient->center(), conicalGradient->angle());
  56. break;
  57. }
  58. default:
  59. break;
  60. }
  61. if (!newGradient)
  62. {
  63. Q_ASSERT(gradient->type() != QGradient::NoGradient);
  64. return newGradient;
  65. }
  66. newGradient->setCoordinateMode(gradient->coordinateMode());
  67. newGradient->setSpread(gradient->spread());
  68. newGradient->setStops(gradient->stops());
  69. return newGradient;
  70. }
  71. // -------------------------------------------------------------------------
  72. ctkPopupWidgetPrivate::ctkPopupWidgetPrivate(ctkPopupWidget& object)
  73. :q_ptr(&object)
  74. {
  75. this->BaseWidget = 0;
  76. this->AutoShow = true;
  77. this->AutoHide = true;
  78. this->Effect = ctkPopupWidget::ScrollEffect;
  79. this->EffectAlpha = 1.;
  80. this->AlphaAnimation = 0;
  81. this->ForcedTranslucent = false;
  82. this->ScrollAnimation = 0;
  83. this->PopupPixmapWidget = 0;
  84. // Geometry attributes
  85. this->Alignment = Qt::AlignJustify | Qt::AlignBottom;
  86. this->Orientations = Qt::Vertical;
  87. this->VerticalDirection = ctkPopupWidget::TopToBottom;
  88. this->HorizontalDirection = Qt::LeftToRight;
  89. }
  90. // -------------------------------------------------------------------------
  91. ctkPopupWidgetPrivate::~ctkPopupWidgetPrivate()
  92. {
  93. delete this->PopupPixmapWidget;
  94. }
  95. // -------------------------------------------------------------------------
  96. void ctkPopupWidgetPrivate::init()
  97. {
  98. Q_Q(ctkPopupWidget);
  99. // By default, Tooltips are shown only on active windows. In a popup widget
  100. // case, we sometimes aren't the active window but we still would like to
  101. // show the children tooltips.
  102. q->setAttribute(Qt::WA_AlwaysShowToolTips, true);
  103. this->AlphaAnimation = new QPropertyAnimation(q, "effectAlpha", q);
  104. this->AlphaAnimation->setDuration(DEFAULT_FADING_DURATION);
  105. this->AlphaAnimation->setStartValue(0.);
  106. this->AlphaAnimation->setEndValue(1.);
  107. QObject::connect(this->AlphaAnimation, SIGNAL(finished()),
  108. q, SLOT(onEffectFinished()));
  109. this->PopupPixmapWidget = new QLabel(0, Qt::ToolTip | Qt::FramelessWindowHint);
  110. this->ScrollAnimation = new QPropertyAnimation(q, "effectGeometry", q);
  111. this->ScrollAnimation->setDuration(DEFAULT_FADING_DURATION);
  112. QObject::connect(this->ScrollAnimation, SIGNAL(finished()),
  113. q, SLOT(onEffectFinished()));
  114. QObject::connect(this->ScrollAnimation, SIGNAL(finished()),
  115. this->PopupPixmapWidget, SLOT(hide()));
  116. qApp->installEventFilter(this);
  117. q->setAnimationEffect(this->Effect);
  118. q->setEasingCurve(QEasingCurve::OutCubic);
  119. }
  120. // -------------------------------------------------------------------------
  121. QPropertyAnimation* ctkPopupWidgetPrivate::currentAnimation()const
  122. {
  123. return this->Effect == ctkPopupWidget::ScrollEffect ?
  124. this->ScrollAnimation : this->AlphaAnimation;
  125. }
  126. // -------------------------------------------------------------------------
  127. bool ctkPopupWidgetPrivate::isOpening()const
  128. {
  129. return this->currentAnimation()->state() == QAbstractAnimation::Running &&
  130. this->currentAnimation()->direction() == QAbstractAnimation::Forward;
  131. }
  132. // -------------------------------------------------------------------------
  133. bool ctkPopupWidgetPrivate::isClosing()const
  134. {
  135. return this->currentAnimation()->state() == QAbstractAnimation::Running &&
  136. this->currentAnimation()->direction() == QAbstractAnimation::Backward;
  137. }
  138. // -------------------------------------------------------------------------
  139. QList<const QWidget*> ctkPopupWidgetPrivate::focusWidgets(bool onlyVisible)const
  140. {
  141. Q_Q(const ctkPopupWidget);
  142. QList<const QWidget*> res;
  143. if (!onlyVisible || q->isVisible())
  144. {
  145. res << q;
  146. }
  147. if (this->BaseWidget && (!onlyVisible || this->BaseWidget->isVisible()))
  148. {
  149. res << this->BaseWidget;
  150. }
  151. if (this->PopupPixmapWidget && (!onlyVisible || this->PopupPixmapWidget->isVisible()))
  152. {
  153. res << this->PopupPixmapWidget;
  154. }
  155. return res;
  156. }
  157. // -------------------------------------------------------------------------
  158. bool ctkPopupWidgetPrivate::mouseOver()
  159. {
  160. Q_Q(ctkPopupWidget);
  161. QList<const QWidget*> widgets = this->focusWidgets(true);
  162. foreach(const QWidget* widget, widgets)
  163. {
  164. if (widget->underMouse())
  165. {
  166. return true;
  167. }
  168. }
  169. // Warning QApplication::widgetAt(QCursor::pos()) can be a bit slow...
  170. const QPoint pos = QCursor::pos();
  171. QWidget* widgetUnderCursor = qApp->widgetAt(pos);
  172. foreach(const QWidget* focusWidget, widgets)
  173. {
  174. if (this->isAncestorOf(focusWidget, widgetUnderCursor) &&
  175. // Ignore when cursor is above a title bar of a focusWidget, underMouse
  176. // wouldn't have return false, but QApplication::widgetAt would return
  177. // the widget
  178. (focusWidget != widgetUnderCursor ||
  179. QRect(QPoint(0,0), focusWidget->size()).contains(
  180. focusWidget->mapFromGlobal(pos))))
  181. {
  182. widgetUnderCursor->installEventFilter(q);
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. // -------------------------------------------------------------------------
  189. bool ctkPopupWidgetPrivate::isAncestorOf(const QWidget* ancestor, const QWidget* child)const
  190. {
  191. while (child)
  192. {
  193. if (child == ancestor)
  194. return true;
  195. child = child->parentWidget();
  196. }
  197. return false;
  198. }
  199. // -------------------------------------------------------------------------
  200. void ctkPopupWidgetPrivate::setupPopupPixmapWidget()
  201. {
  202. Q_Q(ctkPopupWidget);
  203. this->PopupPixmapWidget->setAlignment(this->pixmapAlignment());
  204. QPixmap pixmap;
  205. if (q->testAttribute(Qt::WA_TranslucentBackground))
  206. {
  207. // only QImage handle transparency correctly
  208. QImage image(q->geometry().size(), QImage::Format_ARGB32);
  209. image.fill(0);
  210. q->render(&image);
  211. pixmap = QPixmap::fromImage(image);
  212. }
  213. else
  214. {
  215. pixmap = QPixmap::grabWidget(q, QRect(QPoint(0,0), q->geometry().size()));
  216. }
  217. this->PopupPixmapWidget->setPixmap(pixmap);
  218. this->PopupPixmapWidget->setAttribute(
  219. Qt::WA_TranslucentBackground, q->testAttribute(Qt::WA_TranslucentBackground));
  220. this->PopupPixmapWidget->setWindowOpacity(q->windowOpacity());
  221. }
  222. // -------------------------------------------------------------------------
  223. Qt::Alignment ctkPopupWidgetPrivate::pixmapAlignment()const
  224. {
  225. Qt::Alignment alignment;
  226. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  227. {
  228. alignment |= Qt::AlignBottom;
  229. }
  230. else// if (this->VerticalDirection == ctkPopupWidget::BottomToTop)
  231. {
  232. alignment |= Qt::AlignTop;
  233. }
  234. if (this->HorizontalDirection == Qt::LeftToRight)
  235. {
  236. alignment |= Qt::AlignRight;
  237. }
  238. else// if (this->VerticalDirection == ctkPopupWidget::BottomToTop)
  239. {
  240. alignment |= Qt::AlignLeft;
  241. }
  242. return alignment;
  243. }
  244. // -------------------------------------------------------------------------
  245. QRect ctkPopupWidgetPrivate::closedGeometry()const
  246. {
  247. Q_Q(const ctkPopupWidget);
  248. return this->closedGeometry(q->geometry());
  249. }
  250. // -------------------------------------------------------------------------
  251. QRect ctkPopupWidgetPrivate::closedGeometry(QRect openGeom)const
  252. {
  253. if (this->Orientations & Qt::Vertical)
  254. {
  255. if (this->VerticalDirection == ctkPopupWidget::BottomToTop)
  256. {
  257. openGeom.moveTop(openGeom.bottom());
  258. }
  259. openGeom.setHeight(0);
  260. }
  261. if (this->Orientations & Qt::Horizontal)
  262. {
  263. if (this->HorizontalDirection == Qt::RightToLeft)
  264. {
  265. openGeom.moveLeft(openGeom.right());
  266. }
  267. openGeom.setWidth(0);
  268. }
  269. return openGeom;
  270. }
  271. // -------------------------------------------------------------------------
  272. QRect ctkPopupWidgetPrivate::desiredOpenGeometry()const
  273. {
  274. Q_Q(const ctkPopupWidget);
  275. QSize size = q->size();
  276. if (!q->testAttribute(Qt::WA_WState_Created))
  277. {
  278. size = q->sizeHint();
  279. }
  280. if (!this->BaseWidget)
  281. {
  282. return QRect(q->pos(), size);
  283. }
  284. QRect geometry;
  285. if (this->Alignment & Qt::AlignJustify)
  286. {
  287. if (this->Orientations & Qt::Vertical)
  288. {
  289. size.setWidth(this->BaseWidget->width());
  290. }
  291. }
  292. if (this->Alignment & Qt::AlignTop &&
  293. this->Alignment & Qt::AlignBottom)
  294. {
  295. size.setHeight(this->BaseWidget->height());
  296. }
  297. geometry.setSize(size);
  298. QPoint topLeft = QPoint(this->BaseWidget->geometry().left(), this->BaseWidget->geometry().top());
  299. QPoint bottomRight = QPoint(this->BaseWidget->geometry().right(), this->BaseWidget->geometry().bottom());
  300. topLeft = this->BaseWidget->parentWidget() ? this->BaseWidget->parentWidget()->mapToGlobal(topLeft) : topLeft;
  301. bottomRight = this->BaseWidget->parentWidget() ? this->BaseWidget->parentWidget()->mapToGlobal(bottomRight) : bottomRight;
  302. if (this->Alignment & Qt::AlignLeft)
  303. {
  304. if (this->HorizontalDirection == Qt::LeftToRight)
  305. {
  306. geometry.moveLeft(topLeft.x());
  307. }
  308. else
  309. {
  310. geometry.moveRight(topLeft.x());
  311. }
  312. }
  313. else if (this->Alignment & Qt::AlignRight)
  314. {
  315. if (this->HorizontalDirection == Qt::LeftToRight)
  316. {
  317. geometry.moveLeft(bottomRight.x());
  318. }
  319. else
  320. {
  321. geometry.moveRight(bottomRight.x());
  322. }
  323. }
  324. else if (this->Alignment & Qt::AlignHCenter)
  325. {
  326. geometry.moveLeft((topLeft.x() + bottomRight.x()) / 2 - size.width() / 2);
  327. }
  328. else if (this->Alignment & Qt::AlignJustify)
  329. {
  330. geometry.moveLeft(topLeft.x());
  331. }
  332. if (this->Alignment & Qt::AlignTop)
  333. {
  334. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  335. {
  336. geometry.moveTop(topLeft.y());
  337. }
  338. else
  339. {
  340. geometry.moveBottom(topLeft.y());
  341. }
  342. }
  343. else if (this->Alignment & Qt::AlignBottom)
  344. {
  345. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  346. {
  347. geometry.moveTop(bottomRight.y());
  348. }
  349. else
  350. {
  351. geometry.moveBottom(bottomRight.y());
  352. }
  353. }
  354. else if (this->Alignment & Qt::AlignVCenter)
  355. {
  356. geometry.moveTop((topLeft.y() + bottomRight.y()) / 2 - size.height() / 2);
  357. }
  358. return geometry;
  359. }
  360. // -------------------------------------------------------------------------
  361. bool ctkPopupWidgetPrivate::eventFilter(QObject* obj, QEvent* event)
  362. {
  363. Q_Q(ctkPopupWidget);
  364. QWidget* widget = qobject_cast<QWidget*>(obj);
  365. // Here are the application events, it's a lot of events, so we need to be
  366. // careful to be fast.
  367. if (event->type() == QEvent::ApplicationDeactivate)
  368. {
  369. // We wait to see if there is no other window being active
  370. QTimer::singleShot(0, this, SLOT(onApplicationDeactivate()));
  371. }
  372. else if (event->type() == QEvent::ApplicationActivate)
  373. {
  374. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  375. }
  376. if (!this->BaseWidget)
  377. {
  378. return false;
  379. }
  380. if (event->type() == QEvent::Move && widget != this->BaseWidget)
  381. {
  382. if (widget->isAncestorOf(this->BaseWidget))
  383. {
  384. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  385. q->move(q->pos() + moveEvent->pos() - moveEvent->oldPos());
  386. }
  387. else if (widget->isWindow() &&
  388. widget->windowType() != Qt::ToolTip &&
  389. widget->windowType() != Qt::Popup)
  390. {
  391. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  392. }
  393. }
  394. else if (event->type() == QEvent::Resize)
  395. {
  396. if (widget->isWindow() &&
  397. widget != this->BaseWidget->window() &&
  398. widget->windowType() != Qt::ToolTip &&
  399. widget->windowType() != Qt::Popup)
  400. {
  401. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  402. }
  403. }
  404. else if (event->type() == QEvent::WindowStateChange &&
  405. widget != this->BaseWidget->window() &&
  406. widget->windowType() != Qt::ToolTip &&
  407. widget->windowType() != Qt::Popup)
  408. {
  409. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  410. }
  411. else if ((event->type() == QEvent::WindowActivate ||
  412. event->type() == QEvent::WindowDeactivate) &&
  413. widget == this->BaseWidget->window())
  414. {
  415. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  416. }
  417. return false;
  418. }
  419. // -------------------------------------------------------------------------
  420. void ctkPopupWidgetPrivate::onApplicationDeactivate()
  421. {
  422. // Still no active window, that means the user now is controlling another
  423. // application, we have no control over when the other app moves over the
  424. // popup, so we hide the popup as it would show on top of the other app.
  425. if (!qApp->activeWindow())
  426. {
  427. this->temporarilyHiddenOn();
  428. }
  429. }
  430. // -------------------------------------------------------------------------
  431. void ctkPopupWidgetPrivate::updateVisibility()
  432. {
  433. Q_Q(ctkPopupWidget);
  434. // If the BaseWidget window is active, then there is no reason to cover the
  435. // popup.
  436. if (!this->BaseWidget ||
  437. (!this->BaseWidget->window()->isActiveWindow() &&
  438. !qApp->activeWindow() ||
  439. (qApp->activeWindow()->windowType() != Qt::ToolTip &&
  440. qApp->activeWindow()->windowType() != Qt::Popup)))
  441. {
  442. foreach(QWidget* topLevelWidget, qApp->topLevelWidgets())
  443. {
  444. // If there is at least 1 window (active or not) that covers the popup,
  445. // then we ensure the popup is hidden.
  446. // We have no way of knowing which toplevel is over (z-order) which one,
  447. // it is an OS specific information.
  448. // Of course, tooltips and popups don't count as covering windows.
  449. if (topLevelWidget->isVisible() &&
  450. !(topLevelWidget->windowState() & Qt::WindowMinimized) &&
  451. topLevelWidget->windowType() != Qt::ToolTip &&
  452. topLevelWidget->windowType() != Qt::Popup &&
  453. topLevelWidget != (this->BaseWidget ? this->BaseWidget->window() : 0) &&
  454. topLevelWidget->frameGeometry().intersects(q->geometry()))
  455. {
  456. //qDebug() << "hide" << q << "because of: " << topLevelWidget
  457. // << " with windowType: " << topLevelWidget->windowType()
  458. // << topLevelWidget->isVisible()
  459. // << (this->BaseWidget ? this->BaseWidget->window() : 0)
  460. // << topLevelWidget->frameGeometry();
  461. this->temporarilyHiddenOn();
  462. return;
  463. }
  464. }
  465. }
  466. // If the base widget is hidden or minimized, we don't want to restore the
  467. // popup.
  468. if (this->BaseWidget &&
  469. (!this->BaseWidget->isVisible() ||
  470. this->BaseWidget->window()->windowState() & Qt::WindowMinimized))
  471. {
  472. return;
  473. }
  474. // Restore the visibility of the popup if it was hidden
  475. this->temporarilyHiddenOff();
  476. }
  477. // -------------------------------------------------------------------------
  478. void ctkPopupWidgetPrivate::onBaseWidgetDestroyed()
  479. {
  480. Q_Q(ctkPopupWidget);
  481. this->hideAll();
  482. q->setBaseWidget(0);
  483. // could be a property.
  484. q->deleteLater();
  485. }
  486. // -------------------------------------------------------------------------
  487. void ctkPopupWidgetPrivate::temporarilyHiddenOn()
  488. {
  489. Q_Q(ctkPopupWidget);
  490. if (!this->AutoHide &&
  491. (q->isVisible() || this->isOpening()) &&
  492. !(q->isHidden() || this->isClosing()))
  493. {
  494. this->setProperty("forcedClosed", this->isOpening() ? 2 : 1);
  495. }
  496. this->currentAnimation()->stop();
  497. this->hideAll();
  498. }
  499. // -------------------------------------------------------------------------
  500. void ctkPopupWidgetPrivate::temporarilyHiddenOff()
  501. {
  502. Q_Q(ctkPopupWidget);
  503. int forcedClosed = this->property("forcedClosed").toInt();
  504. if (forcedClosed > 0)
  505. {
  506. q->show();
  507. if (forcedClosed == 2)
  508. {
  509. emit q->popupOpened(true);
  510. }
  511. this->setProperty("forcedClosed", 0);
  512. }
  513. else
  514. {
  515. q->updatePopup();
  516. }
  517. }
  518. // -------------------------------------------------------------------------
  519. void ctkPopupWidgetPrivate::hideAll()
  520. {
  521. Q_Q(ctkPopupWidget);
  522. // Before hiding, transfer the active window flag to its parent, this will
  523. // prevent the application to send a ApplicationDeactivate signal that
  524. // doesn't need to be done.
  525. if (q->isActiveWindow() && this->BaseWidget)
  526. {
  527. qApp->setActiveWindow(this->BaseWidget->window());
  528. }
  529. q->hide();
  530. this->PopupPixmapWidget->hide();
  531. // If there is a popup open in the ctkPopupWidget children, then hide it
  532. // as well so we don't have a popup open while the ctkPopupWidget is hidden.
  533. QPointer<QWidget> activePopupWidget = qApp->activePopupWidget();
  534. if (activePopupWidget && this->isAncestorOf(q, activePopupWidget))
  535. {
  536. activePopupWidget->close();
  537. }
  538. }
  539. // -------------------------------------------------------------------------
  540. // Qt::FramelessWindowHint is required on Windows for Translucent background
  541. // Qt::Toolip is preferred to Qt::Popup as it would close itself at the first
  542. // click outside the widget (typically a click in the BaseWidget)
  543. ctkPopupWidget::ctkPopupWidget(QWidget* parentWidget)
  544. : Superclass(QApplication::desktop()->screen(QApplication::desktop()->screenNumber(parentWidget)),
  545. Qt::ToolTip | Qt::FramelessWindowHint)
  546. , d_ptr(new ctkPopupWidgetPrivate(*this))
  547. {
  548. Q_D(ctkPopupWidget);
  549. d->init();
  550. }
  551. // -------------------------------------------------------------------------
  552. ctkPopupWidget::~ctkPopupWidget()
  553. {
  554. }
  555. // -------------------------------------------------------------------------
  556. QWidget* ctkPopupWidget::baseWidget()const
  557. {
  558. Q_D(const ctkPopupWidget);
  559. return d->BaseWidget;
  560. }
  561. // -------------------------------------------------------------------------
  562. void ctkPopupWidget::setBaseWidget(QWidget* widget)
  563. {
  564. Q_D(ctkPopupWidget);
  565. if (d->BaseWidget)
  566. {
  567. d->BaseWidget->removeEventFilter(this);
  568. disconnect(d->BaseWidget, SIGNAL(destroyed(QObject*)),
  569. d, SLOT(onBaseWidgetDestroyed()));
  570. }
  571. d->BaseWidget = widget;
  572. if (d->BaseWidget)
  573. {
  574. d->BaseWidget->installEventFilter(this);
  575. connect(d->BaseWidget, SIGNAL(destroyed(QObject*)),
  576. d, SLOT(onBaseWidgetDestroyed()));
  577. }
  578. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  579. }
  580. // -------------------------------------------------------------------------
  581. bool ctkPopupWidget::autoShow()const
  582. {
  583. Q_D(const ctkPopupWidget);
  584. return d->AutoShow;
  585. }
  586. // -------------------------------------------------------------------------
  587. void ctkPopupWidget::setAutoShow(bool mode)
  588. {
  589. Q_D(ctkPopupWidget);
  590. d->AutoShow = mode;
  591. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  592. }
  593. // -------------------------------------------------------------------------
  594. bool ctkPopupWidget::autoHide()const
  595. {
  596. Q_D(const ctkPopupWidget);
  597. return d->AutoHide;
  598. }
  599. // -------------------------------------------------------------------------
  600. void ctkPopupWidget::setAutoHide(bool mode)
  601. {
  602. Q_D(ctkPopupWidget);
  603. d->AutoHide = mode;
  604. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  605. }
  606. // -------------------------------------------------------------------------
  607. ctkPopupWidget::AnimationEffect ctkPopupWidget::animationEffect()const
  608. {
  609. Q_D(const ctkPopupWidget);
  610. return d->Effect;
  611. }
  612. // -------------------------------------------------------------------------
  613. void ctkPopupWidget::setAnimationEffect(ctkPopupWidget::AnimationEffect effect)
  614. {
  615. Q_D(ctkPopupWidget);
  616. /// TODO: handle the case where there is an animation running
  617. d->Effect = effect;
  618. }
  619. // -------------------------------------------------------------------------
  620. QEasingCurve::Type ctkPopupWidget::easingCurve()const
  621. {
  622. Q_D(const ctkPopupWidget);
  623. return d->AlphaAnimation->easingCurve().type();
  624. }
  625. // -------------------------------------------------------------------------
  626. void ctkPopupWidget::setEasingCurve(QEasingCurve::Type easingCurve)
  627. {
  628. Q_D(ctkPopupWidget);
  629. d->AlphaAnimation->setEasingCurve(easingCurve);
  630. d->ScrollAnimation->setEasingCurve(easingCurve);
  631. }
  632. // -------------------------------------------------------------------------
  633. Qt::Alignment ctkPopupWidget::alignment()const
  634. {
  635. Q_D(const ctkPopupWidget);
  636. return d->Alignment;
  637. }
  638. // -------------------------------------------------------------------------
  639. void ctkPopupWidget::setAlignment(Qt::Alignment alignment)
  640. {
  641. Q_D(ctkPopupWidget);
  642. d->Alignment = alignment;
  643. }
  644. // -------------------------------------------------------------------------
  645. Qt::Orientations ctkPopupWidget::orientation()const
  646. {
  647. Q_D(const ctkPopupWidget);
  648. return d->Orientations;
  649. }
  650. // -------------------------------------------------------------------------
  651. void ctkPopupWidget::setOrientation(Qt::Orientations orientations)
  652. {
  653. Q_D(ctkPopupWidget);
  654. d->Orientations = orientations;
  655. }
  656. // -------------------------------------------------------------------------
  657. ctkPopupWidget::VerticalDirection ctkPopupWidget::verticalDirection()const
  658. {
  659. Q_D(const ctkPopupWidget);
  660. return d->VerticalDirection;
  661. }
  662. // -------------------------------------------------------------------------
  663. void ctkPopupWidget::setVerticalDirection(ctkPopupWidget::VerticalDirection verticalDirection)
  664. {
  665. Q_D(ctkPopupWidget);
  666. d->VerticalDirection = verticalDirection;
  667. }
  668. // -------------------------------------------------------------------------
  669. Qt::LayoutDirection ctkPopupWidget::horizontalDirection()const
  670. {
  671. Q_D(const ctkPopupWidget);
  672. return d->HorizontalDirection;
  673. }
  674. // -------------------------------------------------------------------------
  675. void ctkPopupWidget::setHorizontalDirection(Qt::LayoutDirection horizontalDirection)
  676. {
  677. Q_D(ctkPopupWidget);
  678. d->HorizontalDirection = horizontalDirection;
  679. }
  680. // -------------------------------------------------------------------------
  681. void ctkPopupWidget::onEffectFinished()
  682. {
  683. Q_D(ctkPopupWidget);
  684. if (d->ForcedTranslucent)
  685. {
  686. d->ForcedTranslucent = false;
  687. this->setAttribute(Qt::WA_TranslucentBackground, false);
  688. }
  689. if (qobject_cast<QAbstractAnimation*>(this->sender())->direction() == QAbstractAnimation::Backward)
  690. {
  691. d->hideAll();
  692. emit this->popupOpened(false);
  693. /// restore the AutoShow if needed.
  694. if (!this->property("AutoShowOnClose").isNull())
  695. {
  696. d->AutoShow = this->property("AutoShowOnClose").toBool();
  697. this->setProperty("AutoShowOnClose", QVariant());
  698. }
  699. }
  700. else
  701. {
  702. this->show();
  703. emit this->popupOpened(true);
  704. }
  705. }
  706. // -------------------------------------------------------------------------
  707. void ctkPopupWidget::paintEvent(QPaintEvent* event)
  708. {
  709. Q_D(ctkPopupWidget);
  710. Q_UNUSED(event);
  711. QPainter painter(this);
  712. QBrush brush = this->palette().window();
  713. if (brush.style() == Qt::LinearGradientPattern ||
  714. brush.style() == Qt::ConicalGradientPattern ||
  715. brush.style() == Qt::RadialGradientPattern)
  716. {
  717. QGradient* newGradient = duplicateGradient(brush.gradient());
  718. QGradientStops stops;
  719. foreach(QGradientStop stop, newGradient->stops())
  720. {
  721. stop.second.setAlpha(stop.second.alpha() * d->EffectAlpha);
  722. stops.push_back(stop);
  723. }
  724. newGradient->setStops(stops);
  725. brush = QBrush(*newGradient);
  726. delete newGradient;
  727. }
  728. else
  729. {
  730. QColor color = brush.color();
  731. color.setAlpha(color.alpha() * d->EffectAlpha);
  732. brush.setColor(color);
  733. }
  734. //QColor semiTransparentColor = this->palette().window().color();
  735. //semiTransparentColor.setAlpha(d->CurrentAlpha);
  736. painter.fillRect(this->rect(), brush);
  737. // Let the QFrame draw itself if needed
  738. this->Superclass::paintEvent(event);
  739. }
  740. // --------------------------------------------------------------------------
  741. void ctkPopupWidget::leaveEvent(QEvent* event)
  742. {
  743. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  744. this->Superclass::leaveEvent(event);
  745. }
  746. // --------------------------------------------------------------------------
  747. void ctkPopupWidget::enterEvent(QEvent* event)
  748. {
  749. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  750. this->Superclass::enterEvent(event);
  751. }
  752. // --------------------------------------------------------------------------
  753. bool ctkPopupWidget::eventFilter(QObject* obj, QEvent* event)
  754. {
  755. Q_D(ctkPopupWidget);
  756. switch(event->type())
  757. {
  758. case QEvent::Move:
  759. {
  760. if (obj != d->BaseWidget)
  761. {
  762. break;
  763. }
  764. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  765. this->move(this->pos() + moveEvent->pos() - moveEvent->oldPos());
  766. this->update();
  767. break;
  768. }
  769. case QEvent::Hide:
  770. case QEvent::Close:
  771. // if the mouse was in a base widget child popup, then when we leave
  772. // the popup we want to check if it needs to be closed.
  773. if (obj != d->BaseWidget &&
  774. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  775. {
  776. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  777. obj->removeEventFilter(this);
  778. break;
  779. }
  780. d->temporarilyHiddenOn();
  781. break;
  782. case QEvent::Show:
  783. if (obj != d->BaseWidget)
  784. {
  785. break;
  786. }
  787. d->temporarilyHiddenOff();
  788. break;
  789. case QEvent::Resize:
  790. if (obj != d->BaseWidget ||
  791. !(d->Alignment & Qt::AlignJustify ||
  792. (d->Alignment & Qt::AlignTop && d->Alignment & Qt::AlignBottom)) ||
  793. !(d->isOpening() || this->isVisible()))
  794. {
  795. break;
  796. }
  797. // TODO: bug when the effect is WindowOpacityFadeEffect
  798. this->setGeometry(d->desiredOpenGeometry());
  799. break;
  800. case QEvent::Enter:
  801. if ( d->currentAnimation()->state() == QAbstractAnimation::Stopped )
  802. {
  803. // Maybe the user moved the mouse on the widget by mistake, don't open
  804. // the popup instantly...
  805. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  806. }
  807. else
  808. {
  809. // ... except if the popup is closing, we want to reopen it as sooon as
  810. // possible.
  811. this->updatePopup();
  812. }
  813. break;
  814. case QEvent::Leave:
  815. // Don't listen to base widget children that are popups as what
  816. // matters here is their close event instead
  817. if (obj != d->BaseWidget &&
  818. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  819. {
  820. break;
  821. }
  822. // The mouse might have left the area that keeps the popup open
  823. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  824. if (obj != d->BaseWidget)
  825. {
  826. obj->removeEventFilter(this);
  827. }
  828. break;
  829. default:
  830. break;
  831. }
  832. return this->QObject::eventFilter(obj, event);
  833. }
  834. // --------------------------------------------------------------------------
  835. void ctkPopupWidget::updatePopup()
  836. {
  837. Q_D(ctkPopupWidget);
  838. // Querying mouseOver can be slow, don't do it if not needed.
  839. bool mouseOver = (d->AutoShow || d->AutoHide) && d->mouseOver();
  840. if (d->AutoShow && mouseOver)
  841. {
  842. this->showPopup();
  843. }
  844. else if (d->AutoHide && !mouseOver)
  845. {
  846. this->hidePopup();
  847. }
  848. }
  849. // --------------------------------------------------------------------------
  850. void ctkPopupWidget::showPopup()
  851. {
  852. Q_D(ctkPopupWidget);
  853. if ((this->isVisible() &&
  854. d->currentAnimation()->state() == QAbstractAnimation::Stopped) ||
  855. (d->BaseWidget && !d->BaseWidget->isVisible()))
  856. {
  857. return;
  858. }
  859. // If the layout has never been activated, the widget doesn't know its
  860. // minSize/maxSize and we then wouldn't know what's its true geometry.
  861. if (this->layout() && !this->testAttribute(Qt::WA_WState_Created))
  862. {
  863. this->layout()->activate();
  864. }
  865. this->setGeometry(d->desiredOpenGeometry());
  866. /// Maybe the popup doesn't allow the desiredOpenGeometry if the widget
  867. /// minimum size is larger than the desired size.
  868. QRect openGeometry = this->geometry();
  869. QRect closedGeometry = d->closedGeometry();
  870. d->currentAnimation()->setDirection(QAbstractAnimation::Forward);
  871. switch(d->Effect)
  872. {
  873. case WindowOpacityFadeEffect:
  874. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  875. {
  876. d->ForcedTranslucent = true;
  877. this->setAttribute(Qt::WA_TranslucentBackground, true);
  878. }
  879. this->show();
  880. break;
  881. case ScrollEffect:
  882. {
  883. d->PopupPixmapWidget->setGeometry(closedGeometry);
  884. d->ScrollAnimation->setStartValue(closedGeometry);
  885. d->ScrollAnimation->setEndValue(openGeometry);
  886. d->setupPopupPixmapWidget();
  887. d->PopupPixmapWidget->show();
  888. break;
  889. }
  890. default:
  891. break;
  892. }
  893. switch(d->currentAnimation()->state())
  894. {
  895. case QAbstractAnimation::Stopped:
  896. d->currentAnimation()->start();
  897. break;
  898. case QAbstractAnimation::Paused:
  899. d->currentAnimation()->resume();
  900. break;
  901. default:
  902. case QAbstractAnimation::Running:
  903. break;
  904. }
  905. }
  906. // --------------------------------------------------------------------------
  907. void ctkPopupWidget::hidePopup()
  908. {
  909. Q_D(ctkPopupWidget);
  910. // just in case it was set.
  911. this->setProperty("forcedClosed", 0);
  912. if (!this->isVisible() &&
  913. d->currentAnimation()->state() == QAbstractAnimation::Stopped)
  914. {
  915. return;
  916. }
  917. d->currentAnimation()->setDirection(QAbstractAnimation::Backward);
  918. QRect openGeometry = this->geometry();
  919. QRect closedGeometry = d->closedGeometry();
  920. switch(d->Effect)
  921. {
  922. case WindowOpacityFadeEffect:
  923. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  924. {
  925. d->ForcedTranslucent = true;
  926. this->setAttribute(Qt::WA_TranslucentBackground, true);
  927. }
  928. break;
  929. case ScrollEffect:
  930. {
  931. d->ScrollAnimation->setStartValue(closedGeometry);
  932. d->ScrollAnimation->setEndValue(openGeometry);
  933. d->setupPopupPixmapWidget();
  934. d->PopupPixmapWidget->setGeometry(this->geometry());
  935. d->PopupPixmapWidget->show();
  936. if (this->isActiveWindow())
  937. {
  938. qApp->setActiveWindow(d->BaseWidget ? d->BaseWidget->window() : 0);
  939. }
  940. this->hide();
  941. break;
  942. }
  943. default:
  944. break;
  945. }
  946. switch(d->currentAnimation()->state())
  947. {
  948. case QAbstractAnimation::Stopped:
  949. d->currentAnimation()->start();
  950. break;
  951. case QAbstractAnimation::Paused:
  952. d->currentAnimation()->resume();
  953. break;
  954. default:
  955. case QAbstractAnimation::Running:
  956. break;
  957. }
  958. }
  959. // --------------------------------------------------------------------------
  960. void ctkPopupWidget::pinPopup(bool pin)
  961. {
  962. Q_D(ctkPopupWidget);
  963. this->setAutoHide(!pin);
  964. if (pin)
  965. {
  966. this->showPopup();
  967. }
  968. else
  969. {
  970. // When closing, we don't want to inadvertently re-open the menu.
  971. this->setProperty("AutoShowOnClose", this->autoShow());
  972. d->AutoShow = false;
  973. this->hidePopup();
  974. }
  975. }
  976. // --------------------------------------------------------------------------
  977. double ctkPopupWidget::effectAlpha()const
  978. {
  979. Q_D(const ctkPopupWidget);
  980. return d->EffectAlpha;
  981. }
  982. // --------------------------------------------------------------------------
  983. void ctkPopupWidget::setEffectAlpha(double alpha)
  984. {
  985. Q_D(ctkPopupWidget);
  986. d->EffectAlpha = alpha;
  987. this->repaint();
  988. }
  989. // --------------------------------------------------------------------------
  990. QRect ctkPopupWidget::effectGeometry()const
  991. {
  992. Q_D(const ctkPopupWidget);
  993. return d->PopupPixmapWidget->geometry();
  994. }
  995. // --------------------------------------------------------------------------
  996. void ctkPopupWidget::setEffectGeometry(QRect newGeometry)
  997. {
  998. Q_D(ctkPopupWidget);
  999. d->PopupPixmapWidget->setGeometry(newGeometry);
  1000. d->PopupPixmapWidget->repaint();
  1001. }