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

List:       kde-core-devel
Subject:    Again: KAction
From:       Martijn Klingens <mklingens () yahoo ! com>
Date:       2001-05-31 14:12:01
[Download RAW message or body]

Like I said yesterday in my previous patch: I encountered the 
icons-in-popupmenus issue by accident, because I was attempting to do 
something else...

Attached is another KAction patch that enables to plug at least a basic 
KAction and a KToggleAction into a plain QWidget. As long as the widget can 
do its own layout management it's enough to do

action->plug( widget );

Otherwise you need to do

int index = action->plug( widget );
layout->addWidget( action->representative( index ), ... );

Or something similar for the used layout.

Possible uses for this are plugging actions into wizard-style apps. Those 
normally have neither a menubar nor a toolbar, but can certainly benefit from 
actions.

I'm not too familiar with KAction, so please review. The patch is so small 
that have a strong feeling to forget something, but it already seems to work 
now, don't ask me why.

It should _not_ affect the behaviour of actions plugged into toolbars or 
popupmenu's, so if it does, it's a bug in my code.

Martijn

PS: Yesterday's patch is also in the diff because it's still not committed to 
CVS. If I commit this before the first patch I'll temporarily remove it from 
my code, I didn't bother to do this now...

["kaction.cpp.patch-2" (text/x-c)]

Index: kaction.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kaction.cpp,v
retrieving revision 1.182
diff -b -u -p -r1.182 kaction.cpp
--- kaction.cpp	2001/05/25 22:32:10	1.182
+++ kaction.cpp	2001/05/31 13:59:21
@@ -34,6 +34,7 @@
 #include <qptrdict.h>
 #include <qfontdatabase.h>
 #include <qwhatsthis.h>
+#include <qpushbutton.h>
 #include <qtooltip.h>
 #include <kiconloader.h>
 #include <kpopupmenu.h>
@@ -428,12 +429,10 @@ int KAction::plug( QWidget *w, int index
     QPopupMenu* menu = static_cast<QPopupMenu*>( w );
     int id;
 
-    if (!inherits("KToggleAction")) {
-        if ( !d->m_pixmap.isNull() ) {
+    if ( !d->m_pixmap.isNull() )
             id = menu->insertItem( d->m_pixmap, this, SLOT( slotActivated() ),
                                    d->m_accel, -1, index );
-        } else {
-            if ( d->m_bIconSet )
+    else if ( d->m_bIconSet )
                 id = menu->insertItem( iconSet(), d->m_text, this,//dsweet
                                        SLOT( slotActivated() ), d->m_accel,
                                        -1, index );
@@ -441,14 +440,6 @@ int KAction::plug( QWidget *w, int index
                 id = menu->insertItem( d->m_text, this,
                                        SLOT( slotActivated() ),  //dsweet
                                        d->m_accel, -1, index );
-        }
-    } else {
-        // toggle actions do not have a pixmap or icon in the menu
-        // Lotzi B.
-        id = menu->insertItem( d->m_text, this,
-                               SLOT( slotActivated() ),
-                               d->m_accel, -1, index );
-    }
 
     // call setItemEnabled only if the item really should be disabled,
     // because that method is slow and the item is per default enabled
@@ -507,6 +498,45 @@ int KAction::plug( QWidget *w, int index
 
     return containerCount() - 1;
   }
+  else
+  {
+    // We're not plugging into a toolbar, nor into a popup menu...
+    // as a last resort, create a normal widget. This will only work
+    // properly if the parent widget automatically manages layout,
+    // but it's better than not being able to plug at all.
+    // Also, an ugly way to find the new widget is by its name:
+    // the widget is called 'action_' + the action's name
+
+    QPushButton *btn;
+
+    if ( !d->m_pixmap.isNull() )
+    {
+        btn = new QPushButton( w, QCString( "action_" ) + name() );
+        btn->setPixmap( d->m_pixmap );
+    }
+    else if ( d->m_bIconSet )
+        btn = new QPushButton( iconSet(), d->m_text, w,
+                               QCString( "action_" ) + name() );
+    else
+        btn = new QPushButton( d->m_text, w,
+                               QCString( "action_" ) + name() );
+
+    connect( btn, SIGNAL( clicked() ), this, SLOT( slotActivated() ) );
+
+    if ( !d->m_enabled )
+        btn->setEnabled( false );
+
+    if ( !d->m_whatsThis.isEmpty() )
+        QWhatsThis::add( btn, d->m_whatsThis );
+
+    addContainer( w, btn );
+    connect( w, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
+
+    if ( m_parentCollection )
+        m_parentCollection->connectHighlight( w, this );
+
+    return d->m_containers.count() - 1;
+  }
 
   return -1;
 }
@@ -951,12 +981,6 @@ KToggleAction::~KToggleAction()
 
 int KToggleAction::plug( QWidget* widget, int index )
 {
-  if ( !widget->inherits("QPopupMenu") && !widget->inherits("KToolBar") )
-  {
-    kdWarning() << "Can not plug KToggleAction in " << widget->className() << endl;
-    return -1;
-  }
-
   int _index = KAction::plug( widget, index );
   if ( _index == -1 )
     return _index;
@@ -971,6 +995,14 @@ int KToggleAction::plug( QWidget* widget
 
     bar->setToggle( itemId( _index ), TRUE );
     bar->setButton( itemId( _index ), isChecked() );
+  }
+  else
+  {
+    // Plugged into plain QWidget. Action is created as QPushButton,
+    // make it a toggle button:
+    QPushButton *btn = static_cast<QPushButton*>( representative( _index ) );
+    btn->setToggleButton( true );
+    btn->setOn( d->m_checked );
   }
 
   return _index;


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

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