- Timestamp:
- Jul 20, 2015 2:12:00 PM (10 years ago)
- svn:sync-xref-src-repo-rev:
- 101740
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIMiniToolBar.cpp
r57021 r57022 112 112 QList<QWidget*> m_margins; 113 113 }; 114 115 UIMiniToolBar::UIMiniToolBar(QWidget *pParent,116 GeometryType geometryType,117 Qt::Alignment alignment,118 bool fAutoHide /* = true */)119 : QWidget(pParent, Qt::Tool | Qt::FramelessWindowHint)120 /* Variables: General stuff: */121 , m_geometryType(geometryType)122 , m_alignment(alignment)123 , m_fAutoHide(fAutoHide)124 /* Variables: Contents stuff: */125 , m_pMdiArea(0)126 , m_pToolbar(0)127 , m_pEmbeddedToolbar(0)128 /* Variables: Hover stuff: */129 , m_fHovered(false)130 , m_pHoverEnterTimer(0)131 , m_pHoverLeaveTimer(0)132 , m_pAnimation(0)133 {134 /* Prepare: */135 prepare();136 }137 138 UIMiniToolBar::~UIMiniToolBar()139 {140 /* Cleanup: */141 cleanup();142 }143 144 void UIMiniToolBar::setAlignment(Qt::Alignment alignment)145 {146 /* Make sure toolbar created: */147 AssertPtrReturnVoid(m_pToolbar);148 149 /* Make sure alignment really changed: */150 if (m_alignment == alignment)151 return;152 153 /* Update alignment: */154 m_alignment = alignment;155 156 /* Re-initialize: */157 adjustGeometry();158 159 /* Propagate to child to update shape: */160 m_pToolbar->setAlignment(m_alignment);161 }162 163 void UIMiniToolBar::setAutoHide(bool fAutoHide, bool fPropagateToChild /* = true */)164 {165 /* Make sure toolbar created: */166 AssertPtrReturnVoid(m_pToolbar);167 168 /* Make sure auto-hide really changed: */169 if (m_fAutoHide == fAutoHide)170 return;171 172 /* Update auto-hide: */173 m_fAutoHide = fAutoHide;174 175 /* Re-initialize: */176 adjustGeometry();177 178 /* Propagate to child to update action if necessary: */179 if (fPropagateToChild)180 m_pToolbar->setAutoHide(m_fAutoHide);181 }182 183 void UIMiniToolBar::setText(const QString &strText)184 {185 /* Make sure toolbar created: */186 AssertPtrReturnVoid(m_pToolbar);187 188 /* Propagate to child: */189 m_pToolbar->setText(strText);190 }191 192 void UIMiniToolBar::addMenus(const QList<QMenu*> &menus)193 {194 /* Make sure toolbar created: */195 AssertPtrReturnVoid(m_pToolbar);196 197 /* Propagate to child: */198 m_pToolbar->addMenus(menus);199 }200 201 void UIMiniToolBar::adjustGeometry(int iHostScreen /* = -1 */)202 {203 #ifndef Q_WS_X11204 /* This method could be called before parent-widget205 * become visible, we should skip everything in that case: */206 if (QApplication::desktop()->screenNumber(parentWidget()) == -1)207 return;208 209 /* Determine host-screen number if necessary: */210 if (iHostScreen == -1)211 iHostScreen = QApplication::desktop()->screenNumber(parentWidget());212 213 /* Reset toolbar geometry: */214 m_pEmbeddedToolbar->move(0, 0);215 m_pEmbeddedToolbar->resize(m_pEmbeddedToolbar->sizeHint());216 217 /* Adjust window geometry: */218 resize(m_pEmbeddedToolbar->size());219 QRect screenRect;220 int iX = 0, iY = 0;221 switch (m_geometryType)222 {223 case GeometryType_Available: screenRect = QApplication::desktop()->availableGeometry(iHostScreen); break;224 case GeometryType_Full: screenRect = QApplication::desktop()->screenGeometry(iHostScreen); break;225 default: break;226 }227 iX = screenRect.x() + screenRect.width() / 2 - width() / 2;228 switch (m_alignment)229 {230 case Qt::AlignTop: iY = screenRect.y(); break;231 case Qt::AlignBottom: iY = screenRect.y() + screenRect.height() - height(); break;232 default: break;233 }234 move(iX, iY);235 236 /* Recalculate auto-hide animation: */237 m_shownToolbarPosition = m_pEmbeddedToolbar->pos();238 switch (m_alignment)239 {240 case Qt::AlignTop:241 m_hiddenToolbarPosition = m_shownToolbarPosition - QPoint(0, m_pEmbeddedToolbar->height() - 3);242 break;243 case Qt::AlignBottom:244 m_hiddenToolbarPosition = m_shownToolbarPosition + QPoint(0, m_pEmbeddedToolbar->height() - 3);245 break;246 }247 m_pAnimation->update();248 249 /* Update toolbar geometry if necessary: */250 const QString strAnimationState = property("AnimationState").toString();251 if (strAnimationState == "Start")252 m_pEmbeddedToolbar->move(m_hiddenToolbarPosition);253 else if (strAnimationState == "Final")254 m_pEmbeddedToolbar->move(m_shownToolbarPosition);255 256 /* Simulate toolbar auto-hiding: */257 simulateToolbarAutoHiding();258 259 #else /* Q_WS_X11 */260 261 /* This method could be called before parent-widget262 * become visible, we should skip everything in that case: */263 if (QApplication::desktop()->screenNumber(parentWidget()) == -1)264 return;265 266 /* Determine host-screen number if necessary: */267 bool fMoveToHostScreen = true;268 if (iHostScreen == -1)269 {270 fMoveToHostScreen = false;271 iHostScreen = QApplication::desktop()->screenNumber(this);272 }273 274 /* Choose window geometry: */275 QRect screenRect;276 switch (m_geometryType)277 {278 case GeometryType_Available: screenRect = QApplication::desktop()->availableGeometry(iHostScreen); break;279 case GeometryType_Full: screenRect = QApplication::desktop()->screenGeometry(iHostScreen); break;280 default: break;281 }282 283 /* Move to corresponding host-screen: */284 if (fMoveToHostScreen)285 move(screenRect.topLeft());286 287 /* Resize embedded-toolbar to minimum size: */288 m_pEmbeddedToolbar->resize(m_pEmbeddedToolbar->sizeHint());289 290 /* Calculate embedded-toolbar position: */291 int iX = 0, iY = 0;292 iX = screenRect.width() / 2 - m_pEmbeddedToolbar->width() / 2;293 switch (m_alignment)294 {295 case Qt::AlignTop: iY = 0; break;296 case Qt::AlignBottom: iY = screenRect.height() - m_pEmbeddedToolbar->height(); break;297 default: break;298 }299 300 /* Update auto-hide animation: */301 m_shownToolbarPosition = QPoint(iX, iY);302 switch (m_alignment)303 {304 case Qt::AlignTop: m_hiddenToolbarPosition = m_shownToolbarPosition - QPoint(0, m_pEmbeddedToolbar->height() - 3); break;305 case Qt::AlignBottom: m_hiddenToolbarPosition = m_shownToolbarPosition + QPoint(0, m_pEmbeddedToolbar->height() - 3); break;306 }307 m_pAnimation->update();308 309 /* Update embedded-toolbar geometry if known: */310 const QString strAnimationState = property("AnimationState").toString();311 if (strAnimationState == "Start")312 m_pEmbeddedToolbar->move(m_hiddenToolbarPosition);313 else if (strAnimationState == "Final")314 m_pEmbeddedToolbar->move(m_shownToolbarPosition);315 316 /* Adjust window mask: */317 setMask(m_pEmbeddedToolbar->geometry());318 319 /* Simulate toolbar auto-hiding: */320 simulateToolbarAutoHiding();321 #endif /* Q_WS_X11 */322 }323 324 void UIMiniToolBar::sltHandleToolbarResize()325 {326 /* Re-initialize: */327 adjustGeometry();328 }329 330 void UIMiniToolBar::sltAutoHideToggled()331 {332 /* Propagate from child: */333 setAutoHide(m_pToolbar->autoHide(), false);334 }335 336 void UIMiniToolBar::sltHoverEnter()337 {338 /* Mark as 'hovered' if necessary: */339 if (!m_fHovered)340 {341 m_fHovered = true;342 emit sigHoverEnter();343 }344 }345 346 void UIMiniToolBar::sltHoverLeave()347 {348 /* Mark as 'unhovered' if necessary: */349 if (m_fHovered)350 {351 m_fHovered = false;352 emit sigHoverLeave();353 }354 }355 356 void UIMiniToolBar::prepare()357 {358 #if defined (Q_WS_X11)359 /* Install own event filter: */360 installEventFilter(this);361 #endif /* Q_WS_X11 */362 363 #if defined(Q_WS_WIN)364 /* Make sure we have no background365 * until the first one paint-event: */366 setAttribute(Qt::WA_NoSystemBackground);367 /* Using Qt API to enable translucent background:368 * - Under Mac host Qt doesn't allows to disable window-shadows369 * until version 4.8, but minimum supported version is 4.7.1.370 * - Under x11 host Qt has broken XComposite support (black background): */371 setAttribute(Qt::WA_TranslucentBackground);372 #elif defined(Q_WS_X11)373 /* Use Qt API to enable translucency if allowed: */374 if (QX11Info::isCompositingManagerRunning())375 setAttribute(Qt::WA_TranslucentBackground);376 #endif /* Q_WS_X11 */377 378 /* Make sure we have no focus: */379 setFocusPolicy(Qt::NoFocus);380 381 /* Prepare mdi-area: */382 m_pMdiArea = new QMdiArea;383 {384 /* Allow any MDI area size: */385 m_pMdiArea->setMinimumSize(QSize(1, 1));386 /* Configure own background: */387 QPalette pal = m_pMdiArea->palette();388 pal.setColor(QPalette::Window, QColor(Qt::transparent));389 m_pMdiArea->setPalette(pal);390 /* Configure viewport background: */391 m_pMdiArea->setBackground(QColor(Qt::transparent));392 /* Layout mdi-area according parent-widget: */393 QVBoxLayout *pMainLayout = new QVBoxLayout(this);394 pMainLayout->setContentsMargins(0, 0, 0, 0);395 pMainLayout->addWidget(m_pMdiArea);396 /* Make sure we have no focus: */397 m_pMdiArea->setFocusPolicy(Qt::NoFocus);398 m_pMdiArea->viewport()->setFocusPolicy(Qt::NoFocus);399 }400 401 /* Prepare mini-toolbar: */402 m_pToolbar = new UIMiniToolBarPrivate;403 {404 /* Make sure we have no focus: */405 m_pToolbar->setFocusPolicy(Qt::NoFocus);406 /* Propagate known options to child: */407 m_pToolbar->setAutoHide(m_fAutoHide);408 m_pToolbar->setAlignment(m_alignment);409 /* Configure own background: */410 QPalette pal = m_pToolbar->palette();411 pal.setColor(QPalette::Window, palette().color(QPalette::Window));412 m_pToolbar->setPalette(pal);413 /* Configure child connections: */414 connect(m_pToolbar, SIGNAL(sigResized()), this, SLOT(sltHandleToolbarResize()));415 connect(m_pToolbar, SIGNAL(sigAutoHideToggled()), this, SLOT(sltAutoHideToggled()));416 connect(m_pToolbar, SIGNAL(sigMinimizeAction()), this, SIGNAL(sigMinimizeAction()));417 connect(m_pToolbar, SIGNAL(sigExitAction()), this, SIGNAL(sigExitAction()));418 connect(m_pToolbar, SIGNAL(sigCloseAction()), this, SIGNAL(sigCloseAction()));419 /* Add child to mdi-area: */420 m_pEmbeddedToolbar = m_pMdiArea->addSubWindow(m_pToolbar, Qt::Window | Qt::FramelessWindowHint);421 /* Make sure we have no focus: */422 m_pEmbeddedToolbar->setFocusPolicy(Qt::NoFocus);423 #ifdef Q_WS_WIN424 /* Install embedded-toolbar event filter: */425 m_pEmbeddedToolbar->installEventFilter(this);426 #endif /* Q_WS_WIN */427 }428 429 /* Prepare hover-enter/leave timers: */430 m_pHoverEnterTimer = new QTimer(this);431 {432 m_pHoverEnterTimer->setSingleShot(true);433 m_pHoverEnterTimer->setInterval(50);434 connect(m_pHoverEnterTimer, SIGNAL(timeout()), this, SLOT(sltHoverEnter()));435 }436 m_pHoverLeaveTimer = new QTimer(this);437 {438 m_pHoverLeaveTimer->setSingleShot(true);439 m_pHoverLeaveTimer->setInterval(500);440 connect(m_pHoverLeaveTimer, SIGNAL(timeout()), this, SLOT(sltHoverLeave()));441 }442 443 /* Install 'auto-hide' animation to 'toolbarPosition' property: */444 m_pAnimation = UIAnimation::installPropertyAnimation(this,445 "toolbarPosition",446 "hiddenToolbarPosition", "shownToolbarPosition",447 SIGNAL(sigHoverEnter()), SIGNAL(sigHoverLeave()),448 true);449 450 /* Adjust geometry finally: */451 adjustGeometry();452 }453 454 void UIMiniToolBar::cleanup()455 {456 /* Stop hover-enter/leave timers: */457 if (m_pHoverEnterTimer && m_pHoverEnterTimer->isActive())458 m_pHoverEnterTimer->stop();459 if (m_pHoverLeaveTimer && m_pHoverLeaveTimer->isActive())460 m_pHoverLeaveTimer->stop();461 462 /* Destroy animation before mdi-toolbar: */463 delete m_pAnimation;464 m_pAnimation = 0;465 466 /* Destroy mdi-toolbar after animation: */467 delete m_pEmbeddedToolbar;468 m_pEmbeddedToolbar = 0;469 }470 471 void UIMiniToolBar::enterEvent(QEvent*)472 {473 /* Stop the hover-leave timer if necessary: */474 if (m_pHoverLeaveTimer && m_pHoverLeaveTimer->isActive())475 m_pHoverLeaveTimer->stop();476 477 /* Start the hover-enter timer: */478 if (m_fAutoHide && m_pHoverEnterTimer)479 m_pHoverEnterTimer->start();480 }481 482 void UIMiniToolBar::leaveEvent(QEvent*)483 {484 /* Stop the hover-enter timer if necessary: */485 if (m_pHoverEnterTimer && m_pHoverEnterTimer->isActive())486 m_pHoverEnterTimer->stop();487 488 /* Start the hover-leave timer: */489 if (m_fAutoHide && m_pHoverLeaveTimer)490 m_pHoverLeaveTimer->start();491 }492 493 #ifdef Q_WS_X11494 void UIMiniToolBar::resizeEvent(QResizeEvent*)495 {496 /* Adjust mini-toolbar on resize: */497 adjustGeometry();498 }499 #endif /* Q_WS_X11 */500 501 bool UIMiniToolBar::eventFilter(QObject *pWatched, QEvent *pEvent)502 {503 #if defined(Q_WS_WIN)504 /* Due to Qt bug QMdiArea can505 * 1. steal focus from current application focus-widget506 * 3. and even request focus stealing if QMdiArea hidden yet.507 * We have to notify listeners about such facts.. */508 if (pWatched && m_pEmbeddedToolbar && pWatched == m_pEmbeddedToolbar &&509 pEvent->type() == QEvent::FocusIn)510 emit sigNotifyAboutFocusStolen();511 #elif defined(Q_WS_X11)512 /* Detect if we have window activation stolen. */513 if (pWatched == this &&514 pEvent->type() == QEvent::WindowActivate)515 emit sigNotifyAboutFocusStolen();516 #endif /* Q_WS_X11 */517 518 /* Call to base-class: */519 return QWidget::eventFilter(pWatched, pEvent);520 }521 522 void UIMiniToolBar::simulateToolbarAutoHiding()523 {524 /* This simulation helps user to notice525 * toolbar location, so it will be used only526 * 1. if toolbar unhovered and527 * 2. auto-hide feature enabled: */528 if (m_fHovered || !m_fAutoHide)529 return;530 531 /* Simulate hover-leave event: */532 m_fHovered = true;533 m_pHoverLeaveTimer->start();534 }535 536 void UIMiniToolBar::setToolbarPosition(QPoint point)537 {538 /* Update position: */539 AssertPtrReturnVoid(m_pEmbeddedToolbar);540 m_pEmbeddedToolbar->move(point);541 }542 543 QPoint UIMiniToolBar::toolbarPosition() const544 {545 /* Return position: */546 AssertPtrReturn(m_pEmbeddedToolbar, QPoint());547 return m_pEmbeddedToolbar->pos();548 }549 550 114 551 115 UIMiniToolBarPrivate::UIMiniToolBarPrivate() … … 805 369 } 806 370 371 UIMiniToolBar::UIMiniToolBar(QWidget *pParent, 372 GeometryType geometryType, 373 Qt::Alignment alignment, 374 bool fAutoHide /* = true */) 375 : QWidget(pParent, Qt::Tool | Qt::FramelessWindowHint) 376 /* Variables: General stuff: */ 377 , m_geometryType(geometryType) 378 , m_alignment(alignment) 379 , m_fAutoHide(fAutoHide) 380 /* Variables: Contents stuff: */ 381 , m_pMdiArea(0) 382 , m_pToolbar(0) 383 , m_pEmbeddedToolbar(0) 384 /* Variables: Hover stuff: */ 385 , m_fHovered(false) 386 , m_pHoverEnterTimer(0) 387 , m_pHoverLeaveTimer(0) 388 , m_pAnimation(0) 389 { 390 /* Prepare: */ 391 prepare(); 392 } 393 394 UIMiniToolBar::~UIMiniToolBar() 395 { 396 /* Cleanup: */ 397 cleanup(); 398 } 399 400 void UIMiniToolBar::setAlignment(Qt::Alignment alignment) 401 { 402 /* Make sure toolbar created: */ 403 AssertPtrReturnVoid(m_pToolbar); 404 405 /* Make sure alignment really changed: */ 406 if (m_alignment == alignment) 407 return; 408 409 /* Update alignment: */ 410 m_alignment = alignment; 411 412 /* Re-initialize: */ 413 adjustGeometry(); 414 415 /* Propagate to child to update shape: */ 416 m_pToolbar->setAlignment(m_alignment); 417 } 418 419 void UIMiniToolBar::setAutoHide(bool fAutoHide, bool fPropagateToChild /* = true */) 420 { 421 /* Make sure toolbar created: */ 422 AssertPtrReturnVoid(m_pToolbar); 423 424 /* Make sure auto-hide really changed: */ 425 if (m_fAutoHide == fAutoHide) 426 return; 427 428 /* Update auto-hide: */ 429 m_fAutoHide = fAutoHide; 430 431 /* Re-initialize: */ 432 adjustGeometry(); 433 434 /* Propagate to child to update action if necessary: */ 435 if (fPropagateToChild) 436 m_pToolbar->setAutoHide(m_fAutoHide); 437 } 438 439 void UIMiniToolBar::setText(const QString &strText) 440 { 441 /* Make sure toolbar created: */ 442 AssertPtrReturnVoid(m_pToolbar); 443 444 /* Propagate to child: */ 445 m_pToolbar->setText(strText); 446 } 447 448 void UIMiniToolBar::addMenus(const QList<QMenu*> &menus) 449 { 450 /* Make sure toolbar created: */ 451 AssertPtrReturnVoid(m_pToolbar); 452 453 /* Propagate to child: */ 454 m_pToolbar->addMenus(menus); 455 } 456 457 void UIMiniToolBar::adjustGeometry(int iHostScreen /* = -1 */) 458 { 459 #ifndef Q_WS_X11 460 /* This method could be called before parent-widget 461 * become visible, we should skip everything in that case: */ 462 if (QApplication::desktop()->screenNumber(parentWidget()) == -1) 463 return; 464 465 /* Determine host-screen number if necessary: */ 466 if (iHostScreen == -1) 467 iHostScreen = QApplication::desktop()->screenNumber(parentWidget()); 468 469 /* Reset toolbar geometry: */ 470 m_pEmbeddedToolbar->move(0, 0); 471 m_pEmbeddedToolbar->resize(m_pEmbeddedToolbar->sizeHint()); 472 473 /* Adjust window geometry: */ 474 resize(m_pEmbeddedToolbar->size()); 475 QRect screenRect; 476 int iX = 0, iY = 0; 477 switch (m_geometryType) 478 { 479 case GeometryType_Available: screenRect = QApplication::desktop()->availableGeometry(iHostScreen); break; 480 case GeometryType_Full: screenRect = QApplication::desktop()->screenGeometry(iHostScreen); break; 481 default: break; 482 } 483 iX = screenRect.x() + screenRect.width() / 2 - width() / 2; 484 switch (m_alignment) 485 { 486 case Qt::AlignTop: iY = screenRect.y(); break; 487 case Qt::AlignBottom: iY = screenRect.y() + screenRect.height() - height(); break; 488 default: break; 489 } 490 move(iX, iY); 491 492 /* Recalculate auto-hide animation: */ 493 m_shownToolbarPosition = m_pEmbeddedToolbar->pos(); 494 switch (m_alignment) 495 { 496 case Qt::AlignTop: 497 m_hiddenToolbarPosition = m_shownToolbarPosition - QPoint(0, m_pEmbeddedToolbar->height() - 3); 498 break; 499 case Qt::AlignBottom: 500 m_hiddenToolbarPosition = m_shownToolbarPosition + QPoint(0, m_pEmbeddedToolbar->height() - 3); 501 break; 502 } 503 m_pAnimation->update(); 504 505 /* Update toolbar geometry if necessary: */ 506 const QString strAnimationState = property("AnimationState").toString(); 507 if (strAnimationState == "Start") 508 m_pEmbeddedToolbar->move(m_hiddenToolbarPosition); 509 else if (strAnimationState == "Final") 510 m_pEmbeddedToolbar->move(m_shownToolbarPosition); 511 512 /* Simulate toolbar auto-hiding: */ 513 simulateToolbarAutoHiding(); 514 515 #else /* Q_WS_X11 */ 516 517 /* This method could be called before parent-widget 518 * become visible, we should skip everything in that case: */ 519 if (QApplication::desktop()->screenNumber(parentWidget()) == -1) 520 return; 521 522 /* Determine host-screen number if necessary: */ 523 bool fMoveToHostScreen = true; 524 if (iHostScreen == -1) 525 { 526 fMoveToHostScreen = false; 527 iHostScreen = QApplication::desktop()->screenNumber(this); 528 } 529 530 /* Choose window geometry: */ 531 QRect screenRect; 532 switch (m_geometryType) 533 { 534 case GeometryType_Available: screenRect = QApplication::desktop()->availableGeometry(iHostScreen); break; 535 case GeometryType_Full: screenRect = QApplication::desktop()->screenGeometry(iHostScreen); break; 536 default: break; 537 } 538 539 /* Move to corresponding host-screen: */ 540 if (fMoveToHostScreen) 541 move(screenRect.topLeft()); 542 543 /* Resize embedded-toolbar to minimum size: */ 544 m_pEmbeddedToolbar->resize(m_pEmbeddedToolbar->sizeHint()); 545 546 /* Calculate embedded-toolbar position: */ 547 int iX = 0, iY = 0; 548 iX = screenRect.width() / 2 - m_pEmbeddedToolbar->width() / 2; 549 switch (m_alignment) 550 { 551 case Qt::AlignTop: iY = 0; break; 552 case Qt::AlignBottom: iY = screenRect.height() - m_pEmbeddedToolbar->height(); break; 553 default: break; 554 } 555 556 /* Update auto-hide animation: */ 557 m_shownToolbarPosition = QPoint(iX, iY); 558 switch (m_alignment) 559 { 560 case Qt::AlignTop: m_hiddenToolbarPosition = m_shownToolbarPosition - QPoint(0, m_pEmbeddedToolbar->height() - 3); break; 561 case Qt::AlignBottom: m_hiddenToolbarPosition = m_shownToolbarPosition + QPoint(0, m_pEmbeddedToolbar->height() - 3); break; 562 } 563 m_pAnimation->update(); 564 565 /* Update embedded-toolbar geometry if known: */ 566 const QString strAnimationState = property("AnimationState").toString(); 567 if (strAnimationState == "Start") 568 m_pEmbeddedToolbar->move(m_hiddenToolbarPosition); 569 else if (strAnimationState == "Final") 570 m_pEmbeddedToolbar->move(m_shownToolbarPosition); 571 572 /* Adjust window mask: */ 573 setMask(m_pEmbeddedToolbar->geometry()); 574 575 /* Simulate toolbar auto-hiding: */ 576 simulateToolbarAutoHiding(); 577 #endif /* Q_WS_X11 */ 578 } 579 580 void UIMiniToolBar::sltHandleToolbarResize() 581 { 582 /* Re-initialize: */ 583 adjustGeometry(); 584 } 585 586 void UIMiniToolBar::sltAutoHideToggled() 587 { 588 /* Propagate from child: */ 589 setAutoHide(m_pToolbar->autoHide(), false); 590 } 591 592 void UIMiniToolBar::sltHoverEnter() 593 { 594 /* Mark as 'hovered' if necessary: */ 595 if (!m_fHovered) 596 { 597 m_fHovered = true; 598 emit sigHoverEnter(); 599 } 600 } 601 602 void UIMiniToolBar::sltHoverLeave() 603 { 604 /* Mark as 'unhovered' if necessary: */ 605 if (m_fHovered) 606 { 607 m_fHovered = false; 608 emit sigHoverLeave(); 609 } 610 } 611 612 void UIMiniToolBar::prepare() 613 { 614 #if defined (Q_WS_X11) 615 /* Install own event filter: */ 616 installEventFilter(this); 617 #endif /* Q_WS_X11 */ 618 619 #if defined(Q_WS_WIN) 620 /* Make sure we have no background 621 * until the first one paint-event: */ 622 setAttribute(Qt::WA_NoSystemBackground); 623 /* Using Qt API to enable translucent background: 624 * - Under Mac host Qt doesn't allows to disable window-shadows 625 * until version 4.8, but minimum supported version is 4.7.1. 626 * - Under x11 host Qt has broken XComposite support (black background): */ 627 setAttribute(Qt::WA_TranslucentBackground); 628 #elif defined(Q_WS_X11) 629 /* Use Qt API to enable translucency if allowed: */ 630 if (QX11Info::isCompositingManagerRunning()) 631 setAttribute(Qt::WA_TranslucentBackground); 632 #endif /* Q_WS_X11 */ 633 634 /* Make sure we have no focus: */ 635 setFocusPolicy(Qt::NoFocus); 636 637 /* Prepare mdi-area: */ 638 m_pMdiArea = new QMdiArea; 639 { 640 /* Allow any MDI area size: */ 641 m_pMdiArea->setMinimumSize(QSize(1, 1)); 642 /* Configure own background: */ 643 QPalette pal = m_pMdiArea->palette(); 644 pal.setColor(QPalette::Window, QColor(Qt::transparent)); 645 m_pMdiArea->setPalette(pal); 646 /* Configure viewport background: */ 647 m_pMdiArea->setBackground(QColor(Qt::transparent)); 648 /* Layout mdi-area according parent-widget: */ 649 QVBoxLayout *pMainLayout = new QVBoxLayout(this); 650 pMainLayout->setContentsMargins(0, 0, 0, 0); 651 pMainLayout->addWidget(m_pMdiArea); 652 /* Make sure we have no focus: */ 653 m_pMdiArea->setFocusPolicy(Qt::NoFocus); 654 m_pMdiArea->viewport()->setFocusPolicy(Qt::NoFocus); 655 } 656 657 /* Prepare mini-toolbar: */ 658 m_pToolbar = new UIMiniToolBarPrivate; 659 { 660 /* Make sure we have no focus: */ 661 m_pToolbar->setFocusPolicy(Qt::NoFocus); 662 /* Propagate known options to child: */ 663 m_pToolbar->setAutoHide(m_fAutoHide); 664 m_pToolbar->setAlignment(m_alignment); 665 /* Configure own background: */ 666 QPalette pal = m_pToolbar->palette(); 667 pal.setColor(QPalette::Window, palette().color(QPalette::Window)); 668 m_pToolbar->setPalette(pal); 669 /* Configure child connections: */ 670 connect(m_pToolbar, SIGNAL(sigResized()), this, SLOT(sltHandleToolbarResize())); 671 connect(m_pToolbar, SIGNAL(sigAutoHideToggled()), this, SLOT(sltAutoHideToggled())); 672 connect(m_pToolbar, SIGNAL(sigMinimizeAction()), this, SIGNAL(sigMinimizeAction())); 673 connect(m_pToolbar, SIGNAL(sigExitAction()), this, SIGNAL(sigExitAction())); 674 connect(m_pToolbar, SIGNAL(sigCloseAction()), this, SIGNAL(sigCloseAction())); 675 /* Add child to mdi-area: */ 676 m_pEmbeddedToolbar = m_pMdiArea->addSubWindow(m_pToolbar, Qt::Window | Qt::FramelessWindowHint); 677 /* Make sure we have no focus: */ 678 m_pEmbeddedToolbar->setFocusPolicy(Qt::NoFocus); 679 #ifdef Q_WS_WIN 680 /* Install embedded-toolbar event filter: */ 681 m_pEmbeddedToolbar->installEventFilter(this); 682 #endif /* Q_WS_WIN */ 683 } 684 685 /* Prepare hover-enter/leave timers: */ 686 m_pHoverEnterTimer = new QTimer(this); 687 { 688 m_pHoverEnterTimer->setSingleShot(true); 689 m_pHoverEnterTimer->setInterval(50); 690 connect(m_pHoverEnterTimer, SIGNAL(timeout()), this, SLOT(sltHoverEnter())); 691 } 692 m_pHoverLeaveTimer = new QTimer(this); 693 { 694 m_pHoverLeaveTimer->setSingleShot(true); 695 m_pHoverLeaveTimer->setInterval(500); 696 connect(m_pHoverLeaveTimer, SIGNAL(timeout()), this, SLOT(sltHoverLeave())); 697 } 698 699 /* Install 'auto-hide' animation to 'toolbarPosition' property: */ 700 m_pAnimation = UIAnimation::installPropertyAnimation(this, 701 "toolbarPosition", 702 "hiddenToolbarPosition", "shownToolbarPosition", 703 SIGNAL(sigHoverEnter()), SIGNAL(sigHoverLeave()), 704 true); 705 706 /* Adjust geometry finally: */ 707 adjustGeometry(); 708 } 709 710 void UIMiniToolBar::cleanup() 711 { 712 /* Stop hover-enter/leave timers: */ 713 if (m_pHoverEnterTimer && m_pHoverEnterTimer->isActive()) 714 m_pHoverEnterTimer->stop(); 715 if (m_pHoverLeaveTimer && m_pHoverLeaveTimer->isActive()) 716 m_pHoverLeaveTimer->stop(); 717 718 /* Destroy animation before mdi-toolbar: */ 719 delete m_pAnimation; 720 m_pAnimation = 0; 721 722 /* Destroy mdi-toolbar after animation: */ 723 delete m_pEmbeddedToolbar; 724 m_pEmbeddedToolbar = 0; 725 } 726 727 void UIMiniToolBar::enterEvent(QEvent*) 728 { 729 /* Stop the hover-leave timer if necessary: */ 730 if (m_pHoverLeaveTimer && m_pHoverLeaveTimer->isActive()) 731 m_pHoverLeaveTimer->stop(); 732 733 /* Start the hover-enter timer: */ 734 if (m_fAutoHide && m_pHoverEnterTimer) 735 m_pHoverEnterTimer->start(); 736 } 737 738 void UIMiniToolBar::leaveEvent(QEvent*) 739 { 740 /* Stop the hover-enter timer if necessary: */ 741 if (m_pHoverEnterTimer && m_pHoverEnterTimer->isActive()) 742 m_pHoverEnterTimer->stop(); 743 744 /* Start the hover-leave timer: */ 745 if (m_fAutoHide && m_pHoverLeaveTimer) 746 m_pHoverLeaveTimer->start(); 747 } 748 749 #ifdef Q_WS_X11 750 void UIMiniToolBar::resizeEvent(QResizeEvent*) 751 { 752 /* Adjust mini-toolbar on resize: */ 753 adjustGeometry(); 754 } 755 #endif /* Q_WS_X11 */ 756 757 bool UIMiniToolBar::eventFilter(QObject *pWatched, QEvent *pEvent) 758 { 759 #if defined(Q_WS_WIN) 760 /* Due to Qt bug QMdiArea can 761 * 1. steal focus from current application focus-widget 762 * 3. and even request focus stealing if QMdiArea hidden yet. 763 * We have to notify listeners about such facts.. */ 764 if (pWatched && m_pEmbeddedToolbar && pWatched == m_pEmbeddedToolbar && 765 pEvent->type() == QEvent::FocusIn) 766 emit sigNotifyAboutFocusStolen(); 767 #elif defined(Q_WS_X11) 768 /* Detect if we have window activation stolen. */ 769 if (pWatched == this && 770 pEvent->type() == QEvent::WindowActivate) 771 emit sigNotifyAboutFocusStolen(); 772 #endif /* Q_WS_X11 */ 773 774 /* Call to base-class: */ 775 return QWidget::eventFilter(pWatched, pEvent); 776 } 777 778 void UIMiniToolBar::simulateToolbarAutoHiding() 779 { 780 /* This simulation helps user to notice 781 * toolbar location, so it will be used only 782 * 1. if toolbar unhovered and 783 * 2. auto-hide feature enabled: */ 784 if (m_fHovered || !m_fAutoHide) 785 return; 786 787 /* Simulate hover-leave event: */ 788 m_fHovered = true; 789 m_pHoverLeaveTimer->start(); 790 } 791 792 void UIMiniToolBar::setToolbarPosition(QPoint point) 793 { 794 /* Update position: */ 795 AssertPtrReturnVoid(m_pEmbeddedToolbar); 796 m_pEmbeddedToolbar->move(point); 797 } 798 799 QPoint UIMiniToolBar::toolbarPosition() const 800 { 801 /* Return position: */ 802 AssertPtrReturn(m_pEmbeddedToolbar, QPoint()); 803 return m_pEmbeddedToolbar->pos(); 804 } 805 807 806 #include "UIMiniToolBar.moc" 808 807
Note:
See TracChangeset
for help on using the changeset viewer.