]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/folders/folderspanel.cpp
Fix style-issues in items when not using Oxygen
[dolphin.git] / src / panels / folders / folderspanel.cpp
index cf458e5089f3dc2a85ffdea1848abd849f78eca4..bb2198d487ea0ee4d2697cfdfda19955527ffd9b 100644 (file)
 
 #include <QApplication>
 #include <QBoxLayout>
+#include <QDropEvent>
+#include <QGraphicsSceneDragDropEvent>
 #include <QGraphicsView>
+#include <QPropertyAnimation>
 #include <QTimer>
 
+#include <views/draganddrophelper.h>
 #include <views/renamedialog.h>
 
 #include <KDebug>
 
 FoldersPanel::FoldersPanel(QWidget* parent) :
     Panel(parent),
-    m_setLeafVisible(false),
-    m_mouseButtons(Qt::NoButton),
+    m_updateCurrentItem(false),
     m_dirLister(0),
-    m_controller(0),
-    m_leafDir()
+    m_controller(0)
 {
     setLayoutDirection(Qt::LeftToRight);
 }
@@ -74,8 +76,7 @@ void FoldersPanel::setHiddenFilesShown(bool show)
     if (m_dirLister) {
         KFileItemModel* model = fileItemModel();
         const QSet<KUrl> expandedUrls = model->expandedUrls();
-        m_dirLister->setShowingDotFiles(show);
-        m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
+        model->setShowHiddenFiles(show);
         model->setExpanded(expandedUrls);
     }
 }
@@ -87,7 +88,7 @@ bool FoldersPanel::hiddenFilesShown() const
 
 void FoldersPanel::setAutoScrolling(bool enable)
 {
-    //m_treeView->setAutoHorizontalScroll(enable);
+    // TODO: Not supported yet in Dolphin 2.0
     FoldersPanelSettings::setAutoScrolling(enable);
 }
 
@@ -121,7 +122,6 @@ bool FoldersPanel::urlChanged()
     }
 
     if (m_dirLister) {
-        m_setLeafVisible = true;
         loadTree(url());
     }
 
@@ -145,7 +145,6 @@ void FoldersPanel::showEvent(QShowEvent* event)
         m_dirLister->setMainWindow(window());
         m_dirLister->setDelayedMimeTypes(true);
         m_dirLister->setAutoErrorHandlingEnabled(false, this);
-        m_dirLister->setShowingDotFiles(FoldersPanelSettings::hiddenFilesShown());
 
         KFileItemListView* view  = new KFileItemListView();
         view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
@@ -158,8 +157,13 @@ void FoldersPanel::showEvent(QShowEvent* event)
         const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height());
         view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.margin));
         view->setItemLayout(KFileItemListView::DetailsLayout);
+        // 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(m_dirLister, this);
+        model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
         // Use a QueuedConnection to give the view the possibility to react first on the
         // finished loading.
         connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
@@ -168,6 +172,14 @@ void FoldersPanel::showEvent(QShowEvent* event)
         m_controller = container->controller();
         m_controller->setView(view);
         m_controller->setModel(model);
+        m_controller->setSelectionBehavior(KItemListController::SingleSelection);
+        m_controller->setAutoActivationDelay(750);
+
+        connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
+        connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
+        connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
+        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.
@@ -194,66 +206,115 @@ void FoldersPanel::showEvent(QShowEvent* event)
     Panel::showEvent(event);
 }
 
-void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
-{
-    Panel::contextMenuEvent(event);
-
-    KFileItem item;
-    /*const QModelIndex index = m_treeView->indexAt(event->pos());
-    if (index.isValid()) {
-        const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
-        item = m_dolphinModel->itemForIndex(dolphinModelIndex);
-    }*/
-
-    QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, item);
-    contextMenu->open();
-    delete contextMenu;
-}
-
 void FoldersPanel::keyPressEvent(QKeyEvent* event)
 {
     const int key = event->key();
     if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
         event->accept();
-        //updateActiveView(m_treeView->currentIndex());
     } else {
         Panel::keyPressEvent(event);
     }
 }
 
-void FoldersPanel::updateMouseButtons()
+void FoldersPanel::slotItemActivated(int index)
 {
-    m_mouseButtons = QApplication::mouseButtons();
+    const KFileItem item = fileItemModel()->fileItem(index);
+    if (!item.isNull()) {
+        emit changeUrl(item.url(), Qt::LeftButton);
+    }
 }
 
