[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-05 21:28:24
Message-ID: 1175808504.160821.5970.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 650940 by ervin:

Add the missing context menu. Now the places are completely editable.
Still some apidox missing, and some polishing required (I've a flicker 
in a few cases), but we're definitely getting there.


 M  +80 -8     kfileplacesmodel.cpp  
 M  +6 -0      kfileplacesmodel.h  
 M  +78 -1     kfileplacesview.cpp  
 M  +7 -0      kfileplacesview.h  


--- trunk/KDE/kdelibs/kfile/kfileplacesmodel.cpp #650939:650940
@@ -50,6 +50,7 @@
 
     QVariant bookmarkData(const QString &address, int role) const;
     QVariant deviceData(const QPersistentModelIndex &index, int role) const;
+    void reloadAndSignal();
 
     void _k_devicesInserted(const QModelIndex &parent, int start, int end);
     void _k_devicesRemoved(const QModelIndex &parent, int start, int end);
@@ -126,6 +127,20 @@
     }
 }
 
+KBookmark KFilePlacesModel::bookmarkForIndex(const QModelIndex &index) const
+{
+    if (!index.isValid())
+        return KBookmark();
+
+    KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
+
+    if (!item->isDevice()) {
+        return d->bookmarkManager->findByAddress(item->bookmarkAddress());
+    } else {
+        return KBookmark();
+    }
+}
+
 QVariant KFilePlacesModel::data(const QModelIndex &index, int role) const
 {
     if (!index.isValid())
@@ -281,9 +296,12 @@
 
     while (!bookmark.isNull()) {
         QString udi = bookmark.metaDataItem("UDI");
+        QString appName = bookmark.metaDataItem("OnlyInApp");
         QPersistentModelIndex index = devices.take(udi);
 
-        if (udi.isEmpty() || index.isValid()) {
+        bool allowedHere = appName.isEmpty() || \
(appName==KGlobal::mainComponent().componentName()); +
+        if ((udi.isEmpty() && allowedHere) || index.isValid()) {
             q->beginInsertRows(QModelIndex(), items.size(), items.size());
 
             KFilePlacesItem *item = new KFilePlacesItem();
@@ -317,6 +335,16 @@
     }
 }
 
+void KFilePlacesModel::Private::reloadAndSignal()
+{
+    bool signalsBlocked = q->blockSignals(true); // Avoid too much signaling...
+    // We reload here to avoid a transitional
+    // period where devices are seen as separators
+    _k_reloadBookmarks();
+    q->blockSignals(signalsBlocked);
+    bookmarkManager->emitChanged(bookmarkManager->root()); // ... we'll get relisted \
anyway +}
+
 Qt::DropActions KFilePlacesModel::supportedDropActions() const
 {
     return Qt::ActionMask;
@@ -337,7 +365,7 @@
 
 static QString _k_internalMimetype(const KFilePlacesModel * const self)
 {
-    return QString("application/x-kfileplacesmodel-")+QString::number( (long)self );
+    return QString("application/x-kfileplacesmodel-")+QString::number((long)self);
 }
 
 QStringList KFilePlacesModel::mimeTypes() const
@@ -447,14 +475,58 @@
         return false;
     }
 
-    bool signalsBlocked = blockSignals(true); // Avoid too much signaling...
-    // We reload here to avoid a transitional
-    // period where devices are seen as separators
-    d->_k_reloadBookmarks();
-    blockSignals(signalsBlocked);
-    d->bookmarkManager->emitChanged(d->bookmarkManager->root()); // ... we'll get \
relisted anyway +    d->reloadAndSignal();
 
     return true;
 }
 
+void KFilePlacesModel::addPlace(const QString &text, const KUrl &url,
+                                const QString &iconName, const QString &appName)
+{
+    KBookmark bookmark = d->bookmarkManager->root().addBookmark(d->bookmarkManager, \
text, url, iconName); +
+    if (!appName.isEmpty()) {
+        bookmark.setMetaDataItem("OnlyInApp", appName);
+    }
+
+    d->reloadAndSignal();
+}
+
+void KFilePlacesModel::editPlace(const QModelIndex &index, const QString &text, \
const KUrl &url, +                                 const QString &iconName, const \
QString &appName) +{
+    if (!index.isValid()) return;
+
+    KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
+
+    if (item->isDevice()) return;
+
+    KBookmark bookmark = d->bookmarkManager->findByAddress(item->bookmarkAddress());
+
+    if (bookmark.isNull()) return;
+
+    bookmark.setFullText(text);
+    bookmark.setUrl(url);
+    bookmark.setIcon(iconName);
+    bookmark.setMetaDataItem("OnlyInApp", appName);
+
+    d->reloadAndSignal();
+}
+
+void KFilePlacesModel::removePlace(const QModelIndex &index) const
+{
+    if (!index.isValid()) return;
+
+    KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
+
+    if (item->isDevice()) return;
+
+    KBookmark bookmark = d->bookmarkManager->findByAddress(item->bookmarkAddress());
+
+    if (bookmark.isNull()) return;
+
+    d->bookmarkManager->root().deleteBookmark(bookmark);
+    d->reloadAndSignal();
+}
+
 #include "kfileplacesmodel.moc"
--- trunk/KDE/kdelibs/kfile/kfileplacesmodel.h #650939:650940
@@ -24,6 +24,7 @@
 
 #include <QAbstractItemModel>
 #include <kurl.h>
+#include <kbookmark.h>
 #include <kicon.h>
 
 #include <solid/device.h>
@@ -54,7 +55,12 @@
     QString text(const QModelIndex &index) const;
     bool isDevice(const QModelIndex &index) const;
     Solid::Device deviceForIndex(const QModelIndex &index) const;
+    KBookmark bookmarkForIndex(const QModelIndex &index) const;
 
+    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; +
     /**
      * @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 #650939:650940
@@ -19,9 +19,17 @@
 
 #include "kfileplacesview.h"
 
+#include <QMenu>
+#include <QContextMenuEvent>
+
+#include <kdebug.h>
+
+#include <kcomponentdata.h>
+#include <klocale.h>
 #include <kjob.h>
 #include <solid/volume.h>
 
+#include "kurlbar.h"
 #include "kfileplacesmodel.h"
 
 class KFilePlacesView::Private
@@ -65,13 +73,82 @@
 
     if (index.isValid()) {
         d->currentUrl = url;
-        selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
+        selectionModel()->setCurrentIndex(index, \
QItemSelectionModel::ClearAndSelect);  } else {
         d->currentUrl = KUrl();
         selectionModel()->clear();
     }
 }
 
+void KFilePlacesView::contextMenuEvent(QContextMenuEvent *event)
+{
+    KFilePlacesModel *placesModel = qobject_cast<KFilePlacesModel*>(model());
+
+    if (placesModel==0) return;
+
+    QModelIndex index = indexAt(event->pos());
+
+    QMenu menu;
+
+    QAction *edit = 0;
+    if (index.isValid() && !placesModel->isDevice(index)) {
+        edit = menu.addAction(KIcon("edit"), i18n("&Edit Entry..."));
+        menu.addSeparator();
+    }
+
+    QAction *add = menu.addAction(KIcon("document-new"), i18n("&Add Entry..."));
+
+    QAction* remove = 0L;
+    if (index.isValid() && !placesModel->isDevice(index)) {
+        remove = menu.addAction( KIcon("edit-delete"), i18n("&Remove Entry"));
+    }
+
+    QAction *result = menu.exec(event->globalPos());
+
+    if (add != 0 &&result == add) {
+        KUrl url;
+        QString description;
+        QString iconName;
+        bool appLocal = false;
+
+        if (KUrlBarItemDialog::getInformation(true, url, description,
+                                              iconName, appLocal, 64, this))
+        {
+            QString appName;
+            if (appLocal) appName = KGlobal::mainComponent().componentName();
+
+            placesModel->addPlace(description, url, iconName, appName);
+        }
+
+    } else if (edit != 0 && result == edit) {
+        KBookmark bookmark = placesModel->bookmarkForIndex(index);
+        KUrl url = bookmark.url();
+        QString description = bookmark.text();
+        QString iconName = bookmark.icon();
+        bool appLocal = !bookmark.metaDataItem("OnlyInApp").isEmpty();
+
+        if (KUrlBarItemDialog::getInformation(true, url, description,
+                                              iconName, appLocal, 64, this))
+        {
+            QString appName;
+            if (appLocal) appName = KGlobal::mainComponent().componentName();
+
+            placesModel->editPlace(index, description, url, iconName, appName);
+        }
+
+    } else if (remove != 0 && result == remove) {
+        placesModel->removePlace(index);
+    }
+
+    setUrl(d->currentUrl);
+}
+
+void KFilePlacesView::rowsInserted(const QModelIndex &parent, int start, int end)
+{
+    QListView::rowsInserted(parent, start, end);
+    setUrl(d->currentUrl);
+}
+
 void KFilePlacesView::Private::_k_placeClicked(const QModelIndex &index)
 {
     KFilePlacesModel *placesModel = qobject_cast<KFilePlacesModel*>(q->model());
--- trunk/KDE/kdelibs/kfile/kfileplacesview.h #650939:650940
@@ -26,6 +26,7 @@
 #include <kurl.h>
 
 class QAbstractItemModel;
+class QContextMenuEvent;
 
 /**
  * This class allows to display a KFilePlacesModel.
@@ -40,6 +41,12 @@
 public Q_SLOTS:
     void setUrl(const KUrl &url);
 
+protected:
+    void contextMenuEvent(QContextMenuEvent *event);
+
+protected Q_SLOTS:
+    void rowsInserted(const QModelIndex &parent, int start, int end);
+
 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