Improve responsiveness of the sidebar

I added a zerotimer to make the sidebar emit
currentChanged only after it has been updated as
pressed.
This commit is contained in:
Jens Bache-Wiig
2010-02-18 12:04:00 +01:00
parent 54b2ab7190
commit bd2ba2307b
3 changed files with 25 additions and 6 deletions

View File

@@ -82,6 +82,10 @@ FancyTabBar::FancyTabBar(QWidget *parent)
setAttribute(Qt::WA_Hover, true); setAttribute(Qt::WA_Hover, true);
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
setMouseTracking(true); // Needed for hover events setMouseTracking(true); // Needed for hover events
m_triggerTimer.setSingleShot(true);
// We use a zerotimer to keep the sidebar responsive
connect(&m_triggerTimer, SIGNAL(timeout()), this, SLOT(emitCurrentIndex()));
} }
FancyTabBar::~FancyTabBar() FancyTabBar::~FancyTabBar()
@@ -136,7 +140,6 @@ void FancyTabBar::mouseMoveEvent(QMouseEvent *e)
if (validIndex(m_hoverIndex)) { if (validIndex(m_hoverIndex)) {
m_tabs[m_hoverIndex]->fadeIn(); m_tabs[m_hoverIndex]->fadeIn();
QRect oldHoverRect = m_hoverRect;
m_hoverRect = tabRect(m_hoverIndex); m_hoverRect = tabRect(m_hoverIndex);
} }
} }
@@ -207,6 +210,14 @@ QRect FancyTabBar::tabRect(int index) const
} }
// This keeps the sidebar responsive since
// we get a repaint before loading the
// mode itself
void FancyTabBar::emitCurrentIndex()
{
emit currentChanged(m_currentIndex);
}
void FancyTabBar::mousePressEvent(QMouseEvent *e) void FancyTabBar::mousePressEvent(QMouseEvent *e)
{ {
e->accept(); e->accept();
@@ -269,15 +280,21 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
painter->save(); painter->save();
QColor whiteOverlay(Qt::white); QColor whiteOverlay(Qt::white);
whiteOverlay.setAlpha(int(m_tabs[tabIndex]->fader()/2)); whiteOverlay.setAlpha(int(m_tabs[tabIndex]->fader()/2));
QRect roundRect = rect.adjusted(5, 3, -5, -3); painter->fillRect(rect, whiteOverlay);
painter->setPen(whiteOverlay);
painter->drawLine(rect.topLeft(), rect.topRight());
painter->drawLine(rect.bottomLeft(), rect.bottomRight());
/* QRect roundRect = rect.adjusted(5, 3, -5, -3);
painter->translate(0.5, 0.5); painter->translate(0.5, 0.5);
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(whiteOverlay); painter->setBrush(whiteOverlay);
whiteOverlay.setAlpha(int(m_tabs[tabIndex]->fader())); whiteOverlay.setAlpha(int(m_tabs[tabIndex]->fader()));
painter->setPen(whiteOverlay); painter->setPen(whiteOverlay);
painter->drawRoundedRect(roundRect, 3, 3); painter->drawRoundedRect(roundRect, 3, 3);
*/
painter->restore(); painter->restore();
} }
} }
QString tabText(this->tabText(tabIndex)); QString tabText(this->tabText(tabIndex));
@@ -312,7 +329,7 @@ void FancyTabBar::setCurrentIndex(int index) {
if (isTabEnabled(index)) { if (isTabEnabled(index)) {
m_currentIndex = index; m_currentIndex = index;
update(); update();
emit currentChanged(index); m_triggerTimer.start(0);
} }
} }

View File

@@ -34,6 +34,7 @@
#include <QtGui/QTabBar> #include <QtGui/QTabBar>
#include <QtGui/QStyleOptionTabV2> #include <QtGui/QStyleOptionTabV2>
#include <QtCore/QTimeLine> #include <QtCore/QTimeLine>
#include <QtCore/QTimer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPainter; class QPainter;
@@ -116,6 +117,7 @@ signals:
public slots: public slots:
void updateHover(); void updateHover();
void emitCurrentIndex();
private: private:
static const int m_rounding; static const int m_rounding;
@@ -124,7 +126,7 @@ private:
int m_hoverIndex; int m_hoverIndex;
int m_currentIndex; int m_currentIndex;
QList<FancyTab*> m_tabs; QList<FancyTab*> m_tabs;
QTimer m_triggerTimer;
QSize tabSizeHint(bool minimum = false) const; QSize tabSizeHint(bool minimum = false) const;
}; };

View File

@@ -1,4 +1,4 @@
/************************************************************************** **************************************************************************
** **
** This file is part of Qt Creator ** This file is part of Qt Creator
** **