-void FoldersPanel::slotLoadingCompleted()
+void FoldersPanel::slotItemMiddleClicked(int index)
+{
+    const KFileItem item = fileItemModel()->fileItem(index);
+    if (!item.isNull()) {
+        emit changeUrl(item.url(), Qt::MiddleButton);
+    }
+}
+
+void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
+{
+    Q_UNUSED(pos);
+
+    const KFileItem fileItem = fileItemModel()->fileItem(index);
+
+    QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
+    contextMenu.data()->open();
+    if (contextMenu.data()) {
+        delete contextMenu.data();
+    }
+}
+
+void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
+{
+    Q_UNUSED(pos);
+
+    QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
+    contextMenu.data()->open();
+    if (contextMenu.data()) {
+        delete contextMenu.data();
+    }
+}
+
+void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
 {
-    const int index = fileItemModel()->index(url());
     if (index >= 0) {
-        m_controller->selectionManager()->setCurrentItem(index);
+        KFileItemModel* model = fileItemModel();
+        KFileItem destItem = model->fileItem(index);
+        if (destItem.isNull()) {
+            destItem = model->rootItem();
+            if (destItem.isNull()) {
+                kWarning() << "No destination item available for drop operation.";
+                return;
+            }
+        }
+
+        QDropEvent dropEvent(event->pos().toPoint(),
+                             event->possibleActions(),
+                             event->mimeData(),
+                             event->buttons(),
+                             event->modifiers());
+
+        DragAndDropHelper::dropUrls(destItem, &dropEvent);
     }
 }
 
-void FoldersPanel::slotHorizontalScrollBarMoved(int value)
+void FoldersPanel::slotLoadingCompleted()
 {
-    Q_UNUSED(value);
-    // Disable the auto-scrolling until the vertical scrollbar has
-    // been moved by the user.
-    //m_treeView->setAutoHorizontalScroll(false);
+    if (m_controller->view()->opacity() == 0) {
+        // The loading of the initial tree after opening the Folders panel
+        // has been finished. Trigger the increasing of the opacity after
+        // a short delay to give the view the chance to finish its internal
+        // animations.
+        // TODO: Check whether it makes sense to allow accessing the
+        // view-internal delay for usecases like this.
+        QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
+    }
+
+    if (!m_updateCurrentItem) {
+        return;
+    }
+
+    const int index = fileItemModel()->index(url());
+    updateCurrentItem(index);
+    m_updateCurrentItem = false;
 }
 
-void FoldersPanel::slotVerticalScrollBarMoved(int value)
+void FoldersPanel::startFadeInAnimation()
 {
-    Q_UNUSED(value);
-    // Enable the auto-scrolling again (it might have been disabled by
-    // moving the horizontal scrollbar).
-    //m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
+    QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
+    anim->setStartValue(0);
+    anim->setEndValue(1);
+    anim->setEasingCurve(QEasingCurve::InOutQuad);
+    anim->start(QAbstractAnimation::DeleteWhenStopped);
+    anim->setDuration(200);
 }
 
 void FoldersPanel::loadTree(const KUrl& url)
 {
     Q_ASSERT(m_dirLister);
-    m_leafDir = url;
+
+    m_updateCurrentItem = false;
 
     KUrl baseUrl;
     if (url.isLocalFile()) {
@@ -266,6 +327,7 @@ void FoldersPanel::loadTree(const KUrl& url)
     }
 
     if (m_dirLister->url() != baseUrl) {
+        m_updateCurrentItem = true;
         m_dirLister->stop();
         m_dirLister->openUrl(baseUrl, KDirLister::Reload);
     }
@@ -273,29 +335,23 @@ void FoldersPanel::loadTree(const KUrl& url)
     KFileItemModel* model = fileItemModel();
     const int index = model->index(url);
     if (index >= 0) {
-        m_controller->selectionManager()->setCurrentItem(index);
+        updateCurrentItem(index);
     } else {
+        m_updateCurrentItem = true;
         model->setExpanded(QSet<KUrl>() << url);
+        // slotLoadingCompleted() will be invoked after the model has
+        // expanded the url
     }
 }
 
-void FoldersPanel::selectLeafDirectory()
+void FoldersPanel::updateCurrentItem(int index)
 {
-    /*const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
-    const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
-
-    if (proxyIndex.isValid()) {
-        QItemSelectionModel* selModel = m_treeView->selectionModel();
-        selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
-
-        if (m_setLeafVisible) {
-            // Invoke scrollToLeaf() asynchronously. This assures that
-            // the horizontal scrollbar is shown after resizing the column
-            // (otherwise the scrollbar might hide the leaf).
-            QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
-            m_setLeafVisible = false;
-        }
-    }*/
+    KItemListSelectionManager* selectionManager = m_controller->selectionManager();
+    selectionManager->setCurrentItem(index);
+    selectionManager->clearSelection();
+    selectionManager->setSelected(index);
+
+    m_controller->view()->scrollToItem(index);
 }
 
 KFileItemModel* FoldersPanel::fileItemModel() const