ctkPopupWidget.cpp 30 KB

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