ctkPopupWidget.cpp 33 KB

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