00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #define INCLUDE_MENUITEM_DEF 1
00025 #include <qpainter.h>
00026 #include <qpopupmenu.h>
00027 #include <kdebug.h>
00028
00029
00030 #include "ktagcombobox.h"
00031 #include "ktagcombobox.moc"
00032
00033 KTagComboBox::~KTagComboBox ()
00034 {
00035 delete _popup;
00036 delete _tags;
00037 }
00038
00039 KTagComboBox::KTagComboBox (QWidget * parent, const char *name)
00040 : QComboBox(parent, name)
00041 {
00042 _popup = new QPopupMenu(this);
00043 _tags = new QStringList;
00044 connect( _popup, SIGNAL(activated(int)),
00045 SLOT(internalActivate(int)) );
00046 connect( _popup, SIGNAL(highlighted(int)),
00047 SLOT(internalHighlight(int)) );
00048 }
00049
00050 void KTagComboBox::popupMenu()
00051 {
00052 _popup->popup( mapToGlobal( QPoint(0,0) ), _current );
00053 }
00054
00055 void KTagComboBox::keyPressEvent( QKeyEvent *e )
00056 {
00057 int c;
00058
00059 if ( ( e->key() == Key_F4 && e->state() == 0 ) ||
00060 ( e->key() == Key_Down && (e->state() & AltButton) ) ||
00061 ( e->key() == Key_Space ) ) {
00062 if ( count() ) {
00063 _popup->setActiveItem( _current );
00064 popupMenu();
00065 }
00066 return;
00067 } else {
00068 e->ignore();
00069 return;
00070 }
00071
00072 c = currentItem();
00073 emit highlighted( c );
00074 emit activated( c );
00075 }
00076
00077 void KTagComboBox::mousePressEvent( QMouseEvent * )
00078 {
00079 popupMenu();
00080 }
00081
00082 void KTagComboBox::internalActivate( int index )
00083 {
00084 if (_current == index) return;
00085 _current = index;
00086 emit activated( index );
00087 repaint();
00088 }
00089
00090 void KTagComboBox::internalHighlight( int index )
00091 {
00092 emit highlighted( index );
00093 }
00094
00095 void KTagComboBox::clear()
00096 {
00097 _popup->clear();
00098 _tags->clear();
00099 }
00100
00101 int KTagComboBox::count() const
00102 {
00103 return _tags->count();
00104 }
00105
00106 static inline QPopupMenu *checkInsertIndex(QPopupMenu *popup, const QStringList *tags, const QString &submenu, int *index)
00107 {
00108 int pos = tags->findIndex(submenu);
00109
00110 QPopupMenu *pi = 0;
00111 if (pos != -1)
00112 {
00113 QMenuItem *p = popup->findItem(popup->idAt(pos));
00114 pi = p?p->popup():0;
00115 }
00116 if (!pi) pi = popup;
00117
00118 if (*index > (int)pi->count())
00119 *index = -1;
00120
00121 return pi;
00122 }
00123
00124 void KTagComboBox::insertItem(const QIconSet& icon, const QString &text, const QString &tag, const QString &submenu, int index )
00125 {
00126 QPopupMenu *pi = checkInsertIndex(_popup, _tags, submenu, &index);
00127 pi->insertItem(icon, text, count(), index);
00128 _tags->append(tag);
00129 }
00130
00131 void KTagComboBox::insertItem(const QString &text, const QString &tag, const QString &submenu, int index )
00132 {
00133 QPopupMenu *pi = checkInsertIndex(_popup, _tags, submenu, &index);
00134 pi->insertItem(text, count(), index);
00135 _tags->append(tag);
00136 }
00137
00138 void KTagComboBox::insertSeparator(const QString &submenu, int index)
00139 {
00140 QPopupMenu *pi = checkInsertIndex(_popup, _tags, submenu, &index);
00141 pi->insertSeparator(index);
00142 _tags->append(QString::null);
00143 }
00144
00145 void KTagComboBox::insertSubmenu(const QString &text, const QString &tag, const QString &submenu, int index)
00146 {
00147 QPopupMenu *pi = checkInsertIndex(_popup, _tags, submenu, &index);
00148 QPopupMenu *p = new QPopupMenu(pi);
00149 pi->insertItem(text, p, count(), index);
00150 _tags->append(tag);
00151 connect( p, SIGNAL(activated(int)),
00152 SLOT(internalActivate(int)) );
00153 connect( p, SIGNAL(highlighted(int)),
00154 SLOT(internalHighlight(int)) );
00155 }
00156
00157 void KTagComboBox::changeItem( const QString &text, int index )
00158 {
00159 _popup->changeItem( text, index);
00160 if (index == _current)
00161 repaint();
00162 }
00163
00164 void KTagComboBox::paintEvent( QPaintEvent * ev)
00165 {
00166 QComboBox::paintEvent(ev);
00167
00168 QPainter p (this);
00169
00170
00171 QRect clip(2, 2, width() - 4, height() - 4);
00172 if ( hasFocus())
00173 p.setPen( colorGroup().highlightedText() );
00174 p.drawText(clip, AlignCenter | SingleLine, _popup->text( _current ));
00175
00176
00177 QIconSet *icon = _popup->iconSet( _current );
00178 if (icon) {
00179 QPixmap pm = icon->pixmap();
00180 p.drawPixmap( 4, (height()-pm.height())/2, pm );
00181 }
00182 }
00183
00184 bool KTagComboBox::containsTag( const QString &str ) const
00185 {
00186 return _tags->contains(str) > 0;
00187 }
00188
00189 QString KTagComboBox::currentTag() const
00190 {
00191 return *_tags->at(currentItem());
00192 }
00193
00194 QString KTagComboBox::tag(int i) const
00195 {
00196 if (i < 0 || i >= count())
00197 {
00198 kdDebug() << "KTagComboBox::tag(), unknown tag " << i << endl;
00199 return QString::null;
00200 }
00201 return *_tags->at(i);
00202 }
00203
00204 int KTagComboBox::currentItem() const
00205 {
00206 return _current;
00207 }
00208
00209 void KTagComboBox::setCurrentItem(int i)
00210 {
00211 if (i < 0 || i >= count()) return;
00212 _current = i;
00213 repaint();
00214 }
00215
00216 void KTagComboBox::setCurrentItem(const QString &code)
00217 {
00218 int i = _tags->findIndex(code);
00219 if (code.isNull())
00220 i = 0;
00221 if (i != -1)
00222 setCurrentItem(i);
00223 }
00224
00225 void KTagComboBox::setFont( const QFont &font )
00226 {
00227 QComboBox::setFont( font );
00228 _popup->setFont( font );
00229 }