lib

ttdeaction.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000 theKompany.com
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20#include "ttdeaction.h"
21#include "ttdetoolbarbutton.h"
22#include "tkcombobox.h"
23
24#include <tqlabel.h>
25#include <tqlayout.h>
26
27#include <tdetoolbar.h>
28#include <kiconloader.h>
29
30#define SET_FOR_ALL_CONTAINER(WIDGET_TYPE,METHOD_NAME,VALUE) \
31 for( int id = 0; id < containerCount(); ++id ) { \
32 TQWidget* w = container(id); \
33 if ( w->inherits("TDEToolBar") ) { \
34 TQWidget* r = static_cast<TDEToolBar*>(w)->getWidget(itemId(id)); \
35 if (qstrcmp(r->name(),"KTToolBarLayout")==0) \
36 r = (TQWidget*)r->child("widget"); \
37 if ( r && r->inherits(#WIDGET_TYPE) ) { \
38 WIDGET_TYPE* b = static_cast<WIDGET_TYPE*>(r); \
39 b->METHOD_NAME(VALUE); \
40 } \
41 } \
42 }
43
44TTDEAction::TTDEAction(TQObject* parent, const char* name)
45: TDEAction( "", 0, parent, name )
46{
47 m_imode = TK::IconOnly;
48}
49
50TTDEAction::~TTDEAction()
51{
52}
53
54int TTDEAction::plug(TQWidget* widget, int index)
55{
56 if ( widget->inherits("TDEToolBar") ) {
57 TDEToolBar* bar = static_cast<TDEToolBar*>(widget);
59 TDEInstance *instance;
60
61 if ( parentCollection() )
62 instance = parentCollection()->instance();
63 else
64 instance = TDEGlobal::instance();
65
66 TTDEToolBarButton* b = new TTDEToolBarButton(icon(),plainText(),bar,name(),instance);
67 // we don't need clicked() and buttonClicked(), do we?
68 // connect(b,TQ_SIGNAL(clicked()),TQ_SLOT(slotActivated()));
69 b->setIconMode(m_imode);
70 initToolBarButton(b);
71
72 bar->insertWidget( id_, 100, b, index );
73 addContainer(bar,id_);
74 connect( bar, TQ_SIGNAL(destroyed()), this, TQ_SLOT(slotDestroyed()) );
75
76 return containerCount() - 1;
77 }
78 return TDEAction::plug(widget,index);
79}
80
81void TTDEAction::initToolBarButton(TTDEToolBarButton* button)
82{
83 connect(button,TQ_SIGNAL(buttonClicked()),TQ_SLOT(slotActivated()));
84}
85
86TK::IconMode TTDEAction::iconMode()
87{
88 return m_imode;
89}
90
91void TTDEAction::setIconMode(TK::IconMode mode)
92{
93 m_imode = mode;
94 SET_FOR_ALL_CONTAINER(TTDEToolBarButton,setIconMode,mode)
95}
96
97void TTDEAction::setText(const TQString& text)
98{
100 updateLayout();
101}
102
103void TTDEAction::setIcon(const TQString& icon)
104{
105 TDEAction::setIcon(icon);
106 updateLayout();
107}
108
109void TTDEAction::updateLayout()
110{
111 int len = containerCount();
112 for( int id = 0; id < len; ++id ) {
113 TQWidget* w = container( id );
114 if (w->inherits("TDEToolBar")) {
115 TQWidget* r = static_cast<TDEToolBar*>(w)->getWidget(itemId(id));
116 if (qstrcmp(r->name(),"KTToolBarLayout")==0) {
117 updateLayout(r);
118 }
119 }
120 }
121}
122
123TQWidget* TTDEAction::createLayout(TQWidget* parent, TQWidget* children)
124{
125 TQWidget* base = new TQWidget(parent,"KTToolBarLayout");
126 TQLabel* textLabel = new TQLabel(base,"text");
127 textLabel->setMinimumHeight(1);
128 TQLabel* pixLabel = new TQLabel(base,"pixmap");
129 children->reparent(base,TQPoint(0,0));
130 children->setName("widget");
131 TQHBoxLayout* layout = new TQHBoxLayout(base,0,3);
132 layout->setResizeMode(TQLayout::Minimum);
133 layout->addWidget(textLabel);
134 layout->addWidget(pixLabel);
135 layout->addWidget(children,1);
136
137 updateLayout(base);
138 return base;
139}
140
141void TTDEAction::updateLayout(TQWidget* base)
142{
143 TQLabel* textLabel = (TQLabel*)base->child("text");
144 TQLabel* pixLabel = (TQLabel*)base->child("pixmap");
145 TQWidget* w = (TQWidget*)base->child("widget");
146
147 if (!textLabel || !pixLabel || !w)
148 return;
149
150 if (!text().isEmpty() && m_imode != TK::IconOnly ) {
151 textLabel->setText(text());
152 textLabel->show();
153 } else
154 textLabel->hide();
155
156 TQPixmap pix;
157 if (hasIcon())
158 pix = iconSet(TDEIcon::Small).pixmap();
159
160 if (!icon().isEmpty())
161 pix = BarIcon(icon());
162
163 if (!pix.isNull() && m_imode != TK::TextOnly) {
164 pixLabel->setPixmap(pix);
165 pixLabel->show();
166 } else
167 pixLabel->hide();
168
169 base->setFixedWidth( w->sizeHint().width() +
170 (textLabel->isVisible() ? textLabel->sizeHint().width():0) +
171 (pixLabel->isVisible() ? pixLabel->sizeHint().width():0) );
172}
173/******************************************************************************/
174TKBaseSelectAction::TKBaseSelectAction( TQObject* parent, const char* name )
175: TTDEAction(parent,name)
176{
177 m_current = 0;
178 m_editable = false;
179}
180
181TKBaseSelectAction::~TKBaseSelectAction()
182{
183}
184
185int TKBaseSelectAction::plug(TQWidget* widget, int index)
186{
187 if ( widget->inherits("TDEToolBar") )
188 {
189 TDEToolBar* bar = static_cast<TDEToolBar*>( widget );
190 int id_ = TDEAction::getToolButtonID();
191
192 TKComboBox* cb = new TKComboBox(m_editable,bar);
193 initComboBox(cb);
194 cb->setMinimumWidth( cb->sizeHint().width() );
195 TQWidget* base = createLayout(bar,cb);
196
197 bar->insertWidget( id_, 100, base, index );
198 addContainer( bar, id_ );
199
200 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
201
202 setCurrentItem(currentItem());
203
204 return containerCount() - 1;
205 }
206 return -1;
207}
208
209int TKBaseSelectAction::currentItem()
210{
211 return m_current;
212}
213
214void TKBaseSelectAction::initComboBox(TKComboBox* cb)
215{
216 connect(cb,TQ_SIGNAL(activated(int)),TQ_SLOT(slotActivated(int)));
217}
218
219void TKBaseSelectAction::setEditable(bool editable)
220{
221 m_editable = editable;
222 SET_FOR_ALL_CONTAINER(TKComboBox,setEditable,editable)
223}
224
225bool TKBaseSelectAction::isEditable()
226{
227 return m_editable;
228}
229
230void TKBaseSelectAction::setCurrentItem(int index)
231{
232 m_current = index;
233 SET_FOR_ALL_CONTAINER(TKComboBox,setCurrentItem,index)
234}
235
236void TKBaseSelectAction::slotActivated(int id)
237{
238 if ( m_current == id )
239 return;
240
241 m_current = id;
242 setCurrentItem(id);
243 activate(id);
244}
245
247{
248 emit activated(id);
249}
250/******************************************************************************/
251TTDESelectAction::TTDESelectAction( TQObject* parent, const char* name )
252: TKBaseSelectAction(parent,name)
253{
254}
255
256TTDESelectAction::~TTDESelectAction()
257{
258}
259
260void TTDESelectAction::initComboBox(TKComboBox* cb)
261{
262 TKBaseSelectAction::initComboBox(cb);
263 connect(cb,TQ_SIGNAL(activated(const TQString&)),TQ_SLOT(slotActivated(const TQString&)));
264 cb->insertStringList(items());
265}
266
267void TTDESelectAction::slotActivated(const TQString& text)
268{
269 emit activated(text);
270}
271
272void TTDESelectAction::setItems(const TQStringList& lst )
273{
274 m_list = lst;
275 m_current = -1;
276
277 SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
278 SET_FOR_ALL_CONTAINER(TKComboBox,insertStringList,lst)
279
280 // Disable if empty and not editable
281 setEnabled ( lst.count() > 0 || m_editable );
282}
283
284TQStringList TTDESelectAction::items() const
285{
286 return m_list;
287}
288
289void TTDESelectAction::clear()
290{
291 SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
292}
293
294void TTDESelectAction::setEditText(const TQString& text)
295{
296 SET_FOR_ALL_CONTAINER(TKComboBox,setEditText,text)
297}
298
299#undef SET_FOR_ALL_CONTAINER
300#include "ttdeaction.moc"
TQIconSet iconSet() const
virtual void activate()
virtual void setEnabled(bool enable)
virtual TQString text() const
virtual void setText(const TQString &text)
void activated()
virtual int plug(TQWidget *widget, int index=-1)
static int getToolButtonID()
int insertWidget(int id, int width, TQWidget *_widget, int index=-1)
const char * name(StdAction id)