lib

KoSelectAction.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Peter Simonsson <psn@linux.se>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KoSelectAction.h"
00021 
00022 #include <qpixmap.h>
00023 #include <qbitmap.h>
00024 #include <qwhatsthis.h>
00025 #include <qmenubar.h>
00026 
00027 #include <kpopupmenu.h>
00028 #include <kapplication.h>
00029 #include <kdebug.h>
00030 #include <ktoolbar.h>
00031 #include <ktoolbarbutton.h>
00032 #include <kiconloader.h>
00033 #include <klocale.h>
00034 
00035 class KoSelectAction::KoSelectActionPrivate
00036 {
00037   public:
00038     KoSelectActionPrivate()
00039     {
00040       m_popup = new KPopupMenu(0L,"KoLineStyleAction::popup");
00041       m_currentSelection = 0;
00042     }
00043     
00044     ~KoSelectActionPrivate()
00045     {
00046       delete m_popup;
00047       m_popup = 0;
00048     }
00049     
00050     KPopupMenu* m_popup;
00051     int m_currentSelection;
00052 };
00053 
00054 KoSelectAction::KoSelectAction(const QString &text, const QString& icon,
00055   QObject* parent, const char* name) : KAction(text, icon, 0, parent, name)
00056 {
00057   d = new KoSelectActionPrivate;
00058   setShowCurrentSelection(true);
00059   
00060   connect(popupMenu(), SIGNAL(activated(int)), this, SLOT(execute(int)));
00061 }
00062 
00063 KoSelectAction::KoSelectAction(const QString &text, const QString& icon, const QObject* receiver,
00064   const char* slot, QObject* parent, const char* name) : KAction(text, icon, 0, parent, name)
00065 {
00066   d = new KoSelectActionPrivate;
00067   
00068   connect(this, SIGNAL(selectionChanged(int)), receiver, slot);
00069   connect(popupMenu(), SIGNAL(activated(int)), this, SLOT(execute(int)));
00070 }
00071 
00072 KoSelectAction::~KoSelectAction()
00073 {
00074   delete d;
00075 }
00076 
00077 KPopupMenu* KoSelectAction::popupMenu() const
00078 {
00079   return d->m_popup;
00080 }
00081 
00082 void KoSelectAction::popup(const QPoint& global)
00083 {
00084   popupMenu()->popup(global);
00085 }
00086 
00087 int KoSelectAction::plug(QWidget* widget, int index)
00088 {
00089   // This function is copied from KActionMenu::plug
00090   if (kapp && !kapp->authorizeKAction(name()))
00091     return -1;
00092   kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
00093   if ( widget->inherits("QPopupMenu") )
00094   {
00095     QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
00096     int id;
00097 
00098     if ( hasIconSet() )
00099       id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
00100     else
00101       id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), KIcon::Small),
00102         text(), popupMenu(), -1, index );
00103 
00104     if ( !isEnabled() )
00105       menu->setItemEnabled( id, false );
00106 
00107     addContainer( menu, id );
00108     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00109 
00110     return containerCount() - 1;
00111   }
00112   else if ( widget->inherits( "KToolBar" ) )
00113   {
00114     KToolBar *bar = static_cast<KToolBar *>( widget );
00115 
00116     int id_ = KAction::getToolButtonID();
00117 
00118     if ( icon().isEmpty() && !iconSet().isNull() ) {
00119       bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
00120                           SLOT( slotActivated() ), isEnabled(), plainText(),
00121                           index );
00122     } else {
00123       KInstance *instance;
00124 
00125       if ( m_parentCollection ) {
00126         instance = m_parentCollection->instance();
00127       } else {
00128         instance = KGlobal::instance();
00129       }
00130 
00131       bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
00132                           SLOT( slotActivated() ), isEnabled(), plainText(),
00133                           index, instance );
00134     }
00135 
00136     addContainer( bar, id_ );
00137 
00138     if (!whatsThis().isEmpty())
00139       QWhatsThis::add( bar->getButton(id_), whatsThis() );
00140 
00141     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00142 
00143     bar->getButton(id_)->setPopup(popupMenu(), true );
00144 
00145     return containerCount() - 1;
00146   }
00147   else if ( widget->inherits( "QMenuBar" ) )
00148   {
00149     QMenuBar *bar = static_cast<QMenuBar *>( widget );
00150 
00151     int id;
00152 
00153     id = bar->insertItem( text(), popupMenu(), -1, index );
00154 
00155     if ( !isEnabled() )
00156       bar->setItemEnabled( id, false );
00157 
00158     addContainer( bar, id );
00159     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00160 
00161     return containerCount() - 1;
00162   }
00163 
00164   return -1;
00165 }
00166 
00167 void KoSelectAction::execute(int index)
00168 {
00169   setCurrentSelection(index);
00170   emit selectionChanged(d->m_currentSelection);
00171 }
00172 
00173 int KoSelectAction::currentSelection()
00174 {
00175   return d->m_currentSelection;
00176 }
00177 
00178 void KoSelectAction::setCurrentSelection(int index)
00179 {
00180   if(popupMenu()->isCheckable()) {
00181     popupMenu()->setItemChecked(d->m_currentSelection, false);
00182     popupMenu()->setItemChecked(index, true);
00183   }
00184 
00185   d->m_currentSelection = index;
00186 }
00187 
00188 void KoSelectAction::setShowCurrentSelection(bool show)
00189 {
00190   popupMenu()->setCheckable(show);
00191 }
00192 
00193 #include "KoSelectAction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys