21#include <kiconloader.h>
24#include <tqpushbutton.h>
26static int g_koKoolBarId = 0;
28KoKoolBar::KoKoolBar( TQWidget *_parent,
const char *_name ) :
29 TQWidget( _parent, _name ), m_iActiveGroup( -1 )
31 m_mapGroups.setAutoDelete(
true );
32 m_pBox =
new KoKoolBarBox(
this );
35int KoKoolBar::insertGroup(
const TQString& _text )
37 KoKoolBarGroup *p =
new KoKoolBarGroup(
this, _text );
38 m_mapGroups.insert( p->id(), p );
40 if ( m_iActiveGroup == -1 )
41 setActiveGroup( p->id() );
47int KoKoolBar::insertItem(
int _grp,
const TQPixmap& _pix,
const TQString& _text,
48 TQObject *_obj,
const char *_slot )
50 KoKoolBarGroup* g = m_mapGroups[ _grp ];
53 KoKoolBarItem *item =
new KoKoolBarItem( g, _pix, _text );
55 if ( _obj != 0L && _slot != 0L )
56 connect( item, TQ_SIGNAL( pressed(
int,
int ) ), _obj, _slot );
59 if ( g->id() == m_iActiveGroup )
65void KoKoolBar::removeGroup(
int _grp )
67 KoKoolBarGroup* g = m_mapGroups[ _grp ];
71 m_mapGroups.remove( _grp );
73 if ( _grp == m_iActiveGroup )
75 if ( m_mapGroups.count() == 0 )
78 m_pBox->setActiveGroup( 0L );
82 TQIntDictIterator<KoKoolBarGroup> it( m_mapGroups );
84 m_iActiveGroup = g->id();
85 m_pBox->setActiveGroup( g );
92void KoKoolBar::removeItem(
int _grp,
int _id )
94 KoKoolBarGroup* g = m_mapGroups[ _grp ];
100 if ( g->id() == m_iActiveGroup )
104void KoKoolBar::renameItem(
int _grp,
int _id,
const TQString & _text )
106 KoKoolBarGroup* g = m_mapGroups[ _grp ];
110 KoKoolBarItem * item = g->item( _id );
114 item->setText( _text );
116 if ( g->id() == m_iActiveGroup )
120void KoKoolBar::setActiveGroup(
int _grp )
122 KoKoolBarGroup* g = m_mapGroups[ _grp ];
126 m_iActiveGroup = g->id();
127 m_pBox->setActiveGroup( g );
132void KoKoolBar::resizeEvent( TQResizeEvent * ev )
134 if ( m_iActiveGroup == -1 )
137 int buttonheight = fontMetrics().height() + 4;
139 KoKoolBarGroup *g = m_mapGroups[ m_iActiveGroup ];
144 TQIntDictIterator<KoKoolBarGroup> it( m_mapGroups );
145 while( it.current() != g )
148 TQIntDictIterator<KoKoolBarGroup> pos = it;
153 TQIntDictIterator<KoKoolBarGroup> i = it;
160 int y = height() - buttonheight * result;
161 for( ; it.current(); ++it )
163 it.current()->button()->setGeometry( 0, y, width(), buttonheight );
164 it.current()->button()->show();
171 while( it.current() != pos.current() )
173 it.current()->button()->setGeometry( 0, y2, width(), buttonheight );
174 it.current()->button()->show();
179 if ( height() - y2 - result * buttonheight >= 0 )
182 m_pBox->setGeometry( 0, y2, width(), height() - y2 - result * buttonheight );
184 m_pBox->sizeChanged();
191void KoKoolBar::enableItem(
int _grp,
int _id,
bool _enable )
193 KoKoolBarGroup* g = m_mapGroups[ _grp ];
196 KoKoolBarItem *item = g->item( _id );
199 item->setEnabled( _enable );
202void KoKoolBar::enableGroup(
int _grp,
bool _enable )
204 KoKoolBarGroup* g = m_mapGroups[ _grp ];
207 g->setEnabled( _enable );
210KoKoolBarBox::KoKoolBarBox( KoKoolBar *_bar ) :
211 TQFrame( _bar ), m_pBar( _bar ),
212 m_pButtonUp( 0L ), m_pButtonDown( 0L )
218 setFrameShape( StyledPanel );
219 setFrameShadow( Sunken );
221 setBackgroundColor( colorGroup().background() );
224void KoKoolBarBox::setActiveGroup( KoKoolBarGroup *_grp )
232bool KoKoolBarBox::needsScrolling()
const
234 if ( m_pGroup == 0L )
237 return ( maxHeight() > height() );
240void KoKoolBarBox::resizeEvent( TQResizeEvent * )
242 if ( needsScrolling() )
244 if ( m_pButtonUp == 0L )
246 m_pButtonUp =
new TQPushButton(
this );
247 m_pButtonUp->setPixmap( TQPixmap( UserIcon(
"koKoolBarUp" ) ) );
248 connect( m_pButtonUp, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( scrollUp() ) );
250 if ( m_pButtonDown == 0L )
252 m_pButtonDown =
new TQPushButton(
this );
253 m_pButtonDown->setPixmap( TQPixmap( UserIcon(
"koKoolBarDown" ) ) );
254 connect( m_pButtonDown, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( scrollDown() ) );
257 m_pButtonUp->raise();
258 m_pButtonDown->show();
259 m_pButtonDown->raise();
260 updateScrollButtons();
267 m_pButtonDown->hide();
271KoKoolBarItem* KoKoolBarBox::findByPos(
int _abs_y )
const
273 if ( m_pGroup == 0L )
278 TQIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
279 for ( ; it.current(); ++it )
281 int dy = it.current()->height();
282 if ( y <= _abs_y && _abs_y <= y + dy )
290int KoKoolBarBox::maxHeight()
const
294 TQIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
295 for ( ; it.current(); ++it )
296 y += it.current()->height();
301bool KoKoolBarBox::isAtTop()
const
303 return ( m_iYIcon == 0 );
306bool KoKoolBarBox::isAtBottom()
const
308 if ( m_pGroup->items() == 0 )
311 if ( height() + m_iYOffset >= h )
313 if ( m_pGroup->items() - 1 == m_iYIcon )
318void KoKoolBarBox::scrollUp()
327 TQIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
328 for ( ; i < m_iYIcon && it.current(); ++it )
330 y += it.current()->height();
334 int old = m_iYOffset;
337 TQWidget::scroll( 0, old - m_iYOffset, contentsRect() );
338 updateScrollButtons();
341void KoKoolBarBox::scrollDown()
350 TQIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
351 for ( ; i < m_iYIcon && it.current(); ++it )
353 y += it.current()->height();
357 if ( y + height() > h )
360 int old = m_iYOffset;
363 TQWidget::scroll( 0, old - m_iYOffset, contentsRect() );
364 updateScrollButtons();
367void KoKoolBarBox::updateScrollButtons()
370 m_pButtonUp->setEnabled(
false );
372 m_pButtonUp->setEnabled(
true );
375 m_pButtonDown->setEnabled(
false );
377 m_pButtonDown->setEnabled(
true );
380 m_pButtonUp->setGeometry( width() - bs, height() - 2 * bs, bs, bs );
381 m_pButtonDown->setGeometry( width() - bs, height() - bs, bs, bs );
384void KoKoolBarBox::drawContents( TQPainter * painter )
386 if ( m_pGroup == 0L )
391 TQIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
392 for ( ; it.current(); ++it )
394 if ( y + it.current()->height() >= 0 && y <= height() )
396 painter->drawPixmap( ( width() - it.current()->pixmap().width() ) / 2, y, it.current()->pixmap() );
397 if ( !it.current()->text().isEmpty() )
399 int y2 = y + it.current()->pixmap().height() + 2;
400 painter->drawText( ( width() - painter->fontMetrics().width( it.current()->text() ) ) / 2,
401 y2 + painter->fontMetrics().ascent(), it.current()->text() );
405 y += it.current()->height();
409KoKoolBarGroup::KoKoolBarGroup( KoKoolBar *_bar,
const TQString& _text ) :
412 m_mapItems.setAutoDelete(
true );
414 m_pButton =
new TQPushButton( _text, _bar );
418 connect( m_pButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( pressed() ) );
419 m_id = g_koKoolBarId++;
422KoKoolBarGroup::~KoKoolBarGroup()
427void KoKoolBarGroup::remove(
int _id )
429 m_mapItems.remove( _id );
432void KoKoolBarGroup::pressed()
434 m_pBar->setActiveGroup( m_id );
437KoKoolBarItem::KoKoolBarItem( KoKoolBarGroup *_grp,
const TQPixmap& _pix,
const TQString&_text )
443 m_id = g_koKoolBarId++;
447void KoKoolBarItem::calc( TQWidget *_widget )
449 m_iHeight = pixmap().height() + 8;
451 if ( !m_strText.isEmpty() )
452 m_iHeight += _widget->fontMetrics().height() + 2;
455void KoKoolBarItem::press()
458 emit pressed( m_pGroup->id(), m_id );
489#include <KoKoolBar.moc>