[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH] KFileItemDelegate API change
From:       Fredrik =?utf-8?q?H=C3=B6glund?= <fredrik () kde ! org>
Date:       2007-10-01 18:28:16
Message-ID: 200710012028.16689.fredrik () kde ! org
[Download RAW message or body]

Hi,

This patch changes the KFileItemDelegate API for showing additional
information about file items, from accepting a single item, to a list.

The purpose is to allow more than one line to be shown, which is especially
useful in listviews, where there is plenty of vertical space next to the icon.
In my opinion it's important to have this capability in KDE 4.

The enum and setter/getter functions are also renamed to something I feel
is a bit more descriptive.

Where you would previously use the API like this:

	delegate->setAdditionalInformation(KFileItemDelegate::Size);

You would now write:

	delegate->setShowInformation(KFileItemDelegate::Size);

Or when setting a list:

	KFileItemDelegate::InformationList list;
	list << KFileItemDelegate::FriendlyMimeType << KFileItemDelegate::Size;
	delegate->setShowInformation(list);

The changes to the internal implementation are unintrusive, since the text
layout and rendering code already supported this.

Peter: The dolphin patch does not attempt to add support for configuring
dolphin to show more than one line, it just does the minimum necessary to
keep it compiling and working as before. I imagine you'll want to make
some changes to it :)

I'd like to commit this change preferably today, or if there's no feedback,
next Monday if no one objects.

Regards,
Fredrik


["kdelibs.patch" (text/x-diff)]

diff --git a/kio/kio/kfileitemdelegate.cpp b/kio/kio/kfileitemdelegate.cpp
index 146f64f..1ab6643 100644
--- a/kio/kio/kfileitemdelegate.cpp
+++ b/kio/kio/kfileitemdelegate.cpp
@@ -116,7 +116,7 @@ class KFileItemDelegate::Private
         QPixmap transition(const QPixmap &from, const QPixmap &to, qreal amount) \
const;  
     public:
-        KFileItemDelegate::AdditionalInformation additionalInformation;
+        KFileItemDelegate::InformationList informationList;
 
     private:
         KFileItemDelegate * const q;
@@ -231,44 +231,67 @@ QString KFileItemDelegate::Private::itemSize(const QModelIndex \
&index, const KFi  QString KFileItemDelegate::Private::information(const \
                QStyleOptionViewItem &option, const QModelIndex &index,
                                                 const KFileItem &item) const
 {
-    if (additionalInformation == KFileItemDelegate::NoInformation || item.isNull() \
                || !isListView(option))
-        return QString();
+    QString string;
 
-    switch (additionalInformation)
+    if (informationList.isEmpty() || item.isNull() || !isListView(option))
+        return string;
+
+    foreach (KFileItemDelegate::Information info, informationList)
     {
-        case KFileItemDelegate::Size:
-            return itemSize(index, item);
+        if (info == KFileItemDelegate::NoInformation)
+            continue;
 
-        case KFileItemDelegate::Permissions:
-            return item.permissionsString();
+        if (!string.isEmpty())
+            string += QChar::LineSeparator;
 
-        case KFileItemDelegate::OctalPermissions:
-            return QString('0') + QString::number(item.permissions(), 8);
+        switch (info)
+        {
+            case KFileItemDelegate::Size:
+                string += itemSize(index, item);
+                break;
 
-        case KFileItemDelegate::Owner:
-            return item.user();
+            case KFileItemDelegate::Permissions:
+                string += item.permissionsString();
+                break;
 
-        case KFileItemDelegate::OwnerAndGroup:
-            return item.user() + ':' + item.group();
+            case KFileItemDelegate::OctalPermissions:
+                string += QString('0') + QString::number(item.permissions(), 8);
+                break;
 
-        case KFileItemDelegate::CreationTime:
-            return item.timeString(KFileItem::CreationTime);
+            case KFileItemDelegate::Owner:
+                string += item.user();
+                break;
 
-        case KFileItemDelegate::ModificationTime:
-            return item.timeString(KFileItem::ModificationTime);
+            case KFileItemDelegate::OwnerAndGroup:
+                string += item.user() + ':' + item.group();
+                break;
 
-        case KFileItemDelegate::AccessTime:
-            return item.timeString(KFileItem::AccessTime);
+            case KFileItemDelegate::CreationTime:
+                string += item.timeString(KFileItem::CreationTime);
+                break;
 
-        case KFileItemDelegate::MimeType:
-            return item.isMimeTypeKnown() ? item.mimetype() : i18nc("@info \
mimetype","Unknown"); +            case KFileItemDelegate::ModificationTime:
+                string += item.timeString(KFileItem::ModificationTime);
+                break;
 
-        case KFileItemDelegate::FriendlyMimeType:
-            return item.isMimeTypeKnown() ? item.mimeComment() : i18nc("@info \
mimetype","Unknown"); +            case KFileItemDelegate::AccessTime:
+                string += item.timeString(KFileItem::AccessTime);
+                break;
 
-        default:
-            return QString();
-    }
+            case KFileItemDelegate::MimeType:
+                string += item.isMimeTypeKnown() ? item.mimetype() : i18nc("@info \
mimetype","Unknown"); +                break;
+
+            case KFileItemDelegate::FriendlyMimeType:
+                string += item.isMimeTypeKnown() ? item.mimeComment() : i18nc("@info \
mimetype","Unknown"); +                break;
+
+            default:
+                break;
+        } // switch (info)
+    } // foreach (info, list)
+
+    return string;
 }
 
 
