]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnviewcontainer.cpp
- fixed issue that the horizontal progress bar and the space info bar get hidden...
[dolphin.git] / src / dolphincolumnviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphincolumnviewcontainer.h"
21
22 #include "dolphincolumnview.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "settings/dolphinsettings.h"
26
27 #include "dolphin_columnmodesettings.h"
28
29 #include <QPoint>
30 #include <QScrollBar>
31 #include <QTimeLine>
32
33 DolphinColumnViewContainer::DolphinColumnViewContainer(QWidget* parent,
34 DolphinController* controller) :
35 QScrollArea(parent),
36 m_controller(controller),
37 m_active(false),
38 m_index(-1),
39 m_contentX(0),
40 m_columns(),
41 m_emptyViewport(0),
42 m_animation(0)
43 {
44 Q_ASSERT(controller != 0);
45
46 setAcceptDrops(true);
47 setFocusPolicy(Qt::NoFocus);
48 setFrameShape(QFrame::NoFrame);
49 setLayoutDirection(Qt::LeftToRight);
50
51 //connect(this, SIGNAL(viewportEntered()),
52 // controller, SLOT(emitViewportEntered()));
53 connect(controller, SIGNAL(activationChanged(bool)),
54 this, SLOT(updateColumnsBackground(bool)));
55
56 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
57 this, SLOT(moveContentHorizontally(int)));
58
59 m_animation = new QTimeLine(500, this);
60 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
61
62 DolphinColumnView* column = new DolphinColumnView(viewport(), this, m_controller->url());
63 m_columns.append(column);
64 setActiveColumnIndex(0);
65
66 m_emptyViewport = new QFrame(viewport());
67 m_emptyViewport->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
68
69 updateColumnsBackground(true);
70 }
71
72 DolphinColumnViewContainer::~DolphinColumnViewContainer()
73 {
74 }
75
76 KUrl DolphinColumnViewContainer::rootUrl() const
77 {
78 return m_columns[0]->url();
79 }
80
81 QAbstractItemView* DolphinColumnViewContainer::activeColumn() const
82 {
83 return m_columns[m_index];
84 }
85
86 void DolphinColumnViewContainer::showColumn(const KUrl& url)
87 {
88 if (!rootUrl().isParentOf(url)) {
89 removeAllColumns();
90 m_columns[0]->setUrl(url);
91 return;
92 }
93
94 int columnIndex = 0;
95 foreach (DolphinColumnView* column, m_columns) {
96 if (column->url() == url) {
97 // the column represents already the requested URL, hence activate it
98 requestActivation(column);
99 layoutColumns();
100 return;
101 } else if (!column->url().isParentOf(url)) {
102 // the column is no parent of the requested URL, hence
103 // just delete all remaining columns
104 if (columnIndex > 0) {
105 QList<DolphinColumnView*>::iterator start = m_columns.begin() + columnIndex;
106 QList<DolphinColumnView*>::iterator end = m_columns.end();
107 for (QList<DolphinColumnView*>::iterator it = start; it != end; ++it) {
108 deleteColumn(*it);
109 }
110 m_columns.erase(start, end);
111
112 const int maxIndex = m_columns.count() - 1;
113 Q_ASSERT(maxIndex >= 0);
114 if (m_index > maxIndex) {
115 m_index = maxIndex;
116 }
117 break;
118 }
119 }
120 ++columnIndex;
121 }
122
123 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
124 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
125 // "c" will be created.
126 const int lastIndex = m_columns.count() - 1;
127 Q_ASSERT(lastIndex >= 0);
128
129 const KUrl& activeUrl = m_columns[lastIndex]->url();
130 Q_ASSERT(activeUrl.isParentOf(url));
131 Q_ASSERT(activeUrl != url);
132
133 QString path = activeUrl.url(KUrl::AddTrailingSlash);
134 const QString targetPath = url.url(KUrl::AddTrailingSlash);
135
136 columnIndex = lastIndex;
137 int slashIndex = path.count('/');
138 bool hasSubPath = (slashIndex >= 0);
139 while (hasSubPath) {
140 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
141 if (subPath.isEmpty()) {
142 hasSubPath = false;
143 } else {
144 path += subPath + '/';
145 ++slashIndex;
146
147 const KUrl childUrl = KUrl(path);
148 m_columns[columnIndex]->setChildUrl(childUrl);
149 columnIndex++;
150
151 DolphinColumnView* column = new DolphinColumnView(viewport(), this, childUrl);
152 column->setActive(false);
153
154 m_columns.append(column);
155
156 // Before invoking layoutColumns() the column must be set visible temporary.
157 // To prevent a flickering the initial geometry is set to a hidden position.
158 column->setGeometry(QRect(-1, -1, 1, 1));
159 column->show();
160 layoutColumns();
161 updateScrollBar();
162 }
163 }
164
165 // set the last column as active column without modifying the controller
166 // and hence the history
167 m_columns[m_index]->setActive(false);
168 m_index = columnIndex;
169 m_columns[m_index]->setActive(true);
170 assureVisibleActiveColumn();
171 }
172
173 void DolphinColumnViewContainer::mousePressEvent(QMouseEvent* event)
174 {
175 m_controller->requestActivation();
176 QScrollArea::mousePressEvent(event);
177 }
178
179 void DolphinColumnViewContainer::resizeEvent(QResizeEvent* event)
180 {
181 QScrollArea::resizeEvent(event);
182 layoutColumns();
183 updateScrollBar();
184 assureVisibleActiveColumn();
185 }
186
187 void DolphinColumnViewContainer::wheelEvent(QWheelEvent* event)
188 {
189 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
190 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
191 event->ignore();
192 } else {
193 QScrollArea::wheelEvent(event);
194 }
195 }
196
197 void DolphinColumnViewContainer::moveContentHorizontally(int x)
198 {
199 m_contentX = isRightToLeft() ? +x : -x;
200 layoutColumns();
201 }
202
203 void DolphinColumnViewContainer::updateColumnsBackground(bool active)
204 {
205 if (active == m_active) {
206 return;
207 }
208
209 m_active = active;
210
211 // dim the background of the viewport
212 const QPalette::ColorRole role = viewport()->backgroundRole();
213 QColor background = viewport()->palette().color(role);
214 background.setAlpha(0); // make background transparent
215
216 QPalette palette = viewport()->palette();
217 palette.setColor(role, background);
218 viewport()->setPalette(palette);
219
220 foreach (DolphinColumnView* column, m_columns) {
221 column->updateBackground();
222 }
223 }
224
225 void DolphinColumnViewContainer::setActiveColumnIndex(int index)
226 {
227 if (m_index == index) {
228 return;
229 }
230
231 const bool hasActiveColumn = (m_index >= 0);
232 if (hasActiveColumn) {
233 m_columns[m_index]->setActive(false);
234 }
235
236 m_index = index;
237 m_columns[m_index]->setActive(true);
238
239 assureVisibleActiveColumn();
240 }
241
242 void DolphinColumnViewContainer::layoutColumns()
243 {
244 const int gap = 4;
245
246 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
247 const int columnWidth = settings->columnWidth();
248
249 QRect emptyViewportRect;
250 if (isRightToLeft()) {
251 int x = viewport()->width() - columnWidth + m_contentX;
252 foreach (DolphinColumnView* column, m_columns) {
253 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
254 x -= columnWidth;
255 }
256 emptyViewportRect = QRect(0, 0, x + columnWidth - gap, viewport()->height());
257 } else {
258 int x = m_contentX;
259 foreach (DolphinColumnView* column, m_columns) {
260 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
261 x += columnWidth;
262 }
263 emptyViewportRect = QRect(x, 0, viewport()->width() - x - gap, viewport()->height());
264 }
265
266 if (emptyViewportRect.isValid()) {
267 m_emptyViewport->show();
268 m_emptyViewport->setGeometry(emptyViewportRect);
269 } else {
270 m_emptyViewport->hide();
271 }
272 }
273
274 void DolphinColumnViewContainer::updateScrollBar()
275 {
276 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
277 const int contentWidth = m_columns.count() * settings->columnWidth();
278
279 horizontalScrollBar()->setPageStep(contentWidth);
280 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
281 }
282
283 void DolphinColumnViewContainer::assureVisibleActiveColumn()
284 {
285 const int viewportWidth = viewport()->width();
286 const int x = activeColumn()->x();
287
288 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
289 const int width = settings->columnWidth();
290
291 if (x + width > viewportWidth) {
292 const int newContentX = m_contentX - x - width + viewportWidth;
293 if (isRightToLeft()) {
294 m_animation->setFrameRange(m_contentX, newContentX);
295 } else {
296 m_animation->setFrameRange(-m_contentX, -newContentX);
297 }
298 if (m_animation->state() != QTimeLine::Running) {
299 m_animation->start();
300 }
301 } else if (x < 0) {
302 const int newContentX = m_contentX - x;
303 if (isRightToLeft()) {
304 m_animation->setFrameRange(m_contentX, newContentX);
305 } else {
306 m_animation->setFrameRange(-m_contentX, -newContentX);
307 }
308 if (m_animation->state() != QTimeLine::Running) {
309 m_animation->start();
310 }
311 }
312 }
313
314 void DolphinColumnViewContainer::requestActivation(DolphinColumnView* column)
315 {
316 m_controller->setItemView(column);
317 if (column->isActive()) {
318 assureVisibleActiveColumn();
319 } else {
320 int index = 0;
321 foreach (DolphinColumnView* currColumn, m_columns) {
322 if (currColumn == column) {
323 setActiveColumnIndex(index);
324 return;
325 }
326 ++index;
327 }
328 }
329 }
330
331 void DolphinColumnViewContainer::removeAllColumns()
332 {
333 QList<DolphinColumnView*>::iterator start = m_columns.begin() + 1;
334 QList<DolphinColumnView*>::iterator end = m_columns.end();
335 for (QList<DolphinColumnView*>::iterator it = start; it != end; ++it) {
336 deleteColumn(*it);
337 }
338 m_columns.erase(start, end);
339 m_index = 0;
340 m_columns[0]->setActive(true);
341 assureVisibleActiveColumn();
342 }
343
344 QPoint DolphinColumnViewContainer::columnPosition(DolphinColumnView* column, const QPoint& point) const
345 {
346 const QPoint topLeft = column->frameGeometry().topLeft();
347 return QPoint(point.x() - topLeft.x(), point.y() - topLeft.y());
348 }
349
350 void DolphinColumnViewContainer::deleteColumn(DolphinColumnView* column)
351 {
352 if (column != 0) {
353 if (m_controller->itemView() == column) {
354 m_controller->setItemView(0);
355 }
356 // deleteWhenNotDragSource(column) does not necessarily delete column,
357 // and we want its preview generator destroyed immediately.
358 column->hide();
359 // Prevent automatic destruction of column when this DolphinColumnViewContainer
360 // is destroyed.
361 column->setParent(0);
362 column->disconnect();
363
364 //emit requestColumnDeletion(column);
365 column->deleteLater();
366 }
367 }
368
369 #include "dolphincolumnviewcontainer.moc"