]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Adapt DolphinCategoryDrawer to new changes on kdelibs :)
authorRafael Fernández López <ereslibre@kde.org>
Sun, 14 Mar 2010 16:31:16 +0000 (16:31 +0000)
committerRafael Fernández López <ereslibre@kde.org>
Sun, 14 Mar 2010 16:31:16 +0000 (16:31 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=1103215

src/dolphincategorydrawer.cpp
src/dolphincategorydrawer.h
src/dolphiniconsview.cpp
src/dolphiniconsview.h

index 7e6ed16edd769bc1784555d88d17d980adb34b56..cb324f0ba47808d62801eb1065f991c68ce2c90c 100644 (file)
 #endif
 
 #include <kiconloader.h>
+#include <kiconeffect.h>
 #include <kcategorizedsortfilterproxymodel.h>
 #include <qimageblitz.h>
 #include <kuser.h>
+#include <kcategorizedview.h>
 
 #include "dolphinview.h"
 #include "dolphinmodel.h"
 
 #define HORIZONTAL_HINT 3
 
-DolphinCategoryDrawer::DolphinCategoryDrawer()
-        : KCategoryDrawer()
+DolphinCategoryDrawer::DolphinCategoryDrawer(KCategorizedView *view)
+        : KCategoryDrawerV3(view)
+        , hotSpotPressed(NoneHotSpot)
+        , selectAll(KIconLoader::global()->loadIcon("list-add", KIconLoader::Desktop, 16))
+        , selectAllHovered(KIconLoader::global()->iconEffect()->apply(selectAll, KIconLoader::Desktop, KIconLoader::ActiveState))
+        , selectAllDisabled(KIconLoader::global()->iconEffect()->apply(selectAll, KIconLoader::Desktop, KIconLoader::DisabledState))
+        , unselectAll(KIconLoader::global()->loadIcon("list-remove", KIconLoader::Desktop, 16))
+        , unselectAllHovered(KIconLoader::global()->iconEffect()->apply(unselectAll, KIconLoader::Desktop, KIconLoader::ActiveState))
+        , unselectAllDisabled(KIconLoader::global()->iconEffect()->apply(unselectAll, KIconLoader::Desktop, KIconLoader::DisabledState))
 {
 }
 
@@ -51,12 +60,38 @@ DolphinCategoryDrawer::~DolphinCategoryDrawer()
 {
 }
 
+bool DolphinCategoryDrawer::allCategorySelected(const QString &category) const
+{
+    const QModelIndexList list = view()->block(category);
+    foreach (const QModelIndex &index, list) {
+        if (!view()->selectionModel()->isSelected(index)) {
+            return false;
+        }
+    }
+    return true;
+}
+
+bool DolphinCategoryDrawer::someCategorySelected(const QString &category) const
+{
+    const QModelIndexList list = view()->block(category);
+    foreach (const QModelIndex &index, list) {
+        if (view()->selectionModel()->isSelected(index)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
                                          const QStyleOption &option, QPainter *painter) const
 {
     Q_UNUSED(sortRole);
     painter->setRenderHint(QPainter::Antialiasing);
 
+    if (!index.isValid()) {
+        return;
+    }
+
     const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
     const QRect optRect = option.rect;
     QFont font(QApplication::font());
@@ -127,10 +162,43 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
     }
     //END: right vertical line
 
-    //BEGIN: category information
+    const int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
+
+    //BEGIN: select/unselect all
     {
-        const int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
+        if (this->category == category) {
+            QRect iconAllRect(option.rect);
+            iconAllRect.setTop(iconAllRect.top() + 4);
+            iconAllRect.setLeft(iconAllRect.right() - 16 - 7);
+            iconAllRect.setSize(QSize(iconSize, iconSize));
+            if (!allCategorySelected(category)) {
+                if (iconAllRect.contains(pos)) {
+                    painter->drawPixmap(iconAllRect, selectAllHovered);
+                } else {
+                    painter->drawPixmap(iconAllRect, selectAll);
+                }
+            } else {
+                painter->drawPixmap(iconAllRect, selectAllDisabled);
+            }
+            QRect iconNoneRect(option.rect);
+            iconNoneRect.setTop(iconNoneRect.top() + 4);
+            iconNoneRect.setLeft(iconNoneRect.right() - 16 * 2 - 7 * 2);
+            iconNoneRect.setSize(QSize(iconSize, iconSize));
+            if (someCategorySelected(category)) {
+                if (iconNoneRect.contains(pos)) {
+                    painter->drawPixmap(iconNoneRect, unselectAllHovered);
+                } else {
+                    painter->drawPixmap(iconNoneRect, unselectAll);
+                }
+            } else {
+                painter->drawPixmap(iconNoneRect, unselectAllDisabled);
+            }
+        }
+    }
+    //END: select/unselect all
 
+    //BEGIN: category information
+    {
         bool paintIcon;
         QPixmap icon;
         switch (index.column()) {
@@ -195,7 +263,10 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 int DolphinCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
 {
     int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
-    int heightWithoutIcon = option.fontMetrics.height() + (iconSize / 4) * 2 + 1; /* 1 pixel-width gradient */
+    QFont font(QApplication::font());
+    font.setBold(true);
+    const QFontMetrics fontMetrics = QFontMetrics(font);
+    int heightWithoutIcon = fontMetrics.height() + (iconSize / 4) * 2 + 1; /* 1 pixel-width gradient */
     bool paintIcon;
 
     switch (index.column()) {
@@ -214,3 +285,94 @@ int DolphinCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyle
 
     return heightWithoutIcon + 5;
 }
+
+void DolphinCategoryDrawer::mouseButtonPressed(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
+{
+    if (!index.isValid()) {
+        event->ignore();
+        return;
+    }
+    const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
+    int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
+    if (this->category == category) {
+        QRect iconAllRect(blockRect);
+        iconAllRect.setTop(iconAllRect.top() + 4);
+        iconAllRect.setLeft(iconAllRect.right() - 16 - 7);
+        iconAllRect.setSize(QSize(iconSize, iconSize));
+        if (iconAllRect.contains(pos)) {
+            event->accept();
+            hotSpotPressed = SelectAllHotSpot;
+            categoryPressed = index;
+            return;
+        }
+        QRect iconNoneRect(blockRect);
+        iconNoneRect.setTop(iconNoneRect.top() + 4);
+        iconNoneRect.setLeft(iconNoneRect.right() - 16 * 2 - 7 * 2);
+        iconNoneRect.setSize(QSize(iconSize, iconSize));
+        if (iconNoneRect.contains(pos)) {
+            event->accept();
+            hotSpotPressed = UnselectAllHotSpot;
+            categoryPressed = index;
+            return;
+        }
+    }
+    event->ignore();
+}
+
+void DolphinCategoryDrawer::mouseButtonReleased(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
+{
+    if (!index.isValid() || hotSpotPressed == NoneHotSpot || categoryPressed != index) {
+        event->ignore();
+        return;
+    }
+    categoryPressed = QModelIndex();
+    const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
+    int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
+    if (this->category == category) {
+        QRect iconAllRect(blockRect);
+        iconAllRect.setTop(iconAllRect.top() + 4);
+        iconAllRect.setLeft(iconAllRect.right() - 16 - 7);
+        iconAllRect.setSize(QSize(iconSize, iconSize));
+        if (iconAllRect.contains(pos)) {
+            if (hotSpotPressed == SelectAllHotSpot) {
+                event->accept();
+                emit actionRequested(SelectAll, index);
+            } else {
+                event->ignore();
+                hotSpotPressed = NoneHotSpot;
+            }
+            return;
+        }
+        QRect iconNoneRect(blockRect);
+        iconNoneRect.setTop(iconNoneRect.top() + 4);
+        iconNoneRect.setLeft(iconNoneRect.right() - 16 * 2 - 7 * 2);
+        iconNoneRect.setSize(QSize(iconSize, iconSize));
+        if (iconNoneRect.contains(pos)) {
+            if (hotSpotPressed == UnselectAllHotSpot) {
+                event->accept();
+                emit actionRequested(UnselectAll, index);
+            } else {
+                event->ignore();
+                hotSpotPressed = NoneHotSpot;
+            }
+            return;
+        }
+    }
+    event->ignore();
+}
+
+void DolphinCategoryDrawer::mouseMoved(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
+{
+    event->ignore();
+    if (!index.isValid()) {
+        return;
+    }
+    pos = event->pos();
+    category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
+}
+
+void DolphinCategoryDrawer::mouseLeft(const QModelIndex &index, const QRect &blockRect)
+{
+    pos = QPoint();
+    category = QString();
+}
index 2eb0e310cb29ba8d9be08f1c702e1a6560997879..e5ba041f392645f51966e61aa6a287417cf6493f 100644 (file)
 #include <libdolphin_export.h>
 
 class LIBDOLPHINPRIVATE_EXPORT DolphinCategoryDrawer
-    : public KCategoryDrawer
+    : public KCategoryDrawerV3
 {
 public:
-    DolphinCategoryDrawer();
+    enum Action {
+        SelectAll = 0,
+        UnselectAll
+    };
+
+    DolphinCategoryDrawer(KCategorizedView *view);
 
     virtual ~DolphinCategoryDrawer();
 
+    bool allCategorySelected(const QString &category) const;
+    
+    bool someCategorySelected(const QString &category) const;
+
     virtual void drawCategory(const QModelIndex &index, int sortRole,
                               const QStyleOption &option, QPainter *painter) const;
 
     virtual int categoryHeight(const QModelIndex &index, const QStyleOption &option) const;
+
+    /**
+      * @warning You explicitly have to determine whether the event has been accepted or not. You
+      *          have to call event->accept() or event->ignore() at all possible case branches in
+      *          your code.
+      */
+    virtual void mouseButtonPressed(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event);
+
+    /**
+      * @warning You explicitly have to determine whether the event has been accepted or not. You
+      *          have to call event->accept() or event->ignore() at all possible case branches in
+      *          your code.
+      */
+    virtual void mouseButtonReleased(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event);
+
+    virtual void mouseMoved(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event);
+
+    virtual void mouseLeft(const QModelIndex &index,const QRect &blockRect);
+
+private:
+    enum HotSpot {
+        NoneHotSpot = 0,
+        SelectAllHotSpot,
+        UnselectAllHotSpot
+    };
+
+    HotSpot hotSpotPressed;
+    QModelIndex categoryPressed;
+
+    QPixmap selectAll;
+    QPixmap selectAllHovered;
+    QPixmap selectAllDisabled;
+    QPixmap unselectAll;
+    QPixmap unselectAllHovered;
+    QPixmap unselectAllDisabled;
+
+    QPoint pos;
+    QString category;
 };
 
 #endif // DOLPHINCATEGORYDRAWER_H
index ef3221422ed6ceea089cf06f5a6f5a1f9461065a..46c27cd28f6cb0f78036b2e9fa5e94bf52e6d53d 100644 (file)
@@ -43,7 +43,7 @@ DolphinIconsView::DolphinIconsView(QWidget* parent,
                                    DolphinSortFilterProxyModel* proxyModel) :
     KCategorizedView(parent),
     m_controller(controller),
-    m_categoryDrawer(0),
+    m_categoryDrawer(new DolphinCategoryDrawer(this)),
     m_extensionsFactory(0),
     m_font(),
     m_decorationSize(),
@@ -113,7 +113,7 @@ DolphinIconsView::DolphinIconsView(QWidget* parent,
         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();
@@ -127,8 +127,6 @@ DolphinIconsView::DolphinIconsView(QWidget* parent,
 
 DolphinIconsView::~DolphinIconsView()
 {
-    delete m_categoryDrawer;
-    m_categoryDrawer = 0;
 }
 
 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
@@ -424,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();
index 52bf9451974b2c2c547cb3e9297860c942524b0e..d49c5d75f6762cac88cd1bb88dac8337d828b20d 100644 (file)
@@ -78,6 +78,7 @@ private slots:
     void setZoomLevel(int level);
     void requestActivation();
     void slotGlobalSettingsChanged(int category);
+    void categoryDrawerActionRequested(int action, const QModelIndex &index);
 
 private:
     /**