]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinview.cpp
- adding missing const
[dolphin.git] / src / dolphinview.cpp
index bddd4b5bf57f552c0287e952eb1c6d4fc01cd7ba..d4dbbd2c28b6eb4fb64132e00ef220d158b2681a 100644 (file)
@@ -104,9 +104,10 @@ DolphinView::DolphinView(QWidget* parent,
     m_previewGenerator(0),
     m_toolTipManager(0),
     m_rootUrl(),
-    m_currentItemUrl(),
+    m_activeItemUrl(),
     m_createdItemUrl(),
     m_selectedItems(),
+    m_newFileNames(),
     m_expandedDragSource(0)
 {
     m_topLayout = new QVBoxLayout(this);
@@ -147,7 +148,7 @@ DolphinView::DolphinView(QWidget* parent,
     connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)),
             this, SIGNAL(redirection(KUrl, KUrl)));
     connect(m_dirLister, SIGNAL(completed()),
-            this, SLOT(restoreCurrentItem()));
+            this, SLOT(slotDirListerCompleted()));
     connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
             this, SLOT(slotRefreshItems()));
 
@@ -328,6 +329,7 @@ void DolphinView::clearSelection()
     const QModelIndex currentIndex = selModel->currentIndex();
     selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current |
                                             QItemSelectionModel::Clear);
+    m_selectedItems.clear();
 }
 
 KFileItemList DolphinView::selectedItems() const
@@ -374,7 +376,7 @@ int DolphinView::selectedItemsCount() const
         return m_columnView->selectedItems().count();
     }
 
-    return itemView()->selectionModel()->selection().count();
+    return itemView()->selectionModel()->selectedIndexes().count();
 }
 
 void DolphinView::setContentsPosition(int x, int y)
@@ -606,8 +608,7 @@ QString DolphinView::statusBarText() const
 
 void DolphinView::setUrl(const KUrl& url)
 {
-    // remember current item candidate (see restoreCurrentItem())
-    m_currentItemUrl = url;
+    m_newFileNames.clear();
     updateView(url, KUrl());
 }
 
@@ -643,16 +644,23 @@ void DolphinView::renameSelectedItems()
     if (itemCount > 1) {
         // More than one item has been selected for renaming. Open
         // a rename dialog and rename all items afterwards.
-        RenameDialog dialog(this, items);
-        if (dialog.exec() == QDialog::Rejected) {
+        QPointer<RenameDialog> dialog = new RenameDialog(this, items);
+        if (dialog->exec() == QDialog::Rejected) {
+            delete dialog;
             return;
         }
 
-        const QString newName = dialog.newName();
+        const QString newName = dialog->newName();
         if (newName.isEmpty()) {
-            emit errorMessage(dialog.errorString());
+            emit errorMessage(dialog->errorString());
+            delete dialog;
             return;
         }
+        delete dialog;
+        
+        // the selection would be invalid after renaming the items, so just clear
+        // it before
+        clearSelection();
 
         // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
         // as one operation instead of n rename operations like it is done now...
@@ -691,16 +699,19 @@ void DolphinView::renameSelectedItems()
     } else {
         Q_ASSERT(itemCount == 1);
 
-        RenameDialog dialog(this, items);
-        if (dialog.exec() == QDialog::Rejected) {
+        QPointer<RenameDialog> dialog = new RenameDialog(this, items);
+        if (dialog->exec() == QDialog::Rejected) {
+            delete dialog;
             return;
         }
 
-        const QString& newName = dialog.newName();
+        const QString newName = dialog->newName();
         if (newName.isEmpty()) {
-            emit errorMessage(dialog.errorString());
+            emit errorMessage(dialog->errorString());
+            delete dialog;
             return;
         }
+        delete dialog;
 
         const KUrl& oldUrl = items.first().url();
         KUrl newUrl = oldUrl;
@@ -979,6 +990,7 @@ void DolphinView::dropUrls(const KFileItem& destItem,
                            const KUrl& destPath,
                            QDropEvent* event)
 {
+    addNewFileNames(event->mimeData());
     DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
 }
 
@@ -1090,6 +1102,11 @@ bool DolphinView::isTabsForFilesEnabled() const
     return m_tabsForFiles;
 }
 
+void DolphinView::activateItem(const KUrl& url)
+{
+    m_activeItemUrl = url;
+}
+
 bool DolphinView::itemsExpandable() const
 {
     return (m_detailsView != 0) && m_detailsView->itemsExpandable();
@@ -1179,10 +1196,11 @@ void DolphinView::slotRequestUrlChange(const KUrl& url)
     m_controller->setUrl(url);
 }
 
-void DolphinView::restoreCurrentItem()
+void DolphinView::slotDirListerCompleted()
 {
-    if (!m_currentItemUrl.isEmpty()) {
-        const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
+    if (!m_activeItemUrl.isEmpty()) {
+        // assure that the current item remains visible
+        const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_activeItemUrl);
         if (dirIndex.isValid()) {
             const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
             QAbstractItemView* view = itemView();
@@ -1191,8 +1209,26 @@ void DolphinView::restoreCurrentItem()
             if (clearSelection) {
                 view->clearSelection();
             }
+            m_activeItemUrl.clear();
         }
-        m_currentItemUrl.clear();
+    }
+
+    if (!m_newFileNames.isEmpty()) {
+        // select all newly added items created by a paste operation or
+        // a drag & drop operation
+        const int rowCount = m_proxyModel->rowCount();
+        QItemSelection selection;
+        for (int row = 0; row < rowCount; ++row) {
+            const QModelIndex proxyIndex = m_proxyModel->index(row, 0);
+            const QModelIndex dirIndex = m_proxyModel->mapToSource(proxyIndex);
+            const KUrl url = m_dolphinModel->itemForIndex(dirIndex).url();
+            if (m_newFileNames.contains(url.fileName())) {
+                selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
+            }
+        }
+        itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
+
+        m_newFileNames.clear();
     }
 }
 
@@ -1472,6 +1508,7 @@ QAbstractItemView* DolphinView::itemView() const
 
 void DolphinView::pasteToUrl(const KUrl& url)
 {
+    addNewFileNames(QApplication::clipboard()->mimeData());
     KonqOperations::doPaste(this, url);
 }
 
@@ -1505,5 +1542,12 @@ QMimeData* DolphinView::selectionMimeData() const
     return m_dolphinModel->mimeData(selection.indexes());
 }
 
+void DolphinView::addNewFileNames(const QMimeData* mimeData)
+{
+    const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
+    foreach (const KUrl& url, urls) {
+        m_newFileNames.insert(url.fileName());
+    }
+}
 
 #include "dolphinview.moc"