ctkPopupWidget.cpp 22 KB

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