]> cloud.milkyroute.net Git - dolphin.git/commitdiff
- Integrate the patch from Matthias Fuchs from http://reviewboard.kde.org/r/5496...
authorPeter Penz <peter.penz19@gmail.com>
Tue, 5 Oct 2010 16:30:03 +0000 (16:30 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Tue, 5 Oct 2010 16:30:03 +0000 (16:30 +0000)
- Move the renaming code into the RenameDialog

CCMAIL: mat69@gmx.net
BUG: 226761
FIXED-IN: 4.6.0

svn path=/trunk/KDE/kdebase/apps/; revision=1182776

src/panels/folders/folderspanel.cpp
src/views/dolphinview.cpp
src/views/renamedialog.cpp
src/views/renamedialog.h

index 729dac3727ad40c287eb7c09e4918acd11f3e6de..1dabefd110dd36fe9ca91ed0bedf7b0334b0b58e 100644 (file)
@@ -95,18 +95,11 @@ void FoldersPanel::rename(const KFileItem& item)
         const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
         m_treeView->edit(proxyIndex);
     } else {
-        KFileItemList items;
-        items.append(item);
-        QPointer<RenameDialog> dialog = new RenameDialog(this, items);
-        if (dialog->exec() == QDialog::Accepted) {
-            const QString newName = dialog->newName();
-            if (!newName.isEmpty()) {
-                KUrl newUrl = item.url();
-                newUrl.setFileName(newName);
-                KonqOperations::rename(this, item.url(), newUrl);
-            }
-        }
-        delete dialog;
+        RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
+        dialog->setAttribute(Qt::WA_DeleteOnClose);
+        dialog->show();
+        dialog->raise();
+        dialog->activateWindow();
     }
 }
 
index e9bee31863936bd671c12ced747830102090d688..79706358ff716b44b28121a8ed5a518aa5309225 100644 (file)
@@ -45,7 +45,6 @@
 #include <konq_fileitemcapabilities.h>
 #include <konq_operations.h>
 #include <konqmimedata.h>
-#include <kstringhandler.h>
 #include <ktoggleaction.h>
 #include <kurl.h>
 
 #include "zoomlevelinfo.h"
 #include "dolphindetailsviewexpander.h"
 
-/**
- * Helper function for sorting items with qSort() in
- * DolphinView::renameSelectedItems().
- */
-bool lessThan(const KFileItem& item1, const KFileItem& item2)
-{
-    return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
-}
-
 DolphinView::DolphinView(QWidget* parent,
                          const KUrl& url,
                          DolphinSortFilterProxyModel* proxyModel) :
@@ -600,7 +590,7 @@ void DolphinView::renameSelectedItems()
     if (itemCount < 1) {
         return;
     }
-
+/*
     if (itemCount > 1) {
         // More than one item has been selected for renaming. Open
         // a rename dialog and rename all items afterwards.
@@ -646,16 +636,21 @@ void DolphinView::renameSelectedItems()
                 KonqOperations::rename(this, oldUrl, newUrl);
             }
         }
-    } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
-        Q_ASSERT(itemCount == 1);
+    } else*/
+
+    if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
         const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
         const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
         m_viewAccessor.itemView()->edit(proxyIndex);
     } else {
-        Q_ASSERT(itemCount == 1);
-
-        QPointer<RenameDialog> dialog = new RenameDialog(this, items);
-        if (dialog->exec() == QDialog::Rejected) {
+        RenameDialog* dialog = new RenameDialog(this, items);
+        dialog->setAttribute(Qt::WA_DeleteOnClose);
+        dialog->show();
+        dialog->raise();
+        dialog->activateWindow();
+
+        // XXX
+      /*  if (dialog->exec() == QDialog::Rejected) {
             delete dialog;
             return;
         }
@@ -671,7 +666,7 @@ void DolphinView::renameSelectedItems()
         const KUrl& oldUrl = items.first().url();
         KUrl newUrl = oldUrl;
         newUrl.setFileName(newName);
-        KonqOperations::rename(this, oldUrl, newUrl);
+        KonqOperations::rename(this, oldUrl, newUrl);*/
     }
 
     // assure that the current index remains visible when KDirLister
index aae546e7a8b20d6aee07faa77d6c7dfe6a27ba44..7f79b0d4304dd4d8eda1cd7456701ad869fd19cc 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at)                  *
+ *   Copyright (C) 2006-2010 by Peter Penz (peter.penz@gmx.at)             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
 
 #include "renamedialog.h"
 
