ctkPopupWidget.cpp 24 KB

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