• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kcombobox.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2
3 Copyright (c) 2000,2001 Dawit Alemayehu <adawit@kde.org>
4 Copyright (c) 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
5 Copyright (c) 2000 Stefan Schimanski <1Stein@gmx.de>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License (LGPL) as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "kcombobox.h"
24
25#include <QtGui/QClipboard>
26#include <QtGui/QLineEdit>
27#include <QtGui/QMenu>
28#include <QtGui/QApplication>
29#include <QtGui/QActionEvent>
30
31#include <kselectaction.h>
32#include <kcompletionbox.h>
33#include <kcursor.h>
34#include <kiconloader.h>
35#include <kicontheme.h>
36#include <klineedit.h>
37#include <klocale.h>
38#include <kurl.h>
39#include <kicon.h>
40
41#include <kdebug.h>
42
43class KComboBox::KComboBoxPrivate
44{
45public:
46 KComboBoxPrivate() : klineEdit(0L), trapReturnKey(false)
47 {
48 }
49 ~KComboBoxPrivate()
50 {
51 }
52
53 KLineEdit *klineEdit;
54 bool trapReturnKey;
55};
56
57KComboBox::KComboBox( QWidget *parent )
58 : QComboBox( parent ), d(new KComboBoxPrivate)
59{
60 init();
61}
62
63KComboBox::KComboBox( bool rw, QWidget *parent )
64 : QComboBox( parent ), d(new KComboBoxPrivate)
65{
66 init();
67 setEditable( rw );
68}
69
70KComboBox::~KComboBox()
71{
72 delete d;
73}
74
75void KComboBox::init()
76{
77 // Permanently set some parameters in the parent object.
78 QComboBox::setAutoCompletion( false );
79
80 // Enable context menu by default if widget
81 // is editable.
82 if (lineEdit()) {
83 lineEdit()->setContextMenuPolicy( Qt::DefaultContextMenu );
84 }
85}
86
87
88bool KComboBox::contains( const QString& _text ) const
89{
90 if ( _text.isEmpty() )
91 return false;
92
93 const int itemCount = count();
94 for (int i = 0; i < itemCount; ++i )
95 {
96 if ( itemText(i) == _text )
97 return true;
98 }
99 return false;
100}
101
102int KComboBox::cursorPosition() const
103{
104 return ( lineEdit() ) ? lineEdit()->cursorPosition() : -1;
105}
106
107void KComboBox::setAutoCompletion( bool autocomplete )
108{
109 if ( d->klineEdit )
110 {
111 if ( autocomplete )
112 {
113 d->klineEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
114 setCompletionMode( KGlobalSettings::CompletionAuto );
115 }
116 else
117 {
118 d->klineEdit->setCompletionMode( KGlobalSettings::completionMode() );
119 setCompletionMode( KGlobalSettings::completionMode() );
120 }
121 }
122}
123
124bool KComboBox::autoCompletion() const
125{
126 return completionMode() == KGlobalSettings::CompletionAuto;
127}
128
129#ifndef KDE_NO_DEPRECATED
130void KComboBox::setContextMenuEnabled( bool showMenu )
131{
132 if( d->klineEdit )
133 d->klineEdit->setContextMenuEnabled( showMenu );
134}
135#endif
136
137
138void KComboBox::setUrlDropsEnabled( bool enable )
139{
140 if ( d->klineEdit )
141 d->klineEdit->setUrlDropsEnabled( enable );
142}
143
144bool KComboBox::urlDropsEnabled() const
145{
146 return d->klineEdit && d->klineEdit->urlDropsEnabled();
147}
148
149
150void KComboBox::setCompletedText( const QString& text, bool marked )
151{
152 if ( d->klineEdit )
153 d->klineEdit->setCompletedText( text, marked );
154}
155
156void KComboBox::setCompletedText( const QString& text )
157{
158 if ( d->klineEdit )
159 d->klineEdit->setCompletedText( text );
160}
161
162void KComboBox::makeCompletion( const QString& text )
163{
164 if( d->klineEdit )
165 d->klineEdit->makeCompletion( text );
166
167 else // read-only combo completion
168 {
169 if( text.isNull() || !view() )
170 return;
171
172 view()->keyboardSearch(text);
173 }
174}
175
176void KComboBox::rotateText( KCompletionBase::KeyBindingType type )
177{
178 if ( d->klineEdit )
179 d->klineEdit->rotateText( type );
180}
181
182// Not needed anymore
183bool KComboBox::eventFilter( QObject* o, QEvent* ev )
184{
185 return QComboBox::eventFilter( o, ev );
186}
187
188void KComboBox::setTrapReturnKey( bool grab )
189{
190 d->trapReturnKey = grab;
191
192 if ( d->klineEdit )
193 d->klineEdit->setTrapReturnKey( grab );
194 else
195 qWarning("KComboBox::setTrapReturnKey not supported with a non-KLineEdit.");
196}
197
198bool KComboBox::trapReturnKey() const
199{
200 return d->trapReturnKey;
201}
202
203
204void KComboBox::setEditUrl( const KUrl& url )
205{
206 QComboBox::setEditText( url.prettyUrl() );
207}
208
209void KComboBox::addUrl( const KUrl& url )
210{
211 QComboBox::addItem( url.prettyUrl() );
212}
213
214void KComboBox::addUrl( const QIcon& icon, const KUrl& url )
215{
216 QComboBox::addItem( icon, url.prettyUrl() );
217}
218
219void KComboBox::insertUrl( int index, const KUrl& url )
220{
221 QComboBox::insertItem( index, url.prettyUrl() );
222}
223
224void KComboBox::insertUrl( int index, const QIcon& icon, const KUrl& url )
225{
226 QComboBox::insertItem( index, icon, url.prettyUrl() );
227}
228
229void KComboBox::changeUrl( int index, const KUrl& url )
230{
231 QComboBox::setItemText( index, url.prettyUrl() );
232}
233
234void KComboBox::changeUrl( int index, const QIcon& icon, const KUrl& url )
235{
236 QComboBox::setItemIcon( index, icon );
237 QComboBox::setItemText( index, url.prettyUrl() );
238}
239
240void KComboBox::setCompletedItems( const QStringList& items, bool autosubject )
241{
242 if ( d->klineEdit )
243 d->klineEdit->setCompletedItems( items, autosubject );
244}
245
246KCompletionBox * KComboBox::completionBox( bool create )
247{
248 if ( d->klineEdit )
249 return d->klineEdit->completionBox( create );
250 return 0;
251}
252
253// QWidget::create() turns off mouse-Tracking which would break auto-hiding
254void KComboBox::create( WId id, bool initializeWindow, bool destroyOldWindow )
255{
256 QComboBox::create( id, initializeWindow, destroyOldWindow );
257 KCursor::setAutoHideCursor( lineEdit(), true, true );
258}
259
260void KComboBox::wheelEvent( QWheelEvent *ev )
261{
262 // Not necessary anymore
263 QComboBox::wheelEvent( ev );
264}
265
266QSize KComboBox::minimumSizeHint() const
267{
268 QSize size = QComboBox::minimumSizeHint();
269 if (isEditable() && d->klineEdit) {
270 // if it's a KLineEdit and it's editable add the clear button size
271 // to the minimum size hint, otherwise looks ugly because the
272 // clear button will cover the last 2/3 letters of the biggest entry
273 QSize bs = d->klineEdit->clearButtonUsedSize();
274 if (bs.isValid()) {
275 size.rwidth() += bs.width();
276 size.rheight() = qMax(size.height(), bs.height());
277 }
278 }
279 return size;
280}
281
282void KComboBox::setLineEdit( QLineEdit *edit )
283{
284 if ( !isEditable() && edit &&
285 !qstrcmp( edit->metaObject()->className(), "QLineEdit" ) )
286 {
287 // uic generates code that creates a read-only KComboBox and then
288 // calls combo->setEditable( true ), which causes QComboBox to set up
289 // a dumb QLineEdit instead of our nice KLineEdit.
290 // As some KComboBox features rely on the KLineEdit, we reject
291 // this order here.
292 delete edit;
293 KLineEdit* kedit = new KLineEdit( this );
294
295 if ( isEditable() ) {
296 kedit->setClearButtonShown( true );
297 }
298
299 edit = kedit;
300 }
301
302 QComboBox::setLineEdit( edit );
303 d->klineEdit = qobject_cast<KLineEdit*>( edit );
304 setDelegate( d->klineEdit );
305
306 // Connect the returnPressed signal for both Q[K]LineEdits'
307 if (edit)
308 connect( edit, SIGNAL(returnPressed()), SIGNAL(returnPressed()));
309
310 if ( d->klineEdit )
311 {
312 // someone calling KComboBox::setEditable( false ) destroys our
313 // lineedit without us noticing. And KCompletionBase::delegate would
314 // be a dangling pointer then, so prevent that. Note: only do this
315 // when it is a KLineEdit!
316 connect( edit, SIGNAL(destroyed()), SLOT(lineEditDeleted()));
317
318 connect( d->klineEdit, SIGNAL(returnPressed(QString)),
319 SIGNAL(returnPressed(QString)));
320
321 connect( d->klineEdit, SIGNAL(completion(QString)),
322 SIGNAL(completion(QString)) );
323
324 connect( d->klineEdit, SIGNAL(substringCompletion(QString)),
325 SIGNAL(substringCompletion(QString)) );
326
327 connect( d->klineEdit,
328 SIGNAL(textRotation(KCompletionBase::KeyBindingType)),
329 SIGNAL(textRotation(KCompletionBase::KeyBindingType)) );
330
331 connect( d->klineEdit,
332 SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
333 SIGNAL(completionModeChanged(KGlobalSettings::Completion)));
334
335 connect( d->klineEdit,
336 SIGNAL(aboutToShowContextMenu(QMenu*)),
337 SIGNAL(aboutToShowContextMenu(QMenu*)) );
338
339 connect( d->klineEdit,
340 SIGNAL(completionBoxActivated(QString)),
341 SIGNAL(activated(QString)) );
342
343 d->klineEdit->setTrapReturnKey( d->trapReturnKey );
344 }
345}
346
347void KComboBox::setCurrentItem( const QString& item, bool insert, int index )
348{
349 int sel = -1;
350
351 const int itemCount = count();
352 for (int i = 0; i < itemCount; ++i)
353 {
354 if (itemText(i) == item)
355 {
356 sel = i;
357 break;
358 }
359 }
360
361 if (sel == -1 && insert)
362 {
363 if (index >= 0) {
364 insertItem(index, item);
365 sel = index;
366 } else {
367 addItem(item);
368 sel = count() - 1;
369 }
370 }
371 setCurrentIndex(sel);
372}
373
374void KComboBox::lineEditDeleted()
375{
376 // yes, we need those ugly casts due to the multiple inheritance
377 // sender() is guaranteed to be a KLineEdit (see the connect() to the
378 // destroyed() signal
379 const KCompletionBase *base = static_cast<const KCompletionBase*>( static_cast<const KLineEdit*>( sender() ));
380
381 // is it our delegate, that is destroyed?
382 if ( base == delegate() )
383 setDelegate( 0L );
384}
385
386void KComboBox::setEditable(bool editable)
387{
388 if (editable) {
389 // Create a KLineEdit instead of a QLineEdit
390 // Compared to QComboBox::setEditable, we might be missing the SH_ComboBox_Popup code though...
391 // If a style needs this, then we'll need to call QComboBox::setEditable and then setLineEdit again
392 KLineEdit *edit = new KLineEdit( this );
393 edit->setClearButtonShown( true );
394 setLineEdit( edit );
395 } else {
396 QComboBox::setEditable(editable);
397 }
398}
399
400#include "kcombobox.moc"
KComboBox::substringCompletion
void substringCompletion(const QString &)
Emitted when the shortcut for substring completion is pressed.
KComboBox::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
Re-implemented for internal reasons.
Definition: kcombobox.cpp:183
KComboBox::KComboBox
KComboBox(QWidget *parent=0)
Constructs a read-only or rather select-only combo box with a parent object and a name.
Definition: kcombobox.cpp:57
KComboBox::addUrl
void addUrl(const KUrl &url)
Appends url to the combobox.
Definition: kcombobox.cpp:209
KComboBox::setCompletedText
virtual void setCompletedText(const QString &)
Sets the completed text in the line-edit appropriately.
Definition: kcombobox.cpp:156
KComboBox::changeUrl
void changeUrl(int index, const KUrl &url)
Replaces the item at position index with url.
Definition: kcombobox.cpp:229
KComboBox::aboutToShowContextMenu
void aboutToShowContextMenu(QMenu *p)
Emitted before the context menu is displayed.
KComboBox::contains
bool contains(const QString &text) const
Convenience method which iterates over all items and checks if any of them is equal to text.
Definition: kcombobox.cpp:88
KComboBox::setCompletedItems
void setCompletedItems(const QStringList &items, bool autosubject=true)
Sets items into the completion-box if completionMode() is CompletionPopup.
Definition: kcombobox.cpp:240
KComboBox::makeCompletion
virtual void makeCompletion(const QString &)
Completes text according to the completion mode.
Definition: kcombobox.cpp:162
KComboBox::~KComboBox
virtual ~KComboBox()
Destructor.
Definition: kcombobox.cpp:70
KComboBox::setEditUrl
void setEditUrl(const KUrl &url)
Sets url into the edit field of the combobox.
Definition: kcombobox.cpp:204
KComboBox::setContextMenuEnabled
virtual void setContextMenuEnabled(bool showMenu)
Enables or disable the popup (context) menu.
Definition: kcombobox.cpp:130
KComboBox::setCurrentItem
void setCurrentItem(const QString &item, bool insert=false, int index=-1)
Selects the first item that matches item.
Definition: kcombobox.cpp:347
KComboBox::urlDropsEnabled
bool urlDropsEnabled
Definition: kcombobox.h:152
KComboBox::autoCompletion
bool autoCompletion
Definition: kcombobox.h:151
KComboBox::textRotation
void textRotation(KCompletionBase::KeyBindingType)
Emitted when the text rotation key-bindings are pressed.
KComboBox::trapReturnKey
bool trapReturnKey
Definition: kcombobox.h:153
KComboBox::setTrapReturnKey
void setTrapReturnKey(bool trap)
By default, KComboBox recognizes Key_Return and Key_Enter and emits the returnPressed() signals,...
Definition: kcombobox.cpp:188
KComboBox::wheelEvent
virtual void wheelEvent(QWheelEvent *ev)
Definition: kcombobox.cpp:260
KComboBox::setEditable
void setEditable(bool editable)
"Re-implemented" so that setEditable(true) creates a KLineEdit instead of QLineEdit.
Definition: kcombobox.cpp:386
KComboBox::completionModeChanged
void completionModeChanged(KGlobalSettings::Completion)
Emitted whenever the completion mode is changed by the user through the context menu.
KComboBox::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: kcombobox.cpp:266
KComboBox::create
virtual void create(WId=0, bool initializeWindow=true, bool destroyOldWindow=true)
Reimplemented for internal reasons, the API is not affected.
Definition: kcombobox.cpp:254
KComboBox::cursorPosition
int cursorPosition() const
Returns the current cursor position.
Definition: kcombobox.cpp:102
KComboBox::completion
void completion(const QString &)
Emitted when the completion key is pressed.
KComboBox::setUrlDropsEnabled
void setUrlDropsEnabled(bool enable)
Enables/Disables handling of URL drops.
Definition: kcombobox.cpp:138
KComboBox::completionBox
KCompletionBox * completionBox(bool create=true)
Definition: kcombobox.cpp:246
KComboBox::returnPressed
void returnPressed()
Emitted when the user presses the Enter key.
KComboBox::insertUrl
void insertUrl(int index, const KUrl &url)
Inserts url at position index into the combobox.
Definition: kcombobox.cpp:219
KComboBox::setAutoCompletion
virtual void setAutoCompletion(bool autocomplete)
Re-implemented from QComboBox.
Definition: kcombobox.cpp:107
KComboBox::rotateText
void rotateText(KCompletionBase::KeyBindingType type)
Iterates through all possible matches of the completed text or the history list.
Definition: kcombobox.cpp:176
KComboBox::setLineEdit
virtual void setLineEdit(QLineEdit *)
Re-implemented for internal reasons.
Definition: kcombobox.cpp:282
KCompletionBase
An abstract base class for adding a completion feature into widgets.
Definition: kcompletion.h:646
KCompletionBase::setDelegate
void setDelegate(KCompletionBase *delegate)
Sets or removes the delegation object.
Definition: kcompletionbase.cpp:78
KCompletionBase::completionMode
KGlobalSettings::Completion completionMode() const
Returns the current completion mode.
Definition: kcompletionbase.cpp:181
KCompletionBase::setCompletionMode
virtual void setCompletionMode(KGlobalSettings::Completion mode)
Sets the type of completion to be used.
Definition: kcompletionbase.cpp:167
KCompletionBase::KeyBindingType
KeyBindingType
Constants that represent the items whose short-cut key-binding is programmable.
Definition: kcompletion.h:653
KCompletionBox
A helper widget for "completion-widgets" (KLineEdit, KComboBox))
Definition: kcompletionbox.h:44
KCursor::setAutoHideCursor
static void setAutoHideCursor(QWidget *w, bool enable, bool customEventFilter=false)
Sets auto-hiding the cursor for widget w.
Definition: kcursor.cpp:202
KGlobalSettings::completionMode
static Completion completionMode()
Returns the preferred completion mode setting.
Definition: kglobalsettings.cpp:267
KGlobalSettings::Completion
Completion
This enum describes the completion mode used for by the KCompletion class.
Definition: kglobalsettings.h:179
KGlobalSettings::CompletionAuto
@ CompletionAuto
Text is automatically filled in whenever possible.
Definition: kglobalsettings.h:187
KLineEdit
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:150
KLineEdit::setClearButtonShown
void setClearButtonShown(bool show)
This makes the line edit display an icon on one side of the line edit which, when clicked,...
Definition: klineedit.cpp:284
KUrl
KUrl::prettyUrl
QString prettyUrl(AdjustPathOption trailing=LeaveTrailingSlash) const
QComboBox
QLineEdit
QMenu
QObject
QWidget
kcombobox.h
kcompletionbox.h
kcursor.h
kdebug.h
kicon.h
kiconloader.h
kicontheme.h
klineedit.h
klocale.h
kselectaction.h
kurl.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal