]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
Prevent unnecessary reloading of KDirLister on startup
[dolphin.git] / src / views / dolphinview.cpp
index 29f62f735d845ff80fb8bed841e408d613694883..6108c9d4a105475fe9b50c0831a8175543d12edf 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at>             *
+ *   Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com>        *
  *   Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net>          *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include <QTimer>
 #include <QScrollBar>
 
-#include <kactioncollection.h>
-#include <kcolorscheme.h>
-#include <kdirlister.h>
-#include <kiconeffect.h>
-#include <kfileitem.h>
-#include <kfileitemlistproperties.h>
-#include <klocale.h>
-#include <kio/deletejob.h>
-#include <kio/netaccess.h>
-#include <kio/previewjob.h>
-#include <kjob.h>
-#include <kmenu.h>
-#include <kmessagebox.h>
+#include <KActionCollection>
+#include <KColorScheme>
+#include <KDirLister>
+#include <KIconEffect>
+#include <KFileItem>
+#include <KFileItemListProperties>
+#include <KLocale>
+#include <KIO/DeleteJob>
+#include <KIO/NetAccess>
+#include <KIO/PreviewJob>
+#include <KJob>
+#include <KMenu>
+#include <KMessageBox>
 #include <konq_fileitemcapabilities.h>
 #include <konq_operations.h>
 #include <konqmimedata.h>
-#include <ktoggleaction.h>
-#include <kurl.h>
+#include <KToggleAction>
+#include <KUrl>
 
 #include "additionalinfoaccessor.h"
+#include "dolphindirlister.h"
 #include "dolphinmodel.h"
 #include "dolphincolumnviewcontainer.h"
 #include "dolphinviewcontroller.h"
@@ -67,9 +68,7 @@
 #include "zoomlevelinfo.h"
 #include "dolphindetailsviewexpander.h"
 
-DolphinView::DolphinView(QWidget* parent,
-                         const KUrl& url,
-                         DolphinSortFilterProxyModel* proxyModel) :
+DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     QWidget(parent),
     m_active(true),
     m_showPreview(false),
@@ -83,7 +82,7 @@ DolphinView::DolphinView(QWidget* parent,
     m_topLayout(0),
     m_dolphinViewController(0),
     m_viewModeController(0),
-    m_viewAccessor(proxyModel),
+    m_viewAccessor(),
     m_selectionChangedTimer(0),
     m_activeItemUrl(),
     m_restoredContentsPosition(),
@@ -277,6 +276,11 @@ bool DolphinView::supportsCategorizedSorting() const
     return m_viewAccessor.supportsCategorizedSorting();
 }
 
