]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinview.cpp
Let the user choose if folders are always shown first in the views of
[dolphin.git] / src / dolphinview.cpp
index 8b914de4079819ab9cdcdf265526cd8c5153f3bd..f2f23d27301da839a03cb323993ccf2cfaba164d 100644 (file)
 #include "dolphinmodel.h"
 #include "dolphincolumnview.h"
 #include "dolphincontroller.h"
+#include "dolphindetailsview.h"
 #include "dolphinfileitemdelegate.h"
+#include "dolphinnewmenuobserver.h"
 #include "dolphinsortfilterproxymodel.h"
-#include "dolphindetailsview.h"
 #include "dolphin_detailsmodesettings.h"
 #include "dolphiniconsview.h"
-#include "settings/dolphinsettings.h"
 #include "dolphin_generalsettings.h"
 #include "draganddrophelper.h"
 #include "folderexpander.h"
 #include "renamedialog.h"
 #include "tooltips/tooltipmanager.h"
+#include "settings/dolphinsettings.h"
 #include "viewproperties.h"
 #include "zoomlevelinfo.h"
 
@@ -104,6 +105,8 @@ DolphinView::DolphinView(QWidget* parent,
     m_toolTipManager(0),
     m_rootUrl(),
     m_currentItemUrl(),
+    m_createdItemUrl(),
+    m_selectedItems(),
     m_expandedDragSource(0)
 {
     m_topLayout = new QVBoxLayout(this);
@@ -126,6 +129,8 @@ DolphinView::DolphinView(QWidget* parent,
             this, SLOT(updateSorting(DolphinView::Sorting)));
     connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
             this, SLOT(updateSortOrder(Qt::SortOrder)));
+    connect(m_controller, SIGNAL(sortFoldersFirstChanged(bool)),
+            this, SLOT(updateSortFoldersFirst(bool)));
     connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
             this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
     connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
@@ -146,6 +151,12 @@ DolphinView::DolphinView(QWidget* parent,
     connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
             this, SLOT(slotRefreshItems()));
 
+    // When a new item has been created by the "Create New..." menu, the item should
+    // get selected and it must be assured that the item will get visible. As the
+    // creation is done asynchronously, several signals must be checked:
+    connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl&)),
+            this, SLOT(observeCreatedItem(const KUrl&)));
+
     applyViewProperties(url);
     m_topLayout->addWidget(itemView());
 }
@@ -400,7 +411,7 @@ void DolphinView::setZoomLevel(int level)
 
     if (level != zoomLevel()) {
         m_controller->setZoomLevel(level);
-        m_previewGenerator->updatePreviews();
+        m_previewGenerator->updateIcons();
         emit zoomLevelChanged(level);
     }
 }
@@ -434,6 +445,18 @@ Qt::SortOrder DolphinView::sortOrder() const
     return m_proxyModel->sortOrder();
 }
 
+void DolphinView::setSortFoldersFirst(bool foldersFirst)
+{
+    if (sortFoldersFirst() != foldersFirst) {
+        updateSortFoldersFirst(foldersFirst);
+    }
+}
+
+bool DolphinView::sortFoldersFirst() const
+{
+    return m_proxyModel->sortFoldersFirst();
+}
+
 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
 {
     const KUrl viewPropsUrl = viewPropertiesUrl();
@@ -600,15 +623,15 @@ void DolphinView::changeSelection(const KFileItemList& selection)
     }
     const KUrl& baseUrl = url();
     KUrl url;
-    QItemSelection new_selection;
+    QItemSelection newSelection;
     foreach(const KFileItem& item, selection) {
         url = item.url().upUrl();
         if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
             QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
-            new_selection.select(index, index);
+            newSelection.select(index, index);
         }
     }
-    itemView()->selectionModel()->select(new_selection,
+    itemView()->selectionModel()->select(newSelection,
                                          QItemSelectionModel::ClearAndSelect
                                          | QItemSelectionModel::Current);
 }
