]> cloud.milkyroute.net Git - dolphin.git/commitdiff
* If one item is selected and the item is (at least partly) visible, assure that...
authorPeter Penz <peter.penz19@gmail.com>
Thu, 8 May 2008 20:44:26 +0000 (20:44 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 8 May 2008 20:44:26 +0000 (20:44 +0000)
* Use QTimeLine instead of QTimer + value

CCMAIL: haraldhv@stud.ntnu.no

svn path=/trunk/KDE/kdebase/apps/; revision=805596

src/ktreeview.cpp
src/ktreeview.h
src/ktreeview_p.h
src/treeviewsidebarpage.cpp
src/treeviewsidebarpage.h

index 7448614f975964d0ffce7b77f19f3996a6dfbcc5..687bfe1e385e5a089489d2cafb19bb489476fda0 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no>                    *
+ *   Copyright (C) 2008 by <peter.penz@gmx.at>                             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
  ***************************************************************************/
 
-#include <KGlobalSettings>
-
-#include <QDebug>
-#include <QScrollBar>
-
 #include "ktreeview.h"
 #include "ktreeview_p.h"
 
-KTreeView::KTreeViewPrivate::KTreeViewPrivate(KTreeView *parent)
-       : parent(parent),
-       autoHorizontalScroll(true),
-       scrollTowards(0),
-       scrollPixels(5),
-       scrollDelay(50),
-       leftSideMargin(30),
-       considerDelay(500),
-       topLeftPoint(QPoint(10,10))
-{
-       Q_ASSERT(parent->verticalScrollBar());
-
-       considerDelayTimer.setInterval(considerDelay);
-
-       connect( &considerDelayTimer,
-                       SIGNAL(timeout()),
-                       this,
-                       SLOT(considerAutoScroll())
-                  );
-
-       connect( parent->verticalScrollBar(),
-                       SIGNAL(rangeChanged(int, int)),
-                       &considerDelayTimer,
-                       SLOT(start())
-                  );
-
-       connect( parent->verticalScrollBar(),
-                       SIGNAL(valueChanged(int)),
-                       &considerDelayTimer,
-                       SLOT(start())
-                  );
-
-       connect( parent,
-                       SIGNAL( collapsed ( const QModelIndex &)),
-                       &considerDelayTimer,
-                       SLOT(start())
-                  );
-
-       connect( parent,
-                       SIGNAL( expanded ( const QModelIndex &)),
-                       &considerDelayTimer,
-                       SLOT(start())
-                  );
+#include <KGlobalSettings>
 
+#include <QItemSelectionModel>
+#include <QScrollBar>
+#include <QTimer>
+#include <QTimeLine>
+
+KTreeView::KTreeViewPrivate::KTreeViewPrivate(KTreeView *parent) :
+    parent(parent),
+    autoHorizontalScroll(false),
+    timeLine(0),
+    startScrollTimer(0)
+{
+    startScrollTimer = new QTimer(this);
+    startScrollTimer->setSingleShot(true);
+    startScrollTimer->setInterval(50);
+    connect(startScrollTimer, SIGNAL(timeout()),
+            this, SLOT(startScrolling()));
+
+    timeLine = new QTimeLine(300, this);
+    connect(timeLine, SIGNAL(frameChanged(int)),
+            this, SLOT(updateVerticalScrollBar(int)));
+
+    connect(parent->verticalScrollBar(), SIGNAL(rangeChanged(int, int)),
+            startScrollTimer, SLOT(start()));
+    connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)),
+            startScrollTimer, SLOT(start()));
+    connect(parent, SIGNAL(collapsed(const QModelIndex&)),
+            startScrollTimer, SLOT(start()));
+    connect(parent, SIGNAL(expanded(const QModelIndex&)),
+            startScrollTimer, SLOT(start()));
 }
 
-void KTreeView::KTreeViewPrivate::considerAutoScroll()
+KTreeView::~KTreeView()
 {
-       qDebug() << "Considering auto scroll";
-
-       QModelIndex i = parent->indexAt(topLeftPoint);
-       int smallest = parent->width();
-
-       while (i.isValid())
-       {
-               QRect r = parent->visualRect(i);
-               if (r.top() > parent->height())
-                       break;
-
-               int leftSide = r.left();
-
-               smallest = qMin(smallest, leftSide);
-               i = parent->indexBelow(i);
-       }
-
-       int currentScroll = parent->horizontalScrollBar()->value();
-
-       setScrollTowards(smallest + currentScroll - leftSideMargin);
-
-       considerDelayTimer.stop();
-
 }
 