+KFileItem DolphinView::rootItem() const
+{
+    return m_viewAccessor.dirLister()->rootItem();
+}
+
 KFileItemList DolphinView::items() const
 {
     return m_viewAccessor.dirLister()->items();
@@ -286,7 +290,7 @@ KFileItemList DolphinView::selectedItems() const
 {
     KFileItemList itemList;
     const QAbstractItemView* view = m_viewAccessor.itemView();
-    if (view == 0) {
+    if (!view) {
         return itemList;
     }
 
@@ -306,7 +310,7 @@ KFileItemList DolphinView::selectedItems() const
 int DolphinView::selectedItemsCount() const
 {
     const QAbstractItemView* view = m_viewAccessor.itemView();
-    if (view == 0) {
+    if (!view) {
         return 0;
     }
 
@@ -442,6 +446,11 @@ void DolphinView::setNameFilter(const QString& nameFilter)
     m_viewModeController->setNameFilter(nameFilter);
 }
 
+QString DolphinView::nameFilter() const
+{
+    return m_viewModeController->nameFilter();
+}
+
 void DolphinView::calculateItemCount(int& fileCount,
                                      int& folderCount,
                                      KIO::filesize_t& totalFileSize) const
@@ -557,15 +566,23 @@ void DolphinView::selectAll()
 
 void DolphinView::invertSelection()
 {
-    QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
-    const QAbstractItemModel* itemModel = selectionModel->model();
-
-    const QModelIndex topLeft = itemModel->index(0, 0);
-    const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
-                                                     itemModel->columnCount() - 1);
+    // Implementation note: Using selectionModel->select(selection, QItemSelectionModel::Toggle) does not
+    // work, as QItemSelectionModel::hasSelection() provides invalid values in this case. This might be a Qt-issue -
+    // when changing the implementation with an updated Qt-version don't forget to run the Dolphin-unit-tests that
+    // verify this usecase.
+    const KFileItemList selItems = selectedItems();
+    clearSelection();
+
+    QItemSelection invertedSelection;
+    foreach (const KFileItem& item, items()) {
+        if (!selItems.contains(item)) {
+            const QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
+            invertedSelection.select(index, index);
+        }
+    }
 
-    const QItemSelection selection(topLeft, bottomRight);
-    selectionModel->select(selection, QItemSelectionModel::Toggle);
+    QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
+    selectionModel->select(invertedSelection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current);
 }
 
 void DolphinView::clearSelection()
@@ -751,6 +768,14 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event)
     return QWidget::eventFilter(watched, event);
 }
 
+void DolphinView::showEvent(QShowEvent* event)
+{
+    QWidget::showEvent(event);
+    if (!event->spontaneous()) {
+        loadDirectory(url());
+    }
+}
+
 void DolphinView::activate()
 {
     setActive(true);
@@ -869,7 +894,7 @@ void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
     foreach (const KFileItemDelegate::Information& info, infoKeys) {
         const QString name = infoAccessor.actionCollectionName(info, AdditionalInfoAccessor::AdditionalInfoType);
         QAction* action = collection->action(name);
-        Q_ASSERT(action != 0);
+        Q_ASSERT(action);
         action->setEnabled(enable);
         action->setChecked(checkedInfo.contains(info));
     }
@@ -912,7 +937,7 @@ void DolphinView::restoreState(QDataStream& stream)
     QSet<KUrl> urlsToExpand;
     stream >> urlsToExpand;
     const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
-    if (expander != 0) {
+    if (expander) {
         m_expanderActive = true;
         connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
     }
@@ -927,7 +952,7 @@ void DolphinView::saveState(QDataStream& stream)
     KFileItem currentItem;
     const QAbstractItemView* view = m_viewAccessor.itemView();
 
-    if (view != 0) {
+    if (view) {
         const QModelIndex proxyIndex = view->currentIndex();
         const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
         currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
@@ -955,7 +980,7 @@ void DolphinView::saveState(QDataStream& stream)
 bool DolphinView::hasSelection() const
 {
     const QAbstractItemView* view = m_viewAccessor.itemView();
-    return (view != 0) && view->selectionModel()->hasSelection();
+    return view && view->selectionModel()->hasSelection();
 }
 
 void DolphinView::observeCreatedItem(const KUrl& url)
@@ -994,7 +1019,7 @@ void DolphinView::restoreContentsPosition()
         m_restoredContentsPosition = QPoint();
 
         QAbstractItemView* view = m_viewAccessor.itemView();
-        Q_ASSERT(view != 0);
+        Q_ASSERT(view);
         view->horizontalScrollBar()->setValue(x);
         view->verticalScrollBar()->setValue(y);
     }
@@ -1091,7 +1116,7 @@ void DolphinView::slotLoadingCompleted()
         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));
+                const QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
                 newSelection.select(index, index);
             }
         }
@@ -1146,12 +1171,12 @@ void DolphinView::applyViewProperties()
 
         updateZoomLevel(oldZoomLevel);
     }
