enhance KRecentFilesAction with a ui action to remove entries

Christoph Pfister christophpfister at gmail.com
Wed Mar 11 21:46:32 GMT 2009


2009/3/7, Christoph Pfister <christophpfister at gmail.com>:
> Patch and screenshot attached - comments welcome (don't know how the
> k-c-d development model works, but I don't mind you / me commiting it
> if it's ok).

v2 of the patch:
- renamed *enable to *visible
- replaced "Remove all entries" by "Clear list"
- only show the clear action if the list isn't empty
- internal: don't delete / reconstruct the special actions, instead
use setVisible
- internal: do not put the special actions into selectableActionGroup,
as the code assumes that those actions are recent actions (e.g. for
maxItems calculation and in saveEntries)

Please treat questions like "clear action visible by default" or
"clear action in general" seperately (the clear action is still
disabled by default in my patch); if you reach consensus about that it
can be easily done as a follow-up commit.

Christoph
-------------- next part --------------
diff -dNpru actions-orig/krecentfilesaction.cpp actions/krecentfilesaction.cpp
--- actions-orig/krecentfilesaction.cpp	2009-03-11 20:10:59.000000000 +0100
+++ actions/krecentfilesaction.cpp	2009-03-11 22:34:41.000000000 +0100
@@ -77,9 +77,12 @@ void KRecentFilesActionPrivate::init()
   delete q->menu();
   q->setMenu(new KMenu());
   q->setToolBarMode(KSelectAction::MenuMode);
-  m_noEntriesAction=new QAction(i18n("No entries"),q->selectableActionGroup());
+  m_noEntriesAction = q->menu()->addAction(i18n("No entries"));
   m_noEntriesAction->setEnabled(false);
-  q->KSelectAction::addAction(m_noEntriesAction);
+  clearSeparator = q->menu()->addSeparator();
+  clearSeparator->setVisible(false);
+  clearAction = q->menu()->addAction(i18n("Clear list"), q, SLOT(clear()));
+  clearAction->setVisible(false);
   q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*)));
 }
 
@@ -145,7 +148,9 @@ void KRecentFilesAction::addUrl( const K
         delete removeAction(selectableActionGroup()->actions().first());
     }
 
-    if (d->m_noEntriesAction) removeAction(d->m_noEntriesAction)->deleteLater();
+    d->m_noEntriesAction->setVisible(false);
+    d->clearSeparator->setVisible(d->clearActionVisible);
+    d->clearAction->setVisible(d->clearActionVisible);
     // add file to list
     const QString title = tmpName + " [" + file + ']';
     QAction* action = new QAction(title, selectableActionGroup());
@@ -199,16 +204,30 @@ KUrl::List KRecentFilesAction::urls() co
   return d->m_urls.values ();
 }
 
+bool KRecentFilesAction::clearActionVisible() const
+{
+    Q_D(const KRecentFilesAction);
+    return d->clearActionVisible;
+}
+
+void KRecentFilesAction::setClearActionVisible(bool visible)
+{
+    Q_D(KRecentFilesAction);
+    d->clearActionVisible = visible;
+    visible = visible && !selectableActionGroup()->actions().isEmpty();
+    d->clearSeparator->setVisible(visible);
+    d->clearAction->setVisible(visible);
+}
+
 void KRecentFilesAction::clear()
 {
     Q_D(KRecentFilesAction);
     KSelectAction::clear();
     d->m_shortNames.clear();
     d->m_urls.clear();
-    if (d->m_noEntriesAction) KSelectAction::removeAction(d->m_noEntriesAction)->deleteLater();
-    d->m_noEntriesAction=new QAction(i18n("No entries"),selectableActionGroup());
-    d->m_noEntriesAction->setEnabled(false);
-    KSelectAction::addAction(d->m_noEntriesAction);
+    d->m_noEntriesAction->setVisible(true);
+    d->clearSeparator->setVisible(false);
+    d->clearAction->setVisible(false);
 }
 
 void KRecentFilesAction::loadEntries( const KConfigGroup& _config)
