]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
Do not show an incorrect file name after a failed rename operation
[dolphin.git] / src / views / dolphinview.cpp
index 6a21885e62d559f3a231a1d0eaa60249503e93e3..8e1a191082dece7997388f05469b434a589fcded 100644 (file)
@@ -98,8 +98,8 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     m_toolTipManager(0),
     m_selectionChangedTimer(0),
     m_currentItemUrl(),
+    m_scrollToCurrentItem(false),
     m_restoredContentsPosition(),
-    m_createdItemUrl(),
     m_selectedUrls(),
     m_versionControlObserver(0)
 {
@@ -122,7 +122,6 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     m_model = new KFileItemModel(this);
     m_view = new DolphinItemListView();
     m_view->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle());
-    m_view->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews());
     m_view->setVisibleRoles(QList<QByteArray>() << "text");
     applyModeToView();
 
@@ -130,6 +129,10 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
     controller->setAutoActivationDelay(delay);
 
+    // The EnlargeSmallPreviews setting can only be changed after the model
+    // has been set in the view by KItemListController.
+    m_view->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews());
+
     m_container = new KItemListContainer(controller, this);
     m_container->installEventFilter(this);
     setFocusProxy(m_container);
@@ -160,6 +163,7 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     connect(m_model, SIGNAL(infoMessage(QString)),            this, SIGNAL(infoMessage(QString)));
     connect(m_model, SIGNAL(errorMessage(QString)),           this, SIGNAL(errorMessage(QString)));
     connect(m_model, SIGNAL(directoryRedirection(KUrl,KUrl)), this, SLOT(slotDirectoryRedirection(KUrl,KUrl)));
+    connect(m_model, SIGNAL(urlIsFileError(KUrl)),            this, SIGNAL(urlIsFileError(KUrl)));
 
     m_view->installEventFilter(this);
     connect(m_view, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
@@ -259,8 +263,14 @@ void DolphinView::setPreviewsShown(bool show)
     ViewProperties props(viewPropertiesUrl());
     props.setPreviewsShown(show);
 
+    const int oldZoomLevel = m_view->zoomLevel();
     m_view->setPreviewsShown(show);
     emit previewsShownChanged(show);
+
+    const int newZoomLevel = m_view->zoomLevel();
+    if (newZoomLevel != oldZoomLevel) {
+        emit zoomLevelChanged(newZoomLevel, oldZoomLevel);
+    }
 }
 
 bool DolphinView::previewsShown() const
@@ -356,6 +366,7 @@ void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
 void DolphinView::markUrlAsCurrent(const KUrl& url)
 {
     m_currentItemUrl = url;
+    m_scrollToCurrentItem = true;
 }
 
 void DolphinView::selectItems(const QRegExp& pattern, bool enabled)
@@ -382,6 +393,7 @@ void DolphinView::setZoomLevel(int level)
     const int oldZoomLevel = zoomLevel();
     m_view->setZoomLevel(level);
     if (zoomLevel() != oldZoomLevel) {
+        hideToolTip();
         emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
     }
 }
@@ -501,6 +513,16 @@ QString DolphinView::nameFilter() const
     return m_model->nameFilter();
 }
 
+void DolphinView::setMimeTypeFilters(const QStringList& filters)
+{
+    return m_model->setMimeTypeFilters(filters);
+}
+
+QStringList DolphinView::mimeTypeFilters() const
+{
+    return m_model->mimeTypeFilters();
+}
+
 QString DolphinView::statusBarText() const
 {
     QString summary;
@@ -616,9 +638,9 @@ void DolphinView::renameSelectedItems()
          return;
      }
 