-    if (m_viewAccessor.itemView() == 0) {
+    if (!m_viewAccessor.itemView()) {
         createView();
     }
 
-    Q_ASSERT(m_viewAccessor.itemView() != 0);
-    Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
+    Q_ASSERT(m_viewAccessor.itemView());
+    Q_ASSERT(m_viewAccessor.itemDelegate());
 
     const bool showHiddenFiles = props.showHiddenFiles();
     if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
@@ -1207,12 +1232,12 @@ void DolphinView::createView()
 {
     deleteView();
 
-    Q_ASSERT(m_viewAccessor.itemView() == 0);
-    Q_ASSERT(m_dolphinViewController->itemView() == 0);
+    Q_ASSERT(!m_viewAccessor.itemView());
+    Q_ASSERT(!m_dolphinViewController->itemView());
     m_viewAccessor.createView(this, m_dolphinViewController, m_viewModeController, m_mode);
 
     QAbstractItemView* view = m_viewAccessor.itemView();
-    Q_ASSERT(view != 0);
+    Q_ASSERT(view);
     view->installEventFilter(this);
     view->viewport()->installEventFilter(this);
 
@@ -1230,10 +1255,10 @@ void DolphinView::createView()
 void DolphinView::deleteView()
 {
     QAbstractItemView* view = m_viewAccessor.itemView();
-    Q_ASSERT((m_dolphinViewController->itemView() == 0) || (m_dolphinViewController->itemView() == view));
+    Q_ASSERT(!m_dolphinViewController->itemView() || (m_dolphinViewController->itemView() == view));
     m_dolphinViewController->setItemView(0);
 
-    if (view != 0) {
+    if (view) {
         disconnectViewAccessor();
 
         if (hasFocus()) {
@@ -1286,7 +1311,7 @@ KUrl::List DolphinView::simplifiedSelectedUrls() const
 QMimeData* DolphinView::selectionMimeData() const
 {
     const QAbstractItemView* view = m_viewAccessor.itemView();
-    Q_ASSERT((view != 0) && (view->selectionModel() != 0));
+    Q_ASSERT((view) && (view->selectionModel()));
     const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
     return m_viewAccessor.dirModel()->mimeData(selection.indexes());
 }
@@ -1326,15 +1351,20 @@ QItemSelection DolphinView::childrenMatchingPattern(const QModelIndex& parent, c
 void DolphinView::connectViewAccessor()
 {
     KDirLister* dirLister = m_viewAccessor.dirLister();
-    connect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
-            this, SLOT(slotRedirection(KUrl,KUrl)));
-    connect(dirLister, SIGNAL(started(KUrl)),
-            this, SLOT(slotDirListerStarted(KUrl)));
-    connect(dirLister, SIGNAL(completed()),
-            this, SLOT(slotDirListerCompleted()));
+    connect(dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
+    connect(dirLister, SIGNAL(started(KUrl)),          this, SLOT(slotDirListerStarted(KUrl)));
+    connect(dirLister, SIGNAL(completed()),            this, SLOT(slotDirListerCompleted()));
     connect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
             this, SLOT(slotRefreshItems()));
 
+    connect(dirLister, SIGNAL(clear()),                      this, SIGNAL(itemCountChanged()));
+    connect(dirLister, SIGNAL(newItems(KFileItemList)),      this, SIGNAL(itemCountChanged()));
+    connect(dirLister, SIGNAL(infoMessage(const QString&)),  this, SIGNAL(infoMessage(const QString&)));
+    connect(dirLister, SIGNAL(errorMessage(const QString&)), this, SIGNAL(infoMessage(const QString&)));
+    connect(dirLister, SIGNAL(percent(int)),                 this, SIGNAL(pathLoadingProgress(int)));
+    connect(dirLister, SIGNAL(urlIsFileError(const KUrl&)),  this, SIGNAL(urlIsFileError(const KUrl&)));
+    connect(dirLister, SIGNAL(itemsDeleted(const KFileItemList&)), this, SIGNAL(itemCountChanged()));
+
     QAbstractItemView* view = m_viewAccessor.itemView();
     connect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
             this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
@@ -1343,15 +1373,20 @@ void DolphinView::connectViewAccessor()
 void DolphinView::disconnectViewAccessor()
 {
     KDirLister* dirLister = m_viewAccessor.dirLister();
-    disconnect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
-               this, SLOT(slotRedirection(KUrl,KUrl)));
-    disconnect(dirLister, SIGNAL(started(KUrl)),
-               this, SLOT(slotDirListerStarted(KUrl)));
-    disconnect(dirLister, SIGNAL(completed()),
-               this, SLOT(slotDirListerCompleted()));
+    disconnect(dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
+    disconnect(dirLister, SIGNAL(started(KUrl)),          this, SLOT(slotDirListerStarted(KUrl)));
+    disconnect(dirLister, SIGNAL(completed()),            this, SLOT(slotDirListerCompleted()));
     disconnect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
                this, SLOT(slotRefreshItems()));
 
+    disconnect(dirLister, SIGNAL(clear()),                      this, SIGNAL(itemCountChanged()));
+    disconnect(dirLister, SIGNAL(newItems(KFileItemList)),      this, SIGNAL(itemCountChanged()));
+    disconnect(dirLister, SIGNAL(infoMessage(const QString&)),  this, SIGNAL(infoMessage(const QString&)));
+    disconnect(dirLister, SIGNAL(errorMessage(const QString&)), this, SIGNAL(errorMessage(const QString&)));
+    disconnect(dirLister, SIGNAL(percent(int)),                 this, SIGNAL(pathLoadingProgress(int)));
+    disconnect(dirLister, SIGNAL(urlIsFileError(const KUrl&)),  this, SIGNAL(urlIsFileError(const KUrl&)));
+    disconnect(dirLister, SIGNAL(itemsDeleted(const KFileItemList&)), this, SIGNAL(itemCountChanged()));
+
     QAbstractItemView* view = m_viewAccessor.itemView();
     disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
                this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
@@ -1372,18 +1407,36 @@ void DolphinView::updateWritableState()
     }
 }
 
-DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
+DolphinView::ViewAccessor::ViewAccessor() :
     m_rootUrl(),
     m_iconsView(0),
     m_detailsView(0),
     m_columnsContainer(0),
-    m_proxyModel(proxyModel),
+    m_dolphinModel(0),
+    m_proxyModel(0),
     m_dragSource(0)
 {
+    DolphinDirLister* dirLister = new DolphinDirLister();
+    dirLister->setAutoUpdate(true);
+    dirLister->setDelayedMimeTypes(true);
+
+    m_dolphinModel = new DolphinModel();
+    m_dolphinModel->setDirLister(dirLister);  // m_dolphinModel takes ownership of dirLister
+    m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
+
+    m_proxyModel = new DolphinSortFilterProxyModel();
+    m_proxyModel->setSourceModel(m_dolphinModel);
+    m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
 }
 
 DolphinView::ViewAccessor::~ViewAccessor()
 {
+    delete m_proxyModel;
+    m_proxyModel = 0;
+
+    delete m_dolphinModel;
+    m_dolphinModel = 0;
+
     delete m_dragSource;
     m_dragSource = 0;
 }
@@ -1393,7 +1446,7 @@ void DolphinView::ViewAccessor::createView(QWidget* parent,
                                            const ViewModeController* viewModeController,
                                            Mode mode)
 {
-    Q_ASSERT(itemView() == 0);
+    Q_ASSERT(!itemView());
 
     switch (mode) {
     case IconsView:
@@ -1429,14 +1482,14 @@ void DolphinView::ViewAccessor::createView(QWidget* parent,
 
 void DolphinView::ViewAccessor::deleteView()
 {
-    if (m_columnsContainer != 0) {
+    if (m_columnsContainer) {
         m_columnsContainer->close();
         m_columnsContainer->disconnect();
         m_columnsContainer->deleteLater();
         m_columnsContainer = 0;
     } else {
         QAbstractItemView* view = itemView();
-        if (view != 0) {
+        if (view) {
             view->close();
             view->disconnect();
 
@@ -1445,7 +1498,7 @@ void DolphinView::ViewAccessor::deleteView()
                 // during drag operations" is used). Deleting the view
                 // during an ongoing drag operation is not allowed, so
                 // this will postponed.
-                if (m_dragSource != 0) {
+                if (m_dragSource) {
                     // the old stored view is obviously not the drag source anymore
                     m_dragSource->deleteLater();
                     m_dragSource = 0;
@@ -1465,22 +1518,22 @@ void DolphinView::ViewAccessor::deleteView()
 
 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
 {
-    if (m_columnsContainer != 0) {
+    if (m_columnsContainer) {
         m_columnsContainer->showColumn(url);
     }
 }
 
 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
 {
-    if (m_iconsView != 0) {
+    if (m_iconsView) {
         return m_iconsView;
     }
 
-    if (m_detailsView != 0) {
+    if (m_detailsView) {
         return m_detailsView;
     }
 
-    if (m_columnsContainer != 0) {
+    if (m_columnsContainer) {
         return m_columnsContainer->activeColumn();
     }
 
@@ -1494,7 +1547,7 @@ KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
 
 QWidget* DolphinView::ViewAccessor::layoutTarget() const
 {
-    if (m_columnsContainer != 0) {
+    if (m_columnsContainer) {
         return m_columnsContainer;
     }
     return itemView();
@@ -1507,7 +1560,7 @@ void DolphinView::ViewAccessor::setRootUrl(const KUrl& rootUrl)
 
 KUrl DolphinView::ViewAccessor::rootUrl() const
 {
-    return (m_columnsContainer != 0) ? m_columnsContainer->rootUrl() : m_rootUrl;
+    return m_columnsContainer ? m_columnsContainer->rootUrl() : m_rootUrl;
 }
 
 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
@@ -1517,12 +1570,12 @@ bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
 
 bool DolphinView::ViewAccessor::itemsExpandable() const
 {
-    return (m_detailsView != 0) && m_detailsView->itemsExpandable();
+    return m_detailsView && m_detailsView->itemsExpandable();
 }
 
 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
 {
-    if (m_detailsView != 0) {
+    if (m_detailsView) {
         return m_detailsView->expandedUrls();
     }
 
@@ -1531,7 +1584,7 @@ QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
 
 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
 {
-    if ((m_detailsView != 0) && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
+    if (m_detailsView && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
         // Check if another expander is already active and stop it if necessary.
         if(!m_detailsViewExpander.isNull()) {
             m_detailsViewExpander->stop();
@@ -1559,7 +1612,7 @@ DolphinModel* DolphinView::ViewAccessor::dirModel() const
 
 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
 {
-    if (m_columnsContainer != 0) {
+    if (m_columnsContainer) {
         return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
     }
     return m_proxyModel;