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

List:       kde-core-devel
Subject:    Re: [PATCH] Recent file action on toolbars
From:       Andras Mantia <amantia () freemail ! hu>
Date:       2003-09-21 12:23:38
[Download RAW message or body]

... and the patch is here...
["kactionclassess.cpp.diff" (text/x-diff)]

--- kactionclasses.cpp.orig	2003-09-21 13:14:40.000000000 +0300
+++ kactionclasses.cpp	2003-09-21 15:05:26.000000000 +0300
@@ -897,6 +897,7 @@
     m_maxItems = 0;
   }
   uint m_maxItems;
+  KPopupMenu *m_popup;
 };
 
 KRecentFilesAction::KRecentFilesAction( const QString& text,
@@ -993,6 +994,7 @@
              receiver, slot );
 }
 
+
 KRecentFilesAction::KRecentFilesAction( QObject* parent, const char* name,
                                         uint maxItems )
   : KListAction( parent, name )
@@ -1009,13 +1011,26 @@
            this, SLOT( itemSelected( const QString& ) ) );
 
   setMenuAccelsEnabled( false );
+  d->m_popup = 0;
 }
 
 KRecentFilesAction::~KRecentFilesAction()
 {
+  delete d->m_popup;
   delete d; d = 0;
 }
 
+KPopupMenu *KRecentFilesAction::popupMenu() const
+{
+    if ( !d->m_popup ) {
+        KRecentFilesAction *that = const_cast<KRecentFilesAction*>(this);
+        that->d->m_popup = new KPopupMenu;
+	connect(d->m_popup, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
+	connect(d->m_popup, SIGNAL(activated(int)), this, SLOT(menuItemActivated(int)));
+    }
+    return d->m_popup;
+}
+
 uint KRecentFilesAction::maxItems() const
 {
     return d->m_maxItems;
@@ -1138,6 +1153,63 @@
     emit urlSelected( KURL( text ) );
 }
 
+void KRecentFilesAction::menuItemActivated( int id )
+{
+    emit urlSelected( KURL(popupMenu()->text(id)) );
+}
+
+void KRecentFilesAction::menuAboutToShow()
+{
+    KPopupMenu *menu = popupMenu();
+    menu->clear();
+    QStringList list = items();
+    for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) 
+	menu->insertItem(*it);
+}
+
+int KRecentFilesAction::plug( QWidget *widget, int index )
+{
+  if (kapp && !kapp->authorizeKAction(name()))
+    return -1;
+  // This is very related to KActionMenu::plug.
+  // In fact this class could be an interesting base class for KActionMenu
+  if ( widget->inherits( "KToolBar" ) )
+  {
+    KToolBar *bar = (KToolBar *)widget;
+
+    int id_ = KAction::getToolButtonID();
+
+    KInstance * instance;
+    if ( m_parentCollection )
+        instance = m_parentCollection->instance();
+    else
+        instance = KGlobal::instance();
+
+    bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
+                       SLOT( slotClicked() ), isEnabled(), plainText(),
+                       index, instance );
+
+    addContainer( bar, id_ );
+
+    connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
+    
+    bar->setDelayedPopup( id_, popupMenu(), true);
+
+    if ( !whatsThis().isEmpty() )
+        QWhatsThis::add( bar->getButton( id_ ), whatsThisWithIcon() );
+
+    return containerCount() - 1;
+  }
+
+  return KListAction::plug( widget, index );
+}
+
+void KRecentFilesAction::slotClicked()
+{
+  KAction::slotActivated();
+}
+
+
 class KFontAction::KFontActionPrivate
 {
 public:

["kactionclassess.h.diff" (text/x-diff)]

--- kactionclasses.h.orig	2003-09-07 23:47:30.000000000 +0300
+++ kactionclasses.h	2003-09-21 14:42:20.000000000 +0300
@@ -741,11 +741,13 @@
    */
   virtual ~KRecentFilesAction();
 
+  virtual int plug( QWidget *widget, int index = -1 );
   /**
    *  Returns the maximum of items in the recent files list.
    */
   uint maxItems() const;
 
+
 public slots:
   /**
    *  Sets the maximum of items in the recent files list.
@@ -809,11 +811,21 @@
    *
    */
   void itemSelected( const QString& string );
+  void menuAboutToShow();
+  void menuItemActivated( int id );
+  void slotClicked();
 
 protected:
   virtual void virtual_hook( int id, void* data );
+  
 private:
   void init();
+    /**
+     * The popup menu that is shown when clicking (some time) on the toolbar
+     * button. You may want to plug items into it on creation, or connect to
+     * aboutToShow for a more dynamic menu.
+     */
+    KPopupMenu *popupMenu() const;
 
   class KRecentFilesActionPrivate;
   KRecentFilesActionPrivate *d;


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

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