F:/KPlato/koffice/libs/kofficeui/KoSelectAction.cpp

Aller à la documentation de ce fichier.
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>
00023 #include <QBitmap>
00024 
00025 #include <qmenubar.h>
00026 //Added by qt3to4:
00027 #include <Q3PopupMenu>
00028 
00029 #include <kmenu.h>
00030 #include <kapplication.h>
00031 #include <kdebug.h>
00032 #include <ktoolbar.h>
00033 
00034 #include <kiconloader.h>
00035 #include <klocale.h>
00036 #include <kauthorized.h>
00037 
00038 class KoSelectAction::KoSelectActionPrivate
00039 {
00040   public:
00041     KoSelectActionPrivate()
00042     {
00043       m_popup = new KMenu(0L);
00044           m_popup->setObjectName("KoLineStyleAction::popup");
00045       m_currentSelection = 0;
00046     }
00047     
00048     ~KoSelectActionPrivate()
00049     {
00050       delete m_popup;
00051       m_popup = 0;
00052     }
00053     
00054     KMenu* m_popup;
00055     int m_currentSelection;
00056 };
00057 
00058 KoSelectAction::KoSelectAction(const QString &text, const QString& icon,
00059   QObject* parent, const char* name) : KAction(text, icon, 0, parent, name)
00060 {
00061   d = new KoSelectActionPrivate;
00062   setShowCurrentSelection(true);
00063   
00064   connect(popupMenu(), SIGNAL(activated(int)), this, SLOT(execute(int)));
00065 }
00066 
00067 KoSelectAction::KoSelectAction(const QString &text, const QString& icon, const QObject* receiver,
00068   const char* slot, QObject* parent, const char* name) : KAction(text, icon, 0, parent, name)
00069 {
00070   d = new KoSelectActionPrivate;
00071   
00072   connect(this, SIGNAL(selectionChanged(int)), receiver, slot);
00073   connect(popupMenu(), SIGNAL(activated(int)), this, SLOT(execute(int)));
00074 }
00075 
00076 KoSelectAction::~KoSelectAction()
00077 {
00078   delete d;
00079 }
00080 
00081 KMenu* KoSelectAction::popupMenu() const
00082 {
00083   return d->m_popup;
00084 }
00085 
00086 void KoSelectAction::popup(const QPoint& global)
00087 {
00088   popupMenu()->popup(global);
00089 }
00090 
00091 int KoSelectAction::plug(QWidget* widget, int index)
00092 {
00093   // This function is copied from KActionMenu::plug
00094   if (kapp && !KAuthorized::authorizeKAction(name()))
00095     return -1;
00096   kDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
00097   if ( widget->inherits("QPopupMenu") )
00098   {
00099     Q3PopupMenu* menu = static_cast<Q3PopupMenu*>( widget );
00100     int id;
00101 
00102     if ( hasIconSet() )
00103       id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
00104     else
00105       id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), K3Icon::Small),
00106         text(), popupMenu(), -1, index );
00107 
00108     if ( !isEnabled() )
00109       menu->setItemEnabled( id, false );
00110 
00111     addContainer( menu, id );
00112     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00113 
00114     return containerCount() - 1;
00115   }
00116   else if ( widget->inherits( "KToolBar" ) )
00117   {
00118     KToolBar *bar = static_cast<KToolBar *>( widget );
00119 
00120     int id_ = KAction::getToolButtonID();
00121 
00122     if ( icon().isEmpty() && !iconSet().isNull() ) {
00123       bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
00124                           SLOT( slotActivated() ), isEnabled(), plainText(),
00125                           index );
00126     } else {
00127       KInstance *instance;
00128 
00129       if ( m_parentCollection ) {
00130         instance = m_parentCollection->instance();
00131       } else {
00132         instance = KGlobal::instance();
00133       }
00134 
00135       bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
00136                           SLOT( slotActivated() ), isEnabled(), plainText(),
00137                           index, instance );
00138     }
00139 
00140     addContainer( bar, id_ );
00141 
00142     if (!whatsThis().isEmpty())
00143       bar->getButton(id_)->setWhatsThis( whatsThis() );
00144 
00145     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00146 
00147     bar->getButton(id_)->setPopup(popupMenu(), true );
00148 
00149     return containerCount() - 1;
00150   }
00151   else if ( widget->inherits( "QMenuBar" ) )
00152   {
00153     QMenuBar *bar = static_cast<QMenuBar *>( widget );
00154 
00155     int id;
00156 
00157     id = bar->insertItem( text(), popupMenu(), -1, index );
00158 
00159     if ( !isEnabled() )
00160       bar->setItemEnabled( id, false );
00161 
00162     addContainer( bar, id );
00163     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00164 
00165     return containerCount() - 1;
00166   }
00167 
00168   return -1;
00169 }
00170 
00171 void KoSelectAction::execute(int index)
00172 {
00173   setCurrentSelection(index);
00174   emit selectionChanged(d->m_currentSelection);
00175 }
00176 
00177 int KoSelectAction::currentSelection()
00178 {
00179   return d->m_currentSelection;
00180 }
00181 
00182 void KoSelectAction::setCurrentSelection(int index)
00183 {
00184   if(popupMenu()->isCheckable()) {
00185     popupMenu()->setItemChecked(d->m_currentSelection, false);
00186     popupMenu()->setItemChecked(index, true);
00187   }
00188 
00189   d->m_currentSelection = index;
00190 }
00191 
00192 void KoSelectAction::setShowCurrentSelection(bool show)
00193 {
00194   popupMenu()->setCheckable(show);
00195 }
00196 
00197 #include "KoSelectAction.moc"

Généré le Wed Nov 22 23:41:05 2006 pour KPlato par  doxygen 1.5.1-p1