]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
Forward-port: Restore keyboard-focus when changing the view-mode
[dolphin.git] / src / views / dolphinview.cpp
index 46f7acf1304fdc7ecb256c77e6d6dec2824bdcce..29f62f735d845ff80fb8bed841e408d613694883 100644 (file)
@@ -34,6 +34,7 @@
 #include <kdirlister.h>
 #include <kiconeffect.h>
 #include <kfileitem.h>
+#include <kfileitemlistproperties.h>
 #include <klocale.h>
 #include <kio/deletejob.h>
 #include <kio/netaccess.h>
@@ -76,6 +77,8 @@ DolphinView::DolphinView(QWidget* parent,
     m_tabsForFiles(false),
     m_isContextMenuOpen(false),
     m_assureVisibleCurrentIndex(false),
+    m_expanderActive(false),
+    m_isFolderWritable(true),
     m_mode(DolphinView::IconsView),
     m_topLayout(0),
     m_dolphinViewController(0),
@@ -123,7 +126,7 @@ DolphinView::DolphinView(QWidget* parent,
     connect(m_dolphinViewController, SIGNAL(viewportEntered()),
             this, SLOT(clearHoverInformation()));
     connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
-            m_viewModeController, SLOT(setUrl(KUrl)));
+            this, SLOT(slotUrlChangeRequested(KUrl)));
 
     // When a new item has been created by the "Create New..." menu, the item should
     // get selected and it must be assured that the item will get visible. As the
@@ -169,9 +172,7 @@ void DolphinView::setActive(bool active)
     m_active = active;
 
     QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
-    if (active) {
-        emitSelectionChangedSignal();
-    } else {
+    if (!active) {
         color.setAlpha(150);
     }
 
@@ -185,6 +186,8 @@ void DolphinView::setActive(bool active)
     if (active) {
         m_viewAccessor.itemView()->setFocus();
         emit activated();
+        emitSelectionChangedSignal();
+        emit writeStateChanged(m_isFolderWritable);
     }
 
     m_viewModeController->indicateActivationChange(active);
@@ -208,6 +211,7 @@ void DolphinView::setMode(Mode mode)
     // be restored after reloading the directory
     m_selectedItems = selectedItems();
 
+    const bool hasFocus = m_viewAccessor.itemView()->hasFocus();
     deleteView();
 
     const KUrl viewPropsUrl = rootUrl();
@@ -215,6 +219,10 @@ void DolphinView::setMode(Mode mode)
     props.setViewMode(m_mode);
     createView();
 
+    if (hasFocus) {
+        m_viewAccessor.itemView()->setFocus();
+    }
+
     // the file item delegate has been recreated, apply the current
     // additional information manually
     const KFileItemDelegate::InformationList infoList = props.additionalInfo();
@@ -970,6 +978,34 @@ void DolphinView::selectAndScrollToCreatedItem()
     m_createdItemUrl = KUrl();
 }
 
+void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
+{
+    if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
+        emit redirection(oldUrl, newUrl);
+        m_viewModeController->redirectToUrl(newUrl); // #186947
+    }
+}
+
+void DolphinView::restoreContentsPosition()
+{
+    if (!m_restoredContentsPosition.isNull()) {
+        const int x = m_restoredContentsPosition.x();
+        const int y = m_restoredContentsPosition.y();
+        m_restoredContentsPosition = QPoint();
+
+        QAbstractItemView* view = m_viewAccessor.itemView();
+        Q_ASSERT(view != 0);
+        view->horizontalScrollBar()->setValue(x);
+        view->verticalScrollBar()->setValue(y);
+    }
+}
+
+void DolphinView::slotUrlChangeRequested(const KUrl& url)
+{
+    m_viewModeController->setUrl(url);
+    updateWritableState();
+}
+
 void DolphinView::showHoverInformation(const KFileItem& item)
 {
     emit requestItemInfo(item);
@@ -989,6 +1025,18 @@ void DolphinView::slotDeleteFileFinished(KJob* job)
     }
 }
 