@@ -261,7 +280,9 @@ void KRecentFilesAction::loadEntries( co
     }
     if (thereAreEntries)
     {
-        if (d->m_noEntriesAction) KSelectAction::removeAction(d->m_noEntriesAction)->deleteLater();
+        d->m_noEntriesAction->setVisible(false);
+        d->clearSeparator->setVisible(d->clearActionVisible);
+        d->clearAction->setVisible(d->clearActionVisible);
     }
 }
 
@@ -279,8 +300,6 @@ void KRecentFilesAction::saveEntries( co
     cg.deleteGroup();
 
     // write file list
-    if ( (selectableActionGroup()->actions().count()>1) ||
-	( (selectableActionGroup()->actions().count()==1) && (selectableActionGroup()->actions()[0]!=d->m_noEntriesAction) ) )
     for ( int i = 1 ; i <= selectableActionGroup()->actions().count() ; i++ )
     {
         key = QString( "File%1" ).arg( i );
diff -dNpru actions-orig/krecentfilesaction.h actions/krecentfilesaction.h
--- actions-orig/krecentfilesaction.h	2009-03-11 20:10:59.000000000 +0100
+++ actions/krecentfilesaction.h	2009-03-11 20:43:53.000000000 +0100
@@ -47,6 +47,7 @@ class KDEUI_EXPORT KRecentFilesAction : 
 {
   Q_OBJECT
   Q_PROPERTY( int maxItems READ maxItems WRITE setMaxItems )
+  Q_PROPERTY( bool clearActionVisible READ clearActionVisible WRITE setClearActionVisible )
   Q_DECLARE_PRIVATE(KRecentFilesAction)
 
 public:
@@ -103,11 +104,6 @@ public:
   virtual QAction* removeAction(QAction* action);
 
   /**
-   * Reimplemented for internal reasons.
-   */
-  virtual void clear();
-
-  /**
    *  Returns the maximum of items in the recent files list.
    */
   int maxItems() const;
@@ -158,6 +154,18 @@ public:
    */
   KUrl::List urls() const;
 
+  /**
+   * Returns whether an entry for clearing the recent files list is visible.
+   */
+  bool clearActionVisible() const;
+
+  /**
+   * Shows or hides an entry for clearing the recent files list (not shown by default).
+   *
+   * @param visible True to show and false to hide.
+   */
+  void setClearActionVisible(bool visible);
+
 Q_SIGNALS:
   /**
    *  This signal gets emitted when the user selects an URL.
@@ -166,6 +174,12 @@ Q_SIGNALS:
    */
   void urlSelected( const KUrl& url );
 
+public Q_SLOTS:
+  /**
+   * Reimplemented for internal reasons.
+   */
+  virtual void clear();
+
 private:
 
     // Don't warn about the virtual overload. As the comment of the other
diff -dNpru actions-orig/krecentfilesaction_p.h actions/krecentfilesaction_p.h
--- actions-orig/krecentfilesaction_p.h	2009-03-11 20:10:59.000000000 +0100
+++ actions/krecentfilesaction_p.h	2009-03-11 21:03:42.000000000 +0100
@@ -39,6 +39,9 @@ public:
   {
     m_maxItems = 10;
     m_noEntriesAction=0;
+    clearActionVisible = false;
+    clearSeparator = 0;
+    clearAction = 0;
   }
 
   virtual ~KRecentFilesActionPrivate()
@@ -52,7 +55,10 @@ public:
   int m_maxItems;
   QMap<QAction*, QString> m_shortNames;
   QMap<QAction*, KUrl> m_urls;
-  QPointer<QAction> m_noEntriesAction;
+  QAction *m_noEntriesAction;
+  bool clearActionVisible;
+  QAction *clearSeparator;
+  QAction *clearAction;
 };
 
 /* vim: et sw=2 ts=2


More information about the kde-core-devel mailing list