-     if (items.count() == 1) {
+     if (items.count() == 1 && GeneralSettings::renameInline()) {
          const int index = m_model->index(items.first());
-         m_container->controller()->view()->editRole(index, "text");
+         m_view->editRole(index, "text");
      } else {
          RenameDialog* dialog = new RenameDialog(this, items);
          dialog->setAttribute(Qt::WA_DeleteOnClose);
@@ -734,6 +756,20 @@ void DolphinView::hideEvent(QHideEvent* event)
     QWidget::hideEvent(event);
 }
 
+bool DolphinView::event(QEvent* event)
+{
+    /* See Bug 297355
+     * Dolphin leaves file preview tooltips open even when is not visible.
+     *
+     * Hide tool-tip when Dolphin loses focus.
+     */
+    if (event->type() == QEvent::WindowDeactivate) {
+        hideToolTip();
+    }
+
+    return QWidget::event(event);
+}
+
 void DolphinView::activate()
 {
     setActive(true);
@@ -1134,25 +1170,8 @@ QString DolphinView::viewPropertiesContext() const
 
 void DolphinView::observeCreatedItem(const KUrl& url)
 {
-    m_createdItemUrl = url;
-    connect(m_model, SIGNAL(directoryLoadingCompleted()),
-            this, SLOT(selectAndScrollToCreatedItem()));
-}
-
-void DolphinView::selectAndScrollToCreatedItem()
-{
-    KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
-    const int index = m_model->index(m_createdItemUrl);
-    if (index != -1) {
-        selectionManager->setCurrentItem(index);
-        selectionManager->clearSelection();
-        selectionManager->setSelected(index);
-        m_view->scrollToItem(index);
-    }
-
-    disconnect(m_model, SIGNAL(directoryLoadingCompleted()),
-               this, SLOT(selectAndScrollToCreatedItem()));
-    m_createdItemUrl = KUrl();
+    markUrlAsCurrent(url);
+    markUrlsAsSelected(QList<KUrl>() << url);
 }
 
 void DolphinView::slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl)
@@ -1170,6 +1189,12 @@ void DolphinView::updateViewState()
         const int currentIndex = m_model->index(m_currentItemUrl);
         if (currentIndex != -1) {
             selectionManager->setCurrentItem(currentIndex);
+
+            // scroll to current item and reset the state
+            if (m_scrollToCurrentItem) {
+                m_view->scrollToItem(currentIndex);
+                m_scrollToCurrentItem = false;
+            }
         } else {
             selectionManager->setCurrentItem(0);
         }
@@ -1313,11 +1338,30 @@ void DolphinView::slotVisibleRolesChangedByHeader(const QList<QByteArray>& curre
 
 void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
 {
+    if (index < 0 || index >= m_model->count()) {
+        return;
+    }
+
     if (role == "text") {
-        const KFileItem item = m_model->fileItem(index);
+        const KFileItem oldItem = m_model->fileItem(index);
         const QString newName = value.toString();
-        if (!newName.isEmpty() && newName != item.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
-            KonqOperations::rename(this, item.url(), newName);
+        if (!newName.isEmpty() && newName != oldItem.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
+            const KUrl oldUrl = oldItem.url();
+
+            const KUrl newUrl(url().path(KUrl::AddTrailingSlash) + newName);
+            const bool newNameExistsAlready = (m_model->index(newUrl) >= 0);
+            if (!newNameExistsAlready) {
+                // Only change the data in the model if no item with the new name
+                // is in the model yet. If there is an item with the new name
+                // already, calling KonqOperations::rename() will open a dialog
+                // asking for a new name, and KFileItemModel will update the
+                // data when the dir lister signals that the file name has changed.
+                QHash<QByteArray, QVariant> data;
+                data.insert(role, value);
+                m_model->setData(index, data);
+            }
+
+            KonqOperations::rename(this, oldUrl, newName);
         }
     }
 }
@@ -1458,7 +1502,7 @@ KUrl::List DolphinView::simplifiedSelectedUrls() const
     KUrl::List urls;
 
     const KFileItemList items = selectedItems();
-    foreach (const KFileItem &item, items) {
+    foreach (const KFileItemitem, items) {
         urls.append(item.url());
     }
 
@@ -1483,7 +1527,7 @@ void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
     const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
     KUrl::List destUrls;
     foreach (const KUrl& source, sourceUrls) {
-        KUrl destination(url().url() + "/" + source.fileName());
+        KUrl destination(url().url() + '/' + source.fileName());
         destUrls << destination;
     }
     markUrlsAsSelected(destUrls);