]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/folders/folderspanel.cpp
Merge remote-tracking branch 'origin/KDE/4.9'
[dolphin.git] / src / panels / folders / folderspanel.cpp
index 2f9e1d323c9dac57279275735f155ada3cc63a24..6e3a7678f6490b8382b7414b8e0bf29dffcb3ba1 100644 (file)
@@ -22,7 +22,9 @@
 #include "dolphin_folderspanelsettings.h"
 #include "dolphin_generalsettings.h"
 #include "treeviewcontextmenu.h"
+#include "foldersitemlistwidget.h"
 
+#include <views/renamedialog.h>
 #include <kitemviews/kitemlistselectionmanager.h>
 #include <kitemviews/kfileitemlistview.h>
 #include <kitemviews/kfileitemlistwidget.h>
 #include <QTimer>
 
 #include <views/draganddrophelper.h>
-#include <views/renamedialog.h>
 
 #include <KDebug>
 
 FoldersPanel::FoldersPanel(QWidget* parent) :
     Panel(parent),
     m_updateCurrentItem(false),
-    m_controller(0)
+    m_controller(0),
+    m_model(0)
 {
     setLayoutDirection(Qt::LeftToRight);
 }
@@ -68,7 +70,7 @@ FoldersPanel::~FoldersPanel()
 void FoldersPanel::setShowHiddenFiles(bool show)
 {
     FoldersPanelSettings::setHiddenFilesShown(show);
-    fileItemModel()->setShowHiddenFiles(show);
+    m_model->setShowHiddenFiles(show);
 }
 
 bool FoldersPanel::showHiddenFiles() const
@@ -89,11 +91,9 @@ bool FoldersPanel::autoScrolling() const
 
 void FoldersPanel::rename(const KFileItem& item)
 {
-    // TODO: Inline renaming is not supported anymore in Dolphin 2.0
-    if (false /* GeneralSettings::renameInline() */) {
-        //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
-        //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
-        //m_treeView->edit(proxyIndex);
+    if (GeneralSettings::renameInline()) {
+        const int index = m_model->index(item);
+        m_controller->view()->editRole(index, "text");
     } else {
         RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
         dialog->setAttribute(Qt::WA_DeleteOnClose);
@@ -126,39 +126,31 @@ void FoldersPanel::showEvent(QShowEvent* event)
     }
 
     if (!m_controller) {
-        // Postpone the creating of the dir lister to the first show event.
-        // This assures that no performance and memory overhead is given when the TreeView is not
-        // used at all (see FoldersPanel::setUrl()).
+        // Postpone the creating of the controller to the first show event.
+        // This assures that no performance and memory overhead is given when the folders panel is not
+        // used at all and stays invisible.
         KFileItemListView* view  = new KFileItemListView();
-        view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
-
-        KItemListStyleOption styleOption = view->styleOption();
-        styleOption.padding = 2;
-        styleOption.iconSize = KIconLoader::SizeSmall;
-        styleOption.extendedSelectionRegion = true;
-        view->setStyleOption(styleOption);
-
-        const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height());
-        view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.padding));
-        view->setItemLayout(KFileItemListView::DetailsLayout);
+        view->setWidgetCreator(new KItemListWidgetCreator<FoldersItemListWidget>());
         view->setSupportsItemExpanding(true);
         // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
         // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
         // opening the folders panel.
         view->setOpacity(0);
 
