Source Code (Use browser search to find items of interest.)
Class Index
kdelibs'KComboBox (./kdelibs/kdeui/kcombobox.h:119)
class KComboBox : public QComboBox, public KCompletionBase
{
Q_OBJECT
public:
/**
* Constructs a read-only or rather select-only combo box with a parent object
* and a name.
*
* @param parent the parent object of this widget
* @param name the name of this widget
*/
KComboBox( QWidget *parent=0, const char *name=0 );
/**
* Constructs a "read-write" or "read-only" combo box depending on the value of
* the first argument( bool rw ) with a parent, a name.
*
* @param rw when @p true widget will be editable.
* @param parent the parent object of this widget.
* @param name the name of this widget.
*/
KComboBox( bool rw, QWidget *parent=0, const char *name=0 );
/**
* Destructor.
*/
virtual ~KComboBox();
/**
* Returns the current cursor position.
*
* This method always returns a -1 if the combo-box is NOT
* editable (read-write).
*
* @return current cursor position.
*/
int cursorPosition() const { return ( m_pEdit ) ? m_pEdit->cursorPosition() : -1; }
/**
* Re-implemented from QComboBox.
*
* If true, the completion mode will be set to automatic.
* Otherwise, it is defaulted to the gloabl setting. This
* methods has been replaced by the more comprehensive @ref
* setCompletionMode.
*
* @param autocomplete flag to enable/disable automatic completion mode.
*/
virtual void setAutoCompletion( bool autocomplete );
/**
* Re-implemented from QComboBox.
*
* Returns true if the current completion mode is set
* to automatic. See its more comprehensive replacement
* @ref completionMode.
*
* @return true when completion mode is automatic.
*/
bool autoCompletion() const { return completionMode() == KGlobalSettings::CompletionAuto; }
/**
* Enables or disables the popup (context) menu.
*
* This method only works if this widget is editable, i.e.
* read-write and allows you to enable/disable the context
* menu. It does nothing if invoked for a none-editable
* combo-box. Note that by default the mode changer item
* is made visiable whenever the context menu is enabled.
* Use * @ref hideModechanger() if you want to hide this
* item. Also by default, the context menu is created if
* this widget is editable. Call this function with the
* argument set to false to disable the popup menu.
*
* @param showMenu if true, show the context menu.
* @param showMode if true, show the mode changer.
*/
virtual void setEnableContextMenu( bool showMenu );
/**
* Returns true when the context menu is enabled.
*
* @return true if context menu is enabled.
*/
bool isContextMenuEnabled() const { return m_bEnableMenu; }
/**
* Returns true if the combo-box is editable.
*
* @return true if combo is editable.
*/
bool isEditable() const { return (m_pEdit!= 0); }
signals:
/**
* This signal is emitted when the user presses
* the return key. Note that this signal is only
* emitted if this widget is editable.
*/
void returnPressed();
/**
* This signal is emitted when the user presses
* the return key. The argument is the current
* text being edited. This signal is just like
* @ref returnPressed() except it contains the
* current text as its argument.
*
* Note that this signal is only emitted if this
* widget is editable.
*/
void returnPressed( const QString& );
/**
* This signal is emitted when the completion key
* is pressed. The argument is the current text
* being edited.
*
* Note that this signal is NOT available if this
* widget is non-editable or the completion mode is
* set to KGlobalSettings::CompletionNone.
*/
void completion( const QString& );
/**
* This signal is emitted when the rotate up key is pressed.
*
* Note that this signal is NOT available if this widget is non-editable
* or the completion mode is set to KGlobalSettings::CompletionNone.
*/
void rotateUp();
/**
* This signal is emitted when the rotate down key is pressed.
*
* Note that this signal is NOT available if this widget is non-editable
* or the completion mode is set to KGlobalSettings::CompletionNone.
*/
void rotateDown();
public slots:
/**
* Iterates in the up (previous match) direction through the
* completion list if it is available.
*
* This slot is intended to make it easy to connect the rotate
* up signal in order to make the widget itself handle rotation
* events internally. Note that no action is taken if there is
* no completion object or the completion object does not contain
* a next match.
*/
virtual void iterateUpInList() { rotateText( completionObject()->previousMatch(), -1 ); }
/**
* Iterates in the down (next match) direction through the
* completion list if it is available.
*
* This slot is intended to make it easy to connect the rotate
* down signal in order to make the widget itself handle rotation
* events internally. Note that no action is taken if there is
* no completion object or the completion object does not contain
* a next match.
*/
virtual void iterateDownInList() { rotateText( completionObject()->nextMatch(), 1 ); }
protected slots:
/**
* changes the completion mode.
*
* This slot sets the completion mode to the one
* requested by the end user through the popup
* menu.
*
* @param itemID the completion mode type
*/
virtual void selectedItem( int );
/**
* Populates the sub menu before it is displayed.
*
* All the items are inserted by the completion base
* class. See @KCompletionBase::insertCompletionItems.
* The items then invoke the slot giiven by the
*/
virtual void showCompletionMenu() { insertCompletionItems( this, SLOT( selectedItem( int ) ) ); }
/**
* Inserts the completion menu item as needed.
*
* Since this widget comes with its own pop-up menu
* this slot is needed to invoke the method need to
* insert the completion menu. This method,
* @ref KCompletionBase::insetCompeltionMenu, is
* defined by the KCompletionBase.
*/
virtual void aboutToShowMenu() { insertCompletionMenu( this, SLOT( showCompletionMenu() ), m_pContextMenu, m_pContextMenu->count() - 1 ); }
/**
* Deals with highlighting the seleted item when
* return is pressed in the list box (editable-mode only).
*/
virtual void itemSelected( QListBoxItem* );
/**
* Deals with text changes in auto completion mode.
*/
virtual void makeCompletion( const QString& );
protected:
/**
* Initializes the variables upon construction.
*/
virtual void init();
/**
* Rotates the text on rotation events.
*
* @param string the text to replace the current one with.
* @param dir rotation direction ( rotateUp or rotateDown ).
*/
void rotateText( const QString&, int /* dir */ );
/**
* Implementation of @ref KCompletionBase::connectSignals().
*
* This function simply connects the signals to appropriate
* slots when they are handled internally.
*
* @param handle if true, handle completion & roation internally.
*/
virtual void connectSignals( bool handle ) const;
/**
* Re-implemented for internal reasons. API is not affected.
*
* See @ref QComboBox::keyPressEvent.
*/
virtual void keyPressEvent ( QKeyEvent* );
/**
* Re-implemented for internal reasons. API is not affected.
*
* See @ref QComboBox::mousePressEvent.
*/
virtual bool eventFilter( QObject *, QEvent * );
private :
// Flag that indicates whether we enable/disable
// the context (popup) menu.
bool m_bEnableMenu;
// Pointer to the line editor.
QLineEdit* m_pEdit;
// Context Menu items.
QPopupMenu *m_pContextMenu;
class KComboBoxPrivate;
KComboBoxPrivate *d;
};
kdelibs'KComboBox::KComboBox() (./kdelibs/kdeui/kcombobox.cpp:30)
KComboBox::KComboBox( QWidget *parent, const char *name )
:QComboBox( parent, name )
{
m_pEdit = 0;
m_pContextMenu = 0;
init();
}
kdelibs'KComboBox::KComboBox() (./kdelibs/kdeui/kcombobox.cpp:38)
KComboBox::KComboBox( bool rw, QWidget *parent, const char *name )
:QComboBox( rw, parent, name )
{
if ( rw )
{
m_pEdit = QComboBox::lineEdit();
m_pEdit->installEventFilter( this );
QObjectList *list = queryList( "QPopupMenu" );
QObjectListIt it ( *list );
m_pContextMenu = (QPopupMenu*) it.current();
delete list;
}
else
{
m_pEdit = 0;
m_pContextMenu = 0;
}
init();
}
kdelibs'KComboBox::~KComboBox() (./kdelibs/kdeui/kcombobox.cpp:58)
KComboBox::~KComboBox()
{
m_pContextMenu = 0; // Reset the pointer to NULL
if( m_pEdit != 0 )
{
m_pEdit->removeEventFilter( this );
m_pEdit = 0; // Reset the pointer to NULL
}
}
kdelibs'KComboBox::init() (./kdelibs/kdeui/kcombobox.cpp:68)
void KComboBox::init()
{
// Permanently set some parameters in the parent object.
setAutoCompletion( false );
// Initialize enable popup menu to false.
// Below it will be enabled if the widget
// is editable.
m_bEnableMenu = false;
// Enable context menu by default if widget
// is editable.
setEnableContextMenu( true );
// Connect the signals and slots.
connect( listBox(), SIGNAL( returnPressed( QListBoxItem* ) ), this, SLOT( itemSelected( QListBoxItem* ) ) );
connect( listBox(), SIGNAL( clicked( QListBoxItem* ) ), this, SLOT( itemSelected( QListBoxItem* ) ) );
}
kdelibs'KComboBox::setAutoCompletion() (./kdelibs/kdeui/kcombobox.cpp:87)
void KComboBox::setAutoCompletion( bool autocomplete )
{
setCompletionMode( autocomplete ? KGlobalSettings::CompletionAuto : KGlobalSettings::completionMode() );
}
kdelibs'KComboBox::setEnableContextMenu() (./kdelibs/kdeui/kcombobox.cpp:92)
void KComboBox::setEnableContextMenu( bool showMenu )
{
if( m_pEdit != 0 )
{
if( !m_bEnableMenu && showMenu )
{
connect ( m_pContextMenu, SIGNAL( aboutToShow() ), this, SLOT( aboutToShowMenu() ) );
showModeChanger();
}
else if( m_bEnableMenu && !showMenu )
{
disconnect ( m_pContextMenu, SIGNAL( aboutToShow() ), this, SLOT( aboutToShowMenu() ) );
hideModeChanger();
}
m_bEnableMenu = showMenu;
}
}
kdelibs'KComboBox::makeCompletion() (./kdelibs/kdeui/kcombobox.cpp:110)
void KComboBox::makeCompletion( const QString& text )
{
if( m_pEdit != 0 )
{
QString match = completionObject()->makeCompletion( text );
// If no match or the same match, simply return
// without completing.
if( match.isNull() || match == text )
return;
if( completionMode() == KGlobalSettings::CompletionShell )
{
m_pEdit->setText( match );
}
else
{
int pos = cursorPosition();
m_pEdit->validateAndSet( match, pos, pos, match.length() );
}
}
else if( m_pEdit == 0 )
{
if( text.isNull() )
return;
int index = listBox()->index( listBox()->findItem( text ) );
if( index >= 0 )
setCurrentItem( index );
}
}
kdelibs'KComboBox::rotateText() (./kdelibs/kdeui/kcombobox.cpp:142)
void KComboBox::rotateText( const QString& input, int dir )
{
if( m_pEdit != 0 )
{
KCompletion* comp = completionObject();
QString str;
int len = m_pEdit->text().length();
if( m_pEdit->hasMarkedText() && !input.isNull() )
{
str = m_pEdit->text();
if( input == str ) return; // Skip rotation if same text
int pos = str.find( m_pEdit->markedText() );
int index = input.find( str.remove( pos , m_pEdit->markedText().length() ) );
if( index == -1 ) return;
else if( index == 0 ) str = input;
m_pEdit->validateAndSet( str, cursorPosition(), pos, str.length() );
}
else
{
QStringList list = comp->items();
if( list.count() == 0 ) return;
int index = list.findIndex( m_pEdit->text() );
if( index == -1 )
{
index = ( dir == 1 ) ? 0 : list.count()-1;
str = ( len == 0 ) ? list[index] : input;
}
else
{
index += dir;
if( index >= (int)list.count() ) index = 0; // rotate back to beginning
else if( index < 0 ) index = list.count() - 1; // rotate back to the end
str = list[index];
}
m_pEdit->setText( str );
}
}
else // non-editable
{
int index = listBox()->index( listBox()->findItem( input ) );
if( index >= 0 )
{
setCurrentItem( index );
}
}
}
kdelibs'KComboBox::itemSelected() (./kdelibs/kdeui/kcombobox.cpp:189)
void KComboBox::itemSelected( QListBoxItem* item )
{
if( item != 0 && m_pEdit != 0 )
{
debug( "Item selected from list" );
m_pEdit->setSelection( 0, m_pEdit->text().length() );
}
}
kdelibs'KComboBox::connectSignals() (./kdelibs/kdeui/kcombobox.cpp:198)
void KComboBox::connectSignals( bool handle ) const
{
if( handle && !handleSignals() )
{
connect( this, SIGNAL( completion( const QString& ) ), this, SLOT( makeCompletion( const QString& ) ) );
connect( this, SIGNAL( rotateUp() ), this, SLOT( iterateUpInList() ) );
connect( this, SIGNAL( rotateDown() ), this, SLOT( iterateDownInList() ) );
}
else if( !handle && handleSignals() )
{
disconnect( this, SIGNAL( completion( const QString& ) ), this, SLOT( makeCompletion( const QString& ) ) );
disconnect( this, SIGNAL( rotateUp() ), this, SLOT( iterateUpInList() ) );
disconnect( this, SIGNAL( rotateDown() ), this, SLOT( iterateDownInList() ) );
}
}
kdelibs'KComboBox::selectedItem() (./kdelibs/kdeui/kcombobox.cpp:214)
void KComboBox::selectedItem( int id )
{
if( id == 0 ) id = KGlobalSettings::completionMode();
setCompletionMode( (KGlobalSettings::Completion)id );
}
kdelibs'KComboBox::keyPressEvent() (./kdelibs/kdeui/kcombobox.cpp:220)
void KComboBox::keyPressEvent ( QKeyEvent * e )
{
if( m_pEdit != 0 && m_pEdit->hasFocus() )
{
KGlobalSettings::Completion mode = completionMode();
// On Return pressed event, emit returnPressed( const QString& )
if( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
{
emit returnPressed();
emit returnPressed( m_pEdit->text() );
}
if( mode == KGlobalSettings::CompletionAuto )
{
QString keycode = e->text();
if( !keycode.isNull() && keycode.unicode()->isPrint() && emitSignals() )
{
QComboBox::keyPressEvent ( e );
emit completion( m_pEdit->text() );
return;
}
}
if( mode != KGlobalSettings::CompletionNone )
{
KCompletion* compObj = completionObject();
bool fireSignals = emitSignals();
int key = ( completionKey() == 0 ) ? KStdAccel::key(KStdAccel::TextCompletion) : completionKey();
if( KStdAccel::isEqual( e, key ) && fireSignals )
{
// Emit completion if the completion mode is NOT
// CompletionAuto and if the mode is CompletionShell,
// the cursor is at the end of the string.
if( (mode == KGlobalSettings::CompletionMan &&
(compObj != 0 && compObj->lastMatch() != m_pEdit->displayText()) ) ||
(mode == KGlobalSettings::CompletionShell &&
m_pEdit->cursorPosition() == (int) m_pEdit->text().length() ))
{
emit completion( m_pEdit->text() );
return;
}
}
// Handles rotateUp.
key = ( rotateUpKey() == 0 ) ? KStdAccel::key(KStdAccel::RotateUp) : rotateUpKey();
if( KStdAccel::isEqual( e, key ) && fireSignals )
{
emit rotateUp ();
e->accept();
return;
}
// Handles rotateDown.
key = ( rotateDownKey() == 0 ) ? KStdAccel::key(KStdAccel::RotateDown) : rotateDownKey();
if( KStdAccel::isEqual( e, key ) && fireSignals )
{
emit rotateDown();
e->accept();
return;
}
}
}
else if( m_pEdit == 0 )
{
QString keycode = e->text();
if ( !keycode.isNull() && keycode.unicode()->isPrint() )
{
emit completion ( keycode );
e->accept();
return;
}
}
QComboBox::keyPressEvent( e );
}
kdelibs'KComboBox::eventFilter() (./kdelibs/kdeui/kcombobox.cpp:291)
bool KComboBox::eventFilter( QObject* o, QEvent* ev )
{
if( o == m_pEdit && ev->type() == QEvent::MouseButtonPress )
{
QMouseEvent* e = (QMouseEvent*) ev;
if( e->button() == Qt::RightButton && !m_bEnableMenu )
return true;
}
return QComboBox::eventFilter( o, ev );
}