moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / keduca / keducabuilder / ktagcombobox.cpp
blob90152eb08281c0ce15f4b63cbfdb8ef2f464e980
1 /*
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
7 * http://www.troll.no/
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
25 #include <qpainter.h>
26 #include <qpopupmenu.h>
27 #include <kdebug.h>
30 #include "ktagcombobox.h"
31 #include "ktagcombobox.moc"
33 KTagComboBox::~KTagComboBox ()
35 delete _popup;
36 delete _tags;
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 )
57 int c;
59 if ( ( e->key() == Key_F4 && e->state() == 0 ) ||
60 ( e->key() == Key_Down && (e->state() & AltButton) ) ||
61 ( e->key() == Key_Space ) ) {
62 if ( count() ) {
63 _popup->setActiveItem( _current );
64 popupMenu();
66 return;
67 } else {
68 e->ignore();
69 return;
72 c = currentItem();
73 emit highlighted( c );
74 emit activated( c );
77 void KTagComboBox::mousePressEvent( QMouseEvent * )
79 popupMenu();
82 void KTagComboBox::internalActivate( int index )
84 if (_current == index) return;
85 _current = index;
86 emit activated( index );
87 repaint();
90 void KTagComboBox::internalHighlight( int index )
92 emit highlighted( index );
95 void KTagComboBox::clear()
97 _popup->clear();
98 _tags->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);
110 QPopupMenu *pi = 0;
111 if (pos != -1)
113 QMenuItem *p = popup->findItem(popup->idAt(pos));
114 pi = p?p->popup():0;
116 if (!pi) pi = popup;
118 if (*index > (int)pi->count())
119 *index = -1;
121 return pi;
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);
128 _tags->append(tag);
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);
135 _tags->append(tag);
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);
150 _tags->append(tag);
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)
161 repaint();
164 void KTagComboBox::paintEvent( QPaintEvent * ev)
166 QComboBox::paintEvent(ev);
168 QPainter p (this);
170 // Text
171 QRect clip(2, 2, width() - 4, height() - 4);
172 if ( hasFocus())
173 p.setPen( colorGroup().highlightedText() );
174 p.drawText(clip, AlignCenter | SingleLine, _popup->text( _current ));
176 // Icon
177 QIconSet *icon = _popup->iconSet( _current );
178 if (icon) {
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
206 return _current;
209 void KTagComboBox::setCurrentItem(int i)
211 if (i < 0 || i >= count()) return;
212 _current = i;
213 repaint();
216 void KTagComboBox::setCurrentItem(const QString &code)
218 int i = _tags->findIndex(code);
219 if (code.isNull())
220 i = 0;
221 if (i != -1)
222 setCurrentItem(i);
225 void KTagComboBox::setFont( const QFont &font )
227 QComboBox::setFont( font );
228 _popup->setFont( font );