@@ -889,7 +912,7 @@ KFileItemDelegate::KFileItemDelegate(QObject *parent)
     d->setVerticalMargin(Private::IconMargin, focusHMargin, focusVMargin);
     d->setVerticalMargin(Private::ItemMargin, 0, 0);
 
-    setAdditionalInformation(NoInformation);
+    setShowInformation(NoInformation);
 }
 
 
@@ -955,15 +978,24 @@ QString KFileItemDelegate::Private::display(const QModelIndex \
&index) const  }
 
 
-void KFileItemDelegate::setAdditionalInformation(AdditionalInformation value)
+void KFileItemDelegate::setShowInformation(InformationList list)
 {
-    d->additionalInformation = value;
+    d->informationList = list;
+}
+
+
+void KFileItemDelegate::setShowInformation(Information value)
+{
+    if (value != NoInformation)
+        d->informationList = InformationList() << value;
+    else
+        d->informationList = InformationList();
 }
 
 
-KFileItemDelegate::AdditionalInformation KFileItemDelegate::additionalInformation() \
const +KFileItemDelegate::InformationList KFileItemDelegate::showInformation() const
 {
-    return d->additionalInformation;
+    return d->informationList;
 }
 
 
diff --git a/kio/kio/kfileitemdelegate.h b/kio/kio/kfileitemdelegate.h
index c2d3c5c..773f6b1 100644
--- a/kio/kio/kfileitemdelegate.h
+++ b/kio/kio/kfileitemdelegate.h
@@ -50,9 +50,9 @@ class QPainter;
  * the file items below the icon labels.
  *
  * Which information should be shown, if any, is controlled by the
- * @ref information property, which can be set by calling
- * setAdditionalInformation(), and read by calling additionalInformation().
- * The default value for this property is @ref NoInformation.
+ * @ref information property, which is a list that can be set by calling
+ * setShowInformation(), and read by calling showInformation().
+ * By default this list is empty.
  *
  * To use KFileItemDelegate, instantiate an object from the delegate,
  * and call setItemDelegate() in one of the standard item views in Qt:
@@ -72,11 +72,11 @@ class KIO_EXPORT KFileItemDelegate : public QAbstractItemDelegate
      * items in icon views.
      *
      * Access functions:
-     * @li void setAdditionalInformation(AdditionalInformation information)
-     * @li Information additionalInformation() const
+     * @li void setShownformation(InformationList information)
+     * @li InformationList showInformation() const
      */
-    Q_PROPERTY(AdditionalInformation information READ additionalInformation WRITE \
                setAdditionalInformation)
