ctkPopupWidget.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 <QPainter>
  24. #include <QPropertyAnimation>
  25. #include <QStyle>
  26. #include <QTimer>
  27. // CTK includes
  28. #include "ctkPopupWidget.h"
  29. #define LEAVE_CLOSING_DELAY 100 // we don't want to be too fast to close
  30. #define ENTER_OPENING_DELAY 20 // we want to be responsive but allow "errors"
  31. #define DEFAULT_FADING_DURATION 333 // fast enough without being too slow
  32. // -------------------------------------------------------------------------
  33. QGradient* duplicateGradient(const QGradient* gradient)
  34. {
  35. QGradient* newGradient = 0;
  36. switch (gradient->type())
  37. {
  38. case QGradient::LinearGradient:
  39. {
  40. const QLinearGradient* linearGradient = static_cast<const QLinearGradient*>(gradient);
  41. newGradient = new QLinearGradient(linearGradient->start(), linearGradient->finalStop());
  42. break;
  43. }
  44. case QGradient::RadialGradient:
  45. {
  46. const QRadialGradient* radialGradient = static_cast<const QRadialGradient*>(gradient);
  47. newGradient = new QRadialGradient(radialGradient->center(), radialGradient->radius());
  48. break;
  49. }
  50. case QGradient::ConicalGradient:
  51. {
  52. const QConicalGradient* conicalGradient = static_cast<const QConicalGradient*>(gradient);
  53. newGradient = new QConicalGradient(conicalGradient->center(), conicalGradient->angle());
  54. break;
  55. }
  56. default:
  57. break;
  58. }
  59. if (!newGradient)
  60. {
  61. Q_ASSERT(gradient->type() != QGradient::NoGradient);
  62. return newGradient;
  63. }
  64. newGradient->setCoordinateMode(gradient->coordinateMode());
  65. newGradient->setSpread(gradient->spread());
  66. newGradient->setStops(gradient->stops());
  67. return newGradient;
  68. }
  69. // -------------------------------------------------------------------------
  70. class ctkPopupWidgetPrivate
  71. {
  72. Q_DECLARE_PUBLIC(ctkPopupWidget);
  73. protected:
  74. ctkPopupWidget* const q_ptr;
  75. public:
  76. ctkPopupWidgetPrivate(ctkPopupWidget& object);
  77. ~ctkPopupWidgetPrivate();
  78. void init();
  79. bool fitBaseWidgetSize()const;
  80. Qt::Alignment pixmapAlignment()const;
  81. void setupPopupPixmapWidget();
  82. QList<const QWidget*> focusWidgets(bool onlyVisible = false)const;
  83. // Return true if the mouse cursor is above any of the focus widgets or their
  84. // children.
  85. // If the cursor is above a child widget, install the event filter to listen
  86. // when the cursor leaves the widget.
  87. bool mouseOver();
  88. // Same as QWidget::isAncestorOf() but don't restrain to the same window
  89. // and apply it to all the focusWidgets
  90. bool isAncestorOf(const QWidget* ancestor, const QWidget* child)const;
  91. /// Return the closed geometry for the popup based on the current geometry
  92. QRect closedGeometry()const;
  93. /// Return the closed geometry for a given open geometry
  94. QRect closedGeometry(QRect openGeom)const;
  95. /// Return the desired geometry, maybe it won't happen if the size is too
  96. /// small for the popup.
  97. QRect desiredOpenGeometry()const;
  98. QPropertyAnimation* currentAnimation()const;
  99. QWidget* BaseWidget;
  100. bool AutoShow;
  101. bool AutoHide;
  102. double EffectAlpha;
  103. ctkPopupWidget::AnimationEffect Effect;
  104. QPropertyAnimation* AlphaAnimation;
  105. bool ForcedTranslucent;
  106. QPropertyAnimation* ScrollAnimation;
  107. QLabel* PopupPixmapWidget;
  108. // Geometry attributes
  109. Qt::Alignment Alignment;
  110. Qt::Orientation Orientation;
  111. ctkPopupWidget::VerticalDirection VerticalDirection;
  112. Qt::LayoutDirection HorizontalDirection;
  113. };
  114. // -------------------------------------------------------------------------
  115. ctkPopupWidgetPrivate::ctkPopupWidgetPrivate(ctkPopupWidget& object)
  116. :q_ptr(&object)
  117. {
  118. this->BaseWidget = 0;
  119. this->AutoShow = true;
  120. this->AutoHide = true;
  121. this->Effect = ctkPopupWidget::ScrollEffect;
  122. this->EffectAlpha = 1.;
  123. this->AlphaAnimation = 0;
  124. this->ForcedTranslucent = false;
  125. this->ScrollAnimation = 0;
  126. this->PopupPixmapWidget = 0;
  127. // Geometry attributes
  128. this->Alignment = Qt::AlignJustify | Qt::AlignBottom;
  129. this->Orientation = Qt::Vertical;
  130. this->VerticalDirection = ctkPopupWidget::TopToBottom;
  131. this->HorizontalDirection = Qt::LeftToRight;
  132. }
  133. // -------------------------------------------------------------------------
  134. ctkPopupWidgetPrivate::~ctkPopupWidgetPrivate()
  135. {
  136. delete this->PopupPixmapWidget;
  137. }
  138. // -------------------------------------------------------------------------
  139. void ctkPopupWidgetPrivate::init()
  140. {
  141. Q_Q(ctkPopupWidget);
  142. this->AlphaAnimation = new QPropertyAnimation(q, "effectAlpha", q);
  143. this->AlphaAnimation->setDuration(DEFAULT_FADING_DURATION);
  144. this->AlphaAnimation->setStartValue(0.);
  145. this->AlphaAnimation->setEndValue(1.);
  146. QObject::connect(this->AlphaAnimation, SIGNAL(finished()),
  147. q, SLOT(onEffectFinished()));
  148. this->PopupPixmapWidget = new QLabel(0, Qt::ToolTip | Qt::FramelessWindowHint);
  149. this->ScrollAnimation = new QPropertyAnimation(q, "effectGeometry", q);
  150. this->ScrollAnimation->setDuration(DEFAULT_FADING_DURATION);
  151. QObject::connect(this->ScrollAnimation, SIGNAL(finished()),
  152. q, SLOT(onEffectFinished()));
  153. QObject::connect(this->ScrollAnimation, SIGNAL(finished()),
  154. this->PopupPixmapWidget, SLOT(hide()));
  155. q->setAnimationEffect(this->Effect);
  156. q->setEasingCurve(QEasingCurve::OutCubic);
  157. }
  158. // -------------------------------------------------------------------------
  159. QPropertyAnimation* ctkPopupWidgetPrivate::currentAnimation()const
  160. {
  161. return this->Effect == ctkPopupWidget::ScrollEffect ?
  162. this->ScrollAnimation : this->AlphaAnimation;
  163. }
  164. // -------------------------------------------------------------------------
  165. QList<const QWidget*> ctkPopupWidgetPrivate::focusWidgets(bool onlyVisible)const
  166. {
  167. Q_Q(const ctkPopupWidget);
  168. QList<const QWidget*> res;
  169. if (!onlyVisible || q->isVisible())
  170. {
  171. res << q;
  172. }
  173. if (this->BaseWidget && (!onlyVisible || this->BaseWidget->isVisible()))
  174. {
  175. res << this->BaseWidget;
  176. }
  177. if (this->PopupPixmapWidget && (!onlyVisible || this->PopupPixmapWidget->isVisible()))
  178. {
  179. res << this->PopupPixmapWidget;
  180. }
  181. return res;
  182. }
  183. // -------------------------------------------------------------------------
  184. bool ctkPopupWidgetPrivate::mouseOver()
  185. {
  186. Q_Q(ctkPopupWidget);
  187. QList<const QWidget*> widgets = this->focusWidgets(true);
  188. foreach(const QWidget* widget, widgets)
  189. {
  190. if (widget->underMouse())
  191. {
  192. return true;
  193. }
  194. }
  195. // Warning QApplication::widgetAt(QCursor::pos()) can be a bit slow...
  196. QWidget* widgetUnderCursor = qApp->widgetAt(QCursor::pos());
  197. foreach(const QWidget* focusWidget, widgets)
  198. {
  199. if (this->isAncestorOf(focusWidget, widgetUnderCursor))
  200. {
  201. widgetUnderCursor->installEventFilter(q);
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. // -------------------------------------------------------------------------
  208. bool ctkPopupWidgetPrivate::isAncestorOf(const QWidget* ancestor, const QWidget* child)const
  209. {
  210. while (child)
  211. {
  212. if (child == ancestor)
  213. return true;
  214. child = child->parentWidget();
  215. }
  216. return false;
  217. }
  218. // -------------------------------------------------------------------------
  219. void ctkPopupWidgetPrivate::setupPopupPixmapWidget()
  220. {
  221. Q_Q(ctkPopupWidget);
  222. this->PopupPixmapWidget->setAlignment(this->pixmapAlignment());
  223. QPixmap pixmap;
  224. if (q->testAttribute(Qt::WA_TranslucentBackground))
  225. {
  226. // only QImage handle transparency correctly
  227. QImage image(q->geometry().size(), QImage::Format_ARGB32);
  228. image.fill(0);
  229. q->render(&image);
  230. pixmap = QPixmap::fromImage(image);
  231. }
  232. else
  233. {
  234. pixmap = QPixmap::grabWidget(q, QRect(QPoint(0,0), q->geometry().size()));
  235. }
  236. this->PopupPixmapWidget->setPixmap(pixmap);
  237. this->PopupPixmapWidget->setAttribute(
  238. Qt::WA_TranslucentBackground, q->testAttribute(Qt::WA_TranslucentBackground));
  239. this->PopupPixmapWidget->setWindowOpacity(q->windowOpacity());
  240. }
  241. // -------------------------------------------------------------------------
  242. Qt::Alignment ctkPopupWidgetPrivate::pixmapAlignment()const
  243. {
  244. Qt::Alignment alignment;
  245. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  246. {
  247. alignment |= Qt::AlignBottom;
  248. }
  249. else// if (this->VerticalDirection == ctkPopupWidget::BottomToTop)
  250. {
  251. alignment |= Qt::AlignTop;
  252. }
  253. if (this->HorizontalDirection == Qt::LeftToRight)
  254. {
  255. alignment |= Qt::AlignRight;
  256. }
  257. else// if (this->VerticalDirection == ctkPopupWidget::BottomToTop)
  258. {
  259. alignment |= Qt::AlignLeft;
  260. }
  261. return alignment;
  262. }
  263. // -------------------------------------------------------------------------
  264. QRect ctkPopupWidgetPrivate::closedGeometry()const
  265. {
  266. Q_Q(const ctkPopupWidget);
  267. return this->closedGeometry(q->geometry());
  268. }
  269. // -------------------------------------------------------------------------
  270. QRect ctkPopupWidgetPrivate::closedGeometry(QRect openGeom)const
  271. {
  272. if (this->Orientation & Qt::Vertical)
  273. {
  274. if (this->VerticalDirection == ctkPopupWidget::BottomToTop)
  275. {
  276. openGeom.moveTop(openGeom.bottom());
  277. }
  278. openGeom.setHeight(0);
  279. }
  280. if (this->Orientation & Qt::Horizontal)
  281. {
  282. if (this->HorizontalDirection == Qt::LeftToRight)
  283. {
  284. openGeom.moveLeft(openGeom.right());
  285. }
  286. openGeom.setWidth(0);
  287. }
  288. return openGeom;
  289. }
  290. // -------------------------------------------------------------------------
  291. QRect ctkPopupWidgetPrivate::desiredOpenGeometry()const
  292. {
  293. Q_Q(const ctkPopupWidget);
  294. QSize size = q->size();
  295. if (!q->testAttribute(Qt::WA_WState_Created))
  296. {
  297. size = q->sizeHint();
  298. }
  299. if (!this->BaseWidget)
  300. {
  301. return QRect(q->pos(), size);
  302. }
  303. QRect geometry;
  304. if (this->Alignment & Qt::AlignJustify)
  305. {
  306. if (this->Orientation & Qt::Vertical)
  307. {
  308. size.setWidth(this->BaseWidget->width());
  309. }
  310. }
  311. if (this->Alignment & Qt::AlignTop &&
  312. this->Alignment & Qt::AlignBottom)
  313. {
  314. size.setHeight(this->BaseWidget->height());
  315. }
  316. geometry.setSize(size);
  317. QPoint topLeft = QPoint(this->BaseWidget->geometry().left(), this->BaseWidget->geometry().top());
  318. QPoint bottomRight = QPoint(this->BaseWidget->geometry().right(), this->BaseWidget->geometry().bottom());
  319. topLeft = this->BaseWidget->parentWidget() ? this->BaseWidget->parentWidget()->mapToGlobal(topLeft) : topLeft;
  320. bottomRight = this->BaseWidget->parentWidget() ? this->BaseWidget->parentWidget()->mapToGlobal(bottomRight) : bottomRight;
  321. if (this->Alignment & Qt::AlignLeft)
  322. {
  323. if (this->HorizontalDirection == Qt::LeftToRight)
  324. {
  325. geometry.moveLeft(topLeft.x());
  326. }
  327. else
  328. {
  329. geometry.moveRight(topLeft.x());
  330. }
  331. }
  332. else if (this->Alignment & Qt::AlignRight)
  333. {
  334. if (this->HorizontalDirection == Qt::LeftToRight)
  335. {
  336. geometry.moveLeft(bottomRight.x());
  337. }
  338. else
  339. {
  340. geometry.moveRight(bottomRight.x());
  341. }
  342. }
  343. else if (this->Alignment & Qt::AlignHCenter)
  344. {
  345. if (this->HorizontalDirection == Qt::LeftToRight)
  346. {
  347. geometry.moveLeft((topLeft.x() + bottomRight.x()) / 2 - size.width() / 2);
  348. }
  349. else
  350. {
  351. geometry.moveRight((topLeft.x() + bottomRight.x()) / 2 + size.width() / 2);
  352. }
  353. }
  354. else if (this->Alignment & Qt::AlignJustify)
  355. {
  356. geometry.moveLeft(topLeft.x());
  357. }
  358. if (this->Alignment & Qt::AlignTop)
  359. {
  360. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  361. {
  362. geometry.moveTop(topLeft.y());
  363. }
  364. else
  365. {
  366. geometry.moveBottom(topLeft.y());
  367. }
  368. }
  369. else if (this->Alignment & Qt::AlignBottom)
  370. {
  371. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  372. {
  373. geometry.moveTop(bottomRight.y());
  374. }
  375. else
  376. {
  377. geometry.moveBottom(bottomRight.y());
  378. }
  379. }
  380. else if (this->Alignment & Qt::AlignVCenter)
  381. {
  382. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  383. {
  384. geometry.moveTop((topLeft.y() + bottomRight.y()) / 2 + size.height() / 2);
  385. }
  386. else
  387. {
  388. geometry.moveBottom((topLeft.y() + bottomRight.y()) / 2 - size.height() / 2);
  389. }
  390. }
  391. return geometry;
  392. }
  393. // -------------------------------------------------------------------------
  394. // Qt::FramelessWindowHint is required on Windows for Translucent background
  395. // Qt::Toolip is preferred to Qt::Popup as it would close itself at the first
  396. // click outside the widget (typically a click in the BaseWidget)
  397. ctkPopupWidget::ctkPopupWidget(QWidget* parentWidget)
  398. : Superclass(parentWidget, Qt::ToolTip | Qt::FramelessWindowHint)
  399. , d_ptr(new ctkPopupWidgetPrivate(*this))
  400. {
  401. Q_D(ctkPopupWidget);
  402. d->init();
  403. }
  404. // -------------------------------------------------------------------------
  405. ctkPopupWidget::~ctkPopupWidget()
  406. {
  407. }
  408. // -------------------------------------------------------------------------
  409. QWidget* ctkPopupWidget::baseWidget()const
  410. {
  411. Q_D(const ctkPopupWidget);
  412. return d->BaseWidget;
  413. }
  414. // -------------------------------------------------------------------------
  415. void ctkPopupWidget::setBaseWidget(QWidget* widget)
  416. {
  417. Q_D(ctkPopupWidget);
  418. if (d->BaseWidget)
  419. {
  420. d->BaseWidget->removeEventFilter(this);
  421. }
  422. d->BaseWidget = widget;
  423. if (d->BaseWidget)
  424. {
  425. d->BaseWidget->installEventFilter(this);
  426. }
  427. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  428. }
  429. // -------------------------------------------------------------------------
  430. bool ctkPopupWidget::autoShow()const
  431. {
  432. Q_D(const ctkPopupWidget);
  433. return d->AutoShow;
  434. }
  435. // -------------------------------------------------------------------------
  436. void ctkPopupWidget::setAutoShow(bool mode)
  437. {
  438. Q_D(ctkPopupWidget);
  439. d->AutoShow = mode;
  440. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  441. }
  442. // -------------------------------------------------------------------------
  443. bool ctkPopupWidget::autoHide()const
  444. {
  445. Q_D(const ctkPopupWidget);
  446. return d->AutoHide;
  447. }
  448. // -------------------------------------------------------------------------
  449. void ctkPopupWidget::setAutoHide(bool mode)
  450. {
  451. Q_D(ctkPopupWidget);
  452. d->AutoHide = mode;
  453. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  454. }
  455. // -------------------------------------------------------------------------
  456. ctkPopupWidget::AnimationEffect ctkPopupWidget::animationEffect()const
  457. {
  458. Q_D(const ctkPopupWidget);
  459. return d->Effect;
  460. }
  461. // -------------------------------------------------------------------------
  462. void ctkPopupWidget::setAnimationEffect(ctkPopupWidget::AnimationEffect effect)
  463. {
  464. Q_D(ctkPopupWidget);
  465. /// TODO: handle the case where there is an animation running
  466. d->Effect = effect;
  467. }
  468. // -------------------------------------------------------------------------
  469. QEasingCurve::Type ctkPopupWidget::easingCurve()const
  470. {
  471. Q_D(const ctkPopupWidget);
  472. return d->AlphaAnimation->easingCurve().type();
  473. }
  474. // -------------------------------------------------------------------------
  475. void ctkPopupWidget::setEasingCurve(QEasingCurve::Type easingCurve)
  476. {
  477. Q_D(ctkPopupWidget);
  478. d->AlphaAnimation->setEasingCurve(easingCurve);
  479. d->ScrollAnimation->setEasingCurve(easingCurve);
  480. }
  481. // -------------------------------------------------------------------------
  482. Qt::Alignment ctkPopupWidget::alignment()const
  483. {
  484. Q_D(const ctkPopupWidget);
  485. return d->Alignment;
  486. }
  487. // -------------------------------------------------------------------------
  488. void ctkPopupWidget::setAlignment(Qt::Alignment alignment)
  489. {
  490. Q_D(ctkPopupWidget);
  491. d->Alignment = alignment;
  492. }
  493. // -------------------------------------------------------------------------
  494. Qt::Orientation ctkPopupWidget::orientation()const
  495. {
  496. Q_D(const ctkPopupWidget);
  497. return d->Orientation;
  498. }
  499. // -------------------------------------------------------------------------
  500. void ctkPopupWidget::setOrientation(Qt::Orientation orientation)
  501. {
  502. Q_D(ctkPopupWidget);
  503. d->Orientation = orientation;
  504. }
  505. // -------------------------------------------------------------------------
  506. ctkPopupWidget::VerticalDirection ctkPopupWidget::verticalDirection()const
  507. {
  508. Q_D(const ctkPopupWidget);
  509. return d->VerticalDirection;
  510. }
  511. // -------------------------------------------------------------------------
  512. void ctkPopupWidget::setVerticalDirection(ctkPopupWidget::VerticalDirection verticalDirection)
  513. {
  514. Q_D(ctkPopupWidget);
  515. d->VerticalDirection = verticalDirection;
  516. }
  517. // -------------------------------------------------------------------------
  518. Qt::LayoutDirection ctkPopupWidget::horizontalDirection()const
  519. {
  520. Q_D(const ctkPopupWidget);
  521. return d->HorizontalDirection;
  522. }
  523. // -------------------------------------------------------------------------
  524. void ctkPopupWidget::setHorizontalDirection(Qt::LayoutDirection horizontalDirection)
  525. {
  526. Q_D(ctkPopupWidget);
  527. d->HorizontalDirection = horizontalDirection;
  528. }
  529. // -------------------------------------------------------------------------
  530. void ctkPopupWidget::onEffectFinished()
  531. {
  532. Q_D(ctkPopupWidget);
  533. if (d->ForcedTranslucent)
  534. {
  535. d->ForcedTranslucent = false;
  536. this->setAttribute(Qt::WA_TranslucentBackground, false);
  537. }
  538. if (qobject_cast<QAbstractAnimation*>(this->sender())->direction() == QAbstractAnimation::Backward)
  539. {
  540. this->hide();
  541. }
  542. else
  543. {
  544. this->show();
  545. }
  546. }
  547. // -------------------------------------------------------------------------
  548. void ctkPopupWidget::paintEvent(QPaintEvent* event)
  549. {
  550. Q_D(ctkPopupWidget);
  551. Q_UNUSED(event);
  552. QPainter painter(this);
  553. QBrush brush = this->palette().window();
  554. if (brush.style() == Qt::LinearGradientPattern ||
  555. brush.style() == Qt::ConicalGradientPattern ||
  556. brush.style() == Qt::RadialGradientPattern)
  557. {
  558. QGradient* newGradient = duplicateGradient(brush.gradient());
  559. QGradientStops stops;
  560. foreach(QGradientStop stop, newGradient->stops())
  561. {
  562. stop.second.setAlpha(stop.second.alpha() * d->EffectAlpha);
  563. stops.push_back(stop);
  564. }
  565. newGradient->setStops(stops);
  566. brush = QBrush(*newGradient);
  567. delete newGradient;
  568. }
  569. else
  570. {
  571. QColor color = brush.color();
  572. color.setAlpha(color.alpha() * d->EffectAlpha);
  573. brush.setColor(color);
  574. }
  575. //QColor semiTransparentColor = this->palette().window().color();
  576. //semiTransparentColor.setAlpha(d->CurrentAlpha);
  577. painter.fillRect(this->rect(), brush);
  578. // Let the QFrame draw itself if needed
  579. this->Superclass::paintEvent(event);
  580. }
  581. // --------------------------------------------------------------------------
  582. void ctkPopupWidget::leaveEvent(QEvent* event)
  583. {
  584. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  585. this->Superclass::leaveEvent(event);
  586. }
  587. // --------------------------------------------------------------------------
  588. void ctkPopupWidget::enterEvent(QEvent* event)
  589. {
  590. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  591. this->Superclass::enterEvent(event);
  592. }
  593. // --------------------------------------------------------------------------
  594. bool ctkPopupWidget::eventFilter(QObject* obj, QEvent* event)
  595. {
  596. Q_D(ctkPopupWidget);
  597. if (event->type() == QEvent::Enter)
  598. {
  599. if ( d->currentAnimation()->state() == QAbstractAnimation::Stopped )
  600. {
  601. // Maybe the user moved the mouse on the widget by mistake, don't open
  602. // the popup instantly...
  603. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  604. }
  605. else
  606. {
  607. // ... except if the popup is closing, we want to reopen it as sooon as
  608. // possible.
  609. this->updatePopup();
  610. }
  611. }
  612. else if (event->type() == QEvent::Leave)
  613. {
  614. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  615. if (obj != d->BaseWidget)
  616. {
  617. obj->removeEventFilter(this);
  618. }
  619. }
  620. return this->QObject::eventFilter(obj, event);
  621. }
  622. // --------------------------------------------------------------------------
  623. void ctkPopupWidget::updatePopup()
  624. {
  625. Q_D(ctkPopupWidget);
  626. // Querying mouseOver can be slow, don't do it if not needed.
  627. bool mouseOver = (d->AutoShow || d->AutoHide) && d->mouseOver();
  628. if (d->AutoShow && mouseOver)
  629. {
  630. this->showPopup();
  631. }
  632. else if (d->AutoHide && !mouseOver)
  633. {
  634. this->hidePopup();
  635. }
  636. }
  637. // --------------------------------------------------------------------------
  638. void ctkPopupWidget::showPopup()
  639. {
  640. Q_D(ctkPopupWidget);
  641. if ((this->isVisible() &&
  642. d->currentAnimation()->state() == QAbstractAnimation::Stopped) ||
  643. (d->BaseWidget && !d->BaseWidget->isVisible()))
  644. {
  645. return;
  646. }
  647. // If the layout has never been activated, the widget doesn't know its
  648. // minSize/maxSize and we then wouldn't know what's its true geometry.
  649. if (this->layout() && !this->testAttribute(Qt::WA_WState_Created))
  650. {
  651. this->layout()->activate();
  652. }
  653. this->setGeometry(d->desiredOpenGeometry());
  654. /// Maybe the popup doesn't allow the desiredOpenGeometry if the widget
  655. /// minimum size is larger than the desired size.
  656. QRect openGeometry = this->geometry();
  657. QRect closedGeometry = d->closedGeometry();
  658. d->currentAnimation()->setDirection(QAbstractAnimation::Forward);
  659. switch(d->Effect)
  660. {
  661. case WindowOpacityFadeEffect:
  662. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  663. {
  664. d->ForcedTranslucent = true;
  665. this->setAttribute(Qt::WA_TranslucentBackground, true);
  666. }
  667. this->show();
  668. break;
  669. case ScrollEffect:
  670. {
  671. d->PopupPixmapWidget->setGeometry(closedGeometry);
  672. d->ScrollAnimation->setStartValue(closedGeometry);
  673. d->ScrollAnimation->setEndValue(openGeometry);
  674. d->setupPopupPixmapWidget();
  675. d->PopupPixmapWidget->show();
  676. break;
  677. }
  678. default:
  679. break;
  680. }
  681. switch(d->currentAnimation()->state())
  682. {
  683. case QAbstractAnimation::Stopped:
  684. d->currentAnimation()->start();
  685. break;
  686. case QAbstractAnimation::Paused:
  687. d->currentAnimation()->resume();
  688. break;
  689. default:
  690. case QAbstractAnimation::Running:
  691. break;
  692. }
  693. }
  694. // --------------------------------------------------------------------------
  695. void ctkPopupWidget::hidePopup()
  696. {
  697. Q_D(ctkPopupWidget);
  698. if (!this->isVisible() &&
  699. d->currentAnimation()->state() == QAbstractAnimation::Stopped)
  700. {
  701. return;
  702. }
  703. d->currentAnimation()->setDirection(QAbstractAnimation::Backward);
  704. QRect openGeometry = this->geometry();
  705. QRect closedGeometry = d->closedGeometry();
  706. switch(d->Effect)
  707. {
  708. case WindowOpacityFadeEffect:
  709. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  710. {
  711. d->ForcedTranslucent = true;
  712. this->setAttribute(Qt::WA_TranslucentBackground, true);
  713. }
  714. break;
  715. case ScrollEffect:
  716. {
  717. d->setupPopupPixmapWidget();
  718. d->PopupPixmapWidget->show();
  719. this->hide();
  720. break;
  721. }
  722. default:
  723. break;
  724. }
  725. switch(d->currentAnimation()->state())
  726. {
  727. case QAbstractAnimation::Stopped:
  728. d->currentAnimation()->start();
  729. break;
  730. case QAbstractAnimation::Paused:
  731. d->currentAnimation()->resume();
  732. break;
  733. default:
  734. case QAbstractAnimation::Running:
  735. break;
  736. }
  737. }
  738. // --------------------------------------------------------------------------
  739. void ctkPopupWidget::pinPopup(bool pin)
  740. {
  741. this->setAutoHide(!pin);
  742. if (pin)
  743. {
  744. this->showPopup();
  745. }
  746. }
  747. // --------------------------------------------------------------------------
  748. double ctkPopupWidget::effectAlpha()const
  749. {
  750. Q_D(const ctkPopupWidget);
  751. return d->EffectAlpha;
  752. }
  753. // --------------------------------------------------------------------------
  754. void ctkPopupWidget::setEffectAlpha(double alpha)
  755. {
  756. Q_D(ctkPopupWidget);
  757. d->EffectAlpha = alpha;
  758. this->repaint();
  759. }
  760. // --------------------------------------------------------------------------
  761. QRect ctkPopupWidget::effectGeometry()const
  762. {
  763. Q_D(const ctkPopupWidget);
  764. return d->PopupPixmapWidget->geometry();
  765. }
  766. // --------------------------------------------------------------------------
  767. void ctkPopupWidget::setEffectGeometry(QRect newGeometry)
  768. {
  769. Q_D(ctkPopupWidget);
  770. d->PopupPixmapWidget->setGeometry(newGeometry);
  771. d->PopupPixmapWidget->repaint();
  772. }