ctkPopupWidget.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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 || !this->BaseWidget->window()->isActiveWindow())
  437. {
  438. foreach(QWidget* topLevelWidget, qApp->topLevelWidgets())
  439. {
  440. // If there is at least 1 window (active or not) that covers the popup,
  441. // then we ensure the popup is hidden.
  442. // Of course, tooltips and popups don't count as covering windows.
  443. if (topLevelWidget->isVisible() &&
  444. !(topLevelWidget->windowState() & Qt::WindowMinimized) &&
  445. topLevelWidget->windowType() != Qt::ToolTip &&
  446. topLevelWidget->windowType() != Qt::Popup &&
  447. topLevelWidget != (this->BaseWidget ? this->BaseWidget->window() : 0) &&
  448. topLevelWidget->frameGeometry().intersects(q->geometry()))
  449. {
  450. //qDebug() << "hide" << q << "because of: " << topLevelWidget
  451. // << " with windowType: " << topLevelWidget->windowType();
  452. this->temporarilyHiddenOn();
  453. return;
  454. }
  455. }
  456. }
  457. // If the base widget is hidden or minimized, we don't want to restore the
  458. // popup.
  459. if (this->BaseWidget &&
  460. (!this->BaseWidget->isVisible() ||
  461. this->BaseWidget->window()->windowState() & Qt::WindowMinimized))
  462. {
  463. return;
  464. }
  465. // Restore the visibility of the popup if it was hidden
  466. this->temporarilyHiddenOff();
  467. }
  468. // -------------------------------------------------------------------------
  469. void ctkPopupWidgetPrivate::onBaseWidgetDestroyed()
  470. {
  471. Q_Q(ctkPopupWidget);
  472. this->hideAll();
  473. q->setBaseWidget(0);
  474. // could be a property.
  475. q->deleteLater();
  476. }
  477. // -------------------------------------------------------------------------
  478. void ctkPopupWidgetPrivate::temporarilyHiddenOn()
  479. {
  480. Q_Q(ctkPopupWidget);
  481. if (!this->AutoHide &&
  482. (q->isVisible() || this->isOpening()) &&
  483. !(q->isHidden() || this->isClosing()))
  484. {
  485. this->setProperty("forcedClosed", this->isOpening() ? 2 : 1);
  486. }
  487. this->currentAnimation()->stop();
  488. this->hideAll();
  489. }
  490. // -------------------------------------------------------------------------
  491. void ctkPopupWidgetPrivate::temporarilyHiddenOff()
  492. {
  493. Q_Q(ctkPopupWidget);
  494. int forcedClosed = this->property("forcedClosed").toInt();
  495. if (forcedClosed > 0)
  496. {
  497. q->show();
  498. if (forcedClosed == 2)
  499. {
  500. emit q->popupOpened(true);
  501. }
  502. this->setProperty("forcedClosed", 0);
  503. }
  504. else
  505. {
  506. q->updatePopup();
  507. }
  508. }
  509. // -------------------------------------------------------------------------
  510. void ctkPopupWidgetPrivate::hideAll()
  511. {
  512. Q_Q(ctkPopupWidget);
  513. // Before hiding, transfer the active window flag to its parent, this will
  514. // prevent the application to send a ApplicationDeactivate signal that
  515. // doesn't need to be done.
  516. if (q->isActiveWindow() && this->BaseWidget)
  517. {
  518. qApp->setActiveWindow(this->BaseWidget->window());
  519. }
  520. q->hide();
  521. this->PopupPixmapWidget->hide();
  522. // If there is a popup open in the ctkPopupWidget children, then hide it
  523. // as well so we don't have a popup open while the ctkPopupWidget is hidden.
  524. QPointer<QWidget> activePopupWidget = qApp->activePopupWidget();
  525. if (activePopupWidget && this->isAncestorOf(q, activePopupWidget))
  526. {
  527. activePopupWidget->close();
  528. }
  529. }
  530. // -------------------------------------------------------------------------
  531. // Qt::FramelessWindowHint is required on Windows for Translucent background
  532. // Qt::Toolip is preferred to Qt::Popup as it would close itself at the first
  533. // click outside the widget (typically a click in the BaseWidget)
  534. ctkPopupWidget::ctkPopupWidget(QWidget* parentWidget)
  535. : Superclass(QApplication::desktop()->screen(QApplication::desktop()->screenNumber(parentWidget)),
  536. Qt::ToolTip | Qt::FramelessWindowHint)
  537. , d_ptr(new ctkPopupWidgetPrivate(*this))
  538. {
  539. Q_D(ctkPopupWidget);
  540. d->init();
  541. }
  542. // -------------------------------------------------------------------------
  543. ctkPopupWidget::~ctkPopupWidget()
  544. {
  545. }
  546. // -------------------------------------------------------------------------
  547. QWidget* ctkPopupWidget::baseWidget()const
  548. {
  549. Q_D(const ctkPopupWidget);
  550. return d->BaseWidget;
  551. }
  552. // -------------------------------------------------------------------------
  553. void ctkPopupWidget::setBaseWidget(QWidget* widget)
  554. {
  555. Q_D(ctkPopupWidget);
  556. if (d->BaseWidget)
  557. {
  558. d->BaseWidget->removeEventFilter(this);
  559. disconnect(d->BaseWidget, SIGNAL(destroyed(QObject*)),
  560. d, SLOT(onBaseWidgetDestroyed()));
  561. }
  562. d->BaseWidget = widget;
  563. if (d->BaseWidget)
  564. {
  565. d->BaseWidget->installEventFilter(this);
  566. connect(d->BaseWidget, SIGNAL(destroyed(QObject*)),
  567. d, SLOT(onBaseWidgetDestroyed()));
  568. }
  569. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  570. }
  571. // -------------------------------------------------------------------------
  572. bool ctkPopupWidget::autoShow()const
  573. {
  574. Q_D(const ctkPopupWidget);
  575. return d->AutoShow;
  576. }
  577. // -------------------------------------------------------------------------
  578. void ctkPopupWidget::setAutoShow(bool mode)
  579. {
  580. Q_D(ctkPopupWidget);
  581. d->AutoShow = mode;
  582. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  583. }
  584. // -------------------------------------------------------------------------
  585. bool ctkPopupWidget::autoHide()const
  586. {
  587. Q_D(const ctkPopupWidget);
  588. return d->AutoHide;
  589. }
  590. // -------------------------------------------------------------------------
  591. void ctkPopupWidget::setAutoHide(bool mode)
  592. {
  593. Q_D(ctkPopupWidget);
  594. d->AutoHide = mode;
  595. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  596. }
  597. // -------------------------------------------------------------------------
  598. ctkPopupWidget::AnimationEffect ctkPopupWidget::animationEffect()const
  599. {
  600. Q_D(const ctkPopupWidget);
  601. return d->Effect;
  602. }
  603. // -------------------------------------------------------------------------
  604. void ctkPopupWidget::setAnimationEffect(ctkPopupWidget::AnimationEffect effect)
  605. {
  606. Q_D(ctkPopupWidget);
  607. /// TODO: handle the case where there is an animation running
  608. d->Effect = effect;
  609. }
  610. // -------------------------------------------------------------------------
  611. QEasingCurve::Type ctkPopupWidget::easingCurve()const
  612. {
  613. Q_D(const ctkPopupWidget);
  614. return d->AlphaAnimation->easingCurve().type();
  615. }
  616. // -------------------------------------------------------------------------
  617. void ctkPopupWidget::setEasingCurve(QEasingCurve::Type easingCurve)
  618. {
  619. Q_D(ctkPopupWidget);
  620. d->AlphaAnimation->setEasingCurve(easingCurve);
  621. d->ScrollAnimation->setEasingCurve(easingCurve);
  622. }
  623. // -------------------------------------------------------------------------
  624. Qt::Alignment ctkPopupWidget::alignment()const
  625. {
  626. Q_D(const ctkPopupWidget);
  627. return d->Alignment;
  628. }
  629. // -------------------------------------------------------------------------
  630. void ctkPopupWidget::setAlignment(Qt::Alignment alignment)
  631. {
  632. Q_D(ctkPopupWidget);
  633. d->Alignment = alignment;
  634. }
  635. // -------------------------------------------------------------------------
  636. Qt::Orientations ctkPopupWidget::orientation()const
  637. {
  638. Q_D(const ctkPopupWidget);
  639. return d->Orientations;
  640. }
  641. // -------------------------------------------------------------------------
  642. void ctkPopupWidget::setOrientation(Qt::Orientations orientations)
  643. {
  644. Q_D(ctkPopupWidget);
  645. d->Orientations = orientations;
  646. }
  647. // -------------------------------------------------------------------------
  648. ctkPopupWidget::VerticalDirection ctkPopupWidget::verticalDirection()const
  649. {
  650. Q_D(const ctkPopupWidget);
  651. return d->VerticalDirection;
  652. }
  653. // -------------------------------------------------------------------------
  654. void ctkPopupWidget::setVerticalDirection(ctkPopupWidget::VerticalDirection verticalDirection)
  655. {
  656. Q_D(ctkPopupWidget);
  657. d->VerticalDirection = verticalDirection;
  658. }
  659. // -------------------------------------------------------------------------
  660. Qt::LayoutDirection ctkPopupWidget::horizontalDirection()const
  661. {
  662. Q_D(const ctkPopupWidget);
  663. return d->HorizontalDirection;
  664. }
  665. // -------------------------------------------------------------------------
  666. void ctkPopupWidget::setHorizontalDirection(Qt::LayoutDirection horizontalDirection)
  667. {
  668. Q_D(ctkPopupWidget);
  669. d->HorizontalDirection = horizontalDirection;
  670. }
  671. // -------------------------------------------------------------------------
  672. void ctkPopupWidget::onEffectFinished()
  673. {
  674. Q_D(ctkPopupWidget);
  675. if (d->ForcedTranslucent)
  676. {
  677. d->ForcedTranslucent = false;
  678. this->setAttribute(Qt::WA_TranslucentBackground, false);
  679. }
  680. if (qobject_cast<QAbstractAnimation*>(this->sender())->direction() == QAbstractAnimation::Backward)
  681. {
  682. d->hideAll();
  683. emit this->popupOpened(false);
  684. /// restore the AutoShow if needed.
  685. if (!this->property("AutoShowOnClose").isNull())
  686. {
  687. d->AutoShow = this->property("AutoShowOnClose").toBool();
  688. this->setProperty("AutoShowOnClose", QVariant());
  689. }
  690. }
  691. else
  692. {
  693. this->show();
  694. emit this->popupOpened(true);
  695. }
  696. }
  697. // -------------------------------------------------------------------------
  698. void ctkPopupWidget::paintEvent(QPaintEvent* event)
  699. {
  700. Q_D(ctkPopupWidget);
  701. Q_UNUSED(event);
  702. QPainter painter(this);
  703. QBrush brush = this->palette().window();
  704. if (brush.style() == Qt::LinearGradientPattern ||
  705. brush.style() == Qt::ConicalGradientPattern ||
  706. brush.style() == Qt::RadialGradientPattern)
  707. {
  708. QGradient* newGradient = duplicateGradient(brush.gradient());
  709. QGradientStops stops;
  710. foreach(QGradientStop stop, newGradient->stops())
  711. {
  712. stop.second.setAlpha(stop.second.alpha() * d->EffectAlpha);
  713. stops.push_back(stop);
  714. }
  715. newGradient->setStops(stops);
  716. brush = QBrush(*newGradient);
  717. delete newGradient;
  718. }
  719. else
  720. {
  721. QColor color = brush.color();
  722. color.setAlpha(color.alpha() * d->EffectAlpha);
  723. brush.setColor(color);
  724. }
  725. //QColor semiTransparentColor = this->palette().window().color();
  726. //semiTransparentColor.setAlpha(d->CurrentAlpha);
  727. painter.fillRect(this->rect(), brush);
  728. // Let the QFrame draw itself if needed
  729. this->Superclass::paintEvent(event);
  730. }
  731. // --------------------------------------------------------------------------
  732. void ctkPopupWidget::leaveEvent(QEvent* event)
  733. {
  734. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  735. this->Superclass::leaveEvent(event);
  736. }
  737. // --------------------------------------------------------------------------
  738. void ctkPopupWidget::enterEvent(QEvent* event)
  739. {
  740. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  741. this->Superclass::enterEvent(event);
  742. }
  743. // --------------------------------------------------------------------------
  744. bool ctkPopupWidget::eventFilter(QObject* obj, QEvent* event)
  745. {
  746. Q_D(ctkPopupWidget);
  747. switch(event->type())
  748. {
  749. case QEvent::Move:
  750. {
  751. if (obj != d->BaseWidget)
  752. {
  753. break;
  754. }
  755. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  756. this->move(this->pos() + moveEvent->pos() - moveEvent->oldPos());
  757. this->update();
  758. break;
  759. }
  760. case QEvent::Hide:
  761. case QEvent::Close:
  762. // if the mouse was in a base widget child popup, then when we leave
  763. // the popup we want to check if it needs to be closed.
  764. if (obj != d->BaseWidget &&
  765. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  766. {
  767. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  768. obj->removeEventFilter(this);
  769. break;
  770. }
  771. d->temporarilyHiddenOn();
  772. break;
  773. case QEvent::Show:
  774. if (obj != d->BaseWidget)
  775. {
  776. break;
  777. }
  778. d->temporarilyHiddenOff();
  779. break;
  780. case QEvent::Resize:
  781. if (obj != d->BaseWidget ||
  782. !(d->Alignment & Qt::AlignJustify ||
  783. (d->Alignment & Qt::AlignTop && d->Alignment & Qt::AlignBottom)) ||
  784. !(d->isOpening() || this->isVisible()))
  785. {
  786. break;
  787. }
  788. // TODO: bug when the effect is WindowOpacityFadeEffect
  789. this->setGeometry(d->desiredOpenGeometry());
  790. break;
  791. case QEvent::Enter:
  792. if ( d->currentAnimation()->state() == QAbstractAnimation::Stopped )
  793. {
  794. // Maybe the user moved the mouse on the widget by mistake, don't open
  795. // the popup instantly...
  796. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  797. }
  798. else
  799. {
  800. // ... except if the popup is closing, we want to reopen it as sooon as
  801. // possible.
  802. this->updatePopup();
  803. }
  804. break;
  805. case QEvent::Leave:
  806. // Don't listen to base widget children that are popups as what
  807. // matters here is their close event instead
  808. if (obj != d->BaseWidget &&
  809. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  810. {
  811. break;
  812. }
  813. // The mouse might have left the area that keeps the popup open
  814. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  815. if (obj != d->BaseWidget)
  816. {
  817. obj->removeEventFilter(this);
  818. }
  819. break;
  820. default:
  821. break;
  822. }
  823. return this->QObject::eventFilter(obj, event);
  824. }
  825. // --------------------------------------------------------------------------
  826. void ctkPopupWidget::updatePopup()
  827. {
  828. Q_D(ctkPopupWidget);
  829. // Querying mouseOver can be slow, don't do it if not needed.
  830. bool mouseOver = (d->AutoShow || d->AutoHide) && d->mouseOver();
  831. if (d->AutoShow && mouseOver)
  832. {
  833. this->showPopup();
  834. }
  835. else if (d->AutoHide && !mouseOver)
  836. {
  837. this->hidePopup();
  838. }
  839. }
  840. // --------------------------------------------------------------------------
  841. void ctkPopupWidget::showPopup()
  842. {
  843. Q_D(ctkPopupWidget);
  844. if ((this->isVisible() &&
  845. d->currentAnimation()->state() == QAbstractAnimation::Stopped) ||
  846. (d->BaseWidget && !d->BaseWidget->isVisible()))
  847. {
  848. return;
  849. }
  850. // If the layout has never been activated, the widget doesn't know its
  851. // minSize/maxSize and we then wouldn't know what's its true geometry.
  852. if (this->layout() && !this->testAttribute(Qt::WA_WState_Created))
  853. {
  854. this->layout()->activate();
  855. }
  856. this->setGeometry(d->desiredOpenGeometry());
  857. /// Maybe the popup doesn't allow the desiredOpenGeometry if the widget
  858. /// minimum size is larger than the desired size.
  859. QRect openGeometry = this->geometry();
  860. QRect closedGeometry = d->closedGeometry();
  861. d->currentAnimation()->setDirection(QAbstractAnimation::Forward);
  862. switch(d->Effect)
  863. {
  864. case WindowOpacityFadeEffect:
  865. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  866. {
  867. d->ForcedTranslucent = true;
  868. this->setAttribute(Qt::WA_TranslucentBackground, true);
  869. }
  870. this->show();
  871. break;
  872. case ScrollEffect:
  873. {
  874. d->PopupPixmapWidget->setGeometry(closedGeometry);
  875. d->ScrollAnimation->setStartValue(closedGeometry);
  876. d->ScrollAnimation->setEndValue(openGeometry);
  877. d->setupPopupPixmapWidget();
  878. d->PopupPixmapWidget->show();
  879. break;
  880. }
  881. default:
  882. break;
  883. }
  884. switch(d->currentAnimation()->state())
  885. {
  886. case QAbstractAnimation::Stopped:
  887. d->currentAnimation()->start();
  888. break;
  889. case QAbstractAnimation::Paused:
  890. d->currentAnimation()->resume();
  891. break;
  892. default:
  893. case QAbstractAnimation::Running:
  894. break;
  895. }
  896. }
  897. // --------------------------------------------------------------------------
  898. void ctkPopupWidget::hidePopup()
  899. {
  900. Q_D(ctkPopupWidget);
  901. // just in case it was set.
  902. this->setProperty("forcedClosed", 0);
  903. if (!this->isVisible() &&
  904. d->currentAnimation()->state() == QAbstractAnimation::Stopped)
  905. {
  906. return;
  907. }
  908. d->currentAnimation()->setDirection(QAbstractAnimation::Backward);
  909. QRect openGeometry = this->geometry();
  910. QRect closedGeometry = d->closedGeometry();
  911. switch(d->Effect)
  912. {
  913. case WindowOpacityFadeEffect:
  914. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  915. {
  916. d->ForcedTranslucent = true;
  917. this->setAttribute(Qt::WA_TranslucentBackground, true);
  918. }
  919. break;
  920. case ScrollEffect:
  921. {
  922. d->ScrollAnimation->setStartValue(closedGeometry);
  923. d->ScrollAnimation->setEndValue(openGeometry);
  924. d->setupPopupPixmapWidget();
  925. d->PopupPixmapWidget->setGeometry(this->geometry());
  926. d->PopupPixmapWidget->show();
  927. if (this->isActiveWindow())
  928. {
  929. qApp->setActiveWindow(d->BaseWidget ? d->BaseWidget->window() : 0);
  930. }
  931. this->hide();
  932. break;
  933. }
  934. default:
  935. break;
  936. }
  937. switch(d->currentAnimation()->state())
  938. {
  939. case QAbstractAnimation::Stopped:
  940. d->currentAnimation()->start();
  941. break;
  942. case QAbstractAnimation::Paused:
  943. d->currentAnimation()->resume();
  944. break;
  945. default:
  946. case QAbstractAnimation::Running:
  947. break;
  948. }
  949. }
  950. // --------------------------------------------------------------------------
  951. void ctkPopupWidget::pinPopup(bool pin)
  952. {
  953. Q_D(ctkPopupWidget);
  954. this->setAutoHide(!pin);
  955. if (pin)
  956. {
  957. this->showPopup();
  958. }
  959. else
  960. {
  961. // When closing, we don't want to inadvertently re-open the menu.
  962. this->setProperty("AutoShowOnClose", this->autoShow());
  963. d->AutoShow = false;
  964. this->hidePopup();
  965. }
  966. }
  967. // --------------------------------------------------------------------------
  968. double ctkPopupWidget::effectAlpha()const
  969. {
  970. Q_D(const ctkPopupWidget);
  971. return d->EffectAlpha;
  972. }
  973. // --------------------------------------------------------------------------
  974. void ctkPopupWidget::setEffectAlpha(double alpha)
  975. {
  976. Q_D(ctkPopupWidget);
  977. d->EffectAlpha = alpha;
  978. this->repaint();
  979. }
  980. // --------------------------------------------------------------------------
  981. QRect ctkPopupWidget::effectGeometry()const
  982. {
  983. Q_D(const ctkPopupWidget);
  984. return d->PopupPixmapWidget->geometry();
  985. }
  986. // --------------------------------------------------------------------------
  987. void ctkPopupWidget::setEffectGeometry(QRect newGeometry)
  988. {
  989. Q_D(ctkPopupWidget);
  990. d->PopupPixmapWidget->setGeometry(newGeometry);
  991. d->PopupPixmapWidget->repaint();
  992. }