1 /***************************************************************************
2 * Copyright (C) 2008 by <haraldhv (at) stud.ntnu.no> *
3 * Copyright (C) 2008 by <peter.penz@gmx.at> *
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),
37 startScrollTimer
= new QTimer(this);
38 startScrollTimer
->setSingleShot(true);
39 startScrollTimer
->setInterval(300);
40 connect(startScrollTimer
, SIGNAL(timeout()),
41 this, SLOT(startScrolling()));
43 timeLine
= new QTimeLine(300, this);
44 connect(timeLine
, SIGNAL(frameChanged(int)),
45 this, SLOT(updateVerticalScrollBar(int)));
47 connect(parent
->verticalScrollBar(), SIGNAL(rangeChanged(int, int)),
48 startScrollTimer
, SLOT(start()));
49 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
50 startScrollTimer
, SLOT(start()));
51 connect(parent
, SIGNAL(collapsed(const QModelIndex
&)),
52 startScrollTimer
, SLOT(start()));
53 connect(parent
, SIGNAL(expanded(const QModelIndex
&)),
54 startScrollTimer
, SLOT(start()));
57 void KTreeView::KTreeViewPrivate::startScrolling()
61 const int viewportHeight
= parent
->viewport()->height();
63 // check whether there is a selected index which is partly visible
64 const QModelIndexList selectedIndexes
= parent
->selectionModel()->selectedIndexes();
65 if (selectedIndexes
.count() == 1) {
66 QModelIndex selectedIndex
= selectedIndexes
.first();
67 const QRect rect
= parent
->visualRect(selectedIndex
);
68 if ((rect
.bottom() >= 0) && (rect
.top() <= viewportHeight
)) {
69 // the selected index is (at least partly) visible, use it as
71 index
= selectedIndex
;
75 if (!index
.isValid()) {
76 // no partly selected index is visible, determine the most left visual index
77 QModelIndex visibleIndex
= parent
->indexAt(QPoint(0, 0));
78 if (!visibleIndex
.isValid()) {
83 int minimum
= parent
->width();
85 const QRect rect
= parent
->visualRect(visibleIndex
);
86 if (rect
.top() > viewportHeight
) {
87 // the current index and all successors are not visible anymore
90 if (rect
.left() < minimum
) {
91 minimum
= rect
.left();
94 visibleIndex
= parent
->indexBelow(visibleIndex
);
95 } while (visibleIndex
.isValid());
98 // start the horizontal scrolling to assure that the item indicated by 'index' gets fully visible
99 Q_ASSERT(index
.isValid());
100 const QRect rect
= parent
->visualRect(index
);
102 QScrollBar
*scrollBar
= parent
->horizontalScrollBar();
103 const int oldScrollBarPos
= scrollBar
->value();
105 const int itemRight
= oldScrollBarPos
+ rect
.left() + rect
.width() - 1;
106 const int availableWidth
= parent
->viewport()->width();
107 int scrollBarPos
= itemRight
- availableWidth
;
108 const int scrollBarPosMax
= oldScrollBarPos
+ rect
.left() - parent
->indentation();
109 if (scrollBarPos
> scrollBarPosMax
) {
110 scrollBarPos
= scrollBarPosMax
;
113 if (scrollBarPos
!= oldScrollBarPos
) {
114 timeLine
->setFrameRange(oldScrollBarPos
, scrollBarPos
);
119 void KTreeView::KTreeViewPrivate::updateVerticalScrollBar(int value
)
121 QScrollBar
*scrollBar
= parent
->horizontalScrollBar();
122 scrollBar
->setValue(value
);
123 startScrollTimer
->stop();
126 // ************************************************
128 KTreeView::KTreeView(QWidget
*parent
) :
130 d(new KTreeViewPrivate(this))
132 if (KGlobalSettings::graphicEffectsLevel() >= KGlobalSettings::SimpleAnimationEffects
) {
133 setAutoHorizontalScroll(true);
137 KTreeView::~KTreeView()
141 void KTreeView::setAutoHorizontalScroll(bool value
)
143 d
->autoHorizontalScroll
= value
;
146 bool KTreeView::autoHorizontalScroll() const
148 return d
->autoHorizontalScroll
;
151 void KTreeView::setSelectionModel(QItemSelectionModel
*selectionModel
)
153 QTreeView::setSelectionModel(selectionModel
);
154 connect(selectionModel
,
155 SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
156 d
->startScrollTimer
, SLOT(start()));
159 void KTreeView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
161 if (d
->autoHorizontalScroll
) {
162 // assure that the value of the horizontal scrollbar stays on its current value,
163 // KTreeView will adjust the value manually
164 const int value
= horizontalScrollBar()->value();
165 QTreeView::scrollTo(index
, hint
);
166 horizontalScrollBar()->setValue(value
);
168 QTreeView::scrollTo(index
, hint
);
172 void KTreeView::hideEvent(QHideEvent
*event
)
174 d
->startScrollTimer
->stop();
176 QTreeView::hideEvent(event
);
179 #include "ktreeview.moc"
180 #include "ktreeview_p.moc"