int ComboTabBar::insertTab(int index, const QIcon &icon, const QString &text, bool pinned) { if (pinned) { index = m_pinnedTabBar->insertTab(index, icon, text); } else { index = m_mainTabBar->insertTab(index - pinnedTabsCount(), icon, text); if (tabsClosable()) { QWidget* closeButton = m_mainTabBar->tabButton(index, closeButtonPosition()); if ((closeButton && closeButton->objectName() != QLatin1String("combotabbar_tabs_close_button")) || !closeButton) { // insert our close button insertCloseButton(index + pinnedTabsCount()); if (closeButton) { closeButton->deleteLater(); } } } index += pinnedTabsCount(); } updatePinnedTabBarVisibility(); tabInserted(index); setMinimumWidths(); return index; }
void ComboTabBar::slotCurrentChanged(int index) { if (m_blockCurrentChangedSignal) { return; } if (sender() == m_pinnedTabBar) { if (index == -1 && m_mainTabBar->count() > 0) { m_mainTabBar->setActiveTabBar(true); m_pinnedTabBar->setActiveTabBar(false); emit currentChanged(pinnedTabsCount()); } else { m_pinnedTabBar->setActiveTabBar(true); m_mainTabBar->setActiveTabBar(false); emit currentChanged(index); } } else { if (index == -1 && pinnedTabsCount() > 0) { m_pinnedTabBar->setActiveTabBar(true); m_mainTabBar->setActiveTabBar(false); emit currentChanged(pinnedTabsCount() - 1); } else { m_mainTabBar->setActiveTabBar(true); m_pinnedTabBar->setActiveTabBar(false); emit currentChanged(index + pinnedTabsCount()); } } }
void ComboTabBar::slotTabMoved(int from, int to) { if (sender() == m_pinnedTabBar) { emit tabMoved(from, to); } else { emit tabMoved(from + pinnedTabsCount(), to + pinnedTabsCount()); } }
void ComboTabBar::slotTabCloseRequested(int index) { if (sender() == m_pinnedTabBar) { emit tabCloseRequested(index); } else { emit tabCloseRequested(index + pinnedTabsCount()); } }
int ComboTabBar::currentIndex() const { if (m_pinnedTabBar->isActiveTabBar()) { return m_pinnedTabBar->currentIndex(); } else { return (m_mainTabBar->currentIndex() == -1 ? -1 : pinnedTabsCount() + m_mainTabBar->currentIndex()); } }
void ComboTabBar::ensureVisible(int index, int xmargin) { if (index == -1) { index = currentIndex(); } if (index < pinnedTabsCount()) { if (xmargin == -1) { xmargin = qMax(20, comboTabBarPixelMetric(PinnedTabWidth)); } m_pinnedTabBarWidget->ensureVisible(index, xmargin); } else { if (xmargin == -1) { xmargin = comboTabBarPixelMetric(OverflowedTabWidth); } index -= pinnedTabsCount(); m_mainTabBarWidget->ensureVisible(index, xmargin); } }
void ComboTabBar::insertCloseButton(int index) { index -= pinnedTabsCount(); if (index < 0) { return; } QAbstractButton* closeButton = new CloseButton(this); closeButton->setToolTip(m_closeButtonsToolTip); connect(closeButton, SIGNAL(clicked()), this, SLOT(closeTabFromButton())); m_mainTabBar->setTabButton(index, closeButtonPosition(), closeButton); }
void ComboTabBar::moveTab(int from, int to) { if (from >= pinnedTabsCount() && to >= pinnedTabsCount()) { m_mainTabBar->moveTab(from - pinnedTabsCount(), to - pinnedTabsCount()); } else if (from < pinnedTabsCount() && to < pinnedTabsCount()) { m_pinnedTabBar->moveTab(from, to); } }
int ComboTabBar::tabAt(const QPoint &pos) const { int index = m_pinnedTabBarWidget->tabAt(m_pinnedTabBarWidget->mapFromParent(pos)); if (index != -1) { return index; } index = m_mainTabBarWidget->tabAt(m_mainTabBarWidget->mapFromParent(pos)); if (index != -1) { index += pinnedTabsCount(); } return index; }
int ComboTabBar::tabAt(const QPoint &pos) const { QWidget* w = QApplication::widgetAt(mapToGlobal(pos)); if (!qobject_cast<TabBarHelper*>(w) && !qobject_cast<TabIcon*>(w)) return -1; int index = m_pinnedTabBarWidget->tabAt(m_pinnedTabBarWidget->mapFromParent(pos)); if (index != -1) return index; index = m_mainTabBarWidget->tabAt(m_mainTabBarWidget->mapFromParent(pos)); if (index != -1) index += pinnedTabsCount(); return index; }
void ComboTabBar::closeTabFromButton() { QWidget* button = qobject_cast<QWidget*>(sender()); int tabToClose = -1; for (int i = 0; i < m_mainTabBar->count(); ++i) { if (m_mainTabBar->tabButton(i, closeButtonPosition()) == button) { tabToClose = i; break; } } if (tabToClose != -1) { emit tabCloseRequested(tabToClose + pinnedTabsCount()); } }
QRect ComboTabBar::tabRect(int index) const { QRect rect; if (index != -1) { bool mainTabBar = index >= pinnedTabsCount(); rect = localTabBar(index)->tabRect(toLocalIndex(index)); if (mainTabBar) { rect.moveLeft(rect.x() + mapFromGlobal(m_mainTabBar->mapToGlobal(QPoint(0, 0))).x()); QRect widgetRect = m_mainTabBarWidget->scrollArea()->viewport()->rect(); widgetRect.moveLeft(widgetRect.x() + mapFromGlobal(m_mainTabBarWidget->scrollArea()->viewport()->mapToGlobal(QPoint(0, 0))).x()); rect = rect.intersected(widgetRect); } else { rect.moveLeft(rect.x() + mapFromGlobal(m_pinnedTabBar->mapToGlobal(QPoint(0, 0))).x()); QRect widgetRect = m_pinnedTabBarWidget->scrollArea()->viewport()->rect(); widgetRect.moveLeft(widgetRect.x() + mapFromGlobal(m_pinnedTabBarWidget->scrollArea()->viewport()->mapToGlobal(QPoint(0, 0))).x()); rect = rect.intersected(widgetRect); } } return rect; }
void ComboTabBar::setTabsClosable(bool closable) { if (closable == tabsClosable()) { return; } if (closable) { // insert our close button for (int i = 0; i < m_mainTabBar->count(); ++i) { QWidget* closeButton = m_mainTabBar->tabButton(i, closeButtonPosition()); if (closeButton) { if (closeButton->objectName() == QLatin1String("combotabbar_tabs_close_button")) { continue; } } insertCloseButton(i + pinnedTabsCount()); if (closeButton) { closeButton->deleteLater(); } } } m_mainTabBar->setTabsClosable(closable); }
bool ComboTabBar::isPinned(int index) const { return index >= 0 && index < pinnedTabsCount(); }
int ComboTabBar::count() const { return pinnedTabsCount() + m_mainTabBar->count(); }
QSize TabBar::tabSizeHint(int index, bool fast) const { if (!m_window->isVisible()) { // Don't calculate it when window is not visible // It produces invalid size anyway return QSize(-1, -1); } const int pinnedTabWidth = comboTabBarPixelMetric(ComboTabBar::PinnedTabWidth); const int minTabWidth = comboTabBarPixelMetric(ComboTabBar::NormalTabMinimumWidth); QSize size = ComboTabBar::tabSizeHint(index); // The overflowed tabs have same size and we can use this fast method if (fast) { size.setWidth(index >= pinnedTabsCount() ? minTabWidth : pinnedTabWidth); return size; } WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index)); TabBar* tabBar = const_cast <TabBar*>(this); if (webTab && webTab->isPinned()) { size.setWidth(pinnedTabWidth); } else { int availableWidth = mainTabBarWidth() - comboTabBarPixelMetric(ExtraReservedWidth); if (availableWidth < 0) { return QSize(-1, -1); } const int normalTabsCount = ComboTabBar::normalTabsCount(); const int maxTabWidth = comboTabBarPixelMetric(ComboTabBar::NormalTabMaximumWidth); if (availableWidth >= maxTabWidth * normalTabsCount) { m_normalTabWidth = maxTabWidth; size.setWidth(m_normalTabWidth); } else if (normalTabsCount > 0) { const int minActiveTabWidth = comboTabBarPixelMetric(ComboTabBar::ActiveTabMinimumWidth); int maxWidthForTab = availableWidth / normalTabsCount; int realTabWidth = maxWidthForTab; bool adjustingActiveTab = false; if (realTabWidth < minActiveTabWidth) { maxWidthForTab = normalTabsCount > 1 ? (availableWidth - minActiveTabWidth) / (normalTabsCount - 1) : 0; realTabWidth = minActiveTabWidth; adjustingActiveTab = true; } bool tryAdjusting = availableWidth >= minTabWidth * normalTabsCount; if (m_showCloseOnInactive != 1 && tabsClosable() && availableWidth < (minTabWidth + 25) * normalTabsCount) { // Hiding close buttons to save some space tabBar->setTabsClosable(false); tabBar->showCloseButton(currentIndex()); } if (m_showCloseOnInactive == 1) { // Always showing close buttons tabBar->setTabsClosable(true); tabBar->showCloseButton(currentIndex()); } if (tryAdjusting) { m_normalTabWidth = maxWidthForTab; // Fill any empty space (we've got from rounding) with active tab if (index == mainTabBarCurrentIndex()) { if (adjustingActiveTab) { m_activeTabWidth = (availableWidth - minActiveTabWidth - maxWidthForTab * (normalTabsCount - 1)) + realTabWidth; } else { m_activeTabWidth = (availableWidth - maxWidthForTab * normalTabsCount) + maxWidthForTab; } size.setWidth(m_activeTabWidth); } else { size.setWidth(m_normalTabWidth); } } } // Restore close buttons according to preferences if (m_showCloseOnInactive != 2 && !tabsClosable() && availableWidth >= (minTabWidth + 25) * normalTabsCount) { tabBar->setTabsClosable(true); // Hide close buttons on pinned tabs for (int i = 0; i < count(); ++i) { tabBar->updatePinnedTabCloseButton(i); } } } if (index == count() - 1) { WebTab* lastMainActiveTab = qobject_cast<WebTab*>(m_tabWidget->widget(mainTabBarCurrentIndex())); int xForAddTabButton = cornerWidth(Qt::TopLeftCorner) + pinTabBarWidth() + normalTabsCount() * m_normalTabWidth; if (lastMainActiveTab && m_activeTabWidth > m_normalTabWidth) { xForAddTabButton += m_activeTabWidth - m_normalTabWidth; } if (QApplication::layoutDirection() == Qt::RightToLeft) { xForAddTabButton = width() - xForAddTabButton; } emit tabBar->moveAddTabButton(xForAddTabButton); } return size; }
int ComboTabBar::mainTabBarCurrentIndex() const { return (m_mainTabBar->currentIndex() == -1 ? -1 : pinnedTabsCount() + m_mainTabBar->currentIndex()); }