2 * ktagcombobox.cpp - A combobox with support for submenues, icons and tags
4 * Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
6 * Requires the Qt widget libraries, available at no cost at
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define INCLUDE_MENUITEM_DEF 1
26 #include <qpopupmenu.h>
30 #include "ktagcombobox.h"
31 #include "ktagcombobox.moc"
33 KTagComboBox::~KTagComboBox ()
39 KTagComboBox::KTagComboBox (QWidget
* parent
, const char *name
)
40 : QComboBox(parent
, name
)
42 _popup
= new QPopupMenu(this);
43 _tags
= new QStringList
;
44 connect( _popup
, SIGNAL(activated(int)),
45 SLOT(internalActivate(int)) );
46 connect( _popup
, SIGNAL(highlighted(int)),
47 SLOT(internalHighlight(int)) );
50 void KTagComboBox::popupMenu()
52 _popup
->popup( mapToGlobal( QPoint(0,0) ), _current
);
55 void KTagComboBox::keyPressEvent( QKeyEvent
*e
)
59 if ( ( e
->key() == Key_F4
&& e
->state() == 0 ) ||
60 ( e
->key() == Key_Down
&& (e
->state() & AltButton
) ) ||
61 ( e
->key() == Key_Space
) ) {
63 _popup
->setActiveItem( _current
);
73 emit
highlighted( c
);
77 void KTagComboBox::mousePressEvent( QMouseEvent
* )
82 void KTagComboBox::internalActivate( int index
)
84 if (_current
== index
) return;
86 emit
activated( index
);
90 void KTagComboBox::internalHighlight( int index
)
92 emit
highlighted( index
);
95 void KTagComboBox::clear()
101 int KTagComboBox::count() const
103 return _tags
->count();
106 static inline QPopupMenu
*checkInsertIndex(QPopupMenu
*popup
, const QStringList
*tags
, const QString
&submenu
, int *index
)
108 int pos
= tags
->findIndex(submenu
);
113 QMenuItem
*p
= popup
->findItem(popup
->idAt(pos
));
118 if (*index
> (int)pi
->count())
124 void KTagComboBox::insertItem(const QIconSet
& icon
, const QString
&text
, const QString
&tag
, const QString
&submenu
, int index
)
126 QPopupMenu
*pi
= checkInsertIndex(_popup
, _tags
, submenu
, &index
);
127 pi
->insertItem(icon
, text
, count(), index
);
131 void KTagComboBox::insertItem(const QString
&text
, const QString
&tag
, const QString
&submenu
, int index
)
133 QPopupMenu
*pi
= checkInsertIndex(_popup
, _tags
, submenu
, &index
);
134 pi
->insertItem(text
, count(), index
);
138 void KTagComboBox::insertSeparator(const QString
&submenu
, int index
)
140 QPopupMenu
*pi
= checkInsertIndex(_popup
, _tags
, submenu
, &index
);
141 pi
->insertSeparator(index
);
142 _tags
->append(QString::null
);
145 void KTagComboBox::insertSubmenu(const QString
&text
, const QString
&tag
, const QString
&submenu
, int index
)
147 QPopupMenu
*pi
= checkInsertIndex(_popup
, _tags
, submenu
, &index
);
148 QPopupMenu
*p
= new QPopupMenu(pi
);
149 pi
->insertItem(text
, p
, count(), index
);
151 connect( p
, SIGNAL(activated(int)),
152 SLOT(internalActivate(int)) );
153 connect( p
, SIGNAL(highlighted(int)),
154 SLOT(internalHighlight(int)) );
157 void KTagComboBox::changeItem( const QString
&text
, int index
)
159 _popup
->changeItem( text
, index
);
160 if (index
== _current
)
164 void KTagComboBox::paintEvent( QPaintEvent
* ev
)
166 QComboBox::paintEvent(ev
);
171 QRect
clip(2, 2, width() - 4, height() - 4);
173 p
.setPen( colorGroup().highlightedText() );
174 p
.drawText(clip
, AlignCenter
| SingleLine
, _popup
->text( _current
));
177 QIconSet
*icon
= _popup
->iconSet( _current
);
179 QPixmap pm
= icon
->pixmap();
180 p
.drawPixmap( 4, (height()-pm
.height())/2, pm
);
184 bool KTagComboBox::containsTag( const QString
&str
) const
186 return _tags
->contains(str
) > 0;
189 QString
KTagComboBox::currentTag() const
191 return *_tags
->at(currentItem());
194 QString
KTagComboBox::tag(int i
) const
196 if (i
< 0 || i
>= count())
198 kdDebug() << "KTagComboBox::tag(), unknown tag " << i
<< endl
;
199 return QString::null
;
201 return *_tags
->at(i
);
204 int KTagComboBox::currentItem() const
209 void KTagComboBox::setCurrentItem(int i
)
211 if (i
< 0 || i
>= count()) return;
216 void KTagComboBox::setCurrentItem(const QString
&code
)
218 int i
= _tags
->findIndex(code
);
225 void KTagComboBox::setFont( const QFont
&font
)
227 QComboBox::setFont( font
);
228 _popup
->setFont( font
);