ctkPopupWidget.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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::baseGeometry()const
  271. {
  272. if (!this->BaseWidget)
  273. {
  274. return QRect();
  275. }
  276. return QRect(this->mapToGlobal(this->BaseWidget->geometry().topLeft()),
  277. this->BaseWidget->size());
  278. }
  279. // -------------------------------------------------------------------------
  280. QPoint ctkPopupWidgetPrivate::mapToGlobal(const QPoint& baseWidgetPoint)const
  281. {
  282. QPoint mappedPoint = baseWidgetPoint;
  283. if (this->BaseWidget && this->BaseWidget->parentWidget())
  284. {
  285. mappedPoint = this->BaseWidget->parentWidget()->mapToGlobal(mappedPoint);
  286. }
  287. return mappedPoint;
  288. }
  289. // -------------------------------------------------------------------------
  290. QRect ctkPopupWidgetPrivate::desiredOpenGeometry()const
  291. {
  292. return this->desiredOpenGeometry(this->baseGeometry());
  293. }
  294. // -------------------------------------------------------------------------
  295. QRect ctkPopupWidgetPrivate::desiredOpenGeometry(QRect baseGeometry)const
  296. {
  297. Q_Q(const ctkPopupWidget);
  298. QSize size = q->size();
  299. if (!q->testAttribute(Qt::WA_WState_Created))
  300. {
  301. size = q->sizeHint();
  302. }
  303. if (baseGeometry.isNull())
  304. {
  305. return QRect(q->pos(), size);
  306. }
  307. QRect geometry;
  308. if (this->Alignment & Qt::AlignJustify)
  309. {
  310. if (this->Orientations & Qt::Vertical)
  311. {
  312. size.setWidth(baseGeometry.width());
  313. }
  314. }
  315. if (this->Alignment & Qt::AlignTop &&
  316. this->Alignment & Qt::AlignBottom)
  317. {
  318. size.setHeight(baseGeometry.height());
  319. }
  320. geometry.setSize(size);
  321. QPoint topLeft = baseGeometry.topLeft();
  322. QPoint bottomRight = baseGeometry.bottomRight();
  323. if (this->Alignment & Qt::AlignLeft)
  324. {
  325. if (this->HorizontalDirection == Qt::LeftToRight)
  326. {
  327. geometry.moveLeft(topLeft.x());
  328. }
  329. else
  330. {
  331. geometry.moveRight(topLeft.x());
  332. }
  333. }
  334. else if (this->Alignment & Qt::AlignRight)
  335. {
  336. if (this->HorizontalDirection == Qt::LeftToRight)
  337. {
  338. geometry.moveLeft(bottomRight.x());
  339. }
  340. else
  341. {
  342. geometry.moveRight(bottomRight.x());
  343. }
  344. }
  345. else if (this->Alignment & Qt::AlignHCenter)
  346. {
  347. geometry.moveLeft((topLeft.x() + bottomRight.x()) / 2 - size.width() / 2);
  348. }
  349. else if (this->Alignment & Qt::AlignJustify)
  350. {
  351. geometry.moveLeft(topLeft.x());
  352. }
  353. if (this->Alignment & Qt::AlignTop)
  354. {
  355. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  356. {
  357. geometry.moveTop(topLeft.y());
  358. }
  359. else
  360. {
  361. geometry.moveBottom(topLeft.y());
  362. }
  363. }
  364. else if (this->Alignment & Qt::AlignBottom)
  365. {
  366. if (this->VerticalDirection == ctkPopupWidget::TopToBottom)
  367. {
  368. geometry.moveTop(bottomRight.y());
  369. }
  370. else
  371. {
  372. geometry.moveBottom(bottomRight.y());
  373. }
  374. }
  375. else if (this->Alignment & Qt::AlignVCenter)
  376. {
  377. geometry.moveTop((topLeft.y() + bottomRight.y()) / 2 - size.height() / 2);
  378. }
  379. return geometry;
  380. }
  381. // -------------------------------------------------------------------------
  382. bool ctkPopupWidgetPrivate::eventFilter(QObject* obj, QEvent* event)
  383. {
  384. Q_Q(ctkPopupWidget);
  385. QWidget* widget = qobject_cast<QWidget*>(obj);
  386. // Here are the application events, it's a lot of events, so we need to be
  387. // careful to be fast.
  388. if (event->type() == QEvent::ApplicationDeactivate)
  389. {
  390. // We wait to see if there is no other window being active
  391. QTimer::singleShot(0, this, SLOT(onApplicationDeactivate()));
  392. }
  393. else if (event->type() == QEvent::ApplicationActivate)
  394. {
  395. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  396. }
  397. if (!this->BaseWidget)
  398. {
  399. return false;
  400. }
  401. if (event->type() == QEvent::Move && widget != this->BaseWidget)
  402. {
  403. if (widget->isAncestorOf(this->BaseWidget))
  404. {
  405. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  406. QPoint topLeft = widget->parentWidget() ? widget->parentWidget()->mapToGlobal(moveEvent->pos()) : moveEvent->pos();
  407. topLeft += this->BaseWidget->mapTo(widget, QPoint(0,0));
  408. //q->move(q->pos() + moveEvent->pos() - moveEvent->oldPos());
  409. QRect newBaseGeometry = this->baseGeometry();
  410. newBaseGeometry.moveTopLeft(topLeft);
  411. QRect desiredGeometry = this->desiredOpenGeometry(newBaseGeometry);
  412. q->move(desiredGeometry.topLeft());
  413. }
  414. else if (widget->isWindow() &&
  415. widget->windowType() != Qt::ToolTip &&
  416. widget->windowType() != Qt::Popup)
  417. {
  418. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  419. }
  420. }
  421. else if (event->type() == QEvent::Resize)
  422. {
  423. if (widget->isWindow() &&
  424. widget != this->BaseWidget->window() &&
  425. widget->windowType() != Qt::ToolTip &&
  426. widget->windowType() != Qt::Popup)
  427. {
  428. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  429. }
  430. }
  431. else if (event->type() == QEvent::WindowStateChange &&
  432. widget != this->BaseWidget->window() &&
  433. widget->windowType() != Qt::ToolTip &&
  434. widget->windowType() != Qt::Popup)
  435. {
  436. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  437. }
  438. else if ((event->type() == QEvent::WindowActivate ||
  439. event->type() == QEvent::WindowDeactivate) &&
  440. widget == this->BaseWidget->window())
  441. {
  442. QTimer::singleShot(0, this, SLOT(updateVisibility()));
  443. }
  444. return false;
  445. }
  446. // -------------------------------------------------------------------------
  447. void ctkPopupWidgetPrivate::onApplicationDeactivate()
  448. {
  449. // Still no active window, that means the user now is controlling another
  450. // application, we have no control over when the other app moves over the
  451. // popup, so we hide the popup as it would show on top of the other app.
  452. if (!qApp->activeWindow())
  453. {
  454. this->temporarilyHiddenOn();
  455. }
  456. }
  457. // -------------------------------------------------------------------------
  458. void ctkPopupWidgetPrivate::updateVisibility()
  459. {
  460. Q_Q(ctkPopupWidget);
  461. // If the BaseWidget window is active, then there is no reason to cover the
  462. // popup.
  463. if (!this->BaseWidget ||
  464. // the popupwidget active window is not active
  465. (!this->BaseWidget->window()->isActiveWindow() &&
  466. // and no other active window
  467. (!qApp->activeWindow() ||
  468. // or the active window is a popup/tooltip
  469. (qApp->activeWindow()->windowType() != Qt::ToolTip &&
  470. qApp->activeWindow()->windowType() != Qt::Popup))))
  471. {
  472. foreach(QWidget* topLevelWidget, qApp->topLevelWidgets())
  473. {
  474. // If there is at least 1 window (active or not) that covers the popup,
  475. // then we ensure the popup is hidden.
  476. // We have no way of knowing which toplevel is over (z-order) which one,
  477. // it is an OS specific information.
  478. // Of course, tooltips and popups don't count as covering windows.
  479. if (topLevelWidget->isVisible() &&
  480. !(topLevelWidget->windowState() & Qt::WindowMinimized) &&
  481. topLevelWidget->windowType() != Qt::ToolTip &&
  482. topLevelWidget->windowType() != Qt::Popup &&
  483. topLevelWidget != (this->BaseWidget ? this->BaseWidget->window() : 0) &&
  484. topLevelWidget->frameGeometry().intersects(q->geometry()))
  485. {
  486. //qDebug() << "hide" << q << "because of: " << topLevelWidget
  487. // << " with windowType: " << topLevelWidget->windowType()
  488. // << topLevelWidget->isVisible()
  489. // << (this->BaseWidget ? this->BaseWidget->window() : 0)
  490. // << topLevelWidget->frameGeometry();
  491. this->temporarilyHiddenOn();
  492. return;
  493. }
  494. }
  495. }
  496. // If the base widget is hidden or minimized, we don't want to restore the
  497. // popup.
  498. if (this->BaseWidget &&
  499. (!this->BaseWidget->isVisible() ||
  500. this->BaseWidget->window()->windowState() & Qt::WindowMinimized))
  501. {
  502. return;
  503. }
  504. // Restore the visibility of the popup if it was hidden
  505. this->temporarilyHiddenOff();
  506. }
  507. // -------------------------------------------------------------------------
  508. void ctkPopupWidgetPrivate::onBaseWidgetDestroyed()
  509. {
  510. Q_Q(ctkPopupWidget);
  511. this->hideAll();
  512. q->setBaseWidget(0);
  513. // could be a property.
  514. q->deleteLater();
  515. }
  516. // -------------------------------------------------------------------------
  517. void ctkPopupWidgetPrivate::temporarilyHiddenOn()
  518. {
  519. Q_Q(ctkPopupWidget);
  520. if (!this->AutoHide &&
  521. (q->isVisible() || this->isOpening()) &&
  522. !(q->isHidden() || this->isClosing()))
  523. {
  524. this->setProperty("forcedClosed", this->isOpening() ? 2 : 1);
  525. }
  526. this->currentAnimation()->stop();
  527. this->hideAll();
  528. }
  529. // -------------------------------------------------------------------------
  530. void ctkPopupWidgetPrivate::temporarilyHiddenOff()
  531. {
  532. Q_Q(ctkPopupWidget);
  533. int forcedClosed = this->property("forcedClosed").toInt();
  534. if (forcedClosed > 0)
  535. {
  536. q->show();
  537. if (forcedClosed == 2)
  538. {
  539. emit q->popupOpened(true);
  540. }
  541. this->setProperty("forcedClosed", 0);
  542. }
  543. else
  544. {
  545. q->updatePopup();
  546. }
  547. }
  548. // -------------------------------------------------------------------------
  549. void ctkPopupWidgetPrivate::hideAll()
  550. {
  551. Q_Q(ctkPopupWidget);
  552. // Before hiding, transfer the active window flag to its parent, this will
  553. // prevent the application to send a ApplicationDeactivate signal that
  554. // doesn't need to be done.
  555. if (q->isActiveWindow() && this->BaseWidget)
  556. {
  557. qApp->setActiveWindow(this->BaseWidget->window());
  558. }
  559. q->hide();
  560. this->PopupPixmapWidget->hide();
  561. // If there is a popup open in the ctkPopupWidget children, then hide it
  562. // as well so we don't have a popup open while the ctkPopupWidget is hidden.
  563. QPointer<QWidget> activePopupWidget = qApp->activePopupWidget();
  564. if (activePopupWidget && this->isAncestorOf(q, activePopupWidget))
  565. {
  566. activePopupWidget->close();
  567. }
  568. }
  569. // -------------------------------------------------------------------------
  570. // Qt::FramelessWindowHint is required on Windows for Translucent background
  571. // Qt::Toolip is preferred to Qt::Popup as it would close itself at the first
  572. // click outside the widget (typically a click in the BaseWidget)
  573. ctkPopupWidget::ctkPopupWidget(QWidget* parentWidget)
  574. : Superclass(QApplication::desktop()->screen(QApplication::desktop()->screenNumber(parentWidget)),
  575. Qt::ToolTip | Qt::FramelessWindowHint)
  576. , d_ptr(new ctkPopupWidgetPrivate(*this))
  577. {
  578. Q_D(ctkPopupWidget);
  579. d->init();
  580. }
  581. // -------------------------------------------------------------------------
  582. ctkPopupWidget::~ctkPopupWidget()
  583. {
  584. }
  585. // -------------------------------------------------------------------------
  586. QWidget* ctkPopupWidget::baseWidget()const
  587. {
  588. Q_D(const ctkPopupWidget);
  589. return d->BaseWidget;
  590. }
  591. // -------------------------------------------------------------------------
  592. void ctkPopupWidget::setBaseWidget(QWidget* widget)
  593. {
  594. Q_D(ctkPopupWidget);
  595. if (d->BaseWidget)
  596. {
  597. d->BaseWidget->removeEventFilter(this);
  598. disconnect(d->BaseWidget, SIGNAL(destroyed(QObject*)),
  599. d, SLOT(onBaseWidgetDestroyed()));
  600. }
  601. d->BaseWidget = widget;
  602. if (d->BaseWidget)
  603. {
  604. d->BaseWidget->installEventFilter(this);
  605. connect(d->BaseWidget, SIGNAL(destroyed(QObject*)),
  606. d, SLOT(onBaseWidgetDestroyed()));
  607. }
  608. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  609. }
  610. // -------------------------------------------------------------------------
  611. bool ctkPopupWidget::autoShow()const
  612. {
  613. Q_D(const ctkPopupWidget);
  614. return d->AutoShow;
  615. }
  616. // -------------------------------------------------------------------------
  617. void ctkPopupWidget::setAutoShow(bool mode)
  618. {
  619. Q_D(ctkPopupWidget);
  620. d->AutoShow = mode;
  621. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  622. }
  623. // -------------------------------------------------------------------------
  624. bool ctkPopupWidget::autoHide()const
  625. {
  626. Q_D(const ctkPopupWidget);
  627. return d->AutoHide;
  628. }
  629. // -------------------------------------------------------------------------
  630. void ctkPopupWidget::setAutoHide(bool mode)
  631. {
  632. Q_D(ctkPopupWidget);
  633. d->AutoHide = mode;
  634. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  635. }
  636. // -------------------------------------------------------------------------
  637. ctkPopupWidget::AnimationEffect ctkPopupWidget::animationEffect()const
  638. {
  639. Q_D(const ctkPopupWidget);
  640. return d->Effect;
  641. }
  642. // -------------------------------------------------------------------------
  643. void ctkPopupWidget::setAnimationEffect(ctkPopupWidget::AnimationEffect effect)
  644. {
  645. Q_D(ctkPopupWidget);
  646. /// TODO: handle the case where there is an animation running
  647. d->Effect = effect;
  648. }
  649. // -------------------------------------------------------------------------
  650. QEasingCurve::Type ctkPopupWidget::easingCurve()const
  651. {
  652. Q_D(const ctkPopupWidget);
  653. return d->AlphaAnimation->easingCurve().type();
  654. }
  655. // -------------------------------------------------------------------------
  656. void ctkPopupWidget::setEasingCurve(QEasingCurve::Type easingCurve)
  657. {
  658. Q_D(ctkPopupWidget);
  659. d->AlphaAnimation->setEasingCurve(easingCurve);
  660. d->ScrollAnimation->setEasingCurve(easingCurve);
  661. }
  662. // -------------------------------------------------------------------------
  663. Qt::Alignment ctkPopupWidget::alignment()const
  664. {
  665. Q_D(const ctkPopupWidget);
  666. return d->Alignment;
  667. }
  668. // -------------------------------------------------------------------------
  669. void ctkPopupWidget::setAlignment(Qt::Alignment alignment)
  670. {
  671. Q_D(ctkPopupWidget);
  672. d->Alignment = alignment;
  673. }
  674. // -------------------------------------------------------------------------
  675. Qt::Orientations ctkPopupWidget::orientation()const
  676. {
  677. Q_D(const ctkPopupWidget);
  678. return d->Orientations;
  679. }
  680. // -------------------------------------------------------------------------
  681. void ctkPopupWidget::setOrientation(Qt::Orientations orientations)
  682. {
  683. Q_D(ctkPopupWidget);
  684. d->Orientations = orientations;
  685. }
  686. // -------------------------------------------------------------------------
  687. ctkPopupWidget::VerticalDirection ctkPopupWidget::verticalDirection()const
  688. {
  689. Q_D(const ctkPopupWidget);
  690. return d->VerticalDirection;
  691. }
  692. // -------------------------------------------------------------------------
  693. void ctkPopupWidget::setVerticalDirection(ctkPopupWidget::VerticalDirection verticalDirection)
  694. {
  695. Q_D(ctkPopupWidget);
  696. d->VerticalDirection = verticalDirection;
  697. }
  698. // -------------------------------------------------------------------------
  699. Qt::LayoutDirection ctkPopupWidget::horizontalDirection()const
  700. {
  701. Q_D(const ctkPopupWidget);
  702. return d->HorizontalDirection;
  703. }
  704. // -------------------------------------------------------------------------
  705. void ctkPopupWidget::setHorizontalDirection(Qt::LayoutDirection horizontalDirection)
  706. {
  707. Q_D(ctkPopupWidget);
  708. d->HorizontalDirection = horizontalDirection;
  709. }
  710. // -------------------------------------------------------------------------
  711. void ctkPopupWidget::onEffectFinished()
  712. {
  713. Q_D(ctkPopupWidget);
  714. if (d->ForcedTranslucent)
  715. {
  716. d->ForcedTranslucent = false;
  717. this->setAttribute(Qt::WA_TranslucentBackground, false);
  718. }
  719. if (qobject_cast<QAbstractAnimation*>(this->sender())->direction() == QAbstractAnimation::Backward)
  720. {
  721. d->hideAll();
  722. emit this->popupOpened(false);
  723. /// restore the AutoShow if needed.
  724. if (!this->property("AutoShowOnClose").isNull())
  725. {
  726. d->AutoShow = this->property("AutoShowOnClose").toBool();
  727. this->setProperty("AutoShowOnClose", QVariant());
  728. }
  729. }
  730. else
  731. {
  732. this->show();
  733. #ifdef Q_WS_X11
  734. // If the OS applies effects on window appearance, it
  735. // can take time for the popup to be displayed, we don't want to
  736. // hide the pixmap too early otherwise to would makes it "flicker"
  737. // It has the disadvantage of 'opaquing' if the popup is
  738. // semi transparent because the pixmap and the popup opacities
  739. // get summed up until the pixmap is hidden.
  740. // Alternatively, you could remove effects on windows with Compiz.
  741. QTimer::singleShot(100, d->PopupPixmapWidget, SLOT(hide()));
  742. #else
  743. d->PopupPixmapWidget->hide();
  744. #endif
  745. emit this->popupOpened(true);
  746. }
  747. }
  748. // -------------------------------------------------------------------------
  749. void ctkPopupWidget::paintEvent(QPaintEvent* event)
  750. {
  751. Q_D(ctkPopupWidget);
  752. Q_UNUSED(event);
  753. QPainter painter(this);
  754. QBrush brush = this->palette().window();
  755. if (brush.style() == Qt::LinearGradientPattern ||
  756. brush.style() == Qt::ConicalGradientPattern ||
  757. brush.style() == Qt::RadialGradientPattern)
  758. {
  759. QGradient* newGradient = duplicateGradient(brush.gradient());
  760. QGradientStops stops;
  761. foreach(QGradientStop stop, newGradient->stops())
  762. {
  763. stop.second.setAlpha(stop.second.alpha() * d->EffectAlpha);
  764. stops.push_back(stop);
  765. }
  766. newGradient->setStops(stops);
  767. brush = QBrush(*newGradient);
  768. delete newGradient;
  769. }
  770. else
  771. {
  772. QColor color = brush.color();
  773. color.setAlpha(color.alpha() * d->EffectAlpha);
  774. brush.setColor(color);
  775. }
  776. //QColor semiTransparentColor = this->palette().window().color();
  777. //semiTransparentColor.setAlpha(d->CurrentAlpha);
  778. painter.fillRect(this->rect(), brush);
  779. // Let the QFrame draw itself if needed
  780. this->Superclass::paintEvent(event);
  781. }
  782. // --------------------------------------------------------------------------
  783. void ctkPopupWidget::leaveEvent(QEvent* event)
  784. {
  785. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  786. this->Superclass::leaveEvent(event);
  787. }
  788. // --------------------------------------------------------------------------
  789. void ctkPopupWidget::enterEvent(QEvent* event)
  790. {
  791. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  792. this->Superclass::enterEvent(event);
  793. }
  794. // --------------------------------------------------------------------------
  795. bool ctkPopupWidget::eventFilter(QObject* obj, QEvent* event)
  796. {
  797. Q_D(ctkPopupWidget);
  798. switch(event->type())
  799. {
  800. case QEvent::Move:
  801. {
  802. if (obj != d->BaseWidget)
  803. {
  804. break;
  805. }
  806. QMoveEvent* moveEvent = dynamic_cast<QMoveEvent*>(event);
  807. QRect newBaseGeometry = d->baseGeometry();
  808. newBaseGeometry.moveTopLeft(d->mapToGlobal(moveEvent->pos()));
  809. QRect desiredGeometry = d->desiredOpenGeometry(newBaseGeometry);
  810. this->move(desiredGeometry.topLeft());
  811. //this->move(this->pos() + moveEvent->pos() - moveEvent->oldPos());
  812. this->update();
  813. break;
  814. }
  815. case QEvent::Hide:
  816. case QEvent::Close:
  817. // if the mouse was in a base widget child popup, then when we leave
  818. // the popup we want to check if it needs to be closed.
  819. if (obj != d->BaseWidget &&
  820. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  821. {
  822. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  823. obj->removeEventFilter(this);
  824. break;
  825. }
  826. d->temporarilyHiddenOn();
  827. break;
  828. case QEvent::Show:
  829. if (obj != d->BaseWidget)
  830. {
  831. break;
  832. }
  833. this->setGeometry(d->desiredOpenGeometry());
  834. d->temporarilyHiddenOff();
  835. break;
  836. case QEvent::Resize:
  837. if (obj != d->BaseWidget ||
  838. !(d->Alignment & Qt::AlignJustify ||
  839. (d->Alignment & Qt::AlignTop && d->Alignment & Qt::AlignBottom)) ||
  840. !(d->isOpening() || this->isVisible()))
  841. {
  842. break;
  843. }
  844. // TODO: bug when the effect is WindowOpacityFadeEffect
  845. this->setGeometry(d->desiredOpenGeometry());
  846. break;
  847. case QEvent::Enter:
  848. if ( d->currentAnimation()->state() == QAbstractAnimation::Stopped )
  849. {
  850. // Maybe the user moved the mouse on the widget by mistake, don't open
  851. // the popup instantly...
  852. QTimer::singleShot(ENTER_OPENING_DELAY, this, SLOT(updatePopup()));
  853. }
  854. else
  855. {
  856. // ... except if the popup is closing, we want to reopen it as sooon as
  857. // possible.
  858. this->updatePopup();
  859. }
  860. break;
  861. case QEvent::Leave:
  862. // Don't listen to base widget children that are popups as what
  863. // matters here is their close event instead
  864. if (obj != d->BaseWidget &&
  865. qobject_cast<QWidget*>(obj)->windowType() == Qt::Popup)
  866. {
  867. break;
  868. }
  869. // The mouse might have left the area that keeps the popup open
  870. QTimer::singleShot(LEAVE_CLOSING_DELAY, this, SLOT(updatePopup()));
  871. if (obj != d->BaseWidget)
  872. {
  873. obj->removeEventFilter(this);
  874. }
  875. break;
  876. default:
  877. break;
  878. }
  879. return this->QObject::eventFilter(obj, event);
  880. }
  881. // --------------------------------------------------------------------------
  882. void ctkPopupWidget::updatePopup()
  883. {
  884. Q_D(ctkPopupWidget);
  885. // Querying mouseOver can be slow, don't do it if not needed.
  886. bool mouseOver = (d->AutoShow || d->AutoHide) && d->mouseOver();
  887. if (d->AutoShow && mouseOver)
  888. {
  889. this->showPopup();
  890. }
  891. else if (d->AutoHide && !mouseOver)
  892. {
  893. this->hidePopup();
  894. }
  895. }
  896. // --------------------------------------------------------------------------
  897. void ctkPopupWidget::showPopup()
  898. {
  899. Q_D(ctkPopupWidget);
  900. if ((this->isVisible() &&
  901. d->currentAnimation()->state() == QAbstractAnimation::Stopped) ||
  902. (d->BaseWidget && !d->BaseWidget->isVisible()))
  903. {
  904. return;
  905. }
  906. // If the layout has never been activated, the widget doesn't know its
  907. // minSize/maxSize and we then wouldn't know what's its true geometry.
  908. if (this->layout() && !this->testAttribute(Qt::WA_WState_Created))
  909. {
  910. this->layout()->activate();
  911. }
  912. this->setGeometry(d->desiredOpenGeometry());
  913. /// Maybe the popup doesn't allow the desiredOpenGeometry if the widget
  914. /// minimum size is larger than the desired size.
  915. QRect openGeometry = this->geometry();
  916. QRect closedGeometry = d->closedGeometry();
  917. d->currentAnimation()->setDirection(QAbstractAnimation::Forward);
  918. switch(d->Effect)
  919. {
  920. case WindowOpacityFadeEffect:
  921. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  922. {
  923. d->ForcedTranslucent = true;
  924. this->setAttribute(Qt::WA_TranslucentBackground, true);
  925. }
  926. this->show();
  927. break;
  928. case ScrollEffect:
  929. {
  930. d->PopupPixmapWidget->setGeometry(closedGeometry);
  931. d->ScrollAnimation->setStartValue(closedGeometry);
  932. d->ScrollAnimation->setEndValue(openGeometry);
  933. d->setupPopupPixmapWidget();
  934. d->PopupPixmapWidget->show();
  935. break;
  936. }
  937. default:
  938. break;
  939. }
  940. switch(d->currentAnimation()->state())
  941. {
  942. case QAbstractAnimation::Stopped:
  943. d->currentAnimation()->start();
  944. break;
  945. case QAbstractAnimation::Paused:
  946. d->currentAnimation()->resume();
  947. break;
  948. default:
  949. case QAbstractAnimation::Running:
  950. break;
  951. }
  952. }
  953. // --------------------------------------------------------------------------
  954. void ctkPopupWidget::hidePopup()
  955. {
  956. Q_D(ctkPopupWidget);
  957. // just in case it was set.
  958. this->setProperty("forcedClosed", 0);
  959. if (!this->isVisible() &&
  960. d->currentAnimation()->state() == QAbstractAnimation::Stopped)
  961. {
  962. return;
  963. }
  964. d->currentAnimation()->setDirection(QAbstractAnimation::Backward);
  965. QRect openGeometry = this->geometry();
  966. QRect closedGeometry = d->closedGeometry();
  967. switch(d->Effect)
  968. {
  969. case WindowOpacityFadeEffect:
  970. if (!this->testAttribute(Qt::WA_TranslucentBackground))
  971. {
  972. d->ForcedTranslucent = true;
  973. this->setAttribute(Qt::WA_TranslucentBackground, true);
  974. }
  975. break;
  976. case ScrollEffect:
  977. {
  978. d->ScrollAnimation->setStartValue(closedGeometry);
  979. d->ScrollAnimation->setEndValue(openGeometry);
  980. d->setupPopupPixmapWidget();
  981. d->PopupPixmapWidget->setGeometry(this->geometry());
  982. d->PopupPixmapWidget->show();
  983. if (this->isActiveWindow())
  984. {
  985. qApp->setActiveWindow(d->BaseWidget ? d->BaseWidget->window() : 0);
  986. }
  987. this->hide();
  988. break;
  989. }
  990. default:
  991. break;
  992. }
  993. switch(d->currentAnimation()->state())
  994. {
  995. case QAbstractAnimation::Stopped:
  996. d->currentAnimation()->start();
  997. break;
  998. case QAbstractAnimation::Paused:
  999. d->currentAnimation()->resume();
  1000. break;
  1001. default:
  1002. case QAbstractAnimation::Running:
  1003. break;
  1004. }
  1005. }
  1006. // --------------------------------------------------------------------------
  1007. void ctkPopupWidget::pinPopup(bool pin)
  1008. {
  1009. Q_D(ctkPopupWidget);
  1010. this->setAutoHide(!pin);
  1011. if (pin)
  1012. {
  1013. this->showPopup();
  1014. }
  1015. else
  1016. {
  1017. // When closing, we don't want to inadvertently re-open the menu.
  1018. this->setProperty("AutoShowOnClose", this->autoShow());
  1019. d->AutoShow = false;
  1020. this->hidePopup();
  1021. }
  1022. }
  1023. // --------------------------------------------------------------------------
  1024. double ctkPopupWidget::effectAlpha()const
  1025. {
  1026. Q_D(const ctkPopupWidget);
  1027. return d->EffectAlpha;
  1028. }
  1029. // --------------------------------------------------------------------------
  1030. void ctkPopupWidget::setEffectAlpha(double alpha)
  1031. {
  1032. Q_D(ctkPopupWidget);
  1033. d->EffectAlpha = alpha;
  1034. this->repaint();
  1035. }
  1036. // --------------------------------------------------------------------------
  1037. QRect ctkPopupWidget::effectGeometry()const
  1038. {
  1039. Q_D(const ctkPopupWidget);
  1040. return d->PopupPixmapWidget->geometry();
  1041. }
  1042. // --------------------------------------------------------------------------
  1043. void ctkPopupWidget::setEffectGeometry(QRect newGeometry)
  1044. {
  1045. Q_D(ctkPopupWidget);
  1046. d->PopupPixmapWidget->setGeometry(newGeometry);
  1047. d->PopupPixmapWidget->repaint();
  1048. }