]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Provide horizontal auto scrolling for the tree view (implemented by Harald Hvaal...
authorPeter Penz <peter.penz19@gmail.com>
Tue, 22 Apr 2008 05:45:16 +0000 (05:45 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Tue, 22 Apr 2008 05:45:16 +0000 (05:45 +0000)
CCMAIL: haraldhv@stud.ntnu.no

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

src/CMakeLists.txt
src/ktreeview.cpp [new file with mode: 0644]
src/ktreeview.h [new file with mode: 0644]
src/ktreeview_p.h [new file with mode: 0644]
src/sidebartreeview.cpp
src/sidebartreeview.h

index 80353a26838bab419d8d632f2041033230505fc5..6d96b7b570c1193957bd41e9747c00907c0293f0 100644 (file)
@@ -105,6 +105,7 @@ set(dolphin_SRCS
     iconsizedialog.cpp
     iconsviewsettingspage.cpp
     infosidebarpage.cpp
+       ktreeview.cpp
     main.cpp
     metadatawidget.cpp
     metatextlabel.cpp
diff --git a/src/ktreeview.cpp b/src/ktreeview.cpp
new file mode 100644 (file)
index 0000000..7448614
--- /dev/null
@@ -0,0 +1,178 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no>                    *
+ *                                                                         *
+ *   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  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   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())
+                  );
+
+}
+
+void KTreeView::KTreeViewPrivate::considerAutoScroll()
+{
+       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()
+{
+
+       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);
+       }
+}
+
+void KTreeView::KTreeViewPrivate::setScrollTowards( int scrollTowards )
+{
+       if (scrollTowards < 0)
+               scrollTowards = 0;
+       this->scrollTowards = scrollTowards;
+       scrollTimer.start(scrollDelay);
+}
+
+//************************************************
+
+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())
+                          );
+
+}
+
+void KTreeView::setAutoHorizontalScroll(bool value)
+{
+       d->autoHorizontalScroll = value;
+}
+
+bool KTreeView::autoHorizontalScroll( void )
+{
+       return d->autoHorizontalScroll;
+}
+
+#include "ktreeview.moc"
+#include "ktreeview_p.moc"
diff --git a/src/ktreeview.h b/src/ktreeview.h
new file mode 100644 (file)
index 0000000..93b4b89
--- /dev/null
@@ -0,0 +1,42 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no>                    *
+ *                                                                         *
+ *   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  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#ifndef _KTREEVIEW_H_
+#define _KTREEVIEW_H_
+
+#include <QTreeView>
+
+class KTreeView : public QTreeView
+{
+
+       Q_OBJECT
+
+       public:
+               KTreeView(QWidget *parent = NULL);
+
+               void setAutoHorizontalScroll(bool value);
+               bool autoHorizontalScroll( void );
+
+       private:
+               class KTreeViewPrivate;
+               KTreeViewPrivate *d;
+
+};
+
+#endif /* ifndef _KTREEVIEW_H_ */
diff --git a/src/ktreeview_p.h b/src/ktreeview_p.h
new file mode 100644 (file)
index 0000000..eb71999
--- /dev/null
@@ -0,0 +1,63 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no>                    *
+ *                                                                         *
+ *   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  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#ifndef _KTREEVIEW_P_H_
+#define _KTREEVIEW_P_H_
+
+#include <QTimer>
+#include <QObject>
+
+#include "ktreeview.h"
+
+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;
+
+               //Constants
+               const int scrollPixels;
+               const int scrollDelay;
+               const int leftSideMargin;
+               const int considerDelay;
+               const QPoint topLeftPoint;
+
+
+};
+
+#endif /* ifndef _KTREEVIEW_P_H_ */
index f14ddbacb399653f9dc76fec99b4c8b85e28aad0..efe406a0e926d7ef66742fbcf1d289efbeef8f4f 100644 (file)
@@ -30,7 +30,7 @@
 #include <QScrollBar>
 
 SidebarTreeView::SidebarTreeView(QWidget* parent) :
-    QTreeView(parent)
+    KTreeView(parent)
 {
     setAcceptDrops(true);
     setUniformRowHeights(true);
index a181290f228e660cfdf6b5b72bb4f07cb7f4cc39..f692be3a2cf1bbc7be0815a7d3cc84abbfb50175 100644 (file)
 #define SIDEBARTREEVIEW_H
 
 #include <kurl.h>
-#include <QtGui/QTreeView>
+#include <ktreeview.h>
 
 /**
  * @brief Tree view widget which is used for the sidebar panel.
  *
  * @see TreeViewSidebarPage
  */
-class SidebarTreeView : public QTreeView
+class SidebarTreeView : public KTreeView
 {
     Q_OBJECT