-        KFileItemModel* model = new KFileItemModel(this);
-        model->setShowFoldersOnly(true);
-        model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
+        connect(view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
+                this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
+
+        m_model = new KFileItemModel(this);
+        m_model->setShowDirectoriesOnly(true);
+        m_model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
         // Use a QueuedConnection to give the view the possibility to react first on the
         // finished loading.
-        connect(model, SIGNAL(dirLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
+        connect(m_model, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
 
-        KItemListContainer* container = new KItemListContainer(this);
-        m_controller = container->controller();
-        m_controller->setView(view);
-        m_controller->setModel(model);
+        m_controller = new KItemListController(m_model, view, this);
         m_controller->setSelectionBehavior(KItemListController::SingleSelection);
+        m_controller->setAutoActivationBehavior(KItemListController::ExpansionOnly);
+        m_controller->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem);
         m_controller->setAutoActivationDelay(750);
         m_controller->setSingleClickActivation(true);
 
@@ -168,21 +160,8 @@ void FoldersPanel::showEvent(QShowEvent* event)
         connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
         connect(m_controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
 
-        // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
-        // to make the background transparent.
-        container->setFrameShape(QFrame::NoFrame);
-        QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(container->viewport());
-        if (graphicsView) {
-            // Make the background of the container transparent and apply the window-text color
-            // to the text color, so that enough contrast is given for all color
-            // schemes
-            QPalette p = graphicsView->palette();
-            p.setColor(QPalette::Active,   QPalette::Text, p.color(QPalette::Active,   QPalette::WindowText));
-            p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
-            p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
-            graphicsView->setPalette(p);
-            graphicsView->viewport()->setAutoFillBackground(false);
-        }
+        KItemListContainer* container = new KItemListContainer(m_controller, this);
+        container->setEnabledFrame(false);
 
         QVBoxLayout* layout = new QVBoxLayout(this);
         layout->setMargin(0);
@@ -205,17 +184,17 @@ void FoldersPanel::keyPressEvent(QKeyEvent* event)
 
 void FoldersPanel::slotItemActivated(int index)
 {
-    const KFileItem item = fileItemModel()->fileItem(index);
+    const KFileItem item = m_model->fileItem(index);
     if (!item.isNull()) {
-        emit changeUrl(item.url(), Qt::LeftButton);
+        emit folderActivated(item.url());
     }
 }
 
 void FoldersPanel::slotItemMiddleClicked(int index)
 {
-    const KFileItem item = fileItemModel()->fileItem(index);
+    const KFileItem item = m_model->fileItem(index);
     if (!item.isNull()) {
-        emit changeUrl(item.url(), Qt::MiddleButton);
+        emit folderMiddleClicked(item.url());
     }
 }
 
@@ -223,7 +202,7 @@ void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
 {
     Q_UNUSED(pos);
 
-    const KFileItem fileItem = fileItemModel()->fileItem(index);
+    const KFileItem fileItem = m_model->fileItem(index);
 
     QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
     contextMenu.data()->open();
@@ -246,7 +225,7 @@ void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
 {
     if (index >= 0) {
-        KFileItem destItem = fileItemModel()->fileItem(index);
+        KFileItem destItem = m_model->fileItem(index);
         if (destItem.isNull()) {
             return;
         }
@@ -257,7 +236,21 @@ void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* eve
                              event->buttons(),
                              event->modifiers());
 
-        DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent);
+        const QString error = DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent);
+        if (!error.isEmpty()) {
+            emit errorMessage(error);
+        }
+    }
+}
+
+void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
+{
+    if (role == "text") {
+        const KFileItem item = 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);
+        }
     }
 }
 
@@ -277,7 +270,7 @@ void FoldersPanel::slotLoadingCompleted()
         return;
     }
 
-    const int index = fileItemModel()->index(url());
+    const int index = m_model->index(url());
     updateCurrentItem(index);
     m_updateCurrentItem = false;
 }
@@ -308,18 +301,17 @@ void FoldersPanel::loadTree(const KUrl& url)
         baseUrl.setPath(QString('/'));
     }
 
-    KFileItemModel* model = fileItemModel();
-    if (model->dir() != baseUrl) {
+    if (m_model->directory() != baseUrl) {
         m_updateCurrentItem = true;
-        model->refreshDir(baseUrl);
+        m_model->refreshDirectory(baseUrl);
     }
 
-    const int index = model->index(url);
+    const int index = m_model->index(url);
     if (index >= 0) {
         updateCurrentItem(index);
     } else {
         m_updateCurrentItem = true;
-        model->expandParentItems(url);
+        m_model->expandParentDirectories(url);
         // slotLoadingCompleted() will be invoked after the model has
         // expanded the url
     }
@@ -335,9 +327,4 @@ void FoldersPanel::updateCurrentItem(int index)
     m_controller->view()->scrollToItem(index);
 }
 
-KFileItemModel* FoldersPanel::fileItemModel() const
-{
-    return static_cast<KFileItemModel*>(m_controller->model());
-}
-
 #include "folderspanel.moc"