]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphiniconsview.cpp
For the URL control of Dolphin and Konqueror to be LTR on RTL desktops (those are...
[dolphin.git] / src / dolphiniconsview.cpp
index 45fbf25b2888f80b83bbadaf020f08f299721df1..db916747e9dbbe89207d7bf9db5f183549e66bf7 100644 (file)
 #include <kcategorizedsortfilterproxymodel.h>
 #include <kdialog.h>
 #include <kdirmodel.h>
+#include <kfileitemdelegate.h>
 
 #include <QAbstractProxyModel>
 #include <QApplication>
-#include <QPainter>
-#include <QPoint>
 #include <QScrollBar>
 
 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
     KCategorizedView(parent),
+    m_enableScrollTo(false),
     m_controller(controller),
+    m_selectionManager(0),
     m_categoryDrawer(0),
     m_font(),
     m_decorationSize(),
@@ -49,11 +50,13 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
     m_dropRect()
 {
     Q_ASSERT(controller != 0);
+    setLayoutDirection(Qt::LeftToRight);
     setViewMode(QListView::IconMode);
     setResizeMode(QListView::Adjust);
     setSpacing(KDialog::spacingHint());
     setMovement(QListView::Static);
     setDragEnabled(true);
+    setEditTriggers(QAbstractItemView::NoEditTriggers);
     viewport()->setAcceptDrops(true);
 
     setMouseTracking(true);
@@ -65,25 +68,25 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
     if (KGlobalSettings::singleClick()) {
         connect(this, SIGNAL(clicked(const QModelIndex&)),
                 controller, SLOT(triggerItem(const QModelIndex&)));
-        if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
-            SelectionManager* selManager = new SelectionManager(this);
-            connect(selManager, SIGNAL(selectionChanged()),
-                    this, SLOT(requestActivation()));
-            connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
-                    selManager, SLOT(reset()));
-        }
     } else {
         connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
                 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()),
             controller, SLOT(emitViewportEntered()));
-    connect(controller, SIGNAL(zoomIn()),
-            this, SLOT(zoomIn()));
-    connect(controller, SIGNAL(zoomOut()),
-            this, SLOT(zoomOut()));
+    connect(controller, SIGNAL(zoomLevelChanged(int)),
+            this, SLOT(setZoomLevel(int)));
 
     const DolphinView* view = controller->dolphinView();
     connect(view, SIGNAL(showPreviewChanged()),
@@ -132,48 +135,29 @@ DolphinIconsView::~DolphinIconsView()
     m_categoryDrawer = 0;
 }
 