-    Q_ENUMS(AdditionalInformation)
+    Q_PROPERTY(InformationList information READ showInformation WRITE \
setShowInformation) +    Q_ENUMS(Information)
 
 
     public:
@@ -97,11 +97,11 @@ class KIO_EXPORT KFileItemDelegate : public QAbstractItemDelegate
          * types are resolved. If the mime type isn't known, "Unknown" will be \
                displayed until
          * the mime type has been successfully resolved.
          *
-         * @see setAdditionalInformation()
-         * @see additionalInformation()
+         * @see setShowInformation()
+         * @see showInformation()
          * @see information
          */
-        enum AdditionalInformation {
+        enum Information {
             NoInformation,     ///< No additional information will be shown for \
                items.
             Size,              ///< The file size for files, and the number of items \
                for folders.
             Permissions,       ///< A UNIX permissions string, e.g. -rwxr-xr-x.
@@ -115,6 +115,8 @@ class KIO_EXPORT KFileItemDelegate : public QAbstractItemDelegate
             FriendlyMimeType   ///< The descriptive name for the mime type, e.g. \
HTML Document.  };
 
+        typedef QList<Information> InformationList;
+
 
         /**
          * Constructs a new KFileItemDelegate.
@@ -200,17 +202,39 @@ class KIO_EXPORT KFileItemDelegate : public \
QAbstractItemDelegate  
 
         /**
-         * Sets the additional information that should be shown below below item \
labels in icon views. +         * Sets the list of information lines that are shown \
below the icon label in list views. +         *
+         * You will typically construct the list like this:
+         * @code
+         * KFileItemDelegate::InformationList list;
+         * list << KFileItemDelegate::FriendlyMimeType << KFileItemDelegate::Size;
+         * delegate->setShowInformation(list);
+         * @endcode
+         *
+         * The information lines will be displayed in the list order.
+         * The delegate will first draw the item label, and then as many information
+         * lines as will fit in the available space.
+         *
+         * @param list A list of information items that should be shown
+         */
+        void setShowInformation(InformationList list);
+
+
+        /**
+         * Sets a single information line that is shown below the icon label in list \
views. +         *
+         * This is a convenience function for when you only want to show a single \
line +         * of information.
          *
          * @param information The information that should be shown
          */
-        void setAdditionalInformation(AdditionalInformation information);
+        void setShowInformation(Information information);
 
 
         /**
-         * Returns the additional information that should be shown below item labels \
in icon views. +         * Returns the file item information that should be shown \
                below item labels in list views.
          */
-        AdditionalInformation additionalInformation() const;
+        InformationList showInformation() const;
 
 
     public Q_SLOTS:


["kdebase.patch" (text/x-diff)]

Index: apps/dolphin/src/dolphinmainwindow.cpp
===================================================================
--- apps/dolphin/src/dolphinmainwindow.cpp	(revision 719260)
+++ apps/dolphin/src/dolphinmainwindow.cpp	(working copy)
@@ -324,9 +324,11 @@
     descending->setChecked(sortDescending);
 }
 
