00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00059 connect(popupMenu(), SIGNAL(activated(int)), this, SLOT(execute(int)));
00060 }
00061
00062 KoSelectAction::KoSelectAction(const QString &text, const QString& icon, const QObject* receiver,
00063 const char* slot, QObject* parent, const char* name) : KAction(text, icon, 0, parent, name)
00064 {
00065 d = new KoSelectActionPrivate;
00066
00067 connect(this, SIGNAL(selectionChanged(int)), receiver, slot);
00068 connect(popupMenu(), SIGNAL(activated(int)), this, SLOT(execute(int)));
00069 }
00070
00071 KoSelectAction::~KoSelectAction()
00072 {
00073 delete d;
00074 }
00075
00076 KPopupMenu* KoSelectAction::popupMenu() const
00077 {
00078 return d->m_popup;
00079 }
00080
00081 void KoSelectAction::popup(const QPoint& global)
00082 {
00083 popupMenu()->popup(global);
00084 }
00085
00086 int KoSelectAction::plug(QWidget* widget, int index)
00087 {
00088
00089 if (kapp && !kapp->authorizeKAction(name()))
00090 return -1;
00091 kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl;
00092 if ( widget->inherits("QPopupMenu") )
00093 {
00094 QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
00095 int id;
00096
00097 if ( hasIconSet() )
00098 id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
00099 else
00100 id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), KIcon::Small),
00101 text(), popupMenu(), -1, index );
00102
00103 if ( !isEnabled() )
00104 menu->setItemEnabled( id, false );
00105
00106 addContainer( menu, id );
00107 connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00108
00109 return containerCount() - 1;
00110 }
00111 else if ( widget->inherits( "KToolBar" ) )
00112 {
00113 KToolBar *bar = static_cast<KToolBar *>( widget );
00114
00115 int id_ = KAction::getToolButtonID();
00116
00117 if ( icon().isEmpty() && !iconSet().isNull() ) {
00118 bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
00119 SLOT( slotActivated() ), isEnabled(), plainText(),
00120 index );
00121 } else {
00122 KInstance *instance;
00123
00124 if ( m_parentCollection ) {
00125 instance = m_parentCollection->instance();
00126 } else {
00127 instance = KGlobal::instance();
00128 }
00129
00130 bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
00131 SLOT( slotActivated() ), isEnabled(), plainText(),
00132 index, instance );
00133 }
00134
00135 addContainer( bar, id_ );
00136
00137 if (!whatsThis().isEmpty())
00138 QWhatsThis::add( bar->getButton(id_), whatsThis() );
00139
00140 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00141
00142 bar->getButton(id_)->setPopup(popupMenu(), true );
00143
00144 return containerCount() - 1;
00145 }
00146 else if ( widget->inherits( "QMenuBar" ) )
00147 {
00148 QMenuBar *bar = static_cast<QMenuBar *>( widget );
00149
00150 int id;
00151
00152 id = bar->insertItem( text(), popupMenu(), -1, index );
00153
00154 if ( !isEnabled() )
00155 bar->setItemEnabled( id, false );
00156
00157 addContainer( bar, id );
00158 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00159
00160 return containerCount() - 1;
00161 }
00162
00163 return -1;
00164 }
00165
00166 void KoSelectAction::execute(int index)
00167 {
00168 setCurrentSelection(index);
00169 emit selectionChanged(d->m_currentSelection);
00170 }
00171
00172 int KoSelectAction::currentSelection()
00173 {
00174 return d->m_currentSelection;
00175 }
00176
00177 void KoSelectAction::setCurrentSelection(int index)
00178 {
00179 popupMenu()->setItemChecked(d->m_currentSelection, false);
00180 popupMenu()->setItemChecked(index, true);
00181 d->m_currentSelection = index;
00182 }
00183
00184 #include "koselectaction.moc"