1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

ComboTabBar: Add draggedTabRect method

This commit is contained in:
David Rosca 2018-01-08 13:22:20 +01:00
parent 84c4f0ff4d
commit 8ea8b56f27
2 changed files with 67 additions and 33 deletions

View File

@ -196,26 +196,16 @@ void ComboTabBar::setTabTextColor(int index, const QColor &color)
QRect ComboTabBar::tabRect(int index) const
{
QRect rect;
if (index != -1) {
bool mainTabBar = index >= pinnedTabsCount();
rect = localTabBar(index)->tabRect(toLocalIndex(index));
return mapFromLocalTabRect(localTabBar(index)->tabRect(toLocalIndex(index)), localTabBar(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);
QRect ComboTabBar::draggedTabRect() const
{
const QRect r = m_pinnedTabBar->draggedTabRect();
if (r.isValid()) {
return mapFromLocalTabRect(r, m_pinnedTabBar);
}
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;
return mapFromLocalTabRect(m_mainTabBar->draggedTabRect(), m_mainTabBar);
}
QPixmap ComboTabBar::tabPixmap(int index) const
@ -931,6 +921,29 @@ int ComboTabBar::toLocalIndex(int globalIndex) const
}
}
QRect ComboTabBar::mapFromLocalTabRect(const QRect &rect, QWidget *tabBar) const
{
if (!rect.isValid()) {
return rect;
}
QRect r = rect;
if (tabBar == m_mainTabBar) {
r.moveLeft(r.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());
r = r.intersected(widgetRect);
} else {
r.moveLeft(r.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());
r = r.intersected(widgetRect);
}
return r;
}
void ComboTabBar::updatePinnedTabBarVisibility()
{
m_pinnedTabBarWidget->setVisible(pinnedTabsCount() > 0);
@ -1021,6 +1034,22 @@ QSize TabBarHelper::baseClassTabSizeHint(int index) const
return QTabBar::tabSizeHint(index);
}
QRect TabBarHelper::draggedTabRect() const
{
if (!m_dragInProgress) {
return QRect();
}
QStyleOptionTab tab;
initStyleOption(&tab, currentIndex());
const int tabDragOffset = dragOffset(&tab, currentIndex());
if (tabDragOffset != 0) {
tab.rect.moveLeft(tab.rect.x() + tabDragOffset);
}
return tab.rect;
}
QPixmap TabBarHelper::tabPixmap(int index) const
{
QStyleOptionTab tab;
@ -1160,6 +1189,22 @@ bool TabBarHelper::event(QEvent* ev)
return false;
}
// Hack to get dragOffset from QTabBar internals
int TabBarHelper::dragOffset(QStyleOptionTab *option, int tabIndex) const
{
QStyle::SubElement element = QStyle::SE_TabBarTabLeftButton;
QWidget *button = tabButton(tabIndex, QTabBar::LeftSide);
if (!button) {
element = QStyle::SE_TabBarTabRightButton;
button = tabButton(tabIndex, QTabBar::RightSide);
}
if (!button) {
return 0;
}
const QPoint p = style()->subElementRect(element, option, this).topLeft();
return button->pos().x() - p.x();
}
// Taken from qtabbar.cpp
void TabBarHelper::initStyleBaseOption(QStyleOptionTabBarBase *optTabBase, QTabBar* tabbar, QSize size)
{
@ -1206,21 +1251,6 @@ void TabBarHelper::paintEvent(QPaintEvent *event)
p.fillRect(rect(), palette().color(QPalette::Background));
}
// Hack to get dragOffset from QTabBar internals
auto dragOffset = [this](QStyleOptionTab *option, int index) {
QStyle::SubElement element = QStyle::SE_TabBarTabLeftButton;
QWidget *button = tabButton(index, QTabBar::LeftSide);
if (!button) {
element = QStyle::SE_TabBarTabRightButton;
button = tabButton(index, QTabBar::RightSide);
}
if (!button) {
return 0;
}
const QPoint p = style()->subElementRect(element, option, this).topLeft();
return button->pos().x() - p.x();
};
QStyleOptionTabBarBase optTabBase;
initStyleBaseOption(&optTabBase, this, size());

View File

@ -75,6 +75,7 @@ public:
void setTabTextColor(int index, const QColor &color);
QRect tabRect(int index) const;
QRect draggedTabRect() const;
QPixmap tabPixmap(int index) const;
// Returns tab index at pos, or -1
@ -197,6 +198,7 @@ private:
TabBarHelper* localTabBar(int index = -1) const;
int toLocalIndex(int globalIndex) const;
QRect mapFromLocalTabRect(const QRect &rect, QWidget *tabBar) const;
void updatePinnedTabBarVisibility();
QHBoxLayout* m_mainLayout;
@ -239,6 +241,7 @@ public:
QSize tabSizeHint(int index) const;
QSize baseClassTabSizeHint(int index) const;
QRect draggedTabRect() const;
QPixmap tabPixmap(int index) const;
bool isActiveTabBar();
@ -269,6 +272,7 @@ private:
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
int dragOffset(QStyleOptionTab *option, int tabIndex) const;
void initStyleOption(QStyleOptionTab* option, int tabIndex) const;
void tabInserted(int index);