@@ -809,6 +832,11 @@ void DolphinView::toggleSortOrder()
     setSortOrder(order);
 }
 
+void DolphinView::toggleSortFoldersFirst()
+{
+    setSortFoldersFirst(!sortFoldersFirst());
+}
+
 void DolphinView::toggleAdditionalInfo(QAction* action)
 {
     const KFileItemDelegate::Information info =
@@ -978,6 +1006,16 @@ void DolphinView::updateSortOrder(Qt::SortOrder order)
     emit sortOrderChanged(order);
 }
 
+void DolphinView::updateSortFoldersFirst(bool foldersFirst)
+{
+    ViewProperties props(viewPropertiesUrl());
+    props.setSortFoldersFirst(foldersFirst);
+
+    m_proxyModel->setSortFoldersFirst(foldersFirst);
+
+    emit sortFoldersFirstChanged(foldersFirst);
+}
+
 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
 {
     ViewProperties props(viewPropertiesUrl());
@@ -1081,6 +1119,32 @@ void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view)
     }
 }
 
+void DolphinView::observeCreatedItem(const KUrl& url)
+{
+    m_createdItemUrl = url;
+    connect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+            this, SLOT(selectAndScrollToCreatedItem()));
+}
+
+void DolphinView::selectAndScrollToCreatedItem()
+{
+    const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_createdItemUrl);
+    if (dirIndex.isValid()) {
+        const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
+        itemView()->setCurrentIndex(proxyIndex);
+    }
+
+    disconnect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+               this, SLOT(selectAndScrollToCreatedItem()));
+    m_createdItemUrl = KUrl();
+}
+
+void DolphinView::restoreSelection()
+{
+    disconnect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
+    changeSelection(m_selectedItems);
+}
+
 void DolphinView::emitContentsMoved()
 {
     // only emit the contents moved signal if:
@@ -1121,15 +1185,18 @@ void DolphinView::slotRequestUrlChange(const KUrl& url)
 
 void DolphinView::restoreCurrentItem()
 {
-    const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
-    if (dirIndex.isValid()) {
-        const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
-        QAbstractItemView* view = itemView();
-        const bool clearSelection = !hasSelection();
-        view->setCurrentIndex(proxyIndex);
-        if (clearSelection) {
-            view->clearSelection();
+    if (!m_currentItemUrl.isEmpty()) {
+        const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
+        if (dirIndex.isValid()) {
+            const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
+            QAbstractItemView* view = itemView();
+            const bool clearSelection = !hasSelection();
+            view->setCurrentIndex(proxyIndex);
+            if (clearSelection) {
+                view->clearSelection();
+            }
         }
+        m_currentItemUrl.clear();
     }
 }
 
@@ -1155,6 +1222,11 @@ void DolphinView::loadDirectory(const KUrl& url, bool reload)
 
     m_loadingDirectory = true;
 
+    if (reload) {
+        m_selectedItems = selectedItems();
+        connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
+    }
+
     m_dirLister->stop();
     m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
 
@@ -1234,6 +1306,12 @@ void DolphinView::applyViewProperties(const KUrl& url)
         emit sortOrderChanged(sortOrder);
     }
 
+    const bool sortFoldersFirst = props.sortFoldersFirst();
+    if (sortFoldersFirst != m_proxyModel->sortFoldersFirst()) {
+        m_proxyModel->setSortFoldersFirst(sortFoldersFirst);
+        emit sortFoldersFirstChanged(sortFoldersFirst);
+    }
+
     KFileItemDelegate::InformationList info = props.additionalInfo();
     if (info != m_fileItemDelegate->showInformation()) {
         m_fileItemDelegate->setShowInformation(info);
@@ -1449,4 +1527,5 @@ QMimeData* DolphinView::selectionMimeData() const
     return m_dolphinModel->mimeData(selection.indexes());
 }
 
+
 #include "dolphinview.moc"