X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/bfb705c5cc9e43f2bf3a1e961dba01f80d165b74..093efca22dfd247f06e2a669ad968300e71ef08d:/src/dolphincolumnview.cpp diff --git a/src/dolphincolumnview.cpp b/src/dolphincolumnview.cpp index 7817bc4b9..3f6523ebe 100644 --- a/src/dolphincolumnview.cpp +++ b/src/dolphincolumnview.cpp @@ -19,6 +19,7 @@ #include "dolphincolumnview.h" +#include "dolphinmodel.h" #include "dolphincontroller.h" #include "dolphinsettings.h" @@ -27,10 +28,13 @@ #include #include #include -#include #include +#include #include +#include +#include +#include /** * Represents one column inside the DolphinColumnView and has been @@ -53,16 +57,21 @@ public: * drawn in a lighter color. All operations are applied to this column. */ void setActive(bool active); - inline bool isActive() const; - - inline const KUrl& url() const; + bool isActive() const; /** - * Updates the selection that the folder gets selected which represents - * the URL \a url. If \a url is empty, the selection of the column widget - * gets cleared. + * Sets the directory URL of the child column that is shown next to + * this column. This property is only used for a visual indication + * of the shown directory, it does not trigger a loading of the model. */ - void updateSelection(const KUrl& url); + void setChildUrl(const KUrl& url); + const KUrl& childUrl() const; + + /** Sets the directory URL that is shown inside the column widget. */ + void setUrl(const KUrl& url); + + /** Returns the directory URL that is shown inside the column widget. */ + inline const KUrl& url() const; protected: virtual QStyleOptionViewItem viewOptions() const; @@ -70,11 +79,11 @@ protected: virtual void dragLeaveEvent(QDragLeaveEvent* event); virtual void dragMoveEvent(QDragMoveEvent* event); virtual void dropEvent(QDropEvent* event); - virtual void mousePressEvent(QMouseEvent* event); - virtual void mouseMoveEvent(QMouseEvent* event); - virtual void mouseReleaseEvent(QMouseEvent* event); virtual void paintEvent(QPaintEvent* event); + virtual void mousePressEvent(QMouseEvent* event); + virtual void keyPressEvent(QKeyEvent* event); virtual void contextMenuEvent(QContextMenuEvent* event); + virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected); private: /** Used by ColumnWidget::setActive(). */ @@ -85,9 +94,9 @@ private: private: bool m_active; - bool m_swallowMouseMoveEvents; DolphinColumnView* m_view; - KUrl m_url; + KUrl m_url; // URL of the directory that is shown + KUrl m_childUrl; // URL of the next column that is shown QStyleOptionViewItem m_viewOptions; bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4 @@ -99,14 +108,22 @@ ColumnWidget::ColumnWidget(QWidget* parent, const KUrl& url) : QListView(parent), m_active(true), - m_swallowMouseMoveEvents(false), m_view(columnView), m_url(url), + m_childUrl(), m_dragging(false), m_dropRect() { setMouseTracking(true); viewport()->setAttribute(Qt::WA_Hover); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); + setSelectionBehavior(SelectItems); + setSelectionMode(QAbstractItemView::ExtendedSelection); + setDragDropMode(QAbstractItemView::DragDrop); + setDropIndicatorShown(false); + setFocusPolicy(Qt::NoFocus); // apply the column mode settings to the widget const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); @@ -122,7 +139,15 @@ ColumnWidget::ColumnWidget(QWidget* parent, const int iconSize = settings->iconSize(); m_viewOptions.decorationSize = QSize(iconSize, iconSize); + KFileItemDelegate* delegate = new KFileItemDelegate(this); + setItemDelegate(delegate); + activate(); + + connect(this, SIGNAL(entered(const QModelIndex&)), + m_view->m_controller, SLOT(emitItemEntered(const QModelIndex&))); + connect(this, SIGNAL(viewportEntered()), + m_view->m_controller, SLOT(emitViewportEntered())); } ColumnWidget::~ColumnWidget() @@ -155,35 +180,24 @@ inline bool ColumnWidget::isActive() const return m_active; } -const KUrl& ColumnWidget::url() const +inline void ColumnWidget::setChildUrl(const KUrl& url) { - return m_url; + m_childUrl = url; } -void ColumnWidget::updateSelection(const KUrl& url) +inline const KUrl& ColumnWidget::childUrl() const { - setSelectionMode(SingleSelection); - QItemSelectionModel* selModel = selectionModel(); - if (url.isEmpty()) { - if (!m_active) { - selModel->clear(); - } - } else { - const QAbstractProxyModel* proxyModel = static_cast(m_view->model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - const QModelIndex dirIndex = dirModel->indexForUrl(url); - const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex); + return m_childUrl; +} - const QItemSelection selection = selModel->selection(); - const bool isIndexSelected = selModel->isSelected(proxyIndex); +inline void ColumnWidget::setUrl(const KUrl& url) +{ + m_url = url; +} - if (!m_active && ((selection.count() > 1) || !isIndexSelected)) { - selModel->clear(); - } - if (!isIndexSelected) { - selModel->select(proxyIndex, QItemSelectionModel::Select); - } - } +const KUrl& ColumnWidget::url() const +{ + return m_url; } QStyleOptionViewItem ColumnWidget::viewOptions() const @@ -213,7 +227,7 @@ void ColumnWidget::dragMoveEvent(QDragMoveEvent* event) { QListView::dragMoveEvent(event); - // TODO: remove this code when the issue #160611 is solved in Qt 4.4 + // TODO: remove this code when the issue #160611 is solved in Qt 4.4 const QModelIndex index = indexAt(event->pos()); setDirtyRegion(m_dropRect); m_dropRect = visualRect(index); @@ -226,6 +240,7 @@ void ColumnWidget::dropEvent(QDropEvent* event) if (!urls.isEmpty()) { event->acceptProposedAction(); m_view->m_controller->indicateDroppedUrls(urls, + url(), indexAt(event->pos()), event->source()); } @@ -233,60 +248,56 @@ void ColumnWidget::dropEvent(QDropEvent* event) m_dragging = false; } -void ColumnWidget::mousePressEvent(QMouseEvent* event) +void ColumnWidget::paintEvent(QPaintEvent* event) { - QListView::mousePressEvent(event); - - const QModelIndex index = indexAt(event->pos()); - - bool requestActivation = false; - if (index.isValid()) { - // A click on an item has been done. Only request an activation - // if the item is not a directory. - const QAbstractProxyModel* proxyModel = static_cast(m_view->model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - const QModelIndex dirIndex = proxyModel->mapToSource(index); - KFileItem* item = dirModel->itemForIndex(dirIndex); - requestActivation = (item != 0) && !item->isDir(); - } else { - // a click on the viewport has been done - requestActivation = true; - - // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView - // triggers an unwanted loading of directories on hovering folder items. - m_swallowMouseMoveEvents = true; + if (!m_childUrl.isEmpty()) { + // indicate the shown URL of the next column by highlighting the shown folder item + const QModelIndex dirIndex = m_view->m_dolphinModel->indexForUrl(m_childUrl); + const QModelIndex proxyIndex = m_view->m_proxyModel->mapFromSource(dirIndex); + if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) { + const QRect itemRect = visualRect(proxyIndex); + QPainter painter(viewport()); + painter.save(); + + QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color(); + color.setAlpha(32); + painter.setPen(Qt::NoPen); + painter.setBrush(color); + painter.drawRect(itemRect); + + painter.restore(); + } } - if (requestActivation) { - m_view->requestActivation(this); - } else { - m_view->updateSelections(); + QListView::paintEvent(event); + + // TODO: remove this code when the issue #160611 is solved in Qt 4.4 + if (m_dragging) { + const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight); + DolphinController::drawHoverIndication(viewport(), m_dropRect, brush); } } -void ColumnWidget::mouseMoveEvent(QMouseEvent* event) +void ColumnWidget::mousePressEvent(QMouseEvent* event) { - // see description in ColumnView::mousePressEvent() - if (!m_swallowMouseMoveEvents) { - QListView::mouseMoveEvent(event); + if (!m_active) { + m_view->requestActivation(this); } -} -void ColumnWidget::mouseReleaseEvent(QMouseEvent* event) -{ - QListView::mouseReleaseEvent(event); - m_swallowMouseMoveEvents = false; + QListView::mousePressEvent(event); } - -void ColumnWidget::paintEvent(QPaintEvent* event) +void ColumnWidget::keyPressEvent(QKeyEvent* event) { - QListView::paintEvent(event); + QListView::keyPressEvent(event); - // TODO: remove this code when the issue #160611 is solved in Qt 4.4 - if (m_dragging) { - const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight); - DolphinController::drawHoverIndication(viewport(), m_dropRect, brush); + const QItemSelectionModel* selModel = selectionModel(); + const QModelIndex currentIndex = selModel->currentIndex(); + const bool triggerItem = currentIndex.isValid() + && (event->key() == Qt::Key_Return) + && (selModel->selectedIndexes().count() <= 1); + if (triggerItem) { + m_view->m_controller->triggerItem(currentIndex); } } @@ -307,49 +318,90 @@ void ColumnWidget::contextMenuEvent(QContextMenuEvent* event) } } +void ColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) +{ + QListView::selectionChanged(selected, deselected); + + QItemSelectionModel* selModel = m_view->selectionModel(); + selModel->select(selected, QItemSelectionModel::Select); + selModel->select(deselected, QItemSelectionModel::Deselect); +} + void ColumnWidget::activate() { - const QColor bgColor = KColorScheme(KColorScheme::View).background(); + if (m_view->hasFocus()) { + setFocus(Qt::OtherFocusReason); + } + m_view->setFocusProxy(this); + + // TODO: Connecting to the signal 'activated()' is not possible, as kstyle + // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is + // necessary connecting the signal 'singleClick()' or 'doubleClick'. + if (KGlobalSettings::singleClick()) { + connect(this, SIGNAL(clicked(const QModelIndex&)), + m_view->m_controller, SLOT(triggerItem(const QModelIndex&))); + } else { + connect(this, SIGNAL(doubleClicked(const QModelIndex&)), + m_view->m_controller, SLOT(triggerItem(const QModelIndex&))); + } + + const QColor bgColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color(); QPalette palette = viewport()->palette(); palette.setColor(viewport()->backgroundRole(), bgColor); viewport()->setPalette(palette); + if (!m_childUrl.isEmpty()) { + // assure that the current index is set on the index that represents + // the child URL + const QModelIndex dirIndex = m_view->m_dolphinModel->indexForUrl(m_childUrl); + const QModelIndex proxyIndex = m_view->m_proxyModel->mapFromSource(dirIndex); + selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::Current); + } + update(); } void ColumnWidget::deactivate() { - QColor bgColor = KColorScheme(KColorScheme::View).background(); - const QColor fgColor = KColorScheme(KColorScheme::View).foreground(); - bgColor = KColorUtils::mix(bgColor, fgColor, 0.04); + // TODO: Connecting to the signal 'activated()' is not possible, as kstyle + // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is + // necessary connecting the signal 'singleClick()' or 'doubleClick'. + if (KGlobalSettings::singleClick()) { + disconnect(this, SIGNAL(clicked(const QModelIndex&)), + m_view->m_controller, SLOT(triggerItem(const QModelIndex&))); + } else { + disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)), + m_view->m_controller, SLOT(triggerItem(const QModelIndex&))); + } - QPalette palette = viewport()->palette(); - palette.setColor(viewport()->backgroundRole(), bgColor); + const QPalette palette = m_view->viewport()->palette(); viewport()->setPalette(palette); + selectionModel()->clear(); update(); } // --- DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) : - QColumnView(parent), - m_controller(controller) + QAbstractItemView(parent), + m_controller(controller), + m_restoreActiveColumnFocus(false), + m_dirListerCompleted(false), + m_index(-1), + m_contentX(0), + m_columns(), + m_animation(0), + m_dolphinModel(0), + m_proxyModel(0) { Q_ASSERT(controller != 0); setAcceptDrops(true); setDragDropMode(QAbstractItemView::DragDrop); setDropIndicatorShown(false); - setSelectionMode(SingleSelection); + setSelectionMode(ExtendedSelection); - if (KGlobalSettings::singleClick()) { - connect(this, SIGNAL(clicked(const QModelIndex&)), - this, SLOT(triggerItem(const QModelIndex&))); - } else { - connect(this, SIGNAL(doubleClicked(const QModelIndex&)), - this, SLOT(triggerItem(const QModelIndex&))); - } connect(this, SIGNAL(entered(const QModelIndex&)), controller, SLOT(emitItemEntered(const QModelIndex&))); connect(this, SIGNAL(viewportEntered()), @@ -359,114 +411,322 @@ DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* control connect(controller, SIGNAL(zoomOut()), this, SLOT(zoomOut())); connect(controller, SIGNAL(urlChanged(const KUrl&)), - this, SLOT(updateColumnsState(const KUrl&))); + this, SLOT(showColumn(const KUrl&))); + + connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), + this, SLOT(moveContentHorizontally(int))); + + ColumnWidget* column = new ColumnWidget(viewport(), this, m_controller->url()); + m_columns.append(column); + setActiveColumnIndex(0); updateDecorationSize(); + + m_animation = new QTimeLine(500, this); + connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int))); + + // dim the background of the viewport + QColor bgColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color(); + const QColor fgColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color(); + bgColor = KColorUtils::mix(bgColor, fgColor, 0.04); + + QPalette palette = viewport()->palette(); + palette.setColor(viewport()->backgroundRole(), bgColor); + viewport()->setPalette(palette); } DolphinColumnView::~DolphinColumnView() { } -QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index) +QModelIndex DolphinColumnView::indexAt(const QPoint& point) const { - // let the column widget be aware about its URL... - KUrl columnUrl; - if (viewport()->children().count() == 0) { - // For the first column widget the directory lister has not been started - // yet, hence use the URL from the controller instead. - columnUrl = m_controller->url(); - } else { - const QAbstractProxyModel* proxyModel = static_cast(model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - - const QModelIndex dirModelIndex = proxyModel->mapToSource(index); - KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex); - if (fileItem != 0) { - columnUrl = fileItem->url(); + foreach (ColumnWidget* column, m_columns) { + const QPoint topLeft = column->frameGeometry().topLeft(); + const QPoint adjustedPoint(point.x() - topLeft.x(), point.y() - topLeft.y()); + const QModelIndex index = column->indexAt(adjustedPoint); + if (index.isValid()) { + return index; } } - ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl); + return QModelIndex(); +} - // The following code has been copied 1:1 from QColumnView::createColumn(). - // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method - // QColumnView::initializeColumn() will be available for this. +void DolphinColumnView::scrollTo(const QModelIndex& index, ScrollHint hint) +{ + activeColumn()->scrollTo(index, hint); +} - view->setFrameShape(QFrame::NoFrame); - view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); - view->setMinimumWidth(100); - view->setAttribute(Qt::WA_MacShowFocusRect, false); +QRect DolphinColumnView::visualRect(const QModelIndex& index) const +{ + return activeColumn()->visualRect(index); +} + +void DolphinColumnView::setModel(QAbstractItemModel* model) +{ + if (m_dolphinModel != 0) { + m_dolphinModel->disconnect(this); + } - // copy the 'view' behavior - view->setDragDropMode(dragDropMode()); - view->setDragDropOverwriteMode(dragDropOverwriteMode()); - view->setDropIndicatorShown(showDropIndicator()); - view->setAlternatingRowColors(alternatingRowColors()); - view->setAutoScroll(hasAutoScroll()); - view->setEditTriggers(editTriggers()); - view->setHorizontalScrollMode(horizontalScrollMode()); - view->setIconSize(iconSize()); - view->setSelectionBehavior(selectionBehavior()); - view->setSelectionMode(selectionMode()); - view->setTabKeyNavigation(tabKeyNavigation()); - view->setTextElideMode(textElideMode()); - view->setVerticalScrollMode(verticalScrollMode()); + m_proxyModel = static_cast(model); + m_dolphinModel = static_cast(m_proxyModel->sourceModel()); + connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)), + this, SLOT(triggerReloadColumns(const QModelIndex&))); - view->setModel(model()); + KDirLister* dirLister = m_dolphinModel->dirLister(); + connect(dirLister, SIGNAL(started(const KUrl&)), + this, SLOT(slotDirListerStarted(const KUrl&))); + connect(dirLister, SIGNAL(completed()), + this, SLOT(slotDirListerCompleted())); - // set the delegate to be the columnview delegate - QAbstractItemDelegate* delegate = view->itemDelegate(); - view->setItemDelegate(itemDelegate()); - delete delegate; + activeColumn()->setModel(model); + QAbstractItemView::setModel(model); +} + +void DolphinColumnView::invertSelection() +{ + // TODO: this approach of inverting the selection is quite slow. It should + // be possible to speedup the implementation by using QItemSelection, but + // all adempts have failed yet... - view->setRootIndex(index); + ColumnWidget* column = activeColumn(); + QItemSelectionModel* selModel = column->selectionModel(); - if (model()->canFetchMore(index)) { - model()->fetchMore(index); + KDirLister* dirLister = m_dolphinModel->dirLister(); + const KFileItemList list = dirLister->itemsForDir(column->url()); + foreach (KFileItem* item, list) { + const QModelIndex index = m_dolphinModel->indexForUrl(item->url()); + selModel->select(m_proxyModel->mapFromSource(index), QItemSelectionModel::Toggle); + } +} + +void DolphinColumnView::reload() +{ + // Due to the reloading of the model all columns will be reset to show + // the same content as the first column. As this is not wanted, all columns + // except of the first column are temporary hidden until the root index can + // be updated again. + m_restoreActiveColumnFocus = false; + QList::iterator start = m_columns.begin() + 1; + QList::iterator end = m_columns.end(); + for (QList::iterator it = start; it != end; ++it) { + ColumnWidget* column = (*it); + if (column->isActive() && column->hasFocus()) { + // because of hiding the column, it will lose the focus + // -> remember that the focus should be restored after reloading + m_restoreActiveColumnFocus = true; + } + column->hide(); } - return view; + // all columns are hidden, now reload the directory lister + KDirLister* dirLister = m_dolphinModel->dirLister(); + const KUrl& rootUrl = m_columns[0]->url(); + dirLister->openUrl(rootUrl, false, true); + updateColumns(); } -void DolphinColumnView::mousePressEvent(QMouseEvent* event) +void DolphinColumnView::showColumn(const KUrl& url) { - m_controller->triggerActivation(); - QColumnView::mousePressEvent(event); + const KUrl& rootUrl = m_columns[0]->url(); + if (!rootUrl.isParentOf(url)) { + // the URL is no child URL of the column view, hence clear all columns + // and reset the root column + QList::iterator start = m_columns.begin() + 1; + QList::iterator end = m_columns.end(); + for (QList::iterator it = start; it != end; ++it) { + (*it)->deleteLater(); + } + m_columns.erase(start, end); + m_index = 0; + m_columns[0]->setActive(true); + m_columns[0]->setUrl(url); + assureVisibleActiveColumn(); + return; + } + + KDirLister* dirLister = m_dolphinModel->dirLister(); + const KUrl dirListerUrl = dirLister->url(); + if (dirListerUrl != rootUrl) { + // It is possible that root URL of the directory lister is adjusted + // after creating the column widget (e. g. when restoring the history + // having a different root URL than the controller indicates). + m_columns[0]->setUrl(dirListerUrl); + } + + int columnIndex = 0; + foreach (ColumnWidget* column, m_columns) { + if (column->url() == url) { + // the column represents already the requested URL, hence activate it + requestActivation(column); + return; + } else if (!column->url().isParentOf(url)) { + // the column is no parent of the requested URL, hence + // just delete all remaining columns + if (columnIndex > 0) { + QList::iterator start = m_columns.begin() + columnIndex; + QList::iterator end = m_columns.end(); + for (QList::iterator it = start; it != end; ++it) { + (*it)->deleteLater(); + } + m_columns.erase(start, end); + + const int maxIndex = m_columns.count() - 1; + Q_ASSERT(maxIndex >= 0); + if (m_index > maxIndex) { + m_index = maxIndex; + } + break; + } + } + ++columnIndex; + } + + // Create missing columns. Assuming that the path is "/home/peter/Temp/" and + // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and + // "c" will be created. + const int lastIndex = m_columns.count() - 1; + Q_ASSERT(lastIndex >= 0); + + const KUrl& activeUrl = m_columns[lastIndex]->url(); + Q_ASSERT(activeUrl.isParentOf(url)); + Q_ASSERT(activeUrl != url); + + QString path = activeUrl.url(KUrl::AddTrailingSlash); + const QString targetPath = url.url(KUrl::AddTrailingSlash); + + columnIndex = lastIndex; + int slashIndex = path.count('/'); + bool hasSubPath = (slashIndex >= 0); + while (hasSubPath) { + const QString subPath = targetPath.section('/', slashIndex, slashIndex); + if (subPath.isEmpty()) { + hasSubPath = false; + } else { + path += subPath + '/'; + ++slashIndex; + + const KUrl childUrl = KUrl(path); + const QModelIndex dirIndex = m_dolphinModel->indexForUrl(KUrl(path)); + const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); + + m_columns[columnIndex]->setChildUrl(childUrl); + columnIndex++; + + ColumnWidget* column = new ColumnWidget(viewport(), this, childUrl); + column->setVerticalScrollMode(ColumnWidget::ScrollPerPixel); + column->setHorizontalScrollMode(ColumnWidget::ScrollPerPixel); + column->setModel(model()); + column->setRootIndex(proxyIndex); + column->setActive(false); + + m_columns.append(column); + + // Before invoking layoutColumns() the column must be set visible temporary. + // To prevent a flickering the initial geometry is set to a hidden position. + column->setGeometry(QRect(-1, -1, 1, 1)); + column->show(); + layoutColumns(); + updateScrollBar(); + + // the layout is finished, now let the column be invisible until it + // gets a valid root index due to expandToActiveUrl() + column->hide(); + } + } + + // set the last column as active column without modifying the controller + // and hence the history + activeColumn()->setActive(false); + m_index = columnIndex; + activeColumn()->setActive(true); } -void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event) +void DolphinColumnView::selectAll() { - if (event->mimeData()->hasUrls()) { - event->acceptProposedAction(); - } + activeColumn()->selectAll(); } -void DolphinColumnView::dropEvent(QDropEvent* event) +bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const { - const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData()); - if (!urls.isEmpty()) { - m_controller->indicateDroppedUrls(urls, - indexAt(event->pos()), - event->source()); - event->acceptProposedAction(); - } - QColumnView::dropEvent(event); + Q_UNUSED(index); + return false;//activeColumn()->isIndexHidden(index); } -void DolphinColumnView::showEvent(QShowEvent* event) +QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) { - QColumnView::showEvent(event); - if (!event->spontaneous()) { - // QColumnView might clear the selection for folders that are shown in the next column. - // As this is not wanted the selection is updated if the directory lister has been completed. - const QAbstractProxyModel* proxyModel = static_cast(model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - KDirLister* dirLister = dirModel->dirLister(); - connect(dirLister, SIGNAL(completed()), - this, SLOT(updateSelections())); + // Parts of this code have been taken from QColumnView::moveCursor(). + // Copyright (C) 1992-2007 Trolltech ASA. + + Q_UNUSED(modifiers); + if (model() == 0) { + return QModelIndex(); } + + const QModelIndex current = currentIndex(); + if (isRightToLeft()) { + if (cursorAction == MoveLeft) { + cursorAction = MoveRight; + } else if (cursorAction == MoveRight) { + cursorAction = MoveLeft; + } + } + + switch (cursorAction) { + case MoveLeft: + if (m_index > 0) { + setActiveColumnIndex(m_index - 1); + } + break; + + case MoveRight: + if (m_index < m_columns.count() - 1) { + setActiveColumnIndex(m_index + 1); + } + break; + + default: + break; + } + + return QModelIndex(); +} + +void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags) +{ + Q_UNUSED(rect); + Q_UNUSED(flags); + //activeColumn()->setSelection(rect, flags); +} + +QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const +{ + Q_UNUSED(selection); + return QRegion(); //activeColumn()->visualRegionForSelection(selection); +} + +int DolphinColumnView::horizontalOffset() const +{ + return -m_contentX; +} + +int DolphinColumnView::verticalOffset() const +{ + return 0; +} + +void DolphinColumnView::mousePressEvent(QMouseEvent* event) +{ + m_controller->triggerActivation(); + QAbstractItemView::mousePressEvent(event); +} + +void DolphinColumnView::resizeEvent(QResizeEvent* event) +{ + QAbstractItemView::resizeEvent(event); + layoutColumns(); + updateScrollBar(); } void DolphinColumnView::zoomIn() @@ -497,23 +757,12 @@ void DolphinColumnView::zoomOut() } } -void DolphinColumnView::triggerItem(const QModelIndex& index) +void DolphinColumnView::moveContentHorizontally(int x) { - m_controller->triggerItem(index); - updateColumnsState(m_controller->url()); + m_contentX = -x; + layoutColumns(); } -void DolphinColumnView::updateColumnsState(const KUrl& url) -{ - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - widget->setActive(widget->url() == url); - } - } -} - - void DolphinColumnView::updateDecorationSize() { ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); @@ -532,21 +781,67 @@ void DolphinColumnView::updateDecorationSize() doItemsLayout(); } -void DolphinColumnView::updateSelections() +void DolphinColumnView::expandToActiveUrl() { - ColumnWidget* previousWidget = 0; - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - if (previousWidget != 0) { - previousWidget->updateSelection(widget->url()); - } - previousWidget = widget; - } + const int lastIndex = m_columns.count() - 1; + Q_ASSERT(lastIndex >= 0); + const KUrl& activeUrl = m_columns[lastIndex]->url(); + const KUrl rootUrl = m_dolphinModel->dirLister()->url(); + const bool expand = m_dirListerCompleted + && rootUrl.isParentOf(activeUrl) + && !rootUrl.equals(activeUrl, KUrl::CompareWithoutTrailingSlash); + if (expand) { + m_dolphinModel->expandToUrl(activeUrl); } - if (previousWidget != 0) { - previousWidget->updateSelection(KUrl()); + updateColumns(); +} + +void DolphinColumnView::triggerUpdateColumns(const QModelIndex& index) +{ + Q_UNUSED(index); + // the updating of the columns may not be done in the context of this slot + QMetaObject::invokeMethod(this, "updateColumns", Qt::QueuedConnection); +} + +void DolphinColumnView::updateColumns() +{ + KDirLister* dirLister = m_dolphinModel->dirLister(); + foreach (ColumnWidget* column, m_columns) { + dirLister->updateDirectory(column->url()); } + + const int end = m_columns.count() - 2; // next to last column + for (int i = 0; i <= end; ++i) { + ColumnWidget* nextColumn = m_columns[i + 1]; + const QModelIndex rootIndex = nextColumn->rootIndex(); + if (rootIndex.isValid()) { + nextColumn->show(); + } else { + const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_columns[i]->childUrl()); + const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); + if (proxyIndex.isValid()) { + nextColumn->setRootIndex(proxyIndex); + nextColumn->show(); + if (nextColumn->isActive() && m_restoreActiveColumnFocus) { + nextColumn->setFocus(); + m_restoreActiveColumnFocus = false; + } + } + } + } + assureVisibleActiveColumn(); +} + +void DolphinColumnView::slotDirListerStarted(const KUrl& url) +{ + Q_UNUSED(url); + m_dirListerCompleted = false; +} + +void DolphinColumnView::slotDirListerCompleted() +{ + m_dirListerCompleted = true; + QMetaObject::invokeMethod(this, "expandToActiveUrl", Qt::QueuedConnection); } bool DolphinColumnView::isZoomInPossible() const @@ -561,19 +856,79 @@ bool DolphinColumnView::isZoomOutPossible() const return settings->iconSize() > K3Icon::SizeSmall; } -void DolphinColumnView::requestActivation(QWidget* column) +void DolphinColumnView::setActiveColumnIndex(int index) { - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - const bool isActive = (widget == column); - widget->setActive(isActive); - if (isActive) { - m_controller->setUrl(widget->url()); + if (m_index == index) { + return; + } + + const bool hasActiveColumn = (m_index >= 0); + if (hasActiveColumn) { + m_columns[m_index]->setActive(false); + } + + m_index = index; + m_columns[m_index]->setActive(true); + + m_controller->setUrl(m_columns[m_index]->url()); +} + +void DolphinColumnView::layoutColumns() +{ + int x = m_contentX; + ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); + const int columnWidth = settings->columnWidth(); + foreach (ColumnWidget* column, m_columns) { + column->setGeometry(QRect(x, 0, columnWidth, viewport()->height())); + x += columnWidth; + } +} + +void DolphinColumnView::updateScrollBar() +{ + int contentWidth = 0; + foreach (ColumnWidget* column, m_columns) { + contentWidth += column->width(); + } + + horizontalScrollBar()->setPageStep(contentWidth); + horizontalScrollBar()->setRange(0, contentWidth - viewport()->width()); +} + +void DolphinColumnView::assureVisibleActiveColumn() +{ + const int viewportWidth = viewport()->width(); + const int x = activeColumn()->x(); + const int width = activeColumn()->width(); + if (x + width > viewportWidth) { + int newContentX = m_contentX - x - width + viewportWidth; + if (newContentX > 0) { + newContentX = 0; + } + m_animation->setFrameRange(-m_contentX, -newContentX); + m_animation->start(); + } else if (x < 0) { + const int newContentX = m_contentX - x; + m_animation->setFrameRange(-m_contentX, -newContentX); + m_animation->start(); + } +} + +void DolphinColumnView::requestActivation(ColumnWidget* column) +{ + if (column->isActive()) { + assureVisibleActiveColumn(); + } else { + int index = 0; + foreach (ColumnWidget* currColumn, m_columns) { + if (currColumn == column) { + setActiveColumnIndex(index); + assureVisibleActiveColumn(); + return; } - } + ++index; + } } - updateSelections(); } #include "dolphincolumnview.moc"