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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
43 class KComboBox::KComboBoxPrivate
44 {
45 public:
46  KComboBoxPrivate() : klineEdit(0L), trapReturnKey(false)
47  {
48  }
49  ~KComboBoxPrivate()
50  {
51  }
52 
53  KLineEdit *klineEdit;
54  bool trapReturnKey;
55 };
56 
57 KComboBox::KComboBox( QWidget *parent )
58  : QComboBox( parent ), d(new KComboBoxPrivate)
59 {
60  init();
61 }
62 
63 KComboBox::KComboBox( bool rw, QWidget *parent )
64  : QComboBox( parent ), d(new KComboBoxPrivate)
65 {
66  init();
67  setEditable( rw );
68 }
69 
70 KComboBox::~KComboBox()
71 {
72  delete d;
73 }
74 
75 void 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 
88 bool 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 
102 int KComboBox::cursorPosition() const
103 {
104  return ( lineEdit() ) ? lineEdit()->cursorPosition() : -1;
105 }
106 
107 void 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 
124 bool KComboBox::autoCompletion() const
125 {
126  return completionMode() == KGlobalSettings::CompletionAuto;
127 }
128 
129 #ifndef KDE_NO_DEPRECATED
130 void KComboBox::setContextMenuEnabled( bool showMenu )
131 {
132  if( d->klineEdit )
133  d->klineEdit->setContextMenuEnabled( showMenu );
134 }
135 #endif
136 
137 
138 void KComboBox::setUrlDropsEnabled( bool enable )
139 {
140  if ( d->klineEdit )
141  d->klineEdit->setUrlDropsEnabled( enable );
142 }
143 
144 bool KComboBox::urlDropsEnabled() const
145 {
146  return d->klineEdit && d->klineEdit->urlDropsEnabled();
147 }
148 
149 
150 void KComboBox::setCompletedText( const QString& text, bool marked )
151 {
152  if ( d->klineEdit )
153  d->klineEdit->setCompletedText( text, marked );
154 }
155 
156 void KComboBox::setCompletedText( const QString& text )
157 {
158  if ( d->klineEdit )
159  d->klineEdit->setCompletedText( text );
160 }
161 
162 void 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 
176 void KComboBox::rotateText( KCompletionBase::KeyBindingType type )
177 {
178  if ( d->klineEdit )
179  d->klineEdit->rotateText( type );
180 }
181 
182 // Not needed anymore
183 bool KComboBox::eventFilter( QObject* o, QEvent* ev )
184 {
185  return QComboBox::eventFilter( o, ev );
186 }
187 
188 void 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 
198 bool KComboBox::trapReturnKey() const
199 {
200  return d->trapReturnKey;
201 }
202 
203 
204 void KComboBox::setEditUrl( const KUrl& url )
205 {
206  QComboBox::setEditText( url.prettyUrl() );
207 }
208 
209 void KComboBox::addUrl( const KUrl& url )
210 {
211  QComboBox::addItem( url.prettyUrl() );
212 }
213 
214 void KComboBox::addUrl( const QIcon& icon, const KUrl& url )
215 {
216  QComboBox::addItem( icon, url.prettyUrl() );
217 }
218 
219 void KComboBox::insertUrl( int index, const KUrl& url )
220 {
221  QComboBox::insertItem( index, url.prettyUrl() );
222 }
223 
224 void KComboBox::insertUrl( int index, const QIcon& icon, const KUrl& url )
225 {
226  QComboBox::insertItem( index, icon, url.prettyUrl() );
227 }
228 
229 void KComboBox::changeUrl( int index, const KUrl& url )
230 {
231  QComboBox::setItemText( index, url.prettyUrl() );
232 }
233 
234 void KComboBox::changeUrl( int index, const QIcon& icon, const KUrl& url )
235 {
236  QComboBox::setItemIcon( index, icon );
237  QComboBox::setItemText( index, url.prettyUrl() );
238 }
239 
240 void KComboBox::setCompletedItems( const QStringList& items, bool autosubject )
241 {
242  if ( d->klineEdit )
243  d->klineEdit->setCompletedItems( items, autosubject );
244 }
245 
246 KCompletionBox * 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
254 void KComboBox::create( WId id, bool initializeWindow, bool destroyOldWindow )
255 {
256  QComboBox::create( id, initializeWindow, destroyOldWindow );
257  KCursor::setAutoHideCursor( lineEdit(), true, true );
258 }
259 
260 void KComboBox::wheelEvent( QWheelEvent *ev )
261 {
262  // Not necessary anymore
263  QComboBox::wheelEvent( ev );
264 }
265 
266 QSize 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 
282 void 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 
347 void 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 
374 void 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 
386 void 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.h
QEvent
QWidget
QSize::isValid
bool isValid() const
QComboBox::setLineEdit
void setLineEdit(QLineEdit *edit)
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::setEditable
void setEditable(bool editable)
"Re-implemented" so that setEditable(true) creates a KLineEdit instead of QLineEdit.
Definition: kcombobox.cpp:386
QWidget::create
void create(WId window, bool initializeWindow, bool destroyOldWindow)
QSize::width
int width() const
kdebug.h
kurl.h
KCompletionBase::delegate
KCompletionBase * delegate() const
Returns the delegation object.
Definition: kcompletionbase.cpp:91
KCompletionBox
A helper widget for "completion-widgets" (KLineEdit, KComboBox))
Definition: kcompletionbox.h:43
QObject::sender
QObject * sender() const
kcompletionbox.h
QSize::rwidth
int & rwidth()
QWheelEvent
QObject::metaObject
virtual const QMetaObject * metaObject() const
QComboBox::setEditText
void setEditText(const QString &text)
QComboBox::itemText
QString itemText(int index) const
kiconloader.h
QComboBox::wheelEvent
virtual void wheelEvent(QWheelEvent *e)
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
QComboBox::isEditable
bool isEditable() const
klocale.h
KComboBox::cursorPosition
int cursorPosition() const
Returns the current cursor position.
Definition: kcombobox.cpp:102
QComboBox::setItemText
void setItemText(int index, const QString &text)
QComboBox::insertItem
void insertItem(int index, const QString &text, const QVariant &userData)
KUrl
QSize::rheight
int & rheight()
QString::isNull
bool isNull() const
KComboBox::aboutToShowContextMenu
void aboutToShowContextMenu(QMenu *p)
Emitted before the context menu is displayed.
KComboBox::setEditUrl
void setEditUrl(const KUrl &url)
Sets url into the edit field of the combobox.
Definition: kcombobox.cpp:204
QComboBox::addItem
void addItem(const QString &text, const QVariant &userData)
KComboBox::setLineEdit
virtual void setLineEdit(QLineEdit *)
Re-implemented for internal reasons.
Definition: kcombobox.cpp:282
kcursor.h
QWidget::size
QSize size() const
KComboBox::setUrlDropsEnabled
void setUrlDropsEnabled(bool enable)
Enables/Disables handling of URL drops.
Definition: kcombobox.cpp:138
QComboBox::count
int count() const
QComboBox::minimumSizeHint
virtual QSize minimumSizeHint() const
KStandardShortcut::insert
const KShortcut & insert()
Toggle insert/overwrite (with visual feedback, e.g.
Definition: kstandardguiitem.cpp:264
QObject
KComboBox::setCurrentItem
void setCurrentItem(const QString &item, bool insert=false, int index=-1)
Selects the first item that matches item.
Definition: kcombobox.cpp:347
QString::isEmpty
bool isEmpty() const
KCompletionBase::setCompletionMode
virtual void setCompletionMode(KGlobalSettings::Completion mode)
Sets the type of completion to be used.
Definition: kcompletionbase.cpp:167
QComboBox::activated
void activated(int index)
KGlobalSettings::CompletionAuto
Text is automatically filled in whenever possible.
Definition: kglobalsettings.h:187
KComboBox::~KComboBox
virtual ~KComboBox()
Destructor.
Definition: kcombobox.cpp:70
KComboBox::returnPressed
void returnPressed()
Emitted when the user presses the Enter key.
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
KComboBox::autoCompletion
bool autoCompletion() const
Re-implemented from QComboBox.
QString
KComboBox::addUrl
void addUrl(const KUrl &url)
Appends url to the combobox.
Definition: kcombobox.cpp:209
QStringList
KCursor::setAutoHideCursor
static void setAutoHideCursor(QWidget *w, bool enable, bool customEventFilter=false)
Sets auto-hiding the cursor for widget w.
Definition: kcursor.cpp:202
QMenu
QSize
QWidget::setContextMenuPolicy
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
QMetaObject::className
const char * className() const
KComboBox::setTrapReturnKey
void setTrapReturnKey(bool trap)
By default, KComboBox recognizes Key_Return and Key_Enter and emits the returnPressed() signals...
Definition: kcombobox.cpp:188
KCompletionBase
An abstract base class for adding a completion feature into widgets.
Definition: kcompletion.h:645
KComboBox::setAutoCompletion
virtual void setAutoCompletion(bool autocomplete)
Re-implemented from QComboBox.
Definition: kcombobox.cpp:107
kicontheme.h
KComboBox::completion
void completion(const QString &)
Emitted when the completion key is pressed.
KComboBox::changeUrl
void changeUrl(int index, const KUrl &url)
Replaces the item at position index with url.
Definition: kcombobox.cpp:229
QComboBox::lineEdit
QLineEdit * lineEdit() const
KLineEdit
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:149
KComboBox::completionModeChanged
void completionModeChanged(KGlobalSettings::Completion)
Emitted whenever the completion mode is changed by the user through the context menu.
kselectaction.h
QComboBox::setItemIcon
void setItemIcon(int index, const QIcon &icon)
QSize::height
int height() const
KStandardAction::create
KAction * create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
Creates an action corresponding to one of the KStandardAction::StandardAction actions, which is connected to the given object and slot, and is owned by parent.
Definition: kstandardaction.cpp:82
QComboBox::setCurrentIndex
void setCurrentIndex(int index)
KGlobalSettings::Completion
Completion
This enum describes the completion mode used for by the KCompletion class.
Definition: kglobalsettings.h:179
KCompletionBase::KeyBindingType
KeyBindingType
Constants that represent the items whose short-cut key-binding is programmable.
Definition: kcompletion.h:653
KComboBox::urlDropsEnabled
bool urlDropsEnabled() const
Returns true when decoded URL drops are enabled.
QAbstractItemView::keyboardSearch
virtual void keyboardSearch(const QString &search)
KComboBox::insertUrl
void insertUrl(int index, const KUrl &url)
Inserts url at position index into the combobox.
Definition: kcombobox.cpp:219
QComboBox::setAutoCompletion
void setAutoCompletion(bool enable)
KComboBox::setCompletedText
virtual void setCompletedText(const QString &)
Sets the completed text in the line-edit appropriately.
Definition: kcombobox.cpp:156
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
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::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: kcombobox.cpp:266
KComboBox::textRotation
void textRotation(KCompletionBase::KeyBindingType)
Emitted when the text rotation key-bindings are pressed.
KComboBox::substringCompletion
void substringCompletion(const QString &)
Emitted when the shortcut for substring completion is pressed.
klineedit.h
QLineEdit
KComboBox::completionBox
KCompletionBox * completionBox(bool create=true)
Definition: kcombobox.cpp:246
KComboBox::setCompletedItems
void setCompletedItems(const QStringList &items, bool autosubject=true)
Sets items into the completion-box if completionMode() is CompletionPopup.
Definition: kcombobox.cpp:240
KGlobalSettings::completionMode
static Completion completionMode()
Returns the preferred completion mode setting.
Definition: kglobalsettings.cpp:267
QLineEdit::cursorPosition
cursorPosition
kicon.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KComboBox::trapReturnKey
bool trapReturnKey() const
KComboBox::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
Re-implemented for internal reasons.
Definition: kcombobox.cpp:183
KComboBox::wheelEvent
virtual void wheelEvent(QWheelEvent *ev)
Definition: kcombobox.cpp:260
KUrl::prettyUrl
QString prettyUrl(AdjustPathOption trailing=LeaveTrailingSlash) const
QObject::destroyed
void destroyed(QObject *obj)
KCompletionBase::setDelegate
void setDelegate(KCompletionBase *delegate)
Sets or removes the delegation object.
Definition: kcompletionbase.cpp:78
KComboBox::setContextMenuEnabled
virtual void setContextMenuEnabled(bool showMenu)
Enables or disable the popup (context) menu.
Definition: kcombobox.cpp:130
KCompletionBase::completionMode
KGlobalSettings::Completion completionMode() const
Returns the current completion mode.
Definition: kcompletionbase.cpp:181
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
QIcon
KComboBox::makeCompletion
virtual void makeCompletion(const QString &)
Completes text according to the completion mode.
Definition: kcombobox.cpp:162
QComboBox::view
QAbstractItemView * view() const
QComboBox
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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