-void DolphinMainWindow::slotAdditionalInfoChanged(KFileItemDelegate::AdditionalInformation \
info) +void DolphinMainWindow::slotAdditionalInfoChanged(KFileItemDelegate::InformationList \
list)  {
     QAction* action = 0;
+    KFileItemDelegate::Information info = list.isEmpty() ? \
KFileItemDelegate::NoInformation : list.first(); +
     switch (info) {
     case KFileItemDelegate::FriendlyMimeType:
         action = actionCollection()->action("show_mime_info");
@@ -1582,8 +1584,8 @@
             this, SLOT(slotSortingChanged(DolphinView::Sorting)));
     connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
             this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
-    connect(view, SIGNAL(additionalInfoChanged(KFileItemDelegate::AdditionalInformation)),
                
-            this, SLOT(slotAdditionalInfoChanged(KFileItemDelegate::AdditionalInformation)));
 +    connect(view, SIGNAL(additionalInfoChanged(KFileItemDelegate::InformationList)),
 +            this, SLOT(slotAdditionalInfoChanged(KFileItemDelegate::InformationList)));
  connect(view, SIGNAL(selectionChanged(QList<KFileItem>)),
             this, SLOT(slotSelectionChanged(QList<KFileItem>)));
     connect(view, SIGNAL(requestItemInfo(KFileItem)),
Index: apps/dolphin/src/viewpropertiesdialog.cpp
===================================================================
--- apps/dolphin/src/viewpropertiesdialog.cpp	(revision 719260)
+++ apps/dolphin/src/viewpropertiesdialog.cpp	(working copy)
@@ -269,11 +269,11 @@
 
 void ViewPropertiesDialog::slotAdditionalInfoChanged(int index)
 {
-    KFileItemDelegate::AdditionalInformation info = \
KFileItemDelegate::NoInformation; +    KFileItemDelegate::InformationList info;
     switch (index) {
-    case 1:  info = KFileItemDelegate::FriendlyMimeType; break;
-    case 2:  info = KFileItemDelegate::Size; break;
-    case 3:  info = KFileItemDelegate::ModificationTime; break;
+    case 1:  info << KFileItemDelegate::FriendlyMimeType; break;
+    case 2:  info << KFileItemDelegate::Size; break;
+    case 3:  info << KFileItemDelegate::ModificationTime; break;
     default: break;
     }
     m_viewProps->setAdditionalInfo(info);
@@ -376,7 +376,8 @@
     m_sorting->setCurrentIndex(m_viewProps->sorting());
 
     // load additional info
-    const int addInfoIndex = \
m_additionalInfo->findData(m_viewProps->additionalInfo()); +    \
KFileItemDelegate::InformationList info = m_viewProps->additionalInfo(); +    const \
int addInfoIndex = m_additionalInfo->findData(info.isEmpty() ? \
KFileItemDelegate::NoInformation : info.first());  \
m_additionalInfo->setCurrentIndex(addInfoIndex);  \
m_additionalInfo->setEnabled(iconsViewEnabled);  
Index: apps/dolphin/src/dolphinview.cpp
===================================================================
--- apps/dolphin/src/dolphinview.cpp	(revision 719260)
+++ apps/dolphin/src/dolphinview.cpp	(working copy)
@@ -407,24 +407,33 @@
     return m_proxyModel->sortOrder();
 }
 
-void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
+void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
 {
     const KUrl viewPropsUrl = viewPropertiesUrl();
     ViewProperties props(viewPropsUrl);
     props.setAdditionalInfo(info);
 
-    m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
-    m_fileItemDelegate->setAdditionalInformation(info);
+    m_controller->setShowAdditionalInfo(!info.isEmpty());
+    m_fileItemDelegate->setShowInformation(info);
 
     emit additionalInfoChanged(info);
     startDirLister(viewPropsUrl, true);
 }
 
-KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
+void DolphinView::setAdditionalInfo(KFileItemDelegate::Information info)
 {
-    return m_fileItemDelegate->additionalInformation();
+    KFileItemDelegate::InformationList list;
+    if (info != KFileItemDelegate::NoInformation)
+        list << info;
+
+    setAdditionalInfo(list);
 }
 
+KFileItemDelegate::InformationList DolphinView::additionalInfo() const
+{
+    return m_fileItemDelegate->showInformation();
+}
+
 void DolphinView::reload()
 {
     setUrl(url());
@@ -648,10 +657,10 @@
         emit sortOrderChanged(sortOrder);
     }
 
-    KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
-    if (info != m_fileItemDelegate->additionalInformation()) {
-        m_controller->setShowAdditionalInfo(info != \
                KFileItemDelegate::NoInformation);
-        m_fileItemDelegate->setAdditionalInformation(info);
+    KFileItemDelegate::InformationList info = props.additionalInfo();
+    if (info != m_fileItemDelegate->showInformation()) {
+        m_controller->setShowAdditionalInfo(!info.isEmpty());
+        m_fileItemDelegate->setShowInformation(info);
         emit additionalInfoChanged(info);
     }
 
Index: apps/dolphin/src/dolphinmainwindow.h
===================================================================
--- apps/dolphin/src/dolphinmainwindow.h	(revision 719260)
+++ apps/dolphin/src/dolphinmainwindow.h	(working copy)
@@ -398,7 +398,7 @@
     void slotSortOrderChanged(Qt::SortOrder order);
 
     /** Updates the state of the 'Additional Information' actions. */
-    void slotAdditionalInfoChanged(KFileItemDelegate::AdditionalInformation info);
+    void slotAdditionalInfoChanged(KFileItemDelegate::InformationList info);
 
     /**
      * Updates the state of the 'Edit' menu actions and emits
Index: apps/dolphin/src/viewproperties.h
===================================================================
--- apps/dolphin/src/viewproperties.h	(revision 719260)
+++ apps/dolphin/src/viewproperties.h	(working copy)
@@ -71,8 +71,8 @@
     void setSortOrder(Qt::SortOrder sortOrder);
     Qt::SortOrder sortOrder() const;
 
-    void setAdditionalInfo(KFileItemDelegate::AdditionalInformation info);
-    KFileItemDelegate::AdditionalInformation additionalInfo() const;
+    void setAdditionalInfo(KFileItemDelegate::InformationList info);
+    KFileItemDelegate::InformationList additionalInfo() const;
 
     /**
      * Sets the directory properties view mode, show preview,
Index: apps/dolphin/src/dolphinview.h
===================================================================
--- apps/dolphin/src/dolphinview.h	(revision 719260)
+++ apps/dolphin/src/dolphinview.h	(working copy)
@@ -279,10 +279,13 @@
     Qt::SortOrder sortOrder() const;
 
     /** Sets the additional information which should be shown for the items. */
-    void setAdditionalInfo(KFileItemDelegate::AdditionalInformation info);
+    void setAdditionalInfo(KFileItemDelegate::InformationList info);
 
+    /** Sets the additional information which should be shown for the items. */
+    void setAdditionalInfo(KFileItemDelegate::Information info);
+
     /** Returns the additional information which should be shown for the items. */
-    KFileItemDelegate::AdditionalInformation additionalInfo() const;
+    KFileItemDelegate::InformationList additionalInfo() const;
 
     /** Reloads the current directory. */
     void reload();
@@ -355,7 +358,7 @@
     void sortOrderChanged(Qt::SortOrder order);
 
     /** Is emitted if the additional information for an item has been changed. */
-    void additionalInfoChanged(KFileItemDelegate::AdditionalInformation info);
+    void additionalInfoChanged(KFileItemDelegate::InformationList info);
 
     /**
      * Is emitted if information of an item is requested to be shown e. g. in the \
                sidebar.
Index: apps/dolphin/src/viewproperties.cpp
===================================================================
--- apps/dolphin/src/viewproperties.cpp	(revision 719260)
+++ apps/dolphin/src/viewproperties.cpp	(working copy)
@@ -197,17 +197,25 @@
     return static_cast<Qt::SortOrder>(m_node->sortOrder());
 }
 
-void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation \
info) +void ViewProperties::setAdditionalInfo(KFileItemDelegate::InformationList \
list)  {
+    KFileItemDelegate::Information info = list.isEmpty() ?
+                            KFileItemDelegate::NoInformation : list.first();
+
     if (m_node->additionalInfo() != info) {
         m_node->setAdditionalInfo(info);
         updateTimeStamp();
     }
 }
 
-KFileItemDelegate::AdditionalInformation ViewProperties::additionalInfo() const
+KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
 {
-    return static_cast<KFileItemDelegate::AdditionalInformation>(m_node->additionalInfo());
 +    KFileItemDelegate::Information info = \
static_cast<KFileItemDelegate::Information>(m_node->additionalInfo()); +
+    if (info != KFileItemDelegate::NoInformation)
+        return KFileItemDelegate::InformationList() << info;
+    else
+        return KFileItemDelegate::InformationList();
 }
 
 



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic