ctkPopupWidget.cpp 32 KB

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