+void DolphinView::slotDirListerStarted(const KUrl& url)
+{
+    // Disable the writestate temporary until it can be determined in a fast way
+    // in DolphinView::slotDirListerCompleted()
+    if (m_isFolderWritable) {
+        m_isFolderWritable = false;
+        emit writeStateChanged(m_isFolderWritable);
+    }
+
+    emit startedPathLoading(url);
+}
+
 void DolphinView::slotDirListerCompleted()
 {
     if (!m_expanderActive) {
@@ -1013,6 +1061,8 @@ void DolphinView::slotDirListerCompleted()
 
         m_newFileNames.clear();
     }
+
+    updateWritableState();
 }
 
 void DolphinView::slotLoadingCompleted()
@@ -1186,12 +1236,14 @@ void DolphinView::deleteView()
     if (view != 0) {
         disconnectViewAccessor();
 
-        // It's important to set the keyboard focus to the parent
-        // before deleting the view: Otherwise when having a split
-        // view the other view will get the focus and will request
-        // an activation (see DolphinView::eventFilter()).
-        setFocusProxy(0);
-        setFocus();
+        if (hasFocus()) {
+            // It's important to set the keyboard focus to the parent
+            // before deleting the view: Otherwise when having a split
+            // view the other view will get the focus and will request
+            // an activation (see DolphinView::eventFilter()).
+            setFocusProxy(0);
+            setFocus();
+        }
 
         m_viewModeController->disconnect(view);
 
@@ -1277,7 +1329,7 @@ void DolphinView::connectViewAccessor()
     connect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
             this, SLOT(slotRedirection(KUrl,KUrl)));
     connect(dirLister, SIGNAL(started(KUrl)),
-            this, SIGNAL(startedPathLoading(KUrl)));
+            this, SLOT(slotDirListerStarted(KUrl)));
     connect(dirLister, SIGNAL(completed()),
             this, SLOT(slotDirListerCompleted()));
     connect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
@@ -1294,7 +1346,7 @@ void DolphinView::disconnectViewAccessor()
     disconnect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
                this, SLOT(slotRedirection(KUrl,KUrl)));
     disconnect(dirLister, SIGNAL(started(KUrl)),
-               this, SIGNAL(startedPathLoading(KUrl)));
+               this, SLOT(slotDirListerStarted(KUrl)));
     disconnect(dirLister, SIGNAL(completed()),
                this, SLOT(slotDirListerCompleted()));
     disconnect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
@@ -1305,6 +1357,21 @@ void DolphinView::disconnectViewAccessor()
                this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
 }
 
+void DolphinView::updateWritableState()
+{
+    const bool wasFolderWritable = m_isFolderWritable;
+    m_isFolderWritable = true;
+
+    const KFileItem item = m_viewAccessor.dirLister()->rootItem();
+    if (!item.isNull()) {
+        KFileItemListProperties capabilities(KFileItemList() << item);
+        m_isFolderWritable = capabilities.supportsWriting();
+    }
+    if (m_isFolderWritable != wasFolderWritable) {
+        emit writeStateChanged(m_isFolderWritable);
+    }
+}
+
 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
     m_rootUrl(),
     m_iconsView(0),
@@ -1503,26 +1570,4 @@ KDirLister* DolphinView::ViewAccessor::dirLister() const
     return dirModel()->dirLister();
 }
 
-void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
-{
-    if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
-        emit redirection(oldUrl, newUrl);
-        m_viewModeController->redirectToUrl(newUrl); // #186947
-    }
-}
-
-void DolphinView::restoreContentsPosition()
-{
-    if (!m_restoredContentsPosition.isNull()) {
-        const int x = m_restoredContentsPosition.x();
-        const int y = m_restoredContentsPosition.y();
-        m_restoredContentsPosition = QPoint();
-
-        QAbstractItemView* view = m_viewAccessor.itemView();
-        Q_ASSERT(view != 0);
-        view->horizontalScrollBar()->setValue(x);
-        view->verticalScrollBar()->setValue(y);
-    }
-}
-
 #include "dolphinview.moc"