]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/ktreeview.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no> *
3 * Copyright (C) 2008 by <peter.penz19@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "ktreeview.h"
22 #include "ktreeview_p.h"
24 #include <KGlobalSettings>
26 #include <QItemSelectionModel>
31 KTreeView::KTreeViewPrivate::KTreeViewPrivate(KTreeView
*parent
) :
33 autoHorizontalScroll(false),
36 timeLine
= new QTimeLine(500, this);
37 connect(timeLine
, SIGNAL(frameChanged(int)),
38 this, SLOT(updateVerticalScrollBar(int)));
40 connect(parent
->verticalScrollBar(), SIGNAL(rangeChanged(int, int)),
41 this, SLOT(startScrolling()));
42 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
43 this, SLOT(startScrolling()));
44 connect(parent
, SIGNAL(collapsed(const QModelIndex
&)),
45 this, SLOT(startScrolling()));
46 connect(parent
, SIGNAL(expanded(const QModelIndex
&)),
47 this, SLOT(startScrolling()));
50 void KTreeView::KTreeViewPrivate::startScrolling()
52 if (!autoHorizontalScroll
) {
56 // Determine the most left visual index
57 QModelIndex visibleIndex
= parent
->indexAt(QPoint(0, 0));
58 if (!visibleIndex
.isValid()) {
62 QModelIndex index
= visibleIndex
;
63 int minimum
= parent
->width();
65 const QRect rect
= parent
->visualRect(visibleIndex
);
66 if (rect
.top() > parent
->viewport()->height()) {
67 // the current index and all successors are not visible anymore
70 if (rect
.left() < minimum
) {
71 minimum
= rect
.left();
74 visibleIndex
= parent
->indexBelow(visibleIndex
);
75 } while (visibleIndex
.isValid());
77 // Start the horizontal scrolling to assure that the item indicated by 'index' gets fully visible
78 Q_ASSERT(index
.isValid());
79 const QRect rect
= parent
->visualRect(index
);
81 QScrollBar
* scrollBar
= parent
->horizontalScrollBar();
82 const int oldScrollBarPos
= scrollBar
->value();
84 const int itemRight
= oldScrollBarPos
+ rect
.left() + rect
.width() - 1;
85 const int availableWidth
= parent
->viewport()->width();
86 int scrollBarPos
= itemRight
- availableWidth
;
87 const int scrollBarPosMax
= oldScrollBarPos
+ rect
.left() - parent
->indentation();
88 if (scrollBarPos
> scrollBarPosMax
) {
89 scrollBarPos
= scrollBarPosMax
;
92 if (scrollBarPos
!= oldScrollBarPos
) {
93 timeLine
->setFrameRange(oldScrollBarPos
, scrollBarPos
);
94 if (timeLine
->state() == QTimeLine::Running
) {
101 void KTreeView::KTreeViewPrivate::updateVerticalScrollBar(int value
)
103 QScrollBar
*scrollBar
= parent
->horizontalScrollBar();
104 scrollBar
->setValue(value
);
107 // ************************************************
109 KTreeView::KTreeView(QWidget
*parent
) :
111 d(new KTreeViewPrivate(this))
113 if (KGlobalSettings::graphicEffectsLevel() >= KGlobalSettings::SimpleAnimationEffects
) {
114 setAutoHorizontalScroll(true);
118 KTreeView::~KTreeView()
122 void KTreeView::setAutoHorizontalScroll(bool enable
)
124 d
->autoHorizontalScroll
= enable
;
130 bool KTreeView::autoHorizontalScroll() const
132 return d
->autoHorizontalScroll
;
135 void KTreeView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
137 const int value
= horizontalScrollBar()->value();
138 QTreeView::scrollTo(index
, hint
);
139 horizontalScrollBar()->setValue(value
);
142 void KTreeView::hideEvent(QHideEvent
*event
)
145 QTreeView::hideEvent(event
);
148 #include "ktreeview.moc"
149 #include "ktreeview_p.moc"