]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphiniconsview.cpp
hide the ratings and tags column in the treeview sidebar
[dolphin.git] / src / dolphiniconsview.cpp
index d21ffba32752ebfd42b8257a7a5aab67125dcd78..d9bfef8a476e2623b28163c376adee2d99cc094e 100644 (file)
 
 #include "dolphincontroller.h"
 #include "dolphinsettings.h"
-#include "dolphinitemcategorizer.h"
 
 #include "dolphin_iconsmodesettings.h"
 
+#include <kdialog.h>
+
 #include <QAbstractProxyModel>
 #include <QApplication>
 #include <QPainter>
 #include <QPoint>
 
 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
-    KListView(parent),
+    KCategorizedView(parent),
     m_controller(controller),
-    m_dragging(false)
+    m_itemSize(),
+    m_dragging(false),
+    m_dropRect()
 {
     Q_ASSERT(controller != 0);
     setViewMode(QListView::IconMode);
     setResizeMode(QListView::Adjust);
-
+    setSpacing(KDialog::spacingHint());
     setMouseTracking(true);
     viewport()->setAttribute(Qt::WA_Hover);
 
+    // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
+    // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
+    // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
+    // RETURN-key in keyPressEvent().
     if (KGlobalSettings::singleClick()) {
         connect(this, SIGNAL(clicked(const QModelIndex&)),
                 controller, SLOT(triggerItem(const QModelIndex&)));
@@ -49,8 +56,6 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
         connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
                 controller, SLOT(triggerItem(const QModelIndex&)));
     }
-    connect(this, SIGNAL(activated(const QModelIndex&)),
-            controller, SLOT(triggerItem(const QModelIndex&)));
     connect(this, SIGNAL(entered(const QModelIndex&)),
             controller, SLOT(emitItemEntered(const QModelIndex&)));
     connect(this, SIGNAL(viewportEntered()),
@@ -68,7 +73,7 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle
     const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
     Q_ASSERT(settings != 0);
 
-    m_viewOptions = KListView::viewOptions();
+    m_viewOptions = KCategorizedView::viewOptions();
     m_viewOptions.showDecorationSelected = true;
 
     QFont font(settings->fontFamily(), settings->fontSize());
@@ -93,6 +98,35 @@ DolphinIconsView::~DolphinIconsView()
 {
 }
 
+QRect DolphinIconsView::visualRect(const QModelIndex& index) const
+{
+    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);
+    }
+
+    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);
+    }
+
+    return itemRect;
+}
+
 QStyleOptionViewItem DolphinIconsView::viewOptions() const
 {
     return m_viewOptions;
@@ -100,7 +134,7 @@ QStyleOptionViewItem DolphinIconsView::viewOptions() const
 
 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
 {
-    KListView::contextMenuEvent(event);
+    KCategorizedView::contextMenuEvent(event);
     m_controller->triggerContextMenuRequest(event->pos());
 }
 
@@ -114,7 +148,7 @@ void DolphinIconsView::mousePressEvent(QMouseEvent* event)
         }
     }
 
-    KListView::mousePressEvent(event);
+    KCategorizedView::mousePressEvent(event);
 }
 
 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
@@ -127,7 +161,7 @@ void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
 
 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
 {
-    KListView::dragLeaveEvent(event);
+    KCategorizedView::dragLeaveEvent(event);
 
     // TODO: remove this code when the issue #160611 is solved in Qt 4.4
     m_dragging = false;
@@ -136,7 +170,7 @@ void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
 
 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
 {
-    KListView::dragMoveEvent(event);
+    KCategorizedView::dragMoveEvent(event);
 
     // TODO: remove this code when the issue #160611 is solved in Qt 4.4
     const QModelIndex index = indexAt(event->pos());
@@ -147,31 +181,43 @@ void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
 
 void DolphinIconsView::dropEvent(QDropEvent* event)
 {
-    const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
-    if (!urls.isEmpty()) {
-        m_controller->indicateDroppedUrls(urls,
-                                          indexAt(event->pos()),
-                                          event->source());
-        event->acceptProposedAction();
+    if (!selectionModel()->isSelected(indexAt(event->pos()))) {
+        const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
+        if (!urls.isEmpty()) {
+            m_controller->indicateDroppedUrls(urls,
+                                              m_controller->url(),
+                                              indexAt(event->pos()),
+                                              event->source());
+            event->acceptProposedAction();
+        }
     }
-    KListView::dropEvent(event);
+
+    KCategorizedView::dropEvent(event);
     m_dragging = false;
 }
 
 void DolphinIconsView::paintEvent(QPaintEvent* event)
 {
-    KListView::paintEvent(event);
+    KCategorizedView::paintEvent(event);
 
+    // TODO: remove this code when the issue #160611 is solved in Qt 4.4
     if (m_dragging) {
-        // TODO: remove this code when the issue #160611 is solved in Qt 4.4
-        QPainter painter(viewport());
-        painter.save();
-        QBrush brush(m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight));
-        QColor color = brush.color();
-        color.setAlpha(64);
-        brush.setColor(color);
-        painter.fillRect(m_dropRect, brush);
-        painter.restore();
+        const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
+        DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
+    }
+}
+
+void DolphinIconsView::keyPressEvent(QKeyEvent* event)
+{
+    KCategorizedView::keyPressEvent(event);
+
+    const QItemSelectionModel* selModel = selectionModel();
+    const QModelIndex currentIndex = selModel->currentIndex();
+    const bool triggerItem = currentIndex.isValid()
+                             && (event->key() == Qt::Key_Return)
+                             && (selModel->selectedIndexes().count() <= 1);
+    if (triggerItem) {
+        m_controller->triggerItem(currentIndex);
     }
 }
 
@@ -325,6 +371,8 @@ void DolphinIconsView::updateGridSize(bool showPreview, bool showAdditionalInfo)
     const int spacing = settings->gridSpacing();
     setGridSize(QSize(itemWidth + spacing, itemHeight + spacing));
 
+    m_itemSize = QSize(itemWidth, itemHeight);
+
     m_controller->setZoomInPossible(isZoomInPossible());
     m_controller->setZoomOutPossible(isZoomOutPossible());
 }