]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Assure that the item delegate draws the hover effect and the selection for the detail...
authorPeter Penz <peter.penz19@gmail.com>
Wed, 15 Oct 2008 21:23:42 +0000 (21:23 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Wed, 15 Oct 2008 21:23:42 +0000 (21:23 +0000)
CCBUG: 165999
CCMAIL: simon@etotheipiplusone.com

svn path=/trunk/KDE/kdebase/apps/; revision=871874

src/CMakeLists.txt
src/dolphindetailsview.cpp
src/dolphinfileitemdelegate.cpp [new file with mode: 0644]
src/dolphinfileitemdelegate.h [new file with mode: 0644]
src/dolphinview.cpp
src/dolphinview.h

index 18c597a6fde9af26a3aa1b9667ccad63a3043063..5f676a4ebd33405f5307bf286b432d018029d7c9 100644 (file)
@@ -26,14 +26,15 @@ set(dolphinprivate_LIB_SRCS
     dolphincolumnview.cpp
     dolphincolumnwidget.cpp
     dolphindropcontroller.cpp
-    dolphinsortfilterproxymodel.cpp
-    draganddrophelper.cpp
+    dolphinfileitemdelegate.cpp
     dolphinmodel.cpp
     dolphinsettings.cpp
+    dolphinsortfilterproxymodel.cpp
     dolphintooltip.cpp
     dolphincategorydrawer.cpp
     dolphinview.cpp
     dolphinviewactionhandler.cpp
+    draganddrophelper.cpp
     folderexpander.cpp
     ktooltip.cpp
     kballoontipdelegate.cpp
index 045ea6a6a74eaf8d61bd60e18cf9037aa135f612..08eef4307c57cf8d0c2ba3c2c3e7feb579dae833 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "dolphinmodel.h"
 #include "dolphincontroller.h"
+#include "dolphinfileitemdelegate.h"
 #include "dolphinsettings.h"
 #include "dolphinsortfilterproxymodel.h"
 #include "draganddrophelper.h"
@@ -737,19 +738,14 @@ void DolphinDetailsView::resizeColumns()
 
 QRect DolphinDetailsView::nameColumnRect(const QModelIndex& index) const
 {
-    // TODO: The code guesses the width of the name, but it is defined
-    // by KFileItemDelegate. Find a way to tell the item delegate to
-    // use a specified width.
-    QRect guessedItemContentRect = visualRect(index);
-    const KFileItem fileItem = m_controller->itemForIndex(index);
-    if (!fileItem.isNull()) {
-        QStyleOptionViewItem itemStyle = viewOptions();        
-        QFontMetrics fontMetrics(itemStyle.font);
-        const int itemContentWidth = itemStyle.decorationSize.width() + fontMetrics.width(fileItem.name());
-        guessedItemContentRect.setWidth(itemContentWidth);
+    QRect rect = visualRect(index);
+    const KFileItem item = m_controller->itemForIndex(index);
+    if (!item.isNull()) {
+        const int width = DolphinFileItemDelegate::nameColumnWidth(item.name(), viewOptions());
+        rect.setWidth(width);
     }
 
-    return guessedItemContentRect;
+    return rect;
 }
 
 void DolphinDetailsView::setSelectionRecursive(const QModelIndex& startIndex,
diff --git a/src/dolphinfileitemdelegate.cpp b/src/dolphinfileitemdelegate.cpp
new file mode 100644 (file)
index 0000000..53c1d78
--- /dev/null
@@ -0,0 +1,66 @@
+/***************************************************************************
+ *   Copyright (C) 2008 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  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#include "dolphinfileitemdelegate.h"
+
+#include <QAbstractItemModel>
+#include <QAbstractProxyModel>
+#include <QFontMetrics>
+
+#include <kdirmodel.h>
+#include <kfileitem.h>
+
+DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
+    KFileItemDelegate(parent),
+    m_hasMinimizedNameColumn(false)
+{
+}
+
+DolphinFileItemDelegate::~DolphinFileItemDelegate()
+{
+}
+
+void DolphinFileItemDelegate::paint(QPainter* painter,
+                                    const QStyleOptionViewItem& option,
+                                    const QModelIndex& index) const
+{
+    if (m_hasMinimizedNameColumn && (index.column() == KDirModel::Name)) {
+        QStyleOptionViewItemV4 opt(option);
+        
+        const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(index.model());
+        const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+        const QModelIndex dirIndex = proxyModel->mapToSource(index);
+        const KFileItem item = dirModel->itemForIndex(dirIndex);
+        if (!item.isNull()) {
+            const int width = nameColumnWidth(item.name(), opt);
+            opt.rect.setWidth(width);
+        }
+        KFileItemDelegate::paint(painter, opt, index);
+    } else {
+        KFileItemDelegate::paint(painter, option, index);
+    }
+}
+
+int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
+{
+    QFontMetrics fontMetrics(option.font);
+    return option.decorationSize.width() + fontMetrics.width(name) + 16;  
+}
+
+#include "dolphinfileitemdelegate.moc"
diff --git a/src/dolphinfileitemdelegate.h b/src/dolphinfileitemdelegate.h
new file mode 100644 (file)
index 0000000..589ba71
--- /dev/null
@@ -0,0 +1,74 @@
+/***************************************************************************
+ *   Copyright (C) 2008 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  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#ifndef DOLPHINFILEITEMDELEGATE_H
+#define DOLPHINFILEITEMDELEGATE_H
+
+#include <kfileitemdelegate.h>
+
+/**
+ * Extends KFileItemDelegate by the ability to show the hover effect
+ * and the selection in a minimized way for the name column of
+ * the details view.
+ *
+ * Note that this is a workaround, as Qt does not support having custom
+ * shapes within the visual rect of an item view. The visual part of
+ * workaround is handled inside DolphinFileItemDelegate, the behavior
+ * changes are handled in DolphinDetailsView.
+ */
+class DolphinFileItemDelegate : public KFileItemDelegate
+{
+public:
+    explicit DolphinFileItemDelegate(QObject* parent = 0);
+    virtual ~DolphinFileItemDelegate();
+
+    /**
+     * If \a minimized is true, the hover effect and the selection are
+     * only drawn above the icon and text of an item. Per default
+     * \a minimized is false, which means that the whole visual rect is
+     * used like in KFileItemDelegate.
+     */
+    void setMinimizedNameColumn(bool minimized);
+    bool hasMinimizedNameColumn() const;
+    
+    virtual void paint(QPainter* painter,
+                       const QStyleOptionViewItem& option,
+                       const QModelIndex& index) const;
+                       
+    /**
+     * Returns the minimized width of the name column for the name \a name. This method
+     * is also used in DolphinDetailsView to handle the selection of items correctly.
+     */
+    static int nameColumnWidth(const QString& name, const QStyleOptionViewItem& option);
+    
+private:
+    bool m_hasMinimizedNameColumn;
+};
+
+inline void DolphinFileItemDelegate::setMinimizedNameColumn(bool minimized)
+{
+    m_hasMinimizedNameColumn = minimized;
+}
+
+inline bool DolphinFileItemDelegate::hasMinimizedNameColumn() const
+{
+    return m_hasMinimizedNameColumn;
+}
+
+#endif
index 2c4efca602b31a6fc5ce7cfa77af3604f6c7d0b5..d0e1b8bd24bb4b78b7c1798342e6509c83639731 100644 (file)
@@ -31,7 +31,6 @@
 #include <kactioncollection.h>
 #include <kcolorscheme.h>
 #include <kdirlister.h>
-#include <kfileitemdelegate.h>
 #include <kfilepreviewgenerator.h>
 #include <kiconeffect.h>
 #include <klocale.h>
@@ -52,6 +51,7 @@
 #include "dolphinmodel.h"
 #include "dolphincolumnview.h"
 #include "dolphincontroller.h"
+#include "dolphinfileitemdelegate.h"
 #include "dolphinsortfilterproxymodel.h"
 #include "dolphindetailsview.h"
 #include "dolphin_detailsmodesettings.h"
@@ -1209,8 +1209,9 @@ void DolphinView::createView()
 
     m_controller->setItemView(view);
 
-    m_fileItemDelegate = new KFileItemDelegate(view);
+    m_fileItemDelegate = new DolphinFileItemDelegate(view);
     m_fileItemDelegate->setShowToolTipWhenElided(false);
+    m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView);
     view->setItemDelegate(m_fileItemDelegate);
 
     view->setModel(m_proxyModel);
index d2d97aec27a7b924081361cf482bc5f43c0fc7fc..2a70352e73884034dccb50a3660344905e3cb241 100644 (file)
@@ -41,6 +41,7 @@
 class DolphinController;
 class DolphinColumnView;
 class DolphinDetailsView;
+class DolphinFileItemDelegate;
 class DolphinIconsView;
 class DolphinMainWindow;
 class DolphinModel;
@@ -49,7 +50,6 @@ class KFilePreviewGenerator;
 class KAction;
 class KActionCollection;
 class KDirLister;
-class KFileItemDelegate;
 class KUrl;
 class KToggleAction;
 class ToolTipManager;
@@ -708,7 +708,7 @@ private:
     DolphinIconsView* m_iconsView;
     DolphinDetailsView* m_detailsView;
     DolphinColumnView* m_columnView;
-    KFileItemDelegate* m_fileItemDelegate;
+    DolphinFileItemDelegate* m_fileItemDelegate;
     QItemSelectionModel* m_selectionModel;
 
     DolphinModel* m_dolphinModel;