X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/81fcd720a2cc095262e52b8a40dd1472d774a415..b1c9b5126d:/src/dolphincolumnview.cpp diff --git a/src/dolphincolumnview.cpp b/src/dolphincolumnview.cpp index e4669442c..3b3e86a75 100644 --- a/src/dolphincolumnview.cpp +++ b/src/dolphincolumnview.cpp @@ -19,709 +19,593 @@ #include "dolphincolumnview.h" +#include "dolphincolumnwidget.h" #include "dolphincontroller.h" -#include "dolphinsettings.h" +#include "settings/dolphinsettings.h" +#include "zoomlevelinfo.h" #include "dolphin_columnmodesettings.h" -#include -#include -#include -#include +#include -#include -#include #include +#include +#include -/* - * General implementation notes - * ---------------------------- - * - * In Qt4.3 the QColumnView widget has a default behavior regarding the - * active column and the selection handling, which leads to some usability - * problems within Dolphin: - * - * - No matter which mouse button has been clicked: If the mouse is above - * a folder, the folder content will be loaded in the next column. The problem - * is that this column also will marked as 'active column' within QColumnView, - * hence it is not possible to select more than one folder within a column. - * - * - The currently opened folder is not always marked in the left column when - * doing drag & drop and selections inside other columns. - * - * - The currently active column is visually not recognizable. - * - * - It is not possible for derived classes to remove inactive columns. - * - * DolphinView tries to bypass those points, but this required some workarounds: - * - * - QColumnView internally maps the selection model from the ColumnView to the - * active column. As the active column from the Dolphin perspective is different - * as the active column from QColumnView, the selection model is adjusted on - * each interaction by the methods QColumnWidget::obtainSelectionModel(), - * QColumnWidget::releaseSelectionModel() and QColumnView::requestSelectionModel(). - * QColumnView offers no hook to adjust this behavior, so those methods have to - * be invoked throughout the code... - * - * - Some copy/paste code from QColumnView is part of DolphinColumnView::createColumn(), but Qt 4.4 - * will offer a solution for this. - * - * - The mousePressEvent() has been customized to prevent that folders are loaded on each - * mouse click. - * - * We'll try to give some input for Trolltech if the Dolphin solution is stable enough, so hopefully - * some workarounds can be removed when switching to Qt 4.4 or later. - */ - -/** - * Represents one column inside the DolphinColumnView and has been - * extended to respect view options and hovering information. - */ -class ColumnWidget : public QListView -{ -public: - ColumnWidget(QWidget* parent, - DolphinColumnView* columnView, - const KUrl& url); - virtual ~ColumnWidget(); - - /** Sets the size of the icons. */ - void setDecorationSize(const QSize& size); - - /** - * An active column is defined as column, which shows the same URL - * as indicated by the URL navigator. The active column is usually - * 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; - - /** - * Obtains the selection model from the column view. This assures that - * selections of the column view will always applied to the active column. - */ - void obtainSelectionModel(); - - /** - * Releases the selection model from the column view and replaces it by - * a custom selection model. - */ - void releaseSelectionModel(); - -protected: - virtual QStyleOptionViewItem viewOptions() const; - virtual void dragEnterEvent(QDragEnterEvent* event); - 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 contextMenuEvent(QContextMenuEvent* event); - -protected slots: - virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected); - -private: - /** Used by ColumnWidget::setActive(). */ - void activate(); - - /** Used by ColumnWidget::setActive(). */ - void deactivate(); - -private: - bool m_active; - bool m_swallowMouseMoveEvents; - DolphinColumnView* m_view; - KUrl m_url; - 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 - QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4 -}; - -ColumnWidget::ColumnWidget(QWidget* parent, - DolphinColumnView* columnView, - 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); - - // apply the column mode settings to the widget - const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - Q_ASSERT(settings != 0); - - m_viewOptions = QListView::viewOptions(); - - QFont font(settings->fontFamily(), settings->fontSize()); - font.setItalic(settings->italicFont()); - font.setBold(settings->boldFont()); - m_viewOptions.font = font; - - const int iconSize = settings->iconSize(); - m_viewOptions.decorationSize = QSize(iconSize, iconSize); - - activate(); -} - -ColumnWidget::~ColumnWidget() -{ -} - -void ColumnWidget::setDecorationSize(const QSize& size) -{ - m_viewOptions.decorationSize = size; - doItemsLayout(); +DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) : + QAbstractItemView(parent), + m_controller(controller), + m_active(false), + m_index(-1), + m_contentX(0), + m_columns(), + m_emptyViewport(0), + m_animation(0), + m_nameFilter() +{ + Q_ASSERT(controller != 0); + + setAcceptDrops(true); + setDragDropMode(QAbstractItemView::DragDrop); + setDropIndicatorShown(false); + setSelectionMode(ExtendedSelection); + setFocusPolicy(Qt::NoFocus); + setFrameShape(QFrame::NoFrame); + setLayoutDirection(Qt::LeftToRight); + + connect(this, SIGNAL(viewportEntered()), + controller, SLOT(emitViewportEntered())); + connect(controller, SIGNAL(zoomLevelChanged(int)), + this, SLOT(setZoomLevel(int))); + connect(controller, SIGNAL(activationChanged(bool)), + this, SLOT(updateColumnsBackground(bool))); + + const DolphinView* view = controller->dolphinView(); + connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)), + this, SLOT(slotSortingChanged(DolphinView::Sorting))); + connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)), + this, SLOT(slotSortOrderChanged(Qt::SortOrder))); + connect(view, SIGNAL(showHiddenFilesChanged()), + this, SLOT(slotShowHiddenFilesChanged())); + connect(view, SIGNAL(showPreviewChanged()), + this, SLOT(slotShowPreviewChanged())); + + connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), + this, SLOT(moveContentHorizontally(int))); + + m_animation = new QTimeLine(500, this); + connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int))); + + DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, m_controller->url()); + m_columns.append(column); + setActiveColumnIndex(0); + + m_emptyViewport = new QFrame(viewport()); + m_emptyViewport->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); + + updateDecorationSize(view->showPreview()); + updateColumnsBackground(true); } -void ColumnWidget::setActive(bool active) +DolphinColumnView::~DolphinColumnView() { - if (active) { - obtainSelectionModel(); - } else { - releaseSelectionModel(); - } +} - if (m_active == active) { - return; +QModelIndex DolphinColumnView::indexAt(const QPoint& point) const +{ + foreach (DolphinColumnWidget* column, m_columns) { + const QModelIndex index = column->indexAt(columnPosition(column, point)); + if (index.isValid()) { + return index; + } } - m_active = active; + return QModelIndex(); +} - if (active) { - activate(); - } else { - deactivate(); +KFileItem DolphinColumnView::itemAt(const QPoint& point) const +{ + foreach (DolphinColumnWidget* column, m_columns) { + KFileItem item = column->itemAt(columnPosition(column, point)); + if (!item.isNull()) { + return item; + } } + + return KFileItem(); } -inline bool ColumnWidget::isActive() const +void DolphinColumnView::scrollTo(const QModelIndex& index, ScrollHint hint) { - return m_active; + activeColumn()->scrollTo(index, hint); } -const KUrl& ColumnWidget::url() const +QRect DolphinColumnView::visualRect(const QModelIndex& index) const { - return m_url; + return activeColumn()->visualRect(index); } -void ColumnWidget::obtainSelectionModel() +void DolphinColumnView::invertSelection() { - if (selectionModel() != m_view->selectionModel()) { - selectionModel()->deleteLater(); - setSelectionModel(m_view->selectionModel()); - clearSelection(); - } + QItemSelectionModel* selectionModel = activeColumn()->selectionModel(); + const QAbstractItemModel* itemModel = selectionModel->model(); + + const QModelIndex topLeft = itemModel->index(0, 0); + const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1, + itemModel->columnCount() - 1); + + const QItemSelection selection(topLeft, bottomRight); + selectionModel->select(selection, QItemSelectionModel::Toggle); } -void ColumnWidget::releaseSelectionModel() +void DolphinColumnView::reload() { - if (selectionModel() == m_view->selectionModel()) { - QItemSelectionModel* replacementModel = new QItemSelectionModel(model()); - setSelectionModel(replacementModel); + foreach (DolphinColumnWidget* column, m_columns) { + column->reload(); } } -QStyleOptionViewItem ColumnWidget::viewOptions() const +void DolphinColumnView::setRootUrl(const KUrl& url) { - return m_viewOptions; + removeAllColumns(); + m_columns[0]->setUrl(url); } -void ColumnWidget::dragEnterEvent(QDragEnterEvent* event) +void DolphinColumnView::setNameFilter(const QString& nameFilter) { - if (event->mimeData()->hasUrls()) { - event->acceptProposedAction(); + if (nameFilter != m_nameFilter) { + m_nameFilter = nameFilter; + foreach (DolphinColumnWidget* column, m_columns) { + column->setNameFilter(nameFilter); + } } - - m_dragging = true; } -void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event) +QString DolphinColumnView::nameFilter() const { - QListView::dragLeaveEvent(event); - - // TODO: remove this code when the issue #160611 is solved in Qt 4.4 - m_dragging = false; - setDirtyRegion(m_dropRect); + return m_nameFilter; } -void ColumnWidget::dragMoveEvent(QDragMoveEvent* event) +KUrl DolphinColumnView::rootUrl() const { - QListView::dragMoveEvent(event); - - // 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); - setDirtyRegion(m_dropRect); + return m_columns[0]->url(); } -void ColumnWidget::dropEvent(QDropEvent* event) +void DolphinColumnView::showColumn(const KUrl& url) { - const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData()); - if (!urls.isEmpty()) { - event->acceptProposedAction(); - m_view->m_controller->indicateDroppedUrls(urls, - indexAt(event->pos()), - event->source()); + if (!rootUrl().isParentOf(url)) { + setRootUrl(url); + return; } - QListView::dropEvent(event); - m_dragging = false; -} - -void ColumnWidget::mousePressEvent(QMouseEvent* event) -{ - // On each mouse press event QColumnView triggers the loading of the - // current folder in the next column. This is not wanted for Dolphin when - // opening a context menu or when the CTRL modifier is pressed. Beside usability - // aspects the loading of the folder also implies losing the current selection, - // which makes it impossible to select folders from the current column. To bypass - // this behavior QListView::mousePressEvent() is not invoked in those cases, which - // is not a nice solution. Maybe another solution can be found in future versions - // of QColumnView. - - m_view->requestSelectionModel(this); - - bool swallowMousePressEvent = false; - const QModelIndex index = indexAt(event->pos()); - if (index.isValid()) { - // a click on an item has been done - 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); - if (!item.isNull()) { - QItemSelectionModel* selModel = selectionModel(); - - bool activate = true; - const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers(); - if (modifier & Qt::ControlModifier) { - m_view->requestActivation(this); - if (!selModel->hasSelection()) { - // Assure to set the current index, so that a selection by the SHIFT key - // will work. TODO: If the index specifies a folder, the loading of the folder will - // be triggered by QColumnView although this is not wanted by Dolphin. - selModel->setCurrentIndex(index, QItemSelectionModel::Select); + + int columnIndex = 0; + foreach (DolphinColumnWidget* column, m_columns) { + if (column->url() == url) { + // the column represents already the requested URL, hence activate it + requestActivation(column); + layoutColumns(); + 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) { + deleteColumn(*it); } - selModel->select(index, QItemSelectionModel::Toggle); - swallowMousePressEvent = true; - } else if (item.isDir()) { - m_childUrl = item.url(); - viewport()->update(); - - // Only request the activation if not the left button is pressed. - // The left button on a directory opens a new column, hence requesting - // an activation is useless as the new column will request the activation - // afterwards. - if (event->button() == Qt::LeftButton) { - activate = false; + 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; + } - if (activate) { - m_view->requestActivation(this); + // 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); + m_columns[columnIndex]->setChildUrl(childUrl); + columnIndex++; + + DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, childUrl); + const QString filter = nameFilter(); + if (!filter.isEmpty()) { + column->setNameFilter(filter); } + column->setActive(false); - // TODO: is the assumption OK that Qt::RightButton always represents the context menu button? - if (event->button() == Qt::RightButton) { - swallowMousePressEvent = true; - if (!selModel->isSelected(index)) { - clearSelection(); - } - selModel->select(index, QItemSelectionModel::Select); - } - } - } else { - // a click on the viewport has been done - m_view->requestActivation(this); + m_columns.append(column); - // 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; - clearSelection(); + // 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(); + } } - if (!swallowMousePressEvent) { - QListView::mousePressEvent(event); - } + // set the last column as active column without modifying the controller + // and hence the history + activeColumn()->setActive(false); + m_index = columnIndex; + activeColumn()->setActive(true); + assureVisibleActiveColumn(); } -void ColumnWidget::mouseMoveEvent(QMouseEvent* event) +void DolphinColumnView::editItem(const KFileItem& item) { - // see description in ColumnView::mousePressEvent() - if (!m_swallowMouseMoveEvents) { - QListView::mouseMoveEvent(event); - } + activeColumn()->editItem(item); } -void ColumnWidget::mouseReleaseEvent(QMouseEvent* event) +KFileItemList DolphinColumnView::selectedItems() const { - QListView::mouseReleaseEvent(event); - m_swallowMouseMoveEvents = false; + return activeColumn()->selectedItems(); } - -void ColumnWidget::paintEvent(QPaintEvent* event) +QMimeData* DolphinColumnView::selectionMimeData() const { - if (!m_childUrl.isEmpty()) { - // indicate the shown URL of the next column by highlighting the shown folder item - const QAbstractProxyModel* proxyModel = static_cast(m_view->model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - const QModelIndex dirIndex = dirModel->indexForUrl(m_childUrl); - const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex); - if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) { - const QRect itemRect = visualRect(proxyIndex); - QPainter painter(viewport()); - painter.save(); + return activeColumn()->selectionMimeData(); +} - QColor color = KColorScheme(KColorScheme::View).foreground(); - color.setAlpha(32); - painter.setPen(Qt::NoPen); - painter.setBrush(color); - painter.drawRect(itemRect); +void DolphinColumnView::selectAll() +{ + activeColumn()->selectAll(); +} - painter.restore(); - } - } +bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const +{ + Q_UNUSED(index); + return false;//activeColumn()->isIndexHidden(index); +} - QListView::paintEvent(event); +QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) +{ + // Parts of this code have been taken from QColumnView::moveCursor(). + // Copyright (C) 1992-2007 Trolltech ASA. - // 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); + Q_UNUSED(modifiers); + if (model() == 0) { + return QModelIndex(); } -} -void ColumnWidget::contextMenuEvent(QContextMenuEvent* event) -{ - if (!m_active) { - m_view->requestActivation(this); + const QModelIndex current = currentIndex(); + if (isRightToLeft()) { + if (cursorAction == MoveLeft) { + cursorAction = MoveRight; + } else if (cursorAction == MoveRight) { + cursorAction = MoveLeft; + } } - QListView::contextMenuEvent(event); + switch (cursorAction) { + case MoveLeft: + if (m_index > 0) { + setActiveColumnIndex(m_index - 1); + m_controller->triggerUrlChangeRequest(activeColumn()->url()); + } + break; + + case MoveRight: + if (m_index < m_columns.count() - 1) { + setActiveColumnIndex(m_index + 1); + m_controller->triggerUrlChangeRequest(m_columns[m_index]->url()); + } + break; - const QModelIndex index = indexAt(event->pos()); - if (index.isValid() || m_active) { - // Only open a context menu above an item or if the mouse is above - // the active column. - const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos()); - m_view->m_controller->triggerContextMenuRequest(pos); + default: + break; } + + return QModelIndex(); } -void ColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) +void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags) { - // inactive views should not have any selection - if (!m_active) { - clearSelection(); - } - QListView::selectionChanged(selected, deselected); + Q_UNUSED(rect); + Q_UNUSED(flags); } -void ColumnWidget::activate() +QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const { - const QColor bgColor = KColorScheme(KColorScheme::View).background(); - QPalette palette = viewport()->palette(); - palette.setColor(viewport()->backgroundRole(), bgColor); - viewport()->setPalette(palette); - - update(); + Q_UNUSED(selection); + return QRegion(); } -void ColumnWidget::deactivate() +int DolphinColumnView::horizontalOffset() const { - QColor bgColor = KColorScheme(KColorScheme::View).background(); - const QColor fgColor = KColorScheme(KColorScheme::View).foreground(); - bgColor = KColorUtils::mix(bgColor, fgColor, 0.04); - - QPalette palette = viewport()->palette(); - palette.setColor(viewport()->backgroundRole(), bgColor); - viewport()->setPalette(palette); - - update(); + return -m_contentX; } -// --- - -DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) : - QColumnView(parent), - m_controller(controller) +int DolphinColumnView::verticalOffset() const { - Q_ASSERT(controller != 0); - - setAcceptDrops(true); - setDragDropMode(QAbstractItemView::DragDrop); - setDropIndicatorShown(false); - 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()), - controller, SLOT(emitViewportEntered())); - connect(controller, SIGNAL(zoomIn()), - this, SLOT(zoomIn())); - connect(controller, SIGNAL(zoomOut()), - this, SLOT(zoomOut())); - connect(controller, SIGNAL(urlChanged(const KUrl&)), - this, SLOT(updateColumnsState(const KUrl&))); - - updateDecorationSize(); + return 0; } -DolphinColumnView::~DolphinColumnView() +void DolphinColumnView::mousePressEvent(QMouseEvent* event) { + m_controller->requestActivation(); + QAbstractItemView::mousePressEvent(event); } -void DolphinColumnView::invertSelection() +void DolphinColumnView::resizeEvent(QResizeEvent* event) { - selectActiveColumn(QItemSelectionModel::Toggle); + QAbstractItemView::resizeEvent(event); + layoutColumns(); + updateScrollBar(); + assureVisibleActiveColumn(); } -void DolphinColumnView::selectAll() +void DolphinColumnView::wheelEvent(QWheelEvent* event) { - selectActiveColumn(QItemSelectionModel::Select); + // let Ctrl+wheel events propagate to the DolphinView for icon zooming + if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) { + event->ignore(); + return; + } + QAbstractItemView::wheelEvent(event); } -QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index) +void DolphinColumnView::setZoomLevel(int level) { - // 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.isNull()) { - columnUrl = fileItem.url(); - } - } + const int size = ZoomLevelInfo::iconSizeForZoomLevel(level); + ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl); - - // 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. - - view->setFrameShape(QFrame::NoFrame); - view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); - view->setMinimumWidth(100); - view->setAttribute(Qt::WA_MacShowFocusRect, false); - - // 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()); - - view->setModel(model()); - - // set the delegate to be the columnview delegate - QAbstractItemDelegate* delegate = view->itemDelegate(); - view->setItemDelegate(itemDelegate()); - delete delegate; - - view->setRootIndex(index); - - if (model()->canFetchMore(index)) { - model()->fetchMore(index); + const bool showPreview = m_controller->dolphinView()->showPreview(); + if (showPreview) { + settings->setPreviewSize(size); + } else { + settings->setIconSize(size); } - return view; + updateDecorationSize(showPreview); } -void DolphinColumnView::mousePressEvent(QMouseEvent* event) +void DolphinColumnView::moveContentHorizontally(int x) { - m_controller->triggerActivation(); - QColumnView::mousePressEvent(event); + m_contentX = isRightToLeft() ? +x : -x; + layoutColumns(); } -void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event) +void DolphinColumnView::updateDecorationSize(bool showPreview) { - if (event->mimeData()->hasUrls()) { - event->acceptProposedAction(); + ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); + const int iconSize = showPreview ? settings->previewSize() : settings->iconSize(); + const QSize size(iconSize, iconSize); + setIconSize(size); + + foreach (QObject* object, viewport()->children()) { + if (object->inherits("QListView")) { + DolphinColumnWidget* widget = static_cast(object); + widget->setDecorationSize(size); + } } + + doItemsLayout(); } -void DolphinColumnView::dropEvent(QDropEvent* event) +void DolphinColumnView::updateColumnsBackground(bool active) { - const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData()); - if (!urls.isEmpty()) { - m_controller->indicateDroppedUrls(urls, - indexAt(event->pos()), - event->source()); - event->acceptProposedAction(); + if (active == m_active) { + return; + } + + m_active = active; + + // dim the background of the viewport + const QPalette::ColorRole role = viewport()->backgroundRole(); + QColor background = viewport()->palette().color(role); + background.setAlpha(0); // make background transparent + + QPalette palette = viewport()->palette(); + palette.setColor(role, background); + viewport()->setPalette(palette); + + foreach (DolphinColumnWidget* column, m_columns) { + column->updateBackground(); } - QColumnView::dropEvent(event); } -void DolphinColumnView::zoomIn() +void DolphinColumnView::slotSortingChanged(DolphinView::Sorting sorting) { - if (isZoomInPossible()) { - ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - // TODO: get rid of K3Icon sizes - switch (settings->iconSize()) { - case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break; - case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break; - default: Q_ASSERT(false); break; - } - updateDecorationSize(); + foreach (DolphinColumnWidget* column, m_columns) { + column->setSorting(sorting); } } -void DolphinColumnView::zoomOut() +void DolphinColumnView::slotSortOrderChanged(Qt::SortOrder order) { - if (isZoomOutPossible()) { - ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - // TODO: get rid of K3Icon sizes - switch (settings->iconSize()) { - case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break; - case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break; - default: Q_ASSERT(false); break; - } - updateDecorationSize(); + foreach (DolphinColumnWidget* column, m_columns) { + column->setSortOrder(order); } } -void DolphinColumnView::triggerItem(const QModelIndex& index) +void DolphinColumnView::slotShowHiddenFilesChanged() { - m_controller->triggerItem(index); - updateColumnsState(m_controller->url()); + const bool show = m_controller->dolphinView()->showHiddenFiles(); + foreach (DolphinColumnWidget* column, m_columns) { + column->setShowHiddenFiles(show); + } } -void DolphinColumnView::updateColumnsState(const KUrl& url) +void DolphinColumnView::slotShowPreviewChanged() { - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - widget->setActive(widget->url() == url); - } + const bool show = m_controller->dolphinView()->showPreview(); + updateDecorationSize(show); + foreach (DolphinColumnWidget* column, m_columns) { + column->setShowPreview(show); } } - -void DolphinColumnView::updateDecorationSize() +void DolphinColumnView::setActiveColumnIndex(int index) { - ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - const int iconSize = settings->iconSize(); + if (m_index == index) { + return; + } - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - widget->setDecorationSize(QSize(iconSize, iconSize)); - } + const bool hasActiveColumn = (m_index >= 0); + if (hasActiveColumn) { + m_columns[m_index]->setActive(false); } - m_controller->setZoomInPossible(isZoomInPossible()); - m_controller->setZoomOutPossible(isZoomOutPossible()); + m_index = index; + m_columns[m_index]->setActive(true); - doItemsLayout(); + assureVisibleActiveColumn(); } -bool DolphinColumnView::isZoomInPossible() const +void DolphinColumnView::layoutColumns() { + const int gap = 4; + ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - return settings->iconSize() < K3Icon::SizeLarge; + const int columnWidth = settings->columnWidth(); + + QRect emptyViewportRect; + if (isRightToLeft()) { + int x = viewport()->width() - columnWidth + m_contentX; + foreach (DolphinColumnWidget* column, m_columns) { + column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height())); + x -= columnWidth; + } + emptyViewportRect = QRect(0, 0, x + columnWidth - gap, viewport()->height()); + } else { + int x = m_contentX; + foreach (DolphinColumnWidget* column, m_columns) { + column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height())); + x += columnWidth; + } + emptyViewportRect = QRect(x, 0, viewport()->width() - x - gap, viewport()->height()); + } + + if (emptyViewportRect.isValid()) { + m_emptyViewport->show(); + m_emptyViewport->setGeometry(emptyViewportRect); + } else { + m_emptyViewport->hide(); + } } -bool DolphinColumnView::isZoomOutPossible() const +void DolphinColumnView::updateScrollBar() { ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); - return settings->iconSize() > K3Icon::SizeSmall; + const int contentWidth = m_columns.count() * settings->columnWidth(); + + horizontalScrollBar()->setPageStep(contentWidth); + horizontalScrollBar()->setRange(0, contentWidth - viewport()->width()); } -void DolphinColumnView::requestActivation(QWidget* column) +void DolphinColumnView::assureVisibleActiveColumn() { - 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()); - } + const int viewportWidth = viewport()->width(); + const int x = activeColumn()->x(); + + ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings(); + const int width = settings->columnWidth(); + + if (x + width > viewportWidth) { + const int newContentX = m_contentX - x - width + viewportWidth; + if (isRightToLeft()) { + m_animation->setFrameRange(m_contentX, newContentX); + } else { + m_animation->setFrameRange(-m_contentX, -newContentX); + } + if (m_animation->state() != QTimeLine::Running) { + m_animation->start(); + } + } else if (x < 0) { + const int newContentX = m_contentX - x; + if (isRightToLeft()) { + m_animation->setFrameRange(m_contentX, newContentX); + } else { + m_animation->setFrameRange(-m_contentX, -newContentX); + } + if (m_animation->state() != QTimeLine::Running) { + m_animation->start(); } } } -void DolphinColumnView::requestSelectionModel(QAbstractItemView* view) +void DolphinColumnView::requestActivation(DolphinColumnWidget* column) { - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - if (widget == view) { - widget->obtainSelectionModel(); - } else { - widget->releaseSelectionModel(); + m_controller->setItemView(column); + if (column->isActive()) { + assureVisibleActiveColumn(); + } else { + int index = 0; + foreach (DolphinColumnWidget* currColumn, m_columns) { + if (currColumn == column) { + setActiveColumnIndex(index); + return; } + ++index; } } } -void DolphinColumnView::selectActiveColumn(QItemSelectionModel::SelectionFlags flags) +void DolphinColumnView::removeAllColumns() { - // TODO: this approach of selecting the active column is very slow. It should be - // possible to speedup the implementation by using QItemSelection, but all adempts - // have failed yet... - - // assure that the selection model of the active column is set properly, otherwise - // no visual update of the selections is done - const KUrl& activeUrl = m_controller->url(); - foreach (QObject* object, viewport()->children()) { - if (object->inherits("QListView")) { - ColumnWidget* widget = static_cast(object); - if (widget->url() == activeUrl) { - widget->obtainSelectionModel(); - } else { - widget->releaseSelectionModel(); - } - } + QList::iterator start = m_columns.begin() + 1; + QList::iterator end = m_columns.end(); + for (QList::iterator it = start; it != end; ++it) { + deleteColumn(*it); } + m_columns.erase(start, end); + m_index = 0; + m_columns[0]->setActive(true); + assureVisibleActiveColumn(); +} - QItemSelectionModel* selModel = selectionModel(); - - const QAbstractProxyModel* proxyModel = static_cast(model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - KDirLister* dirLister = dirModel->dirLister(); +QPoint DolphinColumnView::columnPosition(DolphinColumnWidget* column, const QPoint& point) const +{ + const QPoint topLeft = column->frameGeometry().topLeft(); + return QPoint(point.x() - topLeft.x(), point.y() - topLeft.y()); +} - const KFileItemList list = dirLister->itemsForDir(activeUrl); - foreach (KFileItem* item, list) { - const QModelIndex index = dirModel->indexForUrl(item->url()); - selModel->select(proxyModel->mapFromSource(index), flags); +void DolphinColumnView::deleteColumn(DolphinColumnWidget* column) +{ + if (column != 0) { + if (m_controller->itemView() == column) { + m_controller->setItemView(0); + } + // deleteWhenNotDragSource(column) does not necessarily delete column, + // and we want its preview generator destroyed immediately. + column->m_previewGenerator->deleteLater(); + column->m_previewGenerator = 0; + column->hide(); + // Prevent automatic destruction of column when this DolphinColumnView + // is destroyed. + column->setParent(0); + column->disconnect(); + emit requestColumnDeletion(column); } }