-#include <kfileitem.h>
 #include <klineedit.h>
 #include <klocale.h>
+#include <konq_operations.h>
+#include <kstringhandler.h>
 
-#include <QtGui/QLabel>
-#include <QtGui/QBoxLayout>
+#include <QLabel>
+#include <QVBoxLayout>
+
+/**
+ * Helper function for sorting items with qSort() in
+ * DolphinView::renameSelectedItems().
+ */
+bool lessThan(const KFileItem& item1, const KFileItem& item2)
+{
+    return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
+}
 
 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
     KDialog(parent),
-    m_renameOneItem(false)
+    m_renameOneItem(false),
+    m_newName(),
+    m_lineEdit(0),
+    m_items(items)
 {
     const QSize minSize = minimumSize();
     setMinimumSize(QSize(320, minSize.height()));
@@ -116,25 +129,77 @@ RenameDialog::~RenameDialog()
 
 void RenameDialog::slotButtonClicked(int button)
 {
-    if (button == Ok) {
-        m_newName = m_lineEdit->text();
-        if (m_newName.isEmpty()) {
-            m_errorString = i18nc("@info:status",
-                                  "The new name is empty. A name with at least one character must be entered.");
-        } else if (!m_renameOneItem && (m_newName.count('#') == 0)) {
-            m_newName.truncate(0);
-            m_errorString = i18nc("@info:status", "The name must contain at least one # character.");
-        }
+    if (button == KDialog::Ok) {
+        renameItems();
     }
 
     KDialog::slotButtonClicked(button);
 }
 
-void RenameDialog::slotTextChanged(const QString &newName)
+void RenameDialog::slotTextChanged(const QStringnewName)
 {
-    const bool enable = !newName.isEmpty() && (m_renameOneItem ? (newName != m_newName) : newName.contains('#'));
+    m_newName = m_lineEdit->text();
+
+    bool enable = !newName.isEmpty() && (m_renameOneItem ? (newName != m_newName) : newName.contains('#'));
+    if (enable) {
+        if (m_renameOneItem) {
+            enable = enable && (newName != m_newName);
+        } else {
+            // Assure that the new name contains exactly one # (or a connected sequence of #'s)
+            const int minSplitCount = 1;
+            int maxSplitCount = 2;
+            if (newName.startsWith(QLatin1Char('#'))) {
+                --maxSplitCount;
+            }
+            if (newName.endsWith(QLatin1Char('#'))) {
+                --maxSplitCount;
+            }
+            const int splitCount = newName.split(QLatin1Char('#'), QString::SkipEmptyParts).count();
+            enable = enable && (splitCount >= minSplitCount) && (splitCount <= maxSplitCount);
+        }
+    }
     enableButtonOk(enable);
 }
 
+void RenameDialog::renameItems()
+{
+    // Currently the items are sorted by the selection order, resort
+    // them by the filename. This assures that the new sort order is similar to
+    // the current filename sort order.
+    qSort(m_items.begin(), m_items.end(), lessThan);
+
+    // Iterate through all items and rename them...
+    int index = 1;
+    foreach (const KFileItem& item, m_items) {
+        const QString newName = indexedName(m_newName, index, QLatin1Char('#'));
+        ++index;
+
+        const KUrl oldUrl = item.url();
+        if (oldUrl.fileName() != newName) {
+            KUrl newUrl = oldUrl;
+            newUrl.setFileName(newName);
+            KonqOperations::rename(this, oldUrl, newUrl);
+        }
+    }
+}
+
+QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
+{
+    QString newName = name;
+
+    QString indexString = QString::number(index);
+
+    // Insert leading zeros if necessary
+    const int minIndexLength = name.count(indexPlaceHolder);
+    while (indexString.length() < minIndexLength) {
+        indexString.prepend(QLatin1Char('0'));
+    }
+
+    // Replace the index placeholders by the indexString
+    const int placeHolderStart = newName.indexOf(indexPlaceHolder);
+    newName.replace(placeHolderStart, minIndexLength, indexString);
+
+    return newName;
+}
 
 #include "renamedialog.moc"
index 5015b51bd2bae8d82434ca2e53f83d9a8b162433..73414093ca4dba682b93103782202d60e4685690 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at)                  *
+ *   Copyright (C) 2006-2010 by Peter Penz (peter.penz@gmx.at)             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   Free Software Foundation, Inc.,                                       *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
  ***************************************************************************/
+
 #ifndef RENAMEDIALOG_H
 #define RENAMEDIALOG_H
 
 #include "libdolphin_export.h"
 
 #include <kdialog.h>
-#include <kurl.h>
+#include <kfileitem.h>
 
-class KFileItem;
-class KFileItemList;
 class KLineEdit;
 
 #include <QString>
 
 /**
  * @brief Dialog for renaming a variable number of files.
- *
- * The renaming is not done by the dialog, the invoker
- * must do this itself:
- * \code
- * RenameDialog dialog(...);
- * if (dialog.exec() == QDialog::Accepted) {
- *     const QString& newName = dialog.newName();
- *     if (newName.isEmpty()) {
- *         // an invalid name has been chosen, use
- *         // dialog.errorString() to tell the user about this
- *     }
- *     else {
- *         // rename items corresponding to the new name
- *     }
- * }
- * \endcode
  */
 class LIBDOLPHINPRIVATE_EXPORT RenameDialog : public KDialog
 {
     Q_OBJECT
 
 public:
-    explicit RenameDialog(QWidget *parent, const KFileItemList& items);
+    explicit RenameDialog(QWidgetparent, const KFileItemList& items);
     virtual ~RenameDialog();
 
-    /**
-     * Returns the new name of the items. If more than one
-     * item should be renamed, then it is assured that the # character
-     * is part of the returned string. If the returned string is empty,
-     * then RenameDialog::errorString() should be used to show the reason
-     * of having an empty string (e. g. if the # character has
-     * been deleted by the user, although more than one item should be
-     * renamed).
-     */
-    QString newName() const;
-
-    /**
-     * Returns the error string, if Dialog::newName() returned an empty string.
-     */
-    QString errorString() const;
-
 protected slots:
     virtual void slotButtonClicked(int button);
 
 private slots:
-    void slotTextChanged(const QString &newName);
+    void slotTextChanged(const QStringnewName);
 
 private:
-    bool m_renameOneItem;
-    KLineEdit* m_lineEdit;
-    QString m_newName;
-    QString m_errorString;
+    void renameItems();
 
-    friend class RenameDialogTest; // allow access for unit testing
-};
 
-inline QString RenameDialog::newName() const
-{
-    return m_newName;
-}
+    /**
+     * @return Returns the string \p name, where the characters represented by
+     *         \p indexPlaceHolder get replaced by the index \p index.
+     *         E. g. Calling indexedName("Test #.jpg", 12, '#') returns "Test 12.jpg".
+     *         A connected sequence of placeholders results in leading zeros:
+     *         indexedName("Test ####.jpg", 12, '#') returns "Test 0012.jpg".
+     */
+    static QString indexedName(const QString& name, int index, const QChar& indexPlaceHolder);
 
-inline QString RenameDialog::errorString() const
-{
-    return m_errorString;
-}
+private:
+    bool m_renameOneItem;
+    QString m_newName;
+    KLineEdit* m_lineEdit;
+    KFileItemList m_items;
+};
 
 #endif