]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphiniconsview.cpp
Get back names, and use "using" keyword to keep GCC silent on "method foo on base...
[dolphin.git] / src / dolphiniconsview.cpp
index 55d70a0a22172862e4487124577656c3ee800099..46c27cd28f6cb0f78036b2e9fa5e94bf52e6d53d 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at)                  *
+ *   Copyright (C) 2006-2009 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 "dolphincategorydrawer.h"
 #include "dolphincontroller.h"
 #include "settings/dolphinsettings.h"
-#include "dolphinviewautoscroller.h"
+#include "dolphinsortfilterproxymodel.h"
 #include "dolphin_iconsmodesettings.h"
 #include "dolphin_generalsettings.h"
 #include "draganddrophelper.h"
 #include "selectionmanager.h"
+#include "viewextensionsfactory.h"
 #include "zoomlevelinfo.h"
 
 #include <kcategorizedsortfilterproxymodel.h>
 #include <kdialog.h>
-#include <kdirmodel.h>
 #include <kfileitemdelegate.h>
 
 #include <QAbstractProxyModel>
 #include <QApplication>
 #include <QScrollBar>
 
-DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
+DolphinIconsView::DolphinIconsView(QWidget* parent,
+                                   DolphinController* controller,
+                                   DolphinSortFilterProxyModel* proxyModel) :
     KCategorizedView(parent),
     m_controller(controller),
-    m_selectionManager(0),
-    m_autoScroller(0),
-    m_categoryDrawer(0),
+    m_categoryDrawer(new DolphinCategoryDrawer(this)),
+    m_extensionsFactory(0),
     m_font(),
     m_decorationSize(),
     m_decorationPosition(QStyleOptionViewItem::Top),
@@ -52,6 +53,7 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
     m_dropRect()
 {
     Q_ASSERT(controller != 0);
+    setModel(proxyModel);
     setLayoutDirection(Qt::LeftToRight);
     setViewMode(QListView::IconMode);
     setResizeMode(QListView::Adjust);
@@ -61,7 +63,6 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
     viewport()->setAcceptDrops(true);
 
     setMouseTracking(true);
-    m_autoScroller = new DolphinViewAutoScroller(this);
 
     connect(this, SIGNAL(clicked(const QModelIndex&)),
             controller, SLOT(requestTab(const QModelIndex&)));
@@ -73,14 +74,6 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
                 controller, SLOT(triggerItem(const QModelIndex&)));
     }
 
-    if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
-        m_selectionManager = new SelectionManager(this);
-        connect(m_selectionManager, SIGNAL(selectionChanged()),
-                this, SLOT(requestActivation()));
-        connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
-                m_selectionManager, SLOT(reset()));
-    }
-
     connect(this, SIGNAL(entered(const QModelIndex&)),
             controller, SLOT(emitItemEntered(const QModelIndex&)));
     connect(this, SIGNAL(viewportEntered()),
@@ -102,13 +95,13 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
         m_font = KGlobalSettings::generalFont();
     } else {
         m_font = QFont(settings->fontFamily(),
-                       settings->fontSize(),
+                       qRound(settings->fontSize()),
                        settings->fontWeight(),
                        settings->italicFont());
+        m_font.setPointSizeF(settings->fontSize());
     }
 
     setWordWrap(settings->numberOfTextlines() > 1);
-    updateGridSize(view->showPreview(), 0);
 
     if (settings->arrangement() == QListView::TopToBottom) {
         setFlow(QListView::LeftToRight);
@@ -120,19 +113,20 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
         m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
     }
 
-    m_categoryDrawer = new DolphinCategoryDrawer();
+    connect(m_categoryDrawer, SIGNAL(actionRequested(int,QModelIndex)), this, SLOT(categoryDrawerActionRequested(int,QModelIndex)));
     setCategoryDrawer(m_categoryDrawer);
 
     setFocus();
 
     connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
             this, SLOT(slotGlobalSettingsChanged(int)));
+
+    updateGridSize(view->showPreview(), 0);
+    m_extensionsFactory = new ViewExtensionsFactory(this, controller);
 }
 
 DolphinIconsView::~DolphinIconsView()
 {
-    delete m_categoryDrawer;
-    m_categoryDrawer = 0;
 }
 
 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
@@ -151,6 +145,7 @@ QStyleOptionViewItem DolphinIconsView::viewOptions() const
 {
     QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
     viewOptions.font = m_font;
+    viewOptions.fontMetrics = QFontMetrics(m_font);
     viewOptions.decorationPosition = m_decorationPosition;
     viewOptions.decorationSize = m_decorationSize;
     viewOptions.displayAlignment = m_displayAlignment;
@@ -177,14 +172,8 @@ void DolphinIconsView::mousePressEvent(QMouseEvent* event)
         setState(QAbstractItemView::DraggingState);
     }
 
-    if (!index.isValid()) {
-        if (QApplication::mouseButtons() & Qt::MidButton) {
-            m_controller->replaceUrlByClipboard();
-        }
-        const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
-        if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
-            clearSelection();
-        }
+    if (!index.isValid() && (QApplication::mouseButtons() & Qt::MidButton)) {
+         m_controller->replaceUrlByClipboard();
     }
 
     KCategorizedView::mousePressEvent(event);
@@ -192,9 +181,6 @@ void DolphinIconsView::mousePressEvent(QMouseEvent* event)
 
 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
 {
-    // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
-    // fix this in KDE 4.1
-    KCategorizedView::startDrag(supportedActions);
     DragAndDropHelper::instance().startDrag(this, supportedActions, m_controller);
 }
 
@@ -241,16 +227,16 @@ void DolphinIconsView::dropEvent(QDropEvent* event)
     const QModelIndex index = indexAt(event->pos());
     const KFileItem item = m_controller->itemForIndex(index);
     m_controller->indicateDroppedUrls(item, m_controller->url(), event);
-
-    KCategorizedView::dropEvent(event);
+    // don't call KCategorizedView::dropEvent(event), as it moves
+    // the items which is not wanted
 }
 
 QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
 {
-    QModelIndex current = currentIndex();
+    const QModelIndex oldCurrent = currentIndex();
 
-    QModelIndex newCurrent = QListView::moveCursor(cursorAction, modifiers);
-    if (newCurrent != current) {
+    QModelIndex newCurrent = KCategorizedView::moveCursor(cursorAction, modifiers);
+    if (newCurrent != oldCurrent) {
         return newCurrent;
     }
 
@@ -264,44 +250,36 @@ QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::Keyboard
             if (newCurrent.row() == 0) {
                 return newCurrent;
             }
-            newCurrent = QListView::moveCursor(MoveLeft, modifiers);
+            newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
             selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
-            newCurrent = QListView::moveCursor(MovePageDown, modifiers);
+            newCurrent = KCategorizedView::moveCursor(MovePageDown, modifiers);
             break;
 
         case MoveDown:
             if (newCurrent.row() == (model()->rowCount() - 1)) {
                 return newCurrent;
             }
-            newCurrent = QListView::moveCursor(MovePageUp, modifiers);
-            selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::Clear);
-            newCurrent = QListView::moveCursor(MoveRight, modifiers);
+            newCurrent = KCategorizedView::moveCursor(MovePageUp, modifiers);
+            selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
+            newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
             break;
 
         default:
             break;
         }
     } else {
+        QModelIndex current = oldCurrent;
         switch (cursorAction) {
         case MoveLeft:
             if (newCurrent.row() == 0) {
                 return newCurrent;
             }
-            newCurrent = QListView::moveCursor(MoveUp, modifiers);
+            newCurrent = KCategorizedView::moveCursor(MoveUp, modifiers);
             do {
                 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
                 current = newCurrent;
-                newCurrent = QListView::moveCursor(MoveRight, modifiers);
+                newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
             } while (newCurrent != current);
-
-            if (!(modifiers & Qt::ControlModifier)) {
-                // Ctrl is not pressed -> selection is updated
-                if (!(modifiers & Qt::ShiftModifier)) {
-                    // Shift is not pressed -> previous selection is lost
-                    selectionModel()->clearSelection();
-                }
-                selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::Select);
-            }
             break;
 
         case MoveRight:
@@ -311,9 +289,9 @@ QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::Keyboard
             do {
                 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
                 current = newCurrent;
-                newCurrent = QListView::moveCursor(MoveLeft, modifiers);
+                newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
             } while (newCurrent != current);
-            newCurrent = QListView::moveCursor(MoveDown, modifiers);
+            newCurrent = KCategorizedView::moveCursor(MoveDown, modifiers);
             break;
 
         default:
@@ -321,6 +299,8 @@ QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::Keyboard
         }
     }
 
+    // Revert all changes of the current item to make sure that item selection works correctly
+    selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
     return newCurrent;
 }
 
@@ -332,18 +312,8 @@ void DolphinIconsView::keyPressEvent(QKeyEvent* event)
 
 void DolphinIconsView::wheelEvent(QWheelEvent* event)
 {
-    if (m_selectionManager != 0) {
-        m_selectionManager->reset();
-    }
-
-    // let Ctrl+wheel events propagate to the DolphinView for icon zooming
-    if (event->modifiers() & Qt::ControlModifier) {
-        event->ignore();
-        return;
-    }
-
-    horizontalScrollBar()->setSingleStep(m_itemSize.width() / 10);
-    verticalScrollBar()->setSingleStep(m_itemSize.height() / 10);
+    horizontalScrollBar()->setSingleStep(m_itemSize.width() / 5);
+    verticalScrollBar()->setSingleStep(m_itemSize.height() / 5);
 
     KCategorizedView::wheelEvent(event);
     // if the icons are aligned left to right, the vertical wheel event should
@@ -381,7 +351,7 @@ void DolphinIconsView::leaveEvent(QEvent* event)
 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
 {
     KCategorizedView::currentChanged(current, previous);
-    m_autoScroller->handleCurrentIndexChange(current, previous);
+    m_extensionsFactory->handleCurrentIndexChange(current, previous);
 }
 
 void DolphinIconsView::resizeEvent(QResizeEvent* event)
@@ -452,6 +422,33 @@ void DolphinIconsView::slotGlobalSettingsChanged(int category)
     }
 }
 
+void DolphinIconsView::categoryDrawerActionRequested(int action, const QModelIndex &index)
+{
+    const QSortFilterProxyModel *model = dynamic_cast<const QSortFilterProxyModel*>(index.model());
+    const QModelIndex topLeft = model->index(index.row(), modelColumn());
+    QModelIndex bottomRight = topLeft;
+    const QString category = model->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
+    QModelIndex current = topLeft;
+    while (true) {
+        current = model->index(current.row() + 1, modelColumn());
+        const QString curCategory = model->data(model->index(current.row(), index.column()), KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
+        if (!current.isValid() || category != curCategory) {
+            break;
+        }
+        bottomRight = current;
+    }
+    switch (action) {
+        case DolphinCategoryDrawer::SelectAll:
+            selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Select);
+            break;
+        case DolphinCategoryDrawer::UnselectAll:
+            selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Deselect);
+            break;
+        default:
+            break;
+    }
+}
+
 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
 {
     const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
@@ -471,7 +468,7 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
     }
 
     Q_ASSERT(additionalInfoCount >= 0);
-    itemHeight += additionalInfoCount * m_font.pointSize() * 2;
+    itemHeight += additionalInfoCount * QFontMetrics(m_font).height();
 
     // Optimize the item size of the grid in a way to prevent large gaps on the
     // right border (= row arrangement) or the bottom border (= column arrangement).
@@ -513,16 +510,12 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
     }
 
     m_itemSize = QSize(itemWidth, itemHeight);
-    setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
+    setGridSizeOwn(QSize(itemWidth + spacing * 2, itemHeight + spacing));
 
     KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
     if (delegate != 0) {
         delegate->setMaximumSize(m_itemSize);
     }
-
-    if (m_selectionManager != 0) {
-        m_selectionManager->reset();
-    }
 }
 
 int DolphinIconsView::additionalInfoCount() const