+ switch (event->type()) {
+ case QEvent::FocusIn:
+ if (watched == m_viewAccessor.itemView()) {
+ m_controller->requestActivation();
+ }
+ break;
+
+ case QEvent::DragEnter:
+ if (watched == m_viewAccessor.itemView()->viewport()) {
+ setActive(true);
+ }
+ break;
+
+ case QEvent::KeyPress:
+ if (watched == m_viewAccessor.itemView()) {
+ // clear the selection when Escape has been pressed
+ QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
+ if (keyEvent->key() == Qt::Key_Escape) {
+ clearSelection();
+ }
+ }
+ break;
+
+ case QEvent::Wheel:
+ if (watched == m_viewAccessor.itemView()->viewport()) {
+ // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
+ // (the user is probably trying to scroll during a selection in that case)
+ QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
+ if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
+ const int delta = wheelEvent->delta();
+ const int level = zoomLevel();
+ if (delta > 0) {
+ setZoomLevel(level + 1);
+ } else if (delta < 0) {
+ setZoomLevel(level - 1);
+ }
+ return true;
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return QWidget::eventFilter(watched, event);
+}
+
+void DolphinView::activate()
+{
+ setActive(true);
+}
+
+void DolphinView::triggerItem(const KFileItem& item)
+{
+ const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
+ if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
+ // items are selected by the user, hence don't trigger the
+ // item specified by 'index'
+ return;
+ }
+
+ // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
+ if (item.isNull() || m_isContextMenuOpen) {
+ return;
+ }
+
+ emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
+}
+
+void DolphinView::emitDelayedSelectionChangedSignal()
+{
+ // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures
+ // that fast selection changes don't result in expensive operations to
+ // collect all file items for the signal (see DolphinView::selectedItems()).
+ m_selectionChangedTimer->start();
+}
+
+void DolphinView::emitSelectionChangedSignal()
+{
+ emit selectionChanged(DolphinView::selectedItems());
+}
+
+void DolphinView::openContextMenu(const QPoint& pos,
+ const QList<QAction*>& customActions)
+{
+ KFileItem item;
+ const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
+ if (index.isValid() && (index.column() == DolphinModel::Name)) {
+ const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
+ item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
+ }
+
+ m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
+ emit requestContextMenu(item, url(), customActions);
+ m_isContextMenuOpen = false;
+}
+
+void DolphinView::dropUrls(const KFileItem& destItem,
+ const KUrl& destPath,
+ QDropEvent* event)
+{
+ addNewFileNames(event->mimeData());
+ DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
+}
+
+void DolphinView::updateSorting(DolphinView::Sorting sorting)
+{
+ ViewProperties props(rootUrl());
+ props.setSorting(sorting);
+
+ m_viewAccessor.proxyModel()->setSorting(sorting);
+
+ emit sortingChanged(sorting);
+}
+
+void DolphinView::updateSortOrder(Qt::SortOrder order)
+{
+ ViewProperties props(rootUrl());
+ props.setSortOrder(order);
+
+ m_viewAccessor.proxyModel()->setSortOrder(order);
+
+ emit sortOrderChanged(order);
+}
+
+void DolphinView::updateSortFoldersFirst(bool foldersFirst)
+{
+ ViewProperties props(rootUrl());
+ props.setSortFoldersFirst(foldersFirst);
+
+ m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
+
+ emit sortFoldersFirstChanged(foldersFirst);
+}
+
+void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
+{
+ ViewProperties props(rootUrl());
+ props.setAdditionalInfo(info);
+ props.save();
+
+ m_viewAccessor.itemDelegate()->setShowInformation(info);
+
+ emit additionalInfoChanged();
+}
+
+void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
+{
+ const bool enable = (m_mode == DolphinView::DetailsView) ||
+ (m_mode == DolphinView::IconsView);
+
+ QAction* showSizeInfo = collection->action("show_size_info");
+ QAction* showDateInfo = collection->action("show_date_info");
+ QAction* showPermissionsInfo = collection->action("show_permissions_info");
+ QAction* showOwnerInfo = collection->action("show_owner_info");
+ QAction* showGroupInfo = collection->action("show_group_info");
+ QAction* showMimeInfo = collection->action("show_mime_info");
+
+ showSizeInfo->setChecked(false);
+ showDateInfo->setChecked(false);
+ showPermissionsInfo->setChecked(false);
+ showOwnerInfo->setChecked(false);
+ showGroupInfo->setChecked(false);
+ showMimeInfo->setChecked(false);
+
+ showSizeInfo->setEnabled(enable);
+ showDateInfo->setEnabled(enable);
+ showPermissionsInfo->setEnabled(enable);
+ showOwnerInfo->setEnabled(enable);
+ showGroupInfo->setEnabled(enable);
+ showMimeInfo->setEnabled(enable);
+
+ foreach (KFileItemDelegate::Information info, m_viewAccessor.itemDelegate()->showInformation()) {
+ switch (info) {
+ case KFileItemDelegate::Size:
+ showSizeInfo->setChecked(true);
+ break;
+ case KFileItemDelegate::ModificationTime:
+ showDateInfo->setChecked(true);
+ break;
+ case KFileItemDelegate::Permissions:
+ showPermissionsInfo->setChecked(true);
+ break;
+ case KFileItemDelegate::Owner:
+ showOwnerInfo->setChecked(true);
+ break;
+ case KFileItemDelegate::OwnerAndGroup:
+ showGroupInfo->setChecked(true);
+ break;
+ case KFileItemDelegate::FriendlyMimeType:
+ showMimeInfo->setChecked(true);
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+QPair<bool, QString> DolphinView::pasteInfo() const
+{
+ return KonqOperations::pasteInfo(url());
+}
+
+void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
+{
+ m_tabsForFiles = tabsForFiles;
+}
+
+bool DolphinView::isTabsForFilesEnabled() const
+{
+ return m_tabsForFiles;
+}
+
+void DolphinView::activateItem(const KUrl& url)
+{
+ // TODO: If DolphinViewContainer uses DolphinView::restoreState(...) to restore the
+ // view state in KDE 4.5, this function can be removed.
+ m_activeItemUrl = url;
+}
+
+bool DolphinView::itemsExpandable() const
+{
+ return m_viewAccessor.itemsExpandable();
+}
+
+void DolphinView::restoreState(QDataStream &stream)
+{
+ // current item
+ stream >> m_activeItemUrl;
+
+ // view position
+ stream >> m_restoredContentsPosition;
+
+ // expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
+ QSet<KUrl> urlsToExpand;
+ stream >> urlsToExpand;
+ const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
+
+ if (expander) {
+ m_expanderActive = true;
+ connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
+ }
+ else {
+ m_expanderActive = false;
+ }
+}
+
+void DolphinView::saveState(QDataStream &stream)
+{
+ // current item
+ KFileItem currentItem;
+ const QAbstractItemView* view = m_viewAccessor.itemView();
+
+ if(view) {
+ const QModelIndex proxyIndex = view->currentIndex();
+ const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
+ currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
+ }
+
+ KUrl currentUrl;
+ if (!currentItem.isNull())
+ currentUrl = currentItem.url();
+
+ stream << currentUrl;
+
+ // view position
+ stream << contentsPosition();
+
+ // expanded folders (only relevant for the details view - the set will be empty in other view modes)
+ stream << m_viewAccessor.expandedUrls();
+}
+
+void DolphinView::observeCreatedItem(const KUrl& url)
+{
+ m_createdItemUrl = url;
+ connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+ this, SLOT(selectAndScrollToCreatedItem()));
+}
+
+void DolphinView::selectAndScrollToCreatedItem()
+{
+ const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
+ if (dirIndex.isValid()) {
+ const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
+ m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
+ }
+
+ disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+ this, SLOT(selectAndScrollToCreatedItem()));
+ m_createdItemUrl = KUrl();
+}
+
+void DolphinView::emitContentsMoved()
+{
+ // TODO: If DolphinViewContainer uses DolphinView::saveState(...) to save the
+ // view state in KDE 4.5, the contentsMoved signal might not be needed anymore,
+ // depending on how the implementation is done.
+ // In that case, the code in contentsPosition() can be moved to saveState().
+
+ // only emit the contents moved signal if no directory loading is ongoing
+ // (this would reset the contents position always to (0, 0))
+ if (!m_loadingDirectory) {
+ const QPoint pos(contentsPosition());
+ emit contentsMoved(pos.x(), pos.y());
+ }
+}
+
+void DolphinView::showHoverInformation(const KFileItem& item)
+{
+ emit requestItemInfo(item);
+}
+
+void DolphinView::clearHoverInformation()
+{
+ emit requestItemInfo(KFileItem());
+}
+
+void DolphinView::slotDeleteFileFinished(KJob* job)
+{
+ if (job->error() == 0) {
+ emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
+ } else if (job->error() != KIO::ERR_USER_CANCELED) {
+ emit errorMessage(job->errorString());
+ }
+}
+
+void DolphinView::slotRequestUrlChange(const KUrl& url)
+{
+ emit requestUrlChange(url);
+ m_controller->setUrl(url);
+}
+
+void DolphinView::slotDirListerCompleted()
+{
+ if (!m_expanderActive) {
+ slotLoadingCompleted();
+ }
+
+ if (!m_newFileNames.isEmpty()) {
+ // select all newly added items created by a paste operation or
+ // a drag & drop operation
+ const int rowCount = m_viewAccessor.proxyModel()->rowCount();
+ QItemSelection selection;
+ for (int row = 0; row < rowCount; ++row) {
+ const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
+ const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
+ const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
+ if (m_newFileNames.contains(url.fileName())) {
+ selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
+ }
+ }
+ m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
+
+ m_newFileNames.clear();
+ }
+}
+
+void DolphinView::slotLoadingCompleted()
+{
+ m_expanderActive = false;
+ m_loadingDirectory = false;
+
+ if (!m_activeItemUrl.isEmpty()) {
+ // assure that the current item remains visible
+ const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
+ if (dirIndex.isValid()) {
+ const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
+ QAbstractItemView* view = m_viewAccessor.itemView();
+ const bool clearSelection = !hasSelection();
+ view->setCurrentIndex(proxyIndex);
+ if (clearSelection) {
+ view->clearSelection();
+ }
+ m_activeItemUrl.clear();
+ }
+ }
+
+ if (!m_selectedItems.isEmpty()) {
+ const KUrl& baseUrl = url();
+ KUrl url;
+ QItemSelection newSelection;
+ foreach(const KFileItem& item, m_selectedItems) {
+ url = item.url().upUrl();
+ if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
+ QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
+ newSelection.select(index, index);
+ }
+ }
+ m_viewAccessor.itemView()->selectionModel()->select(newSelection,
+ QItemSelectionModel::ClearAndSelect
+ | QItemSelectionModel::Current);
+ m_selectedItems.clear();
+ }
+
+ // Restore the contents position. This has to be done using a Qt::QueuedConnection
+ // because the view might not be in its final state yet.
+ QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
+}
+
+void DolphinView::slotRefreshItems()
+{
+ if (m_assureVisibleCurrentIndex) {
+ m_assureVisibleCurrentIndex = false;
+ m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
+ }
+}
+
+void DolphinView::loadDirectory(const KUrl& url, bool reload)
+{
+ if (!url.isValid()) {
+ const QString location(url.pathOrUrl());
+ if (location.isEmpty()) {
+ emit errorMessage(i18nc("@info:status", "The location is empty."));
+ } else {
+ emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
+ }
+ return;
+ }
+
+ m_loadingDirectory = true;
+ m_expanderActive = false;
+
+ m_viewAccessor.dirLister()->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
+}
+
+void DolphinView::applyViewProperties()
+{
+ if (m_ignoreViewProperties) {
+ return;
+ }
+
+ const ViewProperties props(rootUrl());
+
+ const Mode mode = props.viewMode();
+ if (m_mode != mode) {
+ const int oldZoomLevel = m_controller->zoomLevel();
+
+ m_mode = mode;
+ createView();
+ emit modeChanged();
+
+ updateZoomLevel(oldZoomLevel);
+ }
+ if (m_viewAccessor.itemView() == 0) {
+ createView();
+ }
+ Q_ASSERT(m_viewAccessor.itemView() != 0);
+ Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
+
+ const bool showHiddenFiles = props.showHiddenFiles();
+ if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
+ m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
+ emit showHiddenFilesChanged();
+ }
+
+ m_storedCategorizedSorting = props.categorizedSorting();
+ const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
+ if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
+ m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
+ emit categorizedSortingChanged();
+ }
+
+ const DolphinView::Sorting sorting = props.sorting();
+ if (sorting != m_viewAccessor.proxyModel()->sorting()) {
+ m_viewAccessor.proxyModel()->setSorting(sorting);
+ emit sortingChanged(sorting);
+ }
+
+ const Qt::SortOrder sortOrder = props.sortOrder();
+ if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
+ m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
+ emit sortOrderChanged(sortOrder);
+ }
+
+ const bool sortFoldersFirst = props.sortFoldersFirst();
+ if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
+ m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
+ emit sortFoldersFirstChanged(sortFoldersFirst);
+ }
+
+ KFileItemDelegate::InformationList info = props.additionalInfo();
+ if (info != m_viewAccessor.itemDelegate()->showInformation()) {
+ m_viewAccessor.itemDelegate()->setShowInformation(info);
+ emit additionalInfoChanged();
+ }
+
+ const bool showPreview = props.showPreview();
+ if (showPreview != m_showPreview) {
+ m_showPreview = showPreview;
+ const int oldZoomLevel = m_controller->zoomLevel();
+ emit showPreviewChanged();
+
+ // Enabling or disabling the preview might change the icon size of the view.
+ // As the view does not emit a signal when the icon size has been changed,
+ // the used zoom level of the controller must be adjusted manually:
+ updateZoomLevel(oldZoomLevel);
+ }
+
+ if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
+ // During the lifetime of a DolphinView instance the global view properties
+ // should not be changed. This allows e. g. to split a view and use different
+ // view properties for each view.
+ m_ignoreViewProperties = true;
+ }
+}
+
+void DolphinView::createView()
+{
+ deleteView();
+
+ Q_ASSERT(m_viewAccessor.itemView() == 0);
+ m_viewAccessor.createView(this, m_controller, m_mode);
+
+ QAbstractItemView* view = m_viewAccessor.itemView();
+ Q_ASSERT(view != 0);
+ view->installEventFilter(this);
+ view->viewport()->installEventFilter(this);
+
+ m_controller->setItemView(view);
+ connect(m_controller, SIGNAL(selectionChanged()),
+ this, SLOT(emitDelayedSelectionChangedSignal()));
+
+ // When changing the view mode, the selection is lost due to reinstantiating
+ // a new item view with a custom selection model. Pass the ownership of the
+ // selection model to DolphinView, so that it can be shared by all item views.
+ if (m_selectionModel != 0) {
+ view->setSelectionModel(m_selectionModel);
+ } else {
+ m_selectionModel = view->selectionModel();
+ }
+ m_selectionModel->setParent(this);
+
+ connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
+ this, SLOT(emitContentsMoved()));
+ connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
+ this, SLOT(emitContentsMoved()));
+
+ setFocusProxy(m_viewAccessor.layoutTarget());
+ m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
+}
+
+void DolphinView::deleteView()
+{
+ QAbstractItemView* view = m_viewAccessor.itemView();