127void QTabBarPrivate::initBasicStyleOption(QStyleOptionTab *option,
int tabIndex)
const
130 const int totalTabs = tabList.size();
132 if (!option || (tabIndex < 0 || tabIndex >= totalTabs))
135 const QTabBarPrivate::Tab &tab = *tabList.at(tabIndex);
137 option->state &= ~(QStyle::State_HasFocus | QStyle::State_MouseOver);
138 option->rect = q->tabRect(tabIndex);
139 const bool isCurrent = tabIndex == currentIndex;
141 if (tabIndex == pressedIndex)
142 option->state |= QStyle::State_Sunken;
144 option->state |= QStyle::State_Selected;
145 if (isCurrent && q->hasFocus())
146 option->state |= QStyle::State_HasFocus;
148 option->state &= ~QStyle::State_Enabled;
149 if (q->isActiveWindow())
150 option->state |= QStyle::State_Active;
151 if (!dragInProgress && option->rect == hoverRect)
152 option->state |= QStyle::State_MouseOver;
153 option->shape = shape;
154 option->text = tab.text;
156 if (tab.textColor.isValid())
157 option->palette.setColor(q->foregroundRole(), tab.textColor);
158 option->icon = tab.icon;
159 option->iconSize = q->iconSize();
161 option->leftButtonSize = tab.leftWidget ? tab.leftWidget->size() : QSize();
162 option->rightButtonSize = tab.rightWidget ? tab.rightWidget->size() : QSize();
163 option->documentMode = documentMode;
165 if (tabIndex > 0 && tabIndex - 1 == currentIndex)
166 option->selectedPosition = QStyleOptionTab::PreviousIsSelected;
167 else if (tabIndex + 1 < totalTabs && tabIndex + 1 == currentIndex)
168 option->selectedPosition = QStyleOptionTab::NextIsSelected;
170 option->selectedPosition = QStyleOptionTab::NotAdjacent;
172 const bool paintBeginning = (tabIndex == firstVisible) || (dragInProgress && tabIndex == pressedIndex + 1);
173 const bool paintEnd = (tabIndex == lastVisible) || (dragInProgress && tabIndex == pressedIndex - 1);
174 if (paintBeginning) {
176 option->position = QStyleOptionTab::OnlyOneTab;
178 option->position = QStyleOptionTab::Beginning;
179 }
else if (paintEnd) {
180 option->position = QStyleOptionTab::End;
182 option->position = QStyleOptionTab::Middle;
185#if QT_CONFIG(tabwidget)
186 if (
const QTabWidget *tw = qobject_cast<
const QTabWidget *>(q->parentWidget())) {
187 option->features |= QStyleOptionTab::HasFrame;
188 if (tw->cornerWidget(Qt::TopLeftCorner) || tw->cornerWidget(Qt::BottomLeftCorner))
189 option->cornerWidgets |= QStyleOptionTab::LeftCornerWidget;
190 if (tw->cornerWidget(Qt::TopRightCorner) || tw->cornerWidget(Qt::BottomRightCorner))
191 option->cornerWidgets |= QStyleOptionTab::RightCornerWidget;
194 if (tab.measuringMinimum)
195 option->features |= QStyleOptionTab::MinimumSizeHint;
196 option->tabIndex = tabIndex;
404void QTabBarPrivate::layoutTabs()
408 QSize size = q->size();
411 bool vertTabs = verticalTabs(shape);
412 int tabChainIndex = 0;
414 const auto *style = q->style();
416 Qt::Alignment tabAlignment = Qt::Alignment(style->styleHint(QStyle::SH_TabBar_Alignment,
nullptr, q));
417 QList<QLayoutStruct> tabChain(tabList.size() + 2);
421 tabChain[tabChainIndex].init();
422 tabChain[tabChainIndex].expansive = (!expanding)
423 && (tabAlignment != Qt::AlignLeft)
424 && (tabAlignment != Qt::AlignJustify);
425 tabChain[tabChainIndex].empty =
true;
440 for (
int i = 0; i < tabList.size(); ++i) {
441 const auto tab = tabList.at(i);
446 QSize sz = q->tabSizeHint(i);
447 tab->maxRect = QRect(x, 0, sz.width(), sz.height());
449 maxHeight = qMax(maxHeight, sz.height());
450 sz = q->minimumTabSizeHint(i);
451 tab->minRect = QRect(minx, 0, sz.width(), sz.height());
453 tabChain[tabChainIndex].init();
454 tabChain[tabChainIndex].sizeHint = tab->maxRect.width();
455 tabChain[tabChainIndex].minimumSize = sz.width();
456 tabChain[tabChainIndex].empty =
false;
457 tabChain[tabChainIndex].expansive =
true;
460 tabChain[tabChainIndex].maximumSize = tabChain[tabChainIndex].sizeHint;
465 available = size.width();
466 maxExtent = maxHeight;
471 for (
int i = 0; i < tabList.size(); ++i) {
472 auto tab = tabList.at(i);
477 QSize sz = q->tabSizeHint(i);
478 tab->maxRect = QRect(0, y, sz.width(), sz.height());
480 maxWidth = qMax(maxWidth, sz.width());
481 sz = q->minimumTabSizeHint(i);
482 tab->minRect = QRect(0, miny, sz.width(), sz.height());
484 tabChain[tabChainIndex].init();
485 tabChain[tabChainIndex].sizeHint = tab->maxRect.height();
486 tabChain[tabChainIndex].minimumSize = sz.height();
487 tabChain[tabChainIndex].empty =
false;
488 tabChain[tabChainIndex].expansive =
true;
491 tabChain[tabChainIndex].maximumSize = tabChain[tabChainIndex].sizeHint;
496 available = size.height();
497 maxExtent = maxWidth;
501 tabChain[tabChainIndex].init();
502 tabChain[tabChainIndex].expansive = (!expanding)
503 && (tabAlignment != Qt::AlignRight)
504 && (tabAlignment != Qt::AlignJustify);
505 tabChain[tabChainIndex].empty =
true;
506 Q_ASSERT(tabChainIndex == tabChain.size() - 1 - hiddenTabs);
509 qGeomCalc(tabChain, 0, tabChain.size(), 0, qMax(available, last), 0);
513 for (
int i = 0; i < tabList.size(); ++i) {
514 auto tab = tabList.at(i);
520 const QLayoutStruct &lstruct = tabChain.at(i + 1 - hiddenTabs);
522 tab->rect.setRect(lstruct.pos, 0, lstruct.size, maxExtent);
524 tab->rect.setRect(0, lstruct.pos, maxExtent, lstruct.size);
527 if (useScrollButtons && tabList.size() && last > available) {
528 const QRect scrollRect = normalizedScrollRect(0);
533 QRect scrollButtonLeftRect = style->subElementRect(QStyle::SE_TabBarScrollLeftButton, &opt, q);
534 QRect scrollButtonRightRect = style->subElementRect(QStyle::SE_TabBarScrollRightButton, &opt, q);
535 int scrollButtonWidth = style->pixelMetric(QStyle::PM_TabBarScrollButtonWidth, &opt, q);
537 const auto setIconOr = [](QToolButton *tb,
const QIcon &icon, Qt::ArrowType arrowType) {
539 tb->setArrowType(arrowType);
547 scrollButtonLeftRect.setHeight(scrollButtonWidth);
548 scrollButtonRightRect.setY(scrollButtonRightRect.bottom() + 1 - scrollButtonWidth);
549 scrollButtonRightRect.setHeight(scrollButtonWidth);
550 const auto upIcon = style->standardIcon(QStyle::SP_TabScrollUpButton, &opt, q);
551 const auto downIcon = style->standardIcon(QStyle::SP_TabScrollDownButton, &opt, q);
552 setIconOr(leftB, upIcon, Qt::UpArrow);
553 setIconOr(rightB, downIcon, Qt::DownArrow);
554 }
else if (q->layoutDirection() == Qt::RightToLeft) {
555 scrollButtonRightRect.setWidth(scrollButtonWidth);
556 scrollButtonLeftRect.setX(scrollButtonLeftRect.right() + 1 - scrollButtonWidth);
557 scrollButtonLeftRect.setWidth(scrollButtonWidth);
558 const auto leftIcon = style->standardIcon(QStyle::SP_TabScrollLeftButton, &opt, q);
559 const auto rightIcon = style->standardIcon(QStyle::SP_TabScrollRightButton, &opt, q);
560 setIconOr(leftB, rightIcon, Qt::RightArrow);
561 setIconOr(rightB, leftIcon, Qt::LeftArrow);
563 scrollButtonLeftRect.setWidth(scrollButtonWidth);
564 scrollButtonRightRect.setX(scrollButtonRightRect.right() + 1 - scrollButtonWidth);
565 scrollButtonRightRect.setWidth(scrollButtonWidth);
566 const auto leftIcon = style->standardIcon(QStyle::SP_TabScrollLeftButton, &opt, q);
567 const auto rightIcon = style->standardIcon(QStyle::SP_TabScrollRightButton, &opt, q);
568 setIconOr(leftB, leftIcon, Qt::LeftArrow);
569 setIconOr(rightB, rightIcon, Qt::RightArrow);
572 leftB->setGeometry(scrollButtonLeftRect);
573 leftB->setEnabled(
false);
576 rightB->setGeometry(scrollButtonRightRect);
577 rightB->setEnabled(last + scrollRect.left() > scrollRect.x() + scrollRect.width());
585 q->tabLayoutChange();
594QRect QTabBarPrivate::normalizedScrollRect(
int index)
const
604 if (leftB->isHidden())
605 return verticalTabs(shape) ? q->rect().transposed() : q->rect();
608 q->initStyleOption(&opt, currentIndex);
609 opt.rect = q->rect();
611 const auto style = q->style();
612 QRect scrollButtonLeftRect = style->subElementRect(QStyle::SE_TabBarScrollLeftButton, &opt, q);
613 QRect scrollButtonRightRect = style->subElementRect(QStyle::SE_TabBarScrollRightButton, &opt, q);
614 QRect tearLeftRect = style->subElementRect(QStyle::SE_TabBarTearIndicatorLeft, &opt, q);
615 QRect tearRightRect = style->subElementRect(QStyle::SE_TabBarTearIndicatorRight, &opt, q);
617 if (verticalTabs(shape)) {
618 int topEdge, bottomEdge;
619 bool leftButtonIsOnTop = scrollButtonLeftRect.y() < q->height() / 2;
620 bool rightButtonIsOnTop = scrollButtonRightRect.y() < q->height() / 2;
622 if (leftButtonIsOnTop && rightButtonIsOnTop) {
623 topEdge = scrollButtonRightRect.bottom() + 1;
624 bottomEdge = q->height();
625 }
else if (!leftButtonIsOnTop && !rightButtonIsOnTop) {
627 bottomEdge = scrollButtonLeftRect.top();
629 topEdge = scrollButtonLeftRect.bottom() + 1;
630 bottomEdge = scrollButtonRightRect.top();
633 const auto lastTab = lastVisibleTab();
636 bool tearTopVisible = index != 0 && topEdge != -scrollOffset;
637 bool tearBottomVisible = index != tabList.size() - 1 && bottomEdge != lastTab->rect.bottom() + 1 - scrollOffset;
638 if (tearTopVisible && !tearLeftRect.isNull())
639 topEdge = tearLeftRect.bottom() + 1;
640 if (tearBottomVisible && !tearRightRect.isNull())
641 bottomEdge = tearRightRect.top();
643 return QRect(topEdge, 0, bottomEdge - topEdge, q->height());
645 if (q->layoutDirection() == Qt::RightToLeft) {
646 scrollButtonLeftRect = QStyle::visualRect(Qt::RightToLeft, q->rect(), scrollButtonLeftRect);
647 scrollButtonRightRect = QStyle::visualRect(Qt::RightToLeft, q->rect(), scrollButtonRightRect);
648 tearLeftRect = QStyle::visualRect(Qt::RightToLeft, q->rect(), tearLeftRect);
649 tearRightRect = QStyle::visualRect(Qt::RightToLeft, q->rect(), tearRightRect);
652 int leftEdge, rightEdge;
653 bool leftButtonIsOnLeftSide = scrollButtonLeftRect.x() < q->width() / 2;
654 bool rightButtonIsOnLeftSide = scrollButtonRightRect.x() < q->width() / 2;
656 if (leftButtonIsOnLeftSide && rightButtonIsOnLeftSide) {
657 leftEdge = scrollButtonRightRect.right() + 1;
658 rightEdge = q->width();
659 }
else if (!leftButtonIsOnLeftSide && !rightButtonIsOnLeftSide) {
661 rightEdge = scrollButtonLeftRect.left();
663 leftEdge = scrollButtonLeftRect.right() + 1;
664 rightEdge = scrollButtonRightRect.left();
667 const auto lastTab = lastVisibleTab();
670 bool tearLeftVisible = index != 0 && leftEdge != -scrollOffset;
671 bool tearRightVisible = index != tabList.size() - 1 && rightEdge != lastTab->rect.right() + 1 - scrollOffset;
672 if (tearLeftVisible && !tearLeftRect.isNull())
673 leftEdge = tearLeftRect.right() + 1;
674 if (tearRightVisible && !tearRightRect.isNull())
675 rightEdge = tearRightRect.left();
677 return QRect(leftEdge, 0, rightEdge - leftEdge, q->height());
690void QTabBarPrivate::makeVisible(
int index)
693 if (!validIndex(index))
696 const auto lastTab = lastVisibleTab();
697 const QRect tabRect = tabList.at(index)->rect;
698 const int oldScrollOffset = scrollOffset;
699 const bool horiz = !verticalTabs(shape);
700 const int available = horiz ? q->width() : q->height();
701 const int tabStart = horiz ? tabRect.left() : tabRect.top();
702 const int tabEnd = horiz ? tabRect.right() : tabRect.bottom();
703 const int lastTabEnd = lastTab ? (horiz ? lastTab->rect.right() : lastTab->rect.bottom()) : 0;
704 const QRect scrollRect = normalizedScrollRect(index);
705 const QRect entireScrollRect = normalizedScrollRect(0);
706 const int scrolledTabBarStart = qMax(1, scrollRect.left() + scrollOffset);
707 const int scrolledTabBarEnd = qMin(lastTabEnd - 1, scrollRect.right() + scrollOffset);
709 if (available >= lastTabEnd) {
712 }
else if (tabStart < scrolledTabBarStart) {
714 scrollOffset = tabStart - scrollRect.left();
715 }
else if (tabEnd > scrolledTabBarEnd) {
717 scrollOffset = qMax(0, tabEnd - scrollRect.right());
718 }
else if (scrollOffset + entireScrollRect.width() > lastTabEnd + 1) {
720 scrollOffset = qMax(0, lastTabEnd - entireScrollRect.width() + 1);
723 leftB->setEnabled(scrollOffset > -scrollRect.left());
724 rightB->setEnabled(scrollOffset < lastTabEnd - scrollRect.right());
726 if (oldScrollOffset != scrollOffset) {
978int QTabBar::insertTab(
int index,
const QIcon& icon,
const QString &text)
981 if (!d->validIndex(index)) {
982 index = d->tabList.size();
983 d->tabList.append(
new QTabBarPrivate::Tab(icon, text));
985 d->tabList.insert(index,
new QTabBarPrivate::Tab(icon, text));
987#ifndef QT_NO_SHORTCUT
988 d->tabList.at(index)->shortcutId = grabShortcut(QKeySequence::mnemonic(text));
990 d->firstVisible = qMax(qMin(index, d->firstVisible), 0);
992 if (d->tabList.size() == 1)
993 setCurrentIndex(index);
994 else if (index <= d->currentIndex)
997 if (index <= d->lastVisible)
1000 d->lastVisible = index;
1002 if (d->closeButtonOnTabs) {
1003 QStyleOptionTab opt;
1004 initStyleOption(&opt, index);
1005 ButtonPosition closeSide = (ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition,
nullptr,
this);
1006 QAbstractButton *closeButton =
new CloseButton(
this);
1007 QObjectPrivate::connect(closeButton, &CloseButton::clicked,
1008 d, &QTabBarPrivate::closeTab);
1009 setTabButton(index, closeSide, closeButton);
1012 for (
const auto tab : std::as_const(d->tabList)) {
1013 if (tab->lastTab >= index)
1017 if (isVisible() && tabAt(d->mousePosition) == index) {
1018 if (d->scrollRect(index).contains(d->mousePosition)) {
1019 d->hoverIndex = index;
1020 d->hoverRect = tabRect(index);
1023 d->hoverRect = QRect();
1038void QTabBar::removeTab(
int index)
1041 if (d->validIndex(index)) {
1042 auto removedTab = d->tabList.at(index);
1043 if (d->dragInProgress)
1044 d->moveTabFinished(d->pressedIndex);
1046#ifndef QT_NO_SHORTCUT
1047 releaseShortcut(d->tabList.at(index)->shortcutId);
1049 if (removedTab->leftWidget) {
1050 removedTab->leftWidget->hide();
1051 removedTab->leftWidget->deleteLater();
1052 removedTab->leftWidget =
nullptr;
1054 if (removedTab->rightWidget) {
1055 removedTab->rightWidget->hide();
1056 removedTab->rightWidget->deleteLater();
1057 removedTab->rightWidget =
nullptr;
1060 int newIndex = removedTab->lastTab;
1061 d->tabList.removeAt(index);
1063 for (
auto tab : std::as_const(d->tabList)) {
1064 if (tab->lastTab == index)
1066 if (tab->lastTab > index)
1070 d->calculateFirstLastVisible(index,
false,
true);
1072 if (index == d->currentIndex) {
1076 d->currentIndex = -1;
1077 if (d->tabList.size() > 0) {
1078 switch(d->selectionBehaviorOnRemove) {
1079 case SelectPreviousTab:
1080 if (newIndex > index)
1082 if (d->validIndex(newIndex) && d->tabList.at(newIndex)->visible)
1085 case SelectRightTab:
1086 newIndex = qBound(d->firstVisible, index, d->lastVisible);
1089 newIndex = qBound(d->firstVisible, index-1, d->lastVisible);
1095 if (d->validIndex(newIndex)) {
1097 int bump = d->tabList.at(newIndex)->lastTab;
1098 setCurrentIndex(newIndex);
1099 d->tabList.at(newIndex)->lastTab = bump;
1102 emit currentChanged(-1);
1105 emit currentChanged(-1);
1107 }
else if (index < d->currentIndex) {
1108 setCurrentIndex(d->currentIndex - 1);
1112 if (d->hoverRect.isValid()) {
1113 update(d->hoverRect);
1114 d->hoverIndex = tabAt(d->mousePosition);
1115 if (d->validIndex(d->hoverIndex)
1116 && d->scrollRect(d->hoverIndex).contains(d->mousePosition)) {
1117 d->hoverRect = tabRect(d->hoverIndex);
1118 update(d->hoverRect);
1120 d->hoverRect = QRect();
1598QSize QTabBar::tabSizeHint(
int index)
const
1602 if (
const QTabBarPrivate::Tab *tab = d->at(index)) {
1603 QStyleOptionTab opt;
1604 d->initBasicStyleOption(&opt, index);
1605 opt.text = tab->text;
1606 QSize iconSize = tab->icon.isNull() ? QSize(0, 0) : opt.iconSize;
1607 int hframe = style()->pixelMetric(QStyle::PM_TabBarTabHSpace, &opt,
this);
1608 int vframe = style()->pixelMetric(QStyle::PM_TabBarTabVSpace, &opt,
this);
1610 int maxWidgetHeight = qMax(opt.leftButtonSize.height(), opt.rightButtonSize.height());
1611 int maxWidgetWidth = qMax(opt.leftButtonSize.width(), opt.rightButtonSize.width());
1613 int widgetWidth = 0;
1614 int widgetHeight = 0;
1616 if (!opt.leftButtonSize.isEmpty()) {
1618 widgetWidth += opt.leftButtonSize.width();
1619 widgetHeight += opt.leftButtonSize.height();
1621 if (!opt.rightButtonSize.isEmpty()) {
1623 widgetWidth += opt.rightButtonSize.width();
1624 widgetHeight += opt.rightButtonSize.height();
1626 if (!opt.icon.isNull())
1629 auto it = d->textSizes.find(tab->text);
1630 if (it == d->textSizes.end()) {
1631 const QFontMetrics fm = fontMetrics();
1632 it = d->textSizes.insert(tab->text, fm.size(Qt::TextShowMnemonic, tab->text)).first;
1634 const int textWidth = it.value().width();
1635 const int textHeight = it.value().height();
1637 if (verticalTabs(d->shape)) {
1638 csz = QSize(qMax(maxWidgetWidth, qMax(textHeight, iconSize.height())) + vframe,
1639 textWidth + iconSize.width() + hframe + widgetHeight + padding);
1641 csz = QSize(textWidth + iconSize.width() + hframe + widgetWidth + padding,
1642 qMax(maxWidgetHeight, qMax(textHeight, iconSize.height())) + vframe);
1645 QSize retSize = style()->sizeFromContents(QStyle::CT_TabBarTab, &opt, csz,
this);
1707bool QTabBar::event(QEvent *event)
1710 switch (event->type()) {
1711 case QEvent::HoverMove:
1712 case QEvent::HoverEnter: {
1713 QHoverEvent *he =
static_cast<QHoverEvent *>(event);
1714 d->mousePosition = he->position().toPoint();
1715 const auto sr = d->scrollRect();
1716 const auto oldHoverRect = d->hoverRect & sr;
1717 if (!oldHoverRect.contains(d->mousePosition)) {
1718 if (d->hoverRect.isValid())
1719 update(d->hoverRect);
1720 d->hoverIndex = tabAt(d->mousePosition);
1721 if (d->validIndex(d->hoverIndex) && sr.contains(d->mousePosition)) {
1722 d->hoverRect = tabRect(d->hoverIndex);
1723 update(d->hoverRect);
1725 d->hoverRect = QRect();
1731 case QEvent::HoverLeave: {
1732 d->mousePosition = {-1, -1};
1733 if (d->hoverRect.isValid())
1734 update(d->hoverRect);
1736 d->hoverRect = QRect();
1737#if QT_CONFIG(wheelevent)
1738 d->accumulatedAngleDelta = QPoint();
1742#if QT_CONFIG(tooltip)
1743 case QEvent::ToolTip:
1744 if (
const QTabBarPrivate::Tab *tab = d->at(tabAt(
static_cast<QHelpEvent*>(event)->pos()))) {
1745 if (!tab->toolTip.isEmpty()) {
1746 QToolTip::showText(
static_cast<QHelpEvent*>(event)->globalPos(), tab->toolTip,
this);
1752#if QT_CONFIG(whatsthis)
1753 case QEvent::QEvent::QueryWhatsThis: {
1754 const QTabBarPrivate::Tab *tab = d->at(d->indexAtPos(
static_cast<QHelpEvent*>(event)->pos()));
1755 if (!tab || tab->whatsThis.isEmpty())
1759 case QEvent::WhatsThis:
1760 if (
const QTabBarPrivate::Tab *tab = d->at(d->indexAtPos(
static_cast<QHelpEvent*>(event)->pos()))) {
1761 if (!tab->whatsThis.isEmpty()) {
1762 QWhatsThis::showText(
static_cast<QHelpEvent*>(event)->globalPos(),
1763 tab->whatsThis,
this);
1769#ifndef QT_NO_SHORTCUT
1771 case QEvent::Shortcut: {
1772 QShortcutEvent *se =
static_cast<QShortcutEvent *>(event);
1773 for (
int i = 0; i < d->tabList.size(); ++i) {
1774 const QTabBarPrivate::Tab *tab = d->tabList.at(i);
1775 if (tab->shortcutId == se->shortcutId()) {
1784 d->updateMacBorderMetrics();
1786#if QT_CONFIG(draganddrop)
1788 case QEvent::DragEnter:
1789 if (d->changeCurrentOnDrag)
1792 case QEvent::DragMove:
1793 if (d->changeCurrentOnDrag) {
1794 const int tabIndex = tabAt(
static_cast<QDragMoveEvent *>(event)->position().toPoint());
1795 if (isTabEnabled(tabIndex) && d->switchTabCurrentIndex != tabIndex) {
1796 d->switchTabCurrentIndex = tabIndex;
1797 d->switchTabTimer.start(
1798 style()->styleHint(QStyle::SH_TabBar_ChangeCurrentDelay,
nullptr,
this) * 1ms,
this);
1803 case QEvent::DragLeave:
1805 d->killSwitchTabTimer();
1809 case QEvent::MouseButtonPress:
1810 case QEvent::MouseButtonRelease:
1811 case QEvent::MouseMove:
1812 d->mousePosition =
static_cast<QMouseEvent *>(event)->position().toPoint();
1813 d->mouseButtons =
static_cast<QMouseEvent *>(event)->buttons();
1819 return QWidget::event(event);
1838void QTabBar::paintEvent(QPaintEvent *)
1842 QStyleOptionTabBarBase optTabBase;
1843 QTabBarPrivate::initStyleBaseOption(&optTabBase,
this, size());
1845 QStylePainter p(
this);
1849 bool vertical = verticalTabs(d->shape);
1850 QStyleOptionTab cutTabLeft;
1851 QStyleOptionTab cutTabRight;
1852 selected = d->currentIndex;
1853 if (d->dragInProgress)
1854 selected = d->pressedIndex;
1855 const QRect scrollRect = d->scrollRect();
1856 const QRect normalizedScrollRect = d->normalizedScrollRect();
1858 for (
int i = 0; i < d->tabList.size(); ++i)
1859 optTabBase.tabBarRect |= tabRect(i);
1861 optTabBase.selectedTabRect = tabRect(selected);
1864 p.drawPrimitive(QStyle::PE_FrameTabBarBase, optTabBase);
1868 if (d->leftB->isVisible() || d->rightB->isVisible()) {
1871 QRegion buttonRegion;
1872 if (d->leftB->isVisible()) {
1873 const auto r = style()->subElementRect(QStyle::SE_TabBarScrollLeftButton, &opt,
this);
1876 if (d->rightB->isVisible()) {
1877 const auto r = style()->subElementRect(QStyle::SE_TabBarScrollRightButton, &opt,
this);
1880 if (!buttonRegion.isEmpty())
1881 p.setClipRegion(QRegion(rect()) - buttonRegion);
1884 for (
int i = 0; i < d->tabList.size(); ++i) {
1885 const auto tab = d->tabList.at(i);
1888 for (
const auto side : { QTabBar::LeftSide, QTabBar::RightSide }) {
1889 if (
auto closeButton = qobject_cast<CloseButton *>(tabButton(i, side)))
1890 closeButton->setParentClipRect(scrollRect);
1892 QStyleOptionTab tabOption;
1893 initStyleOption(&tabOption, i);
1894 if (tab->dragOffset != 0) {
1896 tabOption.rect.moveTop(tabOption.rect.y() + tab->dragOffset);
1898 tabOption.rect.moveLeft(tabOption.rect.x() + tab->dragOffset);
1901 if (!(tabOption.state & QStyle::State_Enabled)) {
1902 tabOption.palette.setCurrentColorGroup(QPalette::Disabled);
1907 QRect tabRect = tab->rect;
1908 int tabStart = vertical ? tabRect.top() : tabRect.left();
1909 int tabEnd = vertical ? tabRect.bottom() : tabRect.right();
1910 if (tabStart < normalizedScrollRect.left() + d->scrollOffset) {
1912 cutTabLeft = tabOption;
1913 }
else if (tabEnd > normalizedScrollRect.right() + d->scrollOffset) {
1915 cutTabRight = tabOption;
1919 if ((!vertical && (tabOption.rect.right() < 0 || tabOption.rect.left() > width()))
1920 || (vertical && (tabOption.rect.bottom() < 0 || tabOption.rect.top() > height())))
1923 optTabBase.tabBarRect |= tabOption.rect;
1927 p.drawControl(QStyle::CE_TabBarTab, tabOption);
1931 if (selected >= 0) {
1932 QStyleOptionTab tabOption;
1933 const auto tab = d->tabList.at(selected);
1934 initStyleOption(&tabOption, selected);
1936 if (tab->dragOffset != 0) {
1939 tabOption.position = QStyleOptionTab::TabPosition::Moving;
1942 tabOption.rect.moveTop(tabOption.rect.y() + tab->dragOffset);
1944 tabOption.rect.moveLeft(tabOption.rect.x() + tab->dragOffset);
1948 const int taboverlap = style()->pixelMetric(QStyle::PM_TabBarTabOverlap,
nullptr,
this);
1949 const QRect &movingRect = verticalTabs(d->shape)
1950 ? tabOption.rect.adjusted(0, -taboverlap, 0, taboverlap)
1951 : tabOption.rect.adjusted(-taboverlap, 0, taboverlap, 0);
1955 if (d->dragInProgress)
1956 d->movingTab->setGeometry(movingRect);
1958 p.drawControl(QStyle::CE_TabBarTab, tabOption);
1962 if (d->leftB->isVisible() && cutLeft >= 0) {
1963 cutTabLeft.rect = rect();
1964 cutTabLeft.rect = style()->subElementRect(QStyle::SE_TabBarTearIndicatorLeft, &cutTabLeft,
this);
1965 p.drawPrimitive(QStyle::PE_IndicatorTabTearLeft, cutTabLeft);
1968 if (d->rightB->isVisible() && cutRight >= 0) {
1969 cutTabRight.rect = rect();
1970 cutTabRight.rect = style()->subElementRect(QStyle::SE_TabBarTearIndicatorRight, &cutTabRight,
this);
1971 p.drawPrimitive(QStyle::PE_IndicatorTabTearRight, cutTabRight);
2053void QTabBar::moveTab(
int from,
int to)
2057 || !d->validIndex(from)
2058 || !d->validIndex(to))
2061 auto &fromTab = *d->tabList.at(from);
2062 auto &toTab = *d->tabList.at(to);
2064 bool vertical = verticalTabs(d->shape);
2065 int oldPressedPosition = 0;
2066 if (d->pressedIndex != -1) {
2068 oldPressedPosition = vertical ? d->tabList.at(d->pressedIndex)->rect.y()
2069 : d->tabList.at(d->pressedIndex)->rect.x();
2073 int start = qMin(from, to);
2074 int end = qMax(from, to);
2075 int width = vertical ? fromTab.rect.height() : fromTab.rect.width();
2078 bool rtl = isRightToLeft();
2079 for (
int i = start; i <= end; ++i) {
2082 auto &tab = *d->tabList.at(i);
2084 tab.rect.moveTop(tab.rect.y() + width);
2086 tab.rect.moveLeft(tab.rect.x() + width);
2088 if (rtl && !vertical)
2090 if (tab.dragOffset != 0)
2091 tab.dragOffset += (direction * width);
2096 fromTab.rect.moveTop(toTab.rect.bottom() + 1);
2098 fromTab.rect.moveTop(toTab.rect.top() - width);
2101 fromTab.rect.moveLeft(toTab.rect.right() + 1);
2103 fromTab.rect.moveLeft(toTab.rect.left() - width);
2107 d->tabList.move(from, to);
2110 for (
const auto tab : std::as_const(d->tabList))
2111 tab->lastTab = d->calculateNewPosition(from, to, tab->lastTab);
2114 int previousIndex = d->currentIndex;
2115 d->currentIndex = d->calculateNewPosition(from, to, d->currentIndex);
2118 if (d->pressedIndex != -1) {
2119 d->pressedIndex = d->calculateNewPosition(from, to, d->pressedIndex);
2120 const auto pressedTab = d->tabList.at(d->pressedIndex);
2121 int newPressedPosition = vertical ? pressedTab->rect.top() : pressedTab->rect.left();
2122 int diff = oldPressedPosition - newPressedPosition;
2123 if (isRightToLeft() && !vertical)
2126 d->dragStartPosition.setY(d->dragStartPosition.y() - diff);
2128 d->dragStartPosition.setX(d->dragStartPosition.x() - diff);
2131 d->layoutWidgets(start);
2133 emit tabMoved(from, to);
2134 if (previousIndex != d->currentIndex)
2135 emit currentChanged(d->currentIndex);
2136 emit tabLayoutChange();
2168void QTabBar::mousePressEvent(QMouseEvent *event)
2172 const QPoint pos = event->position().toPoint();
2173 const bool isEventInCornerButtons = (!d->leftB->isHidden() && d->leftB->geometry().contains(pos))
2174 || (!d->rightB->isHidden() && d->rightB->geometry().contains(pos));
2175 if (!isEventInCornerButtons) {
2176 const int index = d->indexAtPos(pos);
2177 emit tabBarClicked(index);
2180 if (event->button() != Qt::LeftButton) {
2185 if (d->pressedIndex != -1 && d->movable)
2186 d->moveTabFinished(d->pressedIndex);
2188 d->pressedIndex = d->indexAtPos(event->position().toPoint());
2190 if (d->validIndex(d->pressedIndex)) {
2191 QStyleOptionTabBarBase optTabBase;
2192 optTabBase.initFrom(
this);
2193 optTabBase.documentMode = d->documentMode;
2194 if (event->type() == style()->styleHint(QStyle::SH_TabBar_SelectMouseType, &optTabBase,
this))
2195 setCurrentIndex(d->pressedIndex);
2197 repaint(tabRect(d->pressedIndex));
2199 d->dragStartPosition = event->position().toPoint();
2206void QTabBar::mouseMoveEvent(QMouseEvent *event)
2211 if (d->pressedIndex != -1
2212 && event->buttons() == Qt::NoButton)
2213 d->moveTabFinished(d->pressedIndex);
2216 if (!d->dragInProgress && d->pressedIndex != -1) {
2217 if ((event->position().toPoint() - d->dragStartPosition).manhattanLength() > QApplication::startDragDistance()) {
2218 d->dragInProgress =
true;
2219 d->setupMovableTab();
2223 if (event->buttons() == Qt::LeftButton
2224 && d->dragInProgress
2225 && d->validIndex(d->pressedIndex)) {
2226 bool vertical = verticalTabs(d->shape);
2229 dragDistance = (event->position().toPoint().y() - d->dragStartPosition.y());
2231 dragDistance = (event->position().toPoint().x() - d->dragStartPosition.x());
2233 d->tabList.at(d->pressedIndex)->dragOffset = dragDistance;
2235 QRect startingRect = tabRect(d->pressedIndex);
2237 startingRect.moveTop(startingRect.y() + dragDistance);
2239 startingRect.moveLeft(startingRect.x() + dragDistance);
2242 if (dragDistance < 0)
2243 overIndex = tabAt(startingRect.topLeft());
2245 overIndex = tabAt(startingRect.topRight());
2247 if (overIndex != d->pressedIndex && overIndex != -1) {
2249 if (isRightToLeft() && !vertical)
2251 if (dragDistance < 0) {
2255 for (
int i = d->pressedIndex;
2256 offset > 0 ? i < overIndex : i > overIndex;
2258 QRect overIndexRect = tabRect(overIndex);
2259 int needsToBeOver = (vertical ? overIndexRect.height() : overIndexRect.width()) / 2;
2260 if (dragDistance > needsToBeOver)
2261 d->slide(i + offset, d->pressedIndex);
2265 if (d->pressedIndex != -1)
2266 d->layoutTab(d->pressedIndex);
2272 if (event->buttons() != Qt::LeftButton) {
2278void QTabBarPrivate::setupMovableTab()
2282 movingTab =
new QMovableTabWidget(q);
2284 int taboverlap = q->style()->pixelMetric(QStyle::PM_TabBarTabOverlap,
nullptr ,q);
2285 QRect grabRect = q->tabRect(pressedIndex);
2286 if (verticalTabs(shape))
2287 grabRect.adjust(0, -taboverlap, 0, taboverlap);
2289 grabRect.adjust(-taboverlap, 0, taboverlap, 0);
2291 QPixmap grabImage(grabRect.size() * q->devicePixelRatio());
2292 grabImage.setDevicePixelRatio(q->devicePixelRatio());
2293 grabImage.fill(Qt::transparent);
2294 QStylePainter p(&grabImage, q);
2296 QStyleOptionTab tab;
2297 q->initStyleOption(&tab, pressedIndex);
2298 tab.position = QStyleOptionTab::Moving;
2299 if (verticalTabs(shape))
2300 tab.rect.moveTopLeft(QPoint(0, taboverlap));
2302 tab.rect.moveTopLeft(QPoint(taboverlap, 0));
2303 p.drawControl(QStyle::CE_TabBarTab, tab);
2306 movingTab->setPixmap(grabImage);
2307 movingTab->setGeometry(grabRect);
2311 const auto &pressedTab = *tabList.at(pressedIndex);
2312 if (pressedTab.leftWidget)
2313 pressedTab.leftWidget->raise();
2314 if (pressedTab.rightWidget)
2315 pressedTab.rightWidget->raise();
2320 movingTab->setVisible(
true);
2358void QTabBar::mouseReleaseEvent(QMouseEvent *event)
2362 if (d->closeButtonOnTabs && event->button() == Qt::MiddleButton) {
2363 const int index = tabAt(event->pos());
2365 emit tabCloseRequested(index);
2370 if (event->button() != Qt::LeftButton) {
2375 if (d->movable && d->dragInProgress && d->validIndex(d->pressedIndex)) {
2376 int length = d->tabList.at(d->pressedIndex)->dragOffset;
2377 int width = verticalTabs(d->shape)
2378 ? tabRect(d->pressedIndex).height()
2379 : tabRect(d->pressedIndex).width();
2380 int duration = qMin(ANIMATION_DURATION,
2381 (qAbs(length) * ANIMATION_DURATION) / width);
2382 d->tabList.at(d->pressedIndex)->startAnimation(d, duration);
2383 d->dragInProgress =
false;
2384 d->movingTab->setVisible(
false);
2385 d->dragStartPosition = QPoint();
2389 int oldPressedIndex = d->pressedIndex;
2390 int i = d->indexAtPos(event->position().toPoint()) == d->pressedIndex ? d->pressedIndex : -1;
2391 d->pressedIndex = -1;
2392 QStyleOptionTabBarBase optTabBase;
2393 optTabBase.initFrom(
this);
2394 optTabBase.documentMode = d->documentMode;
2395 const bool selectOnRelease =
2396 (style()->styleHint(QStyle::SH_TabBar_SelectMouseType, &optTabBase,
this) == QEvent::MouseButtonRelease);
2397 if (selectOnRelease)
2399 if (d->validIndex(oldPressedIndex))
2400 update(tabRect(oldPressedIndex));