-void KTreeView::KTreeViewPrivate::autoScrollTimeout()
+void KTreeView::KTreeViewPrivate::startScrolling()
 {
-
-       Q_ASSERT(parent);
-
-       QScrollBar *scrollBar = parent->horizontalScrollBar();
-       if (scrollBar == NULL)
-       {
-               qDebug() << "Warning: no scrollbar present, but told to scroll.";
-               scrollTimer.stop();
-               return;
-       }
-
-       int currentScroll = scrollBar->value();
-
-       int difference = currentScroll - scrollTowards;
-
-       if (qAbs(difference) < scrollPixels)
-       {
-               scrollBar->setValue(scrollTowards);
-               scrollTimer.stop();
-               return;
-       }
-
-       if (difference < 0)
-       {
-               scrollBar->setValue(currentScroll + scrollPixels);
-       }
-       else
-       {
-               scrollBar->setValue(currentScroll - scrollPixels);
-       }
+    QModelIndex index;
+
+    const int viewportHeight = parent->viewport()->height();
+
+    // check whether there is a selected index which is partly visible
+    const QModelIndexList selectedIndexes = parent->selectionModel()->selectedIndexes();
+    if (selectedIndexes.count() == 1) {
+        QModelIndex selectedIndex = selectedIndexes.first();
+        const QRect rect = parent->visualRect(selectedIndex);
+        if ((rect.bottom() >= 0) && (rect.top() <= viewportHeight)) {
+            // the selected index is (at least partly) visible, use it as
+            // scroll target
+            index = selectedIndex;
+        }
+    }
+
+    if (!index.isValid()) {
+        // no partly selected index is visible, determine the most left visual index
+        QModelIndex visibleIndex = parent->indexAt(QPoint(0, 0));
+        if (!visibleIndex.isValid()) {
+            return;
+        }
+
+        index = visibleIndex;
+        int minimum = parent->width();
+        do {
+            const QRect rect = parent->visualRect(visibleIndex);
+            if (rect.top() > viewportHeight) {
+                // the current index and all successors are not visible anymore
+                break;
+            }
+            if (rect.left() < minimum) {
+                minimum = rect.left();
+                index = visibleIndex;
+            }
+            visibleIndex = parent->indexBelow(visibleIndex);
+        } while (visibleIndex.isValid());
+    }
+
+    // start the horizontal scrolling to assure that the item indicated by 'index' gets fully visible
+    Q_ASSERT(index.isValid());
+    const QRect rect = parent->visualRect(index);
+
+    QScrollBar *scrollBar = parent->horizontalScrollBar();
+    const int oldScrollBarPos = scrollBar->value();
+
+    const int itemRight = oldScrollBarPos + rect.left() + rect.width() - 1;
+    const int availableWidth = parent->viewport()->width();
+    int scrollBarPos = itemRight - availableWidth;
+    const int scrollBarPosMax = oldScrollBarPos + rect.left() - parent->indentation();
+    if (scrollBarPos > scrollBarPosMax) {
+        scrollBarPos = scrollBarPosMax;
+    }
+
+    if (scrollBarPos != oldScrollBarPos) {
+        timeLine->setFrameRange(oldScrollBarPos, scrollBarPos);
+        timeLine->start();
+    }
 }
 
-void KTreeView::KTreeViewPrivate::setScrollTowards( int scrollTowards )
+void KTreeView::KTreeViewPrivate::updateVerticalScrollBar(int value)
 {
-       if (scrollTowards < 0)
-               scrollTowards = 0;
-       this->scrollTowards = scrollTowards;
-       scrollTimer.start(scrollDelay);
+    QScrollBar *scrollBar = parent->horizontalScrollBar();
+    scrollBar->setValue(value);
+    startScrollTimer->stop();
 }
 
-//************************************************
+// ************************************************
 
-KTreeView::KTreeView(QWidget *parent)
-       : QTreeView(parent)
-       d(new KTreeViewPrivate(this))
+KTreeView::KTreeView(QWidget *parent) :
+       QTreeView(parent),
+       d(new KTreeViewPrivate(this))
 {
-       /* The graphicEffectsLevel was not available in the 4.0.3 version of
-        * the libs I was compiling with, so this is left out for now and
-        * enabled by default...
-        */
-       //if (KGlobalSettings::graphicEffectsLevel() >=
-                       //KGlobalSettings::SimpleAnimationEffects)
-       //{
-               setAutoHorizontalScroll(true);
-       //}
-               connect(
-                               &d->scrollTimer,
-                               SIGNAL(timeout()),
-                               d,
-                               SLOT(autoScrollTimeout())
-                          );
-
+    if (KGlobalSettings::graphicEffectsLevel() >= KGlobalSettings::SimpleAnimationEffects) {
+        setAutoHorizontalScroll(true);
+    }
 }
 
 void KTreeView::setAutoHorizontalScroll(bool value)
@@ -169,10 +143,18 @@ void KTreeView::setAutoHorizontalScroll(bool value)
        d->autoHorizontalScroll = value;
 }
 
-bool KTreeView::autoHorizontalScroll( void )
+bool KTreeView::autoHorizontalScroll() const
 {
        return d->autoHorizontalScroll;
 }
 
+void KTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
+{
+    QTreeView::setSelectionModel(selectionModel);
+    connect(selectionModel,
+            SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
+            d->startScrollTimer, SLOT(start()));
+}
+
 #include "ktreeview.moc"
 #include "ktreeview_p.moc"
index 93b4b892ea3ef4ad58e5cfbf230defbab6d9d580..4a6262621f396e7bb33d7b98a77be453db534032 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no>                    *
+ *   Copyright (C) 2008 by <peter.penz@gmx.at>                             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
  ***************************************************************************/
 
