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

List:       kde-commits
Subject:    KDE/kdelibs/kfile
From:       Kevin Ottens <ervin () kde ! org>
Date:       2007-04-08 18:15:12
Message-ID: 1176056112.132852.11576.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 651641 by ervin:

Allow the user to hide less used places as discussed with Ellen.
They're shown again dynamically when the user enters ine if the hidden 
places. We also allow to show them all using the context menu.

CCMAIL: ellen@kde.org


 M  +46 -1     kfileplacesmodel.cpp  
 M  +4 -0      kfileplacesmodel.h  
 M  +76 -4     kfileplacesview.cpp  
 M  +2 -0      kfileplacesview.h  


--- trunk/KDE/kdelibs/kfile/kfileplacesmodel.cpp #651640:651641
@@ -21,6 +21,7 @@
 #include "kfileplacesitem_p.h"
 
 #include <QMimeData>
+#include <QColor>
 
 #include <kglobal.h>
 #include <klocale.h>
@@ -111,6 +112,11 @@
     return data(index, Qt::DisplayRole).toString();
 }
 
+bool KFilePlacesModel::isHidden(const QModelIndex &index) const
+{
+    return data(index, HiddenRole).toBool();
+}
+
 bool KFilePlacesModel::isDevice(const QModelIndex &index) const
 {
     if (!index.isValid())
@@ -158,7 +164,7 @@
 
     KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
 
-    if (item->isDevice()) {
+    if (item->isDevice() && role!=HiddenRole && role!=Qt::BackgroundRole) {
         returnData = d->deviceData(item->deviceIndex(), role);
     } else {
         returnData = d->bookmarkData(item->bookmarkAddress(), role);
@@ -243,10 +249,18 @@
         return bookmark.text();
     case Qt::DecorationRole:
         return KIcon(bookmark.icon());
+    case Qt::BackgroundRole:
+        if (bookmark.metaDataItem("IsHidden")=="true") {
+            return Qt::lightGray;
+        } else {
+            return QVariant();
+        }
     case UrlRole:
         return QUrl(bookmark.url());
     case MountNeededRole:
         return false;
+    case HiddenRole:
+        return bookmark.metaDataItem("IsHidden")=="true";
     default:
         return QVariant();
     }
@@ -550,4 +564,35 @@
     d->reloadAndSignal();
 }
 
+void KFilePlacesModel::setPlaceHidden(const QModelIndex &index, bool hidden)
+{
+    if (!index.isValid()) return;
+
+    KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
+
+    KBookmark bookmark = d->bookmarkManager->findByAddress(item->bookmarkAddress());
+
+    if (bookmark.isNull()) return;
+
+    bookmark.setMetaDataItem("IsHidden", (hidden ? "true" : "false"));
+
+    d->bookmarkManager->save();
+
+    emit dataChanged(index, index);
+}
+
+int KFilePlacesModel::hiddenCount() const
+{
+    int rows = rowCount();
+    int hidden = 0;
+
+    for (int i=0; i<rows; ++i) {
+        if (isHidden(index(i, 0))) {
+            hidden++;
+        }
+    }
+
+    return hidden;
+}
+
 #include "kfileplacesmodel.moc"
--- trunk/KDE/kdelibs/kfile/kfileplacesmodel.h #651640:651641
@@ -53,6 +53,7 @@
     bool mountNeeded(const QModelIndex &index) const;
     KIcon icon(const QModelIndex &index) const;
     QString text(const QModelIndex &index) const;
+    bool isHidden(const QModelIndex &index) const;
     bool isDevice(const QModelIndex &index) const;
     Solid::Device deviceForIndex(const QModelIndex &index) const;
     KBookmark bookmarkForIndex(const QModelIndex &index) const;
@@ -60,7 +61,10 @@
     void addPlace(const QString &text, const KUrl &url, const QString &iconName = \
                QString(), const QString &appName = QString());
     void editPlace(const QModelIndex &index, const QString &text, const KUrl &url, \
const QString &iconName = QString(), const QString &appName = QString());  void \
removePlace(const QModelIndex &index) const; +    void setPlaceHidden(const \
QModelIndex &index, bool hidden);  
+    int hiddenCount() const;
+
     /**
      * @brief Get a visible data based on Qt role for the given index.
      * Return the device information for the give index.
--- trunk/KDE/kdelibs/kfile/kfileplacesview.cpp #651640:651641
@@ -40,8 +40,10 @@
     KFilePlacesView * const q;
 
     KUrl currentUrl;
+    bool showAll;
 
     void adaptItemSize();
+    void updateHiddenRows();
 
     void _k_placeClicked(const QModelIndex &index);
 };
@@ -49,6 +51,8 @@
 KFilePlacesView::KFilePlacesView(QWidget *parent)
     : QListView(parent), d(new Private(this))
 {
+    d->showAll = false;
+
     setSelectionRectVisible(false);
     setSelectionMode(SingleSelection);
 
@@ -80,8 +84,19 @@
         d->currentUrl = KUrl();
         selectionModel()->clear();
     }
+    d->updateHiddenRows();
 }
 
+void KFilePlacesView::setShowAll(bool showAll)
+{
+    KFilePlacesModel *placesModel = qobject_cast<KFilePlacesModel*>(model());
+
+    if (placesModel==0) return;
+
+    d->showAll = showAll;
+    d->updateHiddenRows();
+}
+
 void KFilePlacesView::contextMenuEvent(QContextMenuEvent *event)
 {
     KFilePlacesModel *placesModel = qobject_cast<KFilePlacesModel*>(model());
@@ -93,11 +108,28 @@
     QMenu menu;
 
     QAction *edit = 0;
-    if (index.isValid() && !placesModel->isDevice(index)) {
-        edit = menu.addAction(KIcon("edit"), i18n("&Edit Entry..."));
+    QAction *hide = 0;
+    if (index.isValid()) {
+        if (!placesModel->isDevice(index)) {
+            edit = menu.addAction(KIcon("edit"), i18n("&Edit Entry..."));
+        }
+
         menu.addSeparator();
+
+        hide = menu.addAction(i18n("&Hide Entry"));
+        hide->setCheckable(true);
+        hide->setChecked(placesModel->isHidden(index));
     }
 
+    QAction *showAll = 0;
+    if (placesModel->hiddenCount()>0) {
+        showAll = menu.addAction(i18n("&Show All Entries"));
+        showAll->setCheckable(true);
+        showAll->setChecked(d->showAll);
+    }
+
+    menu.addSeparator();
+
     QAction *add = menu.addAction(KIcon("document-new"), i18n("&Add Entry..."));
 
     QAction* remove = 0L;
@@ -140,6 +172,10 @@
 
     } else if (remove != 0 && result == remove) {
         placesModel->removePlace(index);
+    } else if (hide != 0 && result == hide) {
+        placesModel->setPlaceHidden(index, hide->isChecked());
+    } else if (showAll != 0 && result == showAll) {
+        setShowAll(showAll->isChecked());
     }
 
     setUrl(d->currentUrl);
@@ -154,7 +190,6 @@
 void KFilePlacesView::rowsInserted(const QModelIndex &parent, int start, int end)
 {
     QListView::rowsInserted(parent, start, end);
-    d->adaptItemSize();
     setUrl(d->currentUrl);
 }
 
@@ -164,8 +199,18 @@
 
     if (placesModel==0) return;
 
-    const int rowCount = placesModel->rowCount();
+    int rowCount = placesModel->rowCount();
 
+    if (!showAll) {
+        rowCount-= placesModel->hiddenCount();
+
+        QModelIndex current = placesModel->closestItem(currentUrl);
+
+        if (placesModel->isHidden(current)) {
+            rowCount++;
+        }
+    }
+
     if (rowCount==0) return; // We've nothing to display anyway
 
     const int minSize = 16;
@@ -191,6 +236,27 @@
     }
 }
 
+void KFilePlacesView::Private::updateHiddenRows()
+{
+    KFilePlacesModel *placesModel = qobject_cast<KFilePlacesModel*>(q->model());
+
+    if (placesModel==0) return;
+
+    int rowCount = placesModel->rowCount();
+    QModelIndex current = placesModel->closestItem(currentUrl);
+
+    for (int i=0; i<rowCount; ++i) {
+        QModelIndex index = placesModel->index(i, 0);
+        if (index!=current && placesModel->isHidden(index)) {
+            q->setRowHidden(i, !showAll);
+        } else {
+            q->setRowHidden(i, false);
+        }
+    }
+
+    adaptItemSize();
+}
+
 void KFilePlacesView::Private::_k_placeClicked(const QModelIndex &index)
 {
     KFilePlacesModel *placesModel = qobject_cast<KFilePlacesModel*>(q->model());
@@ -209,10 +275,16 @@
 
     if (url.isValid()) {
         currentUrl = url;
+        updateHiddenRows();
         emit q->urlChanged(url);
     } else {
         q->setUrl(currentUrl);
     }
 }
 
+void KFilePlacesView::dataChanged(const QModelIndex &/*topLeft*/, const QModelIndex \
&/*bottomRight*/) +{
+    d->updateHiddenRows();
+}
+
 #include "kfileplacesview.moc"
--- trunk/KDE/kdelibs/kfile/kfileplacesview.h #651640:651641
@@ -41,6 +41,7 @@
 
 public Q_SLOTS:
     void setUrl(const KUrl &url);
+    void setShowAll(bool showAll);
 
 protected:
     void contextMenuEvent(QContextMenuEvent *event);
@@ -48,6 +49,7 @@
 
 protected Q_SLOTS:
     void rowsInserted(const QModelIndex &parent, int start, int end);
+    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
 
 Q_SIGNALS:
     void urlChanged(const KUrl &url);


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

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