lib

KoSelectAction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Peter Simonsson <psn@linux.se>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KoSelectAction.h"
21
22#include <tqpixmap.h>
23#include <tqbitmap.h>
24#include <tqwhatsthis.h>
25#include <tqmenubar.h>
26
27#include <tdepopupmenu.h>
28#include <tdeapplication.h>
29#include <kdebug.h>
30#include <tdetoolbar.h>
31#include <tdetoolbarbutton.h>
32#include <kiconloader.h>
33#include <tdelocale.h>
34
35class KoSelectAction::KoSelectActionPrivate
36{
37 public:
38 KoSelectActionPrivate()
39 {
40 m_popup = new TDEPopupMenu(0L,"KoLineStyleAction::popup");
41 m_currentSelection = 0;
42 }
43
44 ~KoSelectActionPrivate()
45 {
46 delete m_popup;
47 m_popup = 0;
48 }
49
50 TDEPopupMenu* m_popup;
51 int m_currentSelection;
52};
53
54KoSelectAction::KoSelectAction(const TQString &text, const TQString& icon,
55 TQObject* parent, const char* name) : TDEAction(text, icon, 0, parent, name)
56{
57 d = new KoSelectActionPrivate;
59
60 connect(popupMenu(), TQ_SIGNAL(activated(int)), this, TQ_SLOT(execute(int)));
61}
62
63KoSelectAction::KoSelectAction(const TQString &text, const TQString& icon, const TQObject* receiver,
64 const char* slot, TQObject* parent, const char* name) : TDEAction(text, icon, 0, parent, name)
65{
66 d = new KoSelectActionPrivate;
67
68 connect(this, TQ_SIGNAL(selectionChanged(int)), receiver, slot);
69 connect(popupMenu(), TQ_SIGNAL(activated(int)), this, TQ_SLOT(execute(int)));
70}
71
72KoSelectAction::~KoSelectAction()
73{
74 delete d;
75}
76
78{
79 return d->m_popup;
80}
81
82void KoSelectAction::popup(const TQPoint& global)
83{
84 popupMenu()->popup(global);
85}
86
87int KoSelectAction::plug(TQWidget* widget, int index)
88{
89 // This function is copied from TDEActionMenu::plug
90 if (kapp && !kapp->authorizeTDEAction(name()))
91 return -1;
92 kdDebug(129) << "TDEAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
93 if ( widget->inherits("TQPopupMenu") )
94 {
95 TQPopupMenu* menu = static_cast<TQPopupMenu*>( widget );
96 int id;
97
98 if ( hasIconSet() )
99 id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
100 else
101 id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), TDEIcon::Small),
102 text(), popupMenu(), -1, index );
103
104 if ( !isEnabled() )
105 menu->setItemEnabled( id, false );
106
107 addContainer( menu, id );
108 connect( menu, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
109
110 return containerCount() - 1;
111 }
112 else if ( widget->inherits( "TDEToolBar" ) )
113 {
114 TDEToolBar *bar = static_cast<TDEToolBar *>( widget );
115
116 int id_ = TDEAction::getToolButtonID();
117
118 if ( icon().isEmpty() && !iconSet().isNull() ) {
119 bar->insertButton( iconSet().pixmap(), id_, TQ_SIGNAL( clicked() ), this,
120 TQ_SLOT( slotActivated() ), isEnabled(), plainText(),
121 index );
122 } else {
123 TDEInstance *instance;
124
125 if ( m_parentCollection ) {
126 instance = m_parentCollection->instance();
127 } else {
128 instance = TDEGlobal::instance();
129 }
130
131 bar->insertButton( icon(), id_, TQ_SIGNAL( clicked() ), this,
132 TQ_SLOT( slotActivated() ), isEnabled(), plainText(),
133 index, instance );
134 }
135
136 addContainer( bar, id_ );
137
138 if (!whatsThis().isEmpty())
139 TQWhatsThis::add( bar->getButton(id_), whatsThis() );
140
141 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
142
143 bar->getButton(id_)->setPopup(popupMenu(), true );
144
145 return containerCount() - 1;
146 }
147 else if ( widget->inherits( "TQMenuBar" ) )
148 {
149 TQMenuBar *bar = static_cast<TQMenuBar *>( widget );
150
151 int id;
152
153 id = bar->insertItem( text(), popupMenu(), -1, index );
154
155 if ( !isEnabled() )
156 bar->setItemEnabled( id, false );
157
158 addContainer( bar, id );
159 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
160
161 return containerCount() - 1;
162 }
163
164 return -1;
165}
166
168{
169 setCurrentSelection(index);
170 emit selectionChanged(d->m_currentSelection);
171}
172
174{
175 return d->m_currentSelection;
176}
177
179{
180 if(popupMenu()->isCheckable()) {
181 popupMenu()->setItemChecked(d->m_currentSelection, false);
182 popupMenu()->setItemChecked(index, true);
183 }
184
185 d->m_currentSelection = index;
186}
187
189{
190 popupMenu()->setCheckable(show);
191}
192
193#include "KoSelectAction.moc"
TDEPopupMenu * popupMenu() const
Returns a pointer to the popup menu.
virtual int currentSelection()
Returns the index of the currently selected item.
virtual void execute(int index)
Execute an item.
void selectionChanged(int)
Emited when the selection changed.
KoSelectAction(const TQString &text, const TQString &icon, TQObject *parent=0, const char *name=0)
Constructs a KoSelectAction with a text and an icon.
void popup(const TQPoint &global)
Shows the popup menu.
virtual void setCurrentSelection(int index)
Set which item that should be selected.
void setShowCurrentSelection(bool show)
If the current selection selection should be shown or not in the menu.
TQIconSet iconSet() const
TDEAction(const TQString &text, const TDEShortcut &cut, const TQObject *receiver, const char *slot, TDEActionCollection *parent, const char *name)
virtual bool isEnabled() const
virtual TQString text() const
virtual TQString whatsThis() const
void activated()
static int getToolButtonID()
void setPopup(TQPopupMenu *p, bool unused=false)
int insertButton(const TQString &icon, int id, bool enabled=true, const TQString &text=TQString::null, int index=-1, TDEInstance *_instance=TDEGlobal::instance())
TDEToolBarButton * getButton(int id)