1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

TabBarHelper: Fix a rare bug and compute pressed index, correctly.

This commit is contained in:
S. Razi Alavizadeh 2014-12-25 15:16:31 +03:30
parent 393009f4d1
commit 37ce38bbf8
2 changed files with 47 additions and 1 deletions

View File

@ -930,6 +930,7 @@ TabBarHelper::TabBarHelper(bool isPinnedTabBar, ComboTabBar* comboTabBar)
, m_isPinnedTabBar(isPinnedTabBar)
, m_useFastTabSizeHint(false)
{
connect(this, SIGNAL(tabMoved(int,int)), this, SLOT(tabWasMoved(int,int)));
}
void TabBarHelper::setTabButton(int index, QTabBar::ButtonPosition position, QWidget* widget)
@ -1181,9 +1182,13 @@ void TabBarHelper::mousePressEvent(QMouseEvent* event)
void TabBarHelper::mouseReleaseEvent(QMouseEvent* event)
{
event->ignore();
if (event->button() != Qt::LeftButton) {
return;
}
QTabBar::mouseReleaseEvent(event);
if (m_pressedIndex >= 0) {
if (m_pressedIndex >= 0 && m_pressedIndex < count()) {
const int length = qAbs(m_pressedGlobalX - event->globalX());
const int duration = qMin((length * ANIMATION_DURATION) / tabRect(m_pressedIndex).width(), ANIMATION_DURATION);
QTimer::singleShot(duration, this, SLOT(resetDragState()));
@ -1229,6 +1234,43 @@ void TabBarHelper::resetDragState()
}
}
void TabBarHelper::tabWasMoved(int from, int to)
{
if (m_pressedIndex != -1) {
if (m_pressedIndex == from) {
m_pressedIndex = to;
}
else {
const int start = qMin(from, to);
const int end = qMax(from, to);
if (m_pressedIndex >= start && m_pressedIndex <= end) {
m_pressedIndex += (from < to) ? -1 : 1;
}
}
}
}
void TabBarHelper::tabInserted(int index)
{
if (m_pressedIndex != -1 && index <= m_pressedIndex) {
++m_pressedIndex;
}
}
void TabBarHelper::tabRemoved(int index)
{
if (m_pressedIndex != -1) {
if (index < m_pressedIndex) {
--m_pressedIndex;
}
else if (index == m_pressedIndex) {
m_pressedIndex = -1;
}
}
}
TabScrollBar::TabScrollBar(QWidget* parent)
: QScrollBar(Qt::Horizontal, parent)
{

View File

@ -237,6 +237,7 @@ public slots:
private slots:
void resetDragState();
void tabWasMoved(int from, int to);
private:
bool event(QEvent* ev);
@ -246,6 +247,9 @@ private:
void initStyleOption(QStyleOptionTab* option, int tabIndex) const;
void tabInserted(int index);
void tabRemoved(int index);
ComboTabBar* m_comboTabBar;
QScrollArea* m_scrollArea;