delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / dolphincolumnview.cpp
blobbc488b8ae5626efb0799ecd59fd8d4988a9c3cf7
1 /***************************************************************************
2 * Copyright (C) 2007 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 ***************************************************************************/
20 #include "dolphincolumnview.h"
22 #include "dolphincolumnwidget.h"
23 #include "dolphincontroller.h"
24 #include "settings/dolphinsettings.h"
25 #include "zoomlevelinfo.h"
27 #include "dolphin_columnmodesettings.h"
29 #include <QPoint>
30 #include <QScrollBar>
31 #include <QTimeLine>
33 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
34 QAbstractItemView(parent),
35 m_controller(controller),
36 m_active(false),
37 m_index(-1),
38 m_contentX(0),
39 m_columns(),
40 m_emptyViewport(0),
41 m_animation(0),
42 m_nameFilter()
44 Q_ASSERT(controller != 0);
46 setAcceptDrops(true);
47 setDragDropMode(QAbstractItemView::DragDrop);
48 setDropIndicatorShown(false);
49 setSelectionMode(ExtendedSelection);
50 setFocusPolicy(Qt::NoFocus);
51 setFrameShape(QFrame::NoFrame);
52 setLayoutDirection(Qt::LeftToRight);
54 connect(this, SIGNAL(viewportEntered()),
55 controller, SLOT(emitViewportEntered()));
56 connect(controller, SIGNAL(zoomLevelChanged(int)),
57 this, SLOT(setZoomLevel(int)));
58 connect(controller, SIGNAL(activationChanged(bool)),
59 this, SLOT(updateColumnsBackground(bool)));
61 const DolphinView* view = controller->dolphinView();
62 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
63 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
64 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
65 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
66 connect(view, SIGNAL(showHiddenFilesChanged()),
67 this, SLOT(slotShowHiddenFilesChanged()));
68 connect(view, SIGNAL(showPreviewChanged()),
69 this, SLOT(slotShowPreviewChanged()));
71 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
72 this, SLOT(moveContentHorizontally(int)));
74 m_animation = new QTimeLine(500, this);
75 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
77 DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, m_controller->url());
78 m_columns.append(column);
79 setActiveColumnIndex(0);
81 m_emptyViewport = new QFrame(viewport());
82 m_emptyViewport->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
84 updateDecorationSize(view->showPreview());
85 updateColumnsBackground(true);
88 DolphinColumnView::~DolphinColumnView()
92 QModelIndex DolphinColumnView::indexAt(const QPoint& point) const
94 foreach (DolphinColumnWidget* column, m_columns) {
95 const QModelIndex index = column->indexAt(columnPosition(column, point));
96 if (index.isValid()) {
97 return index;
101 return QModelIndex();
104 KFileItem DolphinColumnView::itemAt(const QPoint& point) const
106 foreach (DolphinColumnWidget* column, m_columns) {
107 KFileItem item = column->itemAt(columnPosition(column, point));
108 if (!item.isNull()) {
109 return item;
113 return KFileItem();
116 void DolphinColumnView::scrollTo(const QModelIndex& index, ScrollHint hint)
118 activeColumn()->scrollTo(index, hint);
121 QRect DolphinColumnView::visualRect(const QModelIndex& index) const
123 return activeColumn()->visualRect(index);
126 void DolphinColumnView::invertSelection()
128 QItemSelectionModel* selectionModel = activeColumn()->selectionModel();
129 const QAbstractItemModel* itemModel = selectionModel->model();
131 const QModelIndex topLeft = itemModel->index(0, 0);
132 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
133 itemModel->columnCount() - 1);
135 const QItemSelection selection(topLeft, bottomRight);
136 selectionModel->select(selection, QItemSelectionModel::Toggle);
139 void DolphinColumnView::reload()
141 foreach (DolphinColumnWidget* column, m_columns) {
142 column->reload();
146 void DolphinColumnView::setRootUrl(const KUrl& url)
148 removeAllColumns();
149 m_columns[0]->setUrl(url);
152 void DolphinColumnView::setNameFilter(const QString& nameFilter)
154 if (nameFilter != m_nameFilter) {
155 m_nameFilter = nameFilter;
156 foreach (DolphinColumnWidget* column, m_columns) {
157 column->setNameFilter(nameFilter);
162 QString DolphinColumnView::nameFilter() const
164 return m_nameFilter;
167 KUrl DolphinColumnView::rootUrl() const
169 return m_columns[0]->url();
172 void DolphinColumnView::showColumn(const KUrl& url)
174 if (!rootUrl().isParentOf(url)) {
175 setRootUrl(url);
176 return;
179 int columnIndex = 0;
180 foreach (DolphinColumnWidget* column, m_columns) {
181 if (column->url() == url) {
182 // the column represents already the requested URL, hence activate it
183 requestActivation(column);
184 layoutColumns();
185 return;
186 } else if (!column->url().isParentOf(url)) {
187 // the column is no parent of the requested URL, hence
188 // just delete all remaining columns
189 if (columnIndex > 0) {
190 QList<DolphinColumnWidget*>::iterator start = m_columns.begin() + columnIndex;
191 QList<DolphinColumnWidget*>::iterator end = m_columns.end();
192 for (QList<DolphinColumnWidget*>::iterator it = start; it != end; ++it) {
193 deleteColumn(*it);
195 m_columns.erase(start, end);
197 const int maxIndex = m_columns.count() - 1;
198 Q_ASSERT(maxIndex >= 0);
199 if (m_index > maxIndex) {
200 m_index = maxIndex;
202 break;
205 ++columnIndex;
208 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
209 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
210 // "c" will be created.
211 const int lastIndex = m_columns.count() - 1;
212 Q_ASSERT(lastIndex >= 0);
214 const KUrl& activeUrl = m_columns[lastIndex]->url();
215 Q_ASSERT(activeUrl.isParentOf(url));
216 Q_ASSERT(activeUrl != url);
218 QString path = activeUrl.url(KUrl::AddTrailingSlash);
219 const QString targetPath = url.url(KUrl::AddTrailingSlash);
221 columnIndex = lastIndex;
222 int slashIndex = path.count('/');
223 bool hasSubPath = (slashIndex >= 0);
224 while (hasSubPath) {
225 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
226 if (subPath.isEmpty()) {
227 hasSubPath = false;
228 } else {
229 path += subPath + '/';
230 ++slashIndex;
232 const KUrl childUrl = KUrl(path);
233 m_columns[columnIndex]->setChildUrl(childUrl);
234 columnIndex++;
236 DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, childUrl);
237 const QString filter = nameFilter();
238 if (!filter.isEmpty()) {
239 column->setNameFilter(filter);
241 column->setActive(false);
243 m_columns.append(column);
245 // Before invoking layoutColumns() the column must be set visible temporary.
246 // To prevent a flickering the initial geometry is set to a hidden position.
247 column->setGeometry(QRect(-1, -1, 1, 1));
248 column->show();
249 layoutColumns();
250 updateScrollBar();
254 // set the last column as active column without modifying the controller
255 // and hence the history
256 activeColumn()->setActive(false);
257 m_index = columnIndex;
258 activeColumn()->setActive(true);
259 assureVisibleActiveColumn();
262 void DolphinColumnView::editItem(const KFileItem& item)
264 activeColumn()->editItem(item);
267 KFileItemList DolphinColumnView::selectedItems() const
269 return activeColumn()->selectedItems();
272 QMimeData* DolphinColumnView::selectionMimeData() const
274 return activeColumn()->selectionMimeData();
277 void DolphinColumnView::selectAll()
279 activeColumn()->selectAll();
282 bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const
284 Q_UNUSED(index);
285 return false;//activeColumn()->isIndexHidden(index);
288 QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
290 // Parts of this code have been taken from QColumnView::moveCursor().
291 // Copyright (C) 1992-2007 Trolltech ASA.
293 Q_UNUSED(modifiers);
294 if (model() == 0) {
295 return QModelIndex();
298 const QModelIndex current = currentIndex();
299 if (isRightToLeft()) {
300 if (cursorAction == MoveLeft) {
301 cursorAction = MoveRight;
302 } else if (cursorAction == MoveRight) {
303 cursorAction = MoveLeft;
307 switch (cursorAction) {
308 case MoveLeft:
309 if (m_index > 0) {
310 setActiveColumnIndex(m_index - 1);
311 m_controller->triggerUrlChangeRequest(activeColumn()->url());
313 break;
315 case MoveRight:
316 if (m_index < m_columns.count() - 1) {
317 setActiveColumnIndex(m_index + 1);
318 m_controller->triggerUrlChangeRequest(m_columns[m_index]->url());
320 break;
322 default:
323 break;
326 return QModelIndex();
329 void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags)
331 Q_UNUSED(rect);
332 Q_UNUSED(flags);
335 QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const
337 Q_UNUSED(selection);
338 return QRegion();
341 int DolphinColumnView::horizontalOffset() const
343 return -m_contentX;
346 int DolphinColumnView::verticalOffset() const
348 return 0;
351 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
353 m_controller->requestActivation();
354 QAbstractItemView::mousePressEvent(event);
357 void DolphinColumnView::resizeEvent(QResizeEvent* event)
359 QAbstractItemView::resizeEvent(event);
360 layoutColumns();
361 updateScrollBar();
362 assureVisibleActiveColumn();
365 void DolphinColumnView::wheelEvent(QWheelEvent* event)
367 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
368 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
369 event->ignore();
370 return;
372 QAbstractItemView::wheelEvent(event);
375 void DolphinColumnView::setZoomLevel(int level)
377 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
378 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
380 const bool showPreview = m_controller->dolphinView()->showPreview();
381 if (showPreview) {
382 settings->setPreviewSize(size);
383 } else {
384 settings->setIconSize(size);
387 updateDecorationSize(showPreview);
390 void DolphinColumnView::moveContentHorizontally(int x)
392 m_contentX = isRightToLeft() ? +x : -x;
393 layoutColumns();
396 void DolphinColumnView::updateDecorationSize(bool showPreview)
398 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
399 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
400 const QSize size(iconSize, iconSize);
401 setIconSize(size);
403 foreach (QObject* object, viewport()->children()) {
404 if (object->inherits("QListView")) {
405 DolphinColumnWidget* widget = static_cast<DolphinColumnWidget*>(object);
406 widget->setDecorationSize(size);
410 doItemsLayout();
413 void DolphinColumnView::updateColumnsBackground(bool active)
415 if (active == m_active) {
416 return;
419 m_active = active;
421 // dim the background of the viewport
422 const QPalette::ColorRole role = viewport()->backgroundRole();
423 QColor background = viewport()->palette().color(role);
424 background.setAlpha(0); // make background transparent
426 QPalette palette = viewport()->palette();
427 palette.setColor(role, background);
428 viewport()->setPalette(palette);
430 foreach (DolphinColumnWidget* column, m_columns) {
431 column->updateBackground();
435 void DolphinColumnView::slotSortingChanged(DolphinView::Sorting sorting)
437 foreach (DolphinColumnWidget* column, m_columns) {
438 column->setSorting(sorting);
442 void DolphinColumnView::slotSortOrderChanged(Qt::SortOrder order)
444 foreach (DolphinColumnWidget* column, m_columns) {
445 column->setSortOrder(order);
449 void DolphinColumnView::slotShowHiddenFilesChanged()
451 const bool show = m_controller->dolphinView()->showHiddenFiles();
452 foreach (DolphinColumnWidget* column, m_columns) {
453 column->setShowHiddenFiles(show);
457 void DolphinColumnView::slotShowPreviewChanged()
459 const bool show = m_controller->dolphinView()->showPreview();
460 updateDecorationSize(show);
461 foreach (DolphinColumnWidget* column, m_columns) {
462 column->setShowPreview(show);
466 void DolphinColumnView::setActiveColumnIndex(int index)
468 if (m_index == index) {
469 return;
472 const bool hasActiveColumn = (m_index >= 0);
473 if (hasActiveColumn) {
474 m_columns[m_index]->setActive(false);
477 m_index = index;
478 m_columns[m_index]->setActive(true);
480 assureVisibleActiveColumn();
483 void DolphinColumnView::layoutColumns()
485 const int gap = 4;
487 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
488 const int columnWidth = settings->columnWidth();
490 QRect emptyViewportRect;
491 if (isRightToLeft()) {
492 int x = viewport()->width() - columnWidth + m_contentX;
493 foreach (DolphinColumnWidget* column, m_columns) {
494 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
495 x -= columnWidth;
497 emptyViewportRect = QRect(0, 0, x + columnWidth - gap, viewport()->height());
498 } else {
499 int x = m_contentX;
500 foreach (DolphinColumnWidget* column, m_columns) {
501 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
502 x += columnWidth;
504 emptyViewportRect = QRect(x, 0, viewport()->width() - x - gap, viewport()->height());
507 if (emptyViewportRect.isValid()) {
508 m_emptyViewport->show();
509 m_emptyViewport->setGeometry(emptyViewportRect);
510 } else {
511 m_emptyViewport->hide();
515 void DolphinColumnView::updateScrollBar()
517 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
518 const int contentWidth = m_columns.count() * settings->columnWidth();
520 horizontalScrollBar()->setPageStep(contentWidth);
521 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
524 void DolphinColumnView::assureVisibleActiveColumn()
526 const int viewportWidth = viewport()->width();
527 const int x = activeColumn()->x();
529 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
530 const int width = settings->columnWidth();
532 if (x + width > viewportWidth) {
533 const int newContentX = m_contentX - x - width + viewportWidth;
534 if (isRightToLeft()) {
535 m_animation->setFrameRange(m_contentX, newContentX);
536 } else {
537 m_animation->setFrameRange(-m_contentX, -newContentX);
539 if (m_animation->state() != QTimeLine::Running) {
540 m_animation->start();
542 } else if (x < 0) {
543 const int newContentX = m_contentX - x;
544 if (isRightToLeft()) {
545 m_animation->setFrameRange(m_contentX, newContentX);
546 } else {
547 m_animation->setFrameRange(-m_contentX, -newContentX);
549 if (m_animation->state() != QTimeLine::Running) {
550 m_animation->start();
555 void DolphinColumnView::requestActivation(DolphinColumnWidget* column)
557 m_controller->setItemView(column);
558 if (column->isActive()) {
559 assureVisibleActiveColumn();
560 } else {
561 int index = 0;
562 foreach (DolphinColumnWidget* currColumn, m_columns) {
563 if (currColumn == column) {
564 setActiveColumnIndex(index);
565 return;
567 ++index;
572 void DolphinColumnView::removeAllColumns()
574 QList<DolphinColumnWidget*>::iterator start = m_columns.begin() + 1;
575 QList<DolphinColumnWidget*>::iterator end = m_columns.end();
576 for (QList<DolphinColumnWidget*>::iterator it = start; it != end; ++it) {
577 deleteColumn(*it);
579 m_columns.erase(start, end);
580 m_index = 0;
581 m_columns[0]->setActive(true);
582 assureVisibleActiveColumn();
585 QPoint DolphinColumnView::columnPosition(DolphinColumnWidget* column, const QPoint& point) const
587 const QPoint topLeft = column->frameGeometry().topLeft();
588 return QPoint(point.x() - topLeft.x(), point.y() - topLeft.y());
591 void DolphinColumnView::deleteColumn(DolphinColumnWidget* column)
593 if (column != 0) {
594 if (m_controller->itemView() == column) {
595 m_controller->setItemView(0);
597 column->disconnect();
598 column->deleteLater();
602 #include "dolphincolumnview.moc"