-#ifndef _KTREEVIEW_H_
-#define _KTREEVIEW_H_
+#ifndef KTREEVIEW_H
+#define KTREEVIEW_H
 
 #include <QTreeView>
 
 class KTreeView : public QTreeView
 {
+    Q_OBJECT
 
-       Q_OBJECT
+public:
+    KTreeView(QWidget *parent = 0);
+    virtual ~KTreeView();
 
-       public:
-               KTreeView(QWidget *parent = NULL);
+    void setAutoHorizontalScroll(bool value);
+    bool autoHorizontalScroll() const;
 
-               void setAutoHorizontalScroll(bool value);
-               bool autoHorizontalScroll( void );
-
-       private:
-               class KTreeViewPrivate;
-               KTreeViewPrivate *d;
+    virtual void setSelectionModel(QItemSelectionModel *selectionModel);
 
+private:
+    class KTreeViewPrivate;
+    KTreeViewPrivate *d;
 };
 
-#endif /* ifndef _KTREEVIEW_H_ */
+#endif /* ifndef KTREEVIEW_H */
index eb71999f7ee7594556d85133010249cdfc58609c..1cfa463cd89e9e3b6669db3d8e1f5faa54352564 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no>                    *
+ *   Copyright (C) 2008 by <peter.penz@gmx.at>                             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
  ***************************************************************************/
 
-#ifndef _KTREEVIEW_P_H_
-#define _KTREEVIEW_P_H_
+#ifndef KTREEVIEW_P_H
+#define KTREEVIEW_P_H
 
-#include <QTimer>
 #include <QObject>
 
 #include "ktreeview.h"
 
+class QTimer;
+class QTimeLine;
+
 class KTreeView::KTreeViewPrivate : public QObject
 {
-       Q_OBJECT
-
-
-
-       public Q_SLOTS:
-               void autoScrollTimeout();
-               void considerAutoScroll();
-
-       public:
-
-               KTreeViewPrivate(KTreeView *parent);
-               //~KTreeViewPrivate();
-               KTreeView *parent;
-
-               //Function for start scrolling towards a certain position
-               void setScrollTowards( int scrollTowards );
-
-               //Privates for doing the scrolling
-               QTimer scrollTimer;
-               QTimer considerDelayTimer;
-               bool autoHorizontalScroll;
-               int scrollTowards;
+    Q_OBJECT
 
-               //Constants
-               const int scrollPixels;
-               const int scrollDelay;
-               const int leftSideMargin;
-               const int considerDelay;
-               const QPoint topLeftPoint;
+public Q_SLOTS:
+    void startScrolling();
+    void updateVerticalScrollBar(int value);
 
+public:
+    KTreeViewPrivate(KTreeView *parent);
+    KTreeView *parent;
+    void setScrollTowards( int scrollTowards );
 
+    bool autoHorizontalScroll;
+    QTimeLine *timeLine;
+    QTimer *startScrollTimer;
 };
 
-#endif /* ifndef _KTREEVIEW_P_H_ */
+#endif /* ifndef KTREEVIEW_P_H */
index c4a793cbd90c54e0d300020354a713d89d2f0b79..6c89e762bb2eefc382b4d1ccea283cb066140c23 100644 (file)
@@ -41,7 +41,6 @@
 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
     SidebarPage(parent),
     m_setLeafVisible(false),
-    m_horizontalPos(0),
     m_dirLister(0),
     m_dolphinModel(0),
     m_proxyModel(0),
@@ -211,8 +210,6 @@ void TreeViewSidebarPage::expandToLeafDir()
 
 void TreeViewSidebarPage::loadSubTree()
 {
-    m_treeView->selectionModel()->clearSelection();
-
     if (m_leafDir.isParentOf(m_dirLister->url())) {
         // The leaf directory is not a child of the base URL, hence
         // no sub directory must be loaded or selected.
@@ -244,8 +241,6 @@ void TreeViewSidebarPage::loadTree(const KUrl& url)
     Q_ASSERT(m_dirLister != 0);
     m_leafDir = url;
 
-    m_horizontalPos = m_treeView->horizontalScrollBar()->value();
-
     KUrl baseUrl = url;
     if (url.isLocalFile()) {
         // use the root directory as base for local URLs
@@ -283,8 +278,6 @@ void TreeViewSidebarPage::selectLeafDirectory()
 
     QItemSelectionModel* selModel = m_treeView->selectionModel();
     selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
-
-    m_treeView->horizontalScrollBar()->setValue(m_horizontalPos);
 }
 
 #include "treeviewsidebarpage.moc"
index 624b6dc5395a80248eac90c289af2e417462c71b..336d2a6ca083d040e8503c895c12652ca89f6f6a 100644 (file)
@@ -124,7 +124,6 @@ private:
 
 private:
     bool m_setLeafVisible;
-    int m_horizontalPos;
     KDirLister* m_dirLister;
     DolphinModel* m_dolphinModel;
     DolphinSortFilterProxyModel* m_proxyModel;