-QRect DolphinIconsView::visualRect(const QModelIndex& index) const
+void DolphinIconsView::scrollTo(const QModelIndex& index, ScrollHint hint)
 {
-    const bool leftToRightFlow = (flow() == QListView::LeftToRight);
-
-    QRect itemRect = KCategorizedView::visualRect(index);
-
-    const int maxWidth  = m_itemSize.width();
-    const int maxHeight = m_itemSize.height();
-
-    if (itemRect.width() > maxWidth) {
-        // assure that the maximum item width is not exceeded
-        if (leftToRightFlow) {
-            const int left = itemRect.left() + (itemRect.width() - maxWidth) / 2;
-            itemRect.setLeft(left);
-        }
-        itemRect.setWidth(maxWidth);
+    // Enable the QListView implementation of scrollTo() only if it has been
+    // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
+    // index each time the layout has been changed. This becomes an issue when
+    // previews are loaded and the scrollbar is used: the scrollbar will always
+    // be reset to 0 on each new preview.
+    if (m_enableScrollTo || (state() != QAbstractItemView::NoState)) {
+        KCategorizedView::scrollTo(index, hint);
+        m_enableScrollTo = false;
     }
+}
 
-    if (itemRect.height() > maxHeight) {
-        // assure that the maximum item height is not exceeded
-        if (!leftToRightFlow) {
-            const int top = itemRect.top() + (itemRect.height() - maxHeight) / 2;
-            itemRect.setTop(top);
-        }
-        itemRect.setHeight(maxHeight);
-    }
+void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
+{
+    KCategorizedView::dataChanged(topLeft, bottomRight);
 
     KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
-    if (leftToRightFlow && !proxyModel->isCategorizedModel()) {
-        // TODO: QListView::visualRect() calculates a wrong position of the items under
-        // certain circumstances (e. g. if the text is too long). This issue is bypassed
-        // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
-        // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
-        const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
-        const int margin = settings->gridSpacing();
-        const int gridWidth = gridSize().width();
-        const int gridIndex = (itemRect.left() - margin + 1) / gridWidth;
-        const int centerInc = (maxWidth - itemRect.width()) / 2;
-        itemRect.moveLeft((gridIndex * gridWidth) + margin + centerInc);
+    if ((flow() == QListView::LeftToRight) && !proxyModel->isCategorizedModel()) {
+        // bypass a QListView issue that items are not layout correctly if the decoration size of
+        // an index changes
+        scheduleDelayedItemsLayout();
     }
-
-    return itemRect;
 }
 
 QStyleOptionViewItem DolphinIconsView::viewOptions() const
@@ -196,7 +180,20 @@ void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
 {
     m_controller->requestActivation();
-    if (!indexAt(event->pos()).isValid()) {
+    const QModelIndex index = indexAt(event->pos());
+    if (index.isValid() && (event->button() == Qt::LeftButton)) {
+        // TODO: It should not be necessary to manually set the dragging state, but I could
+        // not reproduce this issue with a Qt-only example yet to find the root cause.
+        // Issue description: start Dolphin, split the view and drag an item from the
+        // inactive view to the active view by a very fast mouse movement. Result:
+        // the item gets selected instead of being dragged...
+        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();
@@ -273,10 +270,15 @@ void DolphinIconsView::keyPressEvent(QKeyEvent* event)
 {
     KCategorizedView::keyPressEvent(event);
     m_controller->handleKeyPressEvent(event);
+    m_enableScrollTo = true; // see DolphinIconsView::scrollTo()
 }
 
 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();
@@ -298,6 +300,23 @@ void DolphinIconsView::wheelEvent(QWheelEvent* event)
     }
 }
 
+void DolphinIconsView::showEvent(QShowEvent* event)
+{
+    KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
+    delegate->setMaximumSize(m_itemSize);
+
+    KCategorizedView::showEvent(event);
+}
+
+void DolphinIconsView::leaveEvent(QEvent* event)
+{
+    KCategorizedView::leaveEvent(event);
+    // if the mouse is above an item and moved very fast outside the widget,
+    // no viewportEntered() signal might be emitted although the mouse has been moved
+    // above the viewport
+    m_controller->emitViewportEntered();
+}
+
 void DolphinIconsView::slotShowPreviewChanged()
 {
     const DolphinView* view = m_controller->dolphinView();
@@ -311,56 +330,28 @@ void DolphinIconsView::slotAdditionalInfoChanged()
     updateGridSize(showPreview, view->additionalInfo().count());
 }
 
-void DolphinIconsView::zoomIn()
+void DolphinIconsView::setZoomLevel(int level)
 {
-    if (isZoomInPossible()) {
-        IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
-
-        const int oldIconSize = settings->iconSize();
-        int newIconSize = oldIconSize;
-
-        const bool showPreview = m_controller->dolphinView()->showPreview();
-        if (showPreview) {
-            const int previewSize = increasedIconSize(settings->previewSize());
-            settings->setPreviewSize(previewSize);
-        } else {
-            newIconSize = increasedIconSize(oldIconSize);
-            settings->setIconSize(newIconSize);
-        }
+    IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
 
-        // increase also the grid size
-        const int diff = newIconSize - oldIconSize;
-        settings->setItemWidth(settings->itemWidth() + diff);
-        settings->setItemHeight(settings->itemHeight() + diff);
+    const int oldIconSize = settings->iconSize();
+    int newIconSize = oldIconSize;
 
-        updateGridSize(showPreview, additionalInfoCount());
+    const bool showPreview = m_controller->dolphinView()->showPreview();
+    if (showPreview) {
+        const int previewSize = DolphinController::iconSizeForZoomLevel(level);
+        settings->setPreviewSize(previewSize);
+    } else {
+        newIconSize = DolphinController::iconSizeForZoomLevel(level);
+        settings->setIconSize(newIconSize);
     }
-}
-
-void DolphinIconsView::zoomOut()
-{
-    if (isZoomOutPossible()) {
-        IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
-
-        const int oldIconSize = settings->iconSize();
-        int newIconSize = oldIconSize;
-
-        const bool showPreview = m_controller->dolphinView()->showPreview();
-        if (showPreview) {
-            const int previewSize = decreasedIconSize(settings->previewSize());
-            settings->setPreviewSize(previewSize);
-        } else {
-            newIconSize = decreasedIconSize(settings->iconSize());
-            settings->setIconSize(newIconSize);
-        }
 
-        // decrease also the grid size
-        const int diff = oldIconSize - newIconSize;
-        settings->setItemWidth(settings->itemWidth() - diff);
-        settings->setItemHeight(settings->itemHeight() - diff);
+    // increase also the grid size
+    const int diff = newIconSize - oldIconSize;
+    settings->setItemWidth(settings->itemWidth() + diff);
+    settings->setItemHeight(settings->itemHeight() + diff);
 
-        updateGridSize(showPreview, additionalInfoCount());
-    }
+    updateGridSize(showPreview, additionalInfoCount());
 }
 
 void DolphinIconsView::requestActivation()
@@ -378,50 +369,6 @@ void DolphinIconsView::updateFont()
     }
 }
 
-bool DolphinIconsView::isZoomInPossible() const
-{
-    IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
-    const bool showPreview = m_controller->dolphinView()->showPreview();
-    const int size = showPreview ? settings->previewSize() : settings->iconSize();
-    return size < KIconLoader::SizeEnormous;
-}
-
-bool DolphinIconsView::isZoomOutPossible() const
-{
-    IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
-    const bool showPreview = m_controller->dolphinView()->showPreview();
-    const int size = showPreview ? settings->previewSize() : settings->iconSize();
-    return size > KIconLoader::SizeSmall;
-}
-
-int DolphinIconsView::increasedIconSize(int size) const
-{
-    int incSize = 0;
-    switch (size) {
-    case KIconLoader::SizeSmall:       incSize = KIconLoader::SizeSmallMedium; break;
-    case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
-    case KIconLoader::SizeMedium:      incSize = KIconLoader::SizeLarge; break;
-    case KIconLoader::SizeLarge:       incSize = KIconLoader::SizeHuge; break;
-    case KIconLoader::SizeHuge:        incSize = KIconLoader::SizeEnormous; break;
-    default: Q_ASSERT(false); break;
-    }
-    return incSize;
-}
-
-int DolphinIconsView::decreasedIconSize(int size) const
-{
-    int decSize = 0;
-    switch (size) {
-    case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
-    case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
-    case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
-    case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
-    case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
-    default: Q_ASSERT(false); break;
-    }
-    return decSize;
-}
-
 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
 {
     const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
@@ -459,8 +406,14 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
     const int spacing = settings->gridSpacing();
     setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
 
-    m_controller->setZoomInPossible(isZoomInPossible());
-    m_controller->setZoomOutPossible(isZoomOutPossible());
+    KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
+    if (delegate != 0) {
+        delegate->setMaximumSize(m_itemSize);
+    }
+
+    if (m_selectionManager != 0) {
+        m_selectionManager->reset();
+    }
 }
 
 int DolphinIconsView::additionalInfoCount() const