KCompletion

klineedit.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 1997 Sven Radej <[email protected]>
5  SPDX-FileCopyrightText: 1999 Patrick Ward <[email protected]>
6  SPDX-FileCopyrightText: 1999 Preston Brown <[email protected]>
7 
8  Re-designed for KDE 2.x by
9  SPDX-FileCopyrightText: 2000, 2001 Dawit Alemayehu <[email protected]>
10  SPDX-FileCopyrightText: 2000, 2001 Carsten Pfeiffer <[email protected]>
11 
12  SPDX-License-Identifier: LGPL-2.0-or-later
13 */
14 
15 #include "klineedit.h"
16 #include "klineedit_p.h"
17 
18 #include <KAuthorized>
19 #include <KSharedConfig>
20 #include <KConfigGroup>
21 #include <KCursor>
22 #include <kcompletionbox.h>
23 #include <KStandardShortcut>
24 #include <lineediturldropeventfilter.h>
25 
26 #include <QTimer>
27 #include <QApplication>
28 #include <QClipboard>
29 #include <QKeyEvent>
30 #include <QMenu>
31 #include <QToolTip>
32 
33 KLineEditPrivate::~KLineEditPrivate()
34 {
35 // causes a weird crash in KWord at least, so let Qt delete it for us.
36 // delete completionBox;
37 }
38 
39 void KLineEditPrivate::_k_textChanged(const QString &text)
40 {
41  Q_Q(KLineEdit);
42  // COMPAT (as documented): emit userTextChanged whenever textChanged is emitted
43  if (!completionRunning && (text != userText)) {
44  userText = text;
45 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(4, 5)
46  emit q->userTextChanged(text);
47 #endif
48  }
49 }
50 
51 // Call this when a completion operation changes the lineedit text
52 // "as if it had been edited by the user".
53 void KLineEditPrivate::updateUserText(const QString &text)
54 {
55  Q_Q(KLineEdit);
56  if (!completionRunning && (text != userText)) {
57  userText = text;
58  q->setModified(true);
59 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(4, 5)
60  emit q->userTextChanged(text);
61 #endif
62  emit q->textEdited(text);
63  emit q->textChanged(text);
64  }
65 }
66 
67 bool KLineEditPrivate::s_backspacePerformsCompletion = false;
68 bool KLineEditPrivate::s_initialized = false;
69 
70 void KLineEditPrivate::init()
71 {
72  Q_Q(KLineEdit);
73  //---
74  completionBox = nullptr;
75  handleURLDrops = true;
76  trapReturnKeyEvents = false;
77 
78  userSelection = true;
79  autoSuggest = false;
80  disableRestoreSelection = false;
81  enableSqueezedText = false;
82 
83  completionRunning = false;
84  if (!s_initialized) {
86  s_backspacePerformsCompletion = config.readEntry("Backspace performs completion", false);
87  s_initialized = true;
88  }
89 
90  urlDropEventFilter = new LineEditUrlDropEventFilter(q);
91 
92  // i18n: Placeholder text in line edit widgets is the text appearing
93  // before any user input, briefly explaining to the user what to type
94  // (e.g. "Enter search pattern").
95  // By default the text is set in italic, which may not be appropriate
96  // for some languages and scripts (e.g. for CJK ideographs).
97  QString metaMsg = KLineEdit::tr("1", "Italic placeholder text in line edits: 0 no, 1 yes");
98  italicizePlaceholder = (metaMsg.trimmed() != QLatin1Char('0'));
99  //---
100  possibleTripleClick = false;
101  bgRole = q->backgroundRole();
102 
103  // Enable the context menu by default.
104  q->QLineEdit::setContextMenuPolicy(Qt::DefaultContextMenu);
105  KCursor::setAutoHideCursor(q, true, true);
106 
107  KCompletion::CompletionMode mode = q->completionMode();
108  autoSuggest = (mode == KCompletion::CompletionMan ||
111  q->connect(q, SIGNAL(selectionChanged()), q, SLOT(_k_restoreSelectionColors()));
112 
113  if (handleURLDrops) {
114  q->installEventFilter(urlDropEventFilter);
115  }
116 
117  const QPalette p = q->palette();
118  if (!previousHighlightedTextColor.isValid()) {
119  previousHighlightedTextColor = p.color(QPalette::Normal, QPalette::HighlightedText);
120  }
121  if (!previousHighlightColor.isValid()) {
122  previousHighlightColor = p.color(QPalette::Normal, QPalette::Highlight);
123  }
124 
125  q->connect(q, SIGNAL(textChanged(QString)), q, SLOT(_k_textChanged(QString)));
126 }
127 
128 KLineEdit::KLineEdit(const QString &string, QWidget *parent)
129  : QLineEdit(string, parent), d_ptr(new KLineEditPrivate(this))
130 {
131  Q_D(KLineEdit);
132  d->init();
133 }
134 
136  : QLineEdit(parent), d_ptr(new KLineEditPrivate(this))
137 {
138  Q_D(KLineEdit);
139  d->init();
140 }
141 
143 {
144 }
145 
146 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 0)
148 {
149  return placeholderText();
150 }
151 #endif
152 
153 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 46)
155 {
156  setClearButtonEnabled(show);
157 }
158 #endif
159 
160 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 46)
162 {
163  return isClearButtonEnabled();
164 }
165 #endif
166 
168 {
169  QSize s;
170 
171  if (isClearButtonEnabled()) {
172  // from qlineedit_p.cpp
173 
174  const int iconSize = height() < 34 ? 16 : 32;
175  const int buttonWidth = iconSize + 6;
176  const int buttonHeight = iconSize + 2;
177 
178  s = QSize(buttonWidth, buttonHeight);
179  }
180 
181  return s;
182 }
183 
185 {
186  Q_D(KLineEdit);
188 
189  if (oldMode != mode && (oldMode == KCompletion::CompletionPopup ||
190  oldMode == KCompletion::CompletionPopupAuto) &&
191  d->completionBox && d->completionBox->isVisible()) {
192  d->completionBox->hide();
193  }
194 
195  // If the widgets echo mode is not Normal, no completion
196  // feature will be enabled even if one is requested.
197  if (echoMode() != QLineEdit::Normal) {
198  mode = KCompletion::CompletionNone; // Override the request.
199  }
200 
201  if (!KAuthorized::authorize(QStringLiteral("lineedit_text_completion"))) {
203  }
204 
205  if (mode == KCompletion::CompletionPopupAuto ||
206  mode == KCompletion::CompletionAuto ||
207  mode == KCompletion::CompletionMan) {
208  d->autoSuggest = true;
209  } else {
210  d->autoSuggest = false;
211  }
212 
214 }
215 
217 {
218  Q_D(KLineEdit);
219  d->disableCompletionMap[ mode ] = disable;
220 }
221 
222 void KLineEdit::setCompletedText(const QString &t, bool marked)
223 {
224  Q_D(KLineEdit);
225  if (!d->autoSuggest) {
226  return;
227  }
228 
229  const QString txt = text();
230 
231  if (t != txt) {
232  setText(t);
233  if (marked) {
234  setSelection(t.length(), txt.length() - t.length());
235  }
236  setUserSelection(false);
237  } else {
238  setUserSelection(true);
239  }
240 
241 }
242 
244 {
246  const bool marked = (mode == KCompletion::CompletionAuto ||
247  mode == KCompletion::CompletionMan ||
250  setCompletedText(text, marked);
251 }
252 
254 {
255  KCompletion *comp = compObj();
256  if (comp &&
259  QString input;
260 
262  input = comp->previousMatch();
263  } else {
264  input = comp->nextMatch();
265  }
266 
267  // Skip rotation if previous/next match is null or the same text
268  if (input.isEmpty() || input == displayText()) {
269  return;
270  }
272  }
273 }
274 
276 {
277  Q_D(KLineEdit);
278  KCompletion *comp = compObj();
280 
281  if (!comp || mode == KCompletion::CompletionNone) {
282  return; // No completion object...
283  }
284 
285  const QString match = comp->makeCompletion(text);
286 
287  if (mode == KCompletion::CompletionPopup ||
289  if (match.isEmpty()) {
290  if (d->completionBox) {
291  d->completionBox->hide();
292  d->completionBox->clear();
293  }
294  } else {
295  setCompletedItems(comp->allMatches());
296  }
297  } else { // Auto, ShortAuto (Man) and Shell
298  // all other completion modes
299  // If no match or the same match, simply return without completing.
300  if (match.isEmpty() || match == text) {
301  return;
302  }
303 
304  if (mode != KCompletion::CompletionShell) {
305  setUserSelection(false);
306  }
307 
308  if (d->autoSuggest) {
309  setCompletedText(match);
310  }
311  }
312 }
313 
315 {
316  Q_D(KLineEdit);
317  // Do not do anything if nothing changed...
318  if (readOnly == isReadOnly()) {
319  return;
320  }
321 
322  QLineEdit::setReadOnly(readOnly);
323 
324  if (readOnly) {
325  d->bgRole = backgroundRole();
327  if (d->enableSqueezedText && d->squeezedText.isEmpty()) {
328  d->squeezedText = text();
329  d->setSqueezedText();
330  }
331  } else {
332  if (!d->squeezedText.isEmpty()) {
333  setText(d->squeezedText);
334  d->squeezedText.clear();
335  }
336 
337  setBackgroundRole(d->bgRole);
338  }
339 }
340 
342 {
344  setText(text);
345 }
346 
348 {
349  Q_D(KLineEdit);
350  d->enableSqueezedText = enable;
351 }
352 
354 {
355  Q_D(const KLineEdit);
356  return d->enableSqueezedText;
357 }
358 
359 void KLineEdit::setText(const QString &text)
360 {
361  Q_D(KLineEdit);
362  if (d->enableSqueezedText && isReadOnly()) {
363  d->squeezedText = text;
364  d->setSqueezedText();
365  return;
366  }
367 
368  QLineEdit::setText(text);
369 }
370 
371 void KLineEditPrivate::setSqueezedText()
372 {
373  Q_Q(KLineEdit);
374  squeezedStart = 0;
375  squeezedEnd = 0;
376  const QString fullText = squeezedText;
377  const int fullLength = fullText.length();
378  const QFontMetrics fm(q->fontMetrics());
379  const int labelWidth = q->size().width() - 2 * q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth) - 2;
380  const int textWidth = fm.boundingRect(fullText).width();
381 
382  // TODO: investigate use of QFontMetrics::elidedText for this
383  if (textWidth > labelWidth) {
384  // TODO: better would be "…" char (0x2026), but for that one would need to ensure it's from the main font,
385  // otherwise if resulting in use of a new fallback font this can affect the metrics of the complete text,
386  // resulting in shifted characters
387  const QString ellipsisText = QStringLiteral("...");
388  // start with the dots only
389  QString squeezedText = ellipsisText;
390  int squeezedWidth = fm.boundingRect(squeezedText).width();
391 
392  // estimate how many letters we can add to the dots on both sides
393  int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2;
394  squeezedText = fullText.leftRef(letters) + ellipsisText + fullText.rightRef(letters);
395  squeezedWidth = fm.boundingRect(squeezedText).width();
396 
397  if (squeezedWidth < labelWidth) {
398  // we estimated too short
399  // add letters while text < label
400  do {
401  letters++;
402  squeezedText = fullText.leftRef(letters) + ellipsisText + fullText.rightRef(letters);
403  squeezedWidth = fm.boundingRect(squeezedText).width();
404  } while (squeezedWidth < labelWidth && letters <= fullLength / 2);
405  letters--;
406  squeezedText = fullText.leftRef(letters) + ellipsisText + fullText.rightRef(letters);
407  } else if (squeezedWidth > labelWidth) {
408  // we estimated too long
409  // remove letters while text > label
410  do {
411  letters--;
412  squeezedText = fullText.leftRef(letters) + ellipsisText + fullText.rightRef(letters);
413  squeezedWidth = fm.boundingRect(squeezedText).width();
414  } while (squeezedWidth > labelWidth && letters >= 5);
415  }
416 
417  if (letters < 5) {
418  // too few letters added -> we give up squeezing
419  q->QLineEdit::setText(fullText);
420  } else {
421  q->QLineEdit::setText(squeezedText);
422  squeezedStart = letters;
423  squeezedEnd = fullText.length() - letters;
424  }
425 
426  q->setToolTip(fullText);
427 
428  } else {
429  q->QLineEdit::setText(fullText);
430 
431  q->setToolTip(QString());
432  QToolTip::showText(q->pos(), QString()); // hide
433  }
434 
435  q->setCursorPosition(0);
436 }
437 
438 void KLineEdit::copy() const
439 {
440  Q_D(const KLineEdit);
441  if (!d->copySqueezedText(true)) {
442  QLineEdit::copy();
443  }
444 }
445 
446 bool KLineEditPrivate::copySqueezedText(bool copy) const
447 {
448  Q_Q(const KLineEdit);
449  if (!squeezedText.isEmpty() && squeezedStart) {
450  KLineEdit *that = const_cast<KLineEdit *>(q);
451  if (!that->hasSelectedText()) {
452  return false;
453  }
454  int start = q->selectionStart(), end = start + q->selectedText().length();
455  if (start >= squeezedStart + 3) {
456  start = start - 3 - squeezedStart + squeezedEnd;
457  } else if (start > squeezedStart) {
458  start = squeezedStart;
459  }
460  if (end >= squeezedStart + 3) {
461  end = end - 3 - squeezedStart + squeezedEnd;
462  } else if (end > squeezedStart) {
463  end = squeezedEnd;
464  }
465  if (start == end) {
466  return false;
467  }
468  QString t = squeezedText;
469  t = t.mid(start, end - start);
470  q->disconnect(QApplication::clipboard(), SIGNAL(selectionChanged()), q, nullptr);
472  q->connect(QApplication::clipboard(), SIGNAL(selectionChanged()), q,
473  SLOT(_q_clipboardChanged()));
474  return true;
475  }
476  return false;
477 }
478 
480 {
481  Q_D(KLineEdit);
482  if (!d->squeezedText.isEmpty()) {
483  d->setSqueezedText();
484  }
485 
487 }
488 
490 {
491  Q_D(KLineEdit);
492  const int key = e->key() | e->modifiers();
493 
494  if (KStandardShortcut::copy().contains(key)) {
495  copy();
496  return;
497  } else if (KStandardShortcut::paste().contains(key)) {
498  // TODO:
499  // we should restore the original text (not autocompleted), otherwise the paste
500  // will get into troubles Bug: 134691
501  if (!isReadOnly()) {
502  paste();
503  }
504  return;
505  } else if (KStandardShortcut::pasteSelection().contains(key)) {
507  insert(text);
508  deselect();
509  return;
510  } else if (KStandardShortcut::cut().contains(key)) {
511  if (!isReadOnly()) {
512  cut();
513  }
514  return;
515  } else if (KStandardShortcut::undo().contains(key)) {
516  if (!isReadOnly()) {
517  undo();
518  }
519  return;
520  } else if (KStandardShortcut::redo().contains(key)) {
521  if (!isReadOnly()) {
522  redo();
523  }
524  return;
525  } else if (KStandardShortcut::deleteWordBack().contains(key)) {
526  cursorWordBackward(true);
527  if (hasSelectedText() && !isReadOnly()) {
528  del();
529  }
530 
531  e->accept();
532  return;
533  } else if (KStandardShortcut::deleteWordForward().contains(key)) {
534  // Workaround for QT bug where
535  cursorWordForward(true);
536  if (hasSelectedText() && !isReadOnly()) {
537  del();
538  }
539 
540  e->accept();
541  return;
542  } else if (KStandardShortcut::backwardWord().contains(key)) {
543  cursorWordBackward(false);
544  e->accept();
545  return;
546  } else if (KStandardShortcut::forwardWord().contains(key)) {
547  cursorWordForward(false);
548  e->accept();
549  return;
550  } else if (KStandardShortcut::beginningOfLine().contains(key)) {
551  home(false);
552  e->accept();
553  return;
554  } else if (KStandardShortcut::endOfLine().contains(key)) {
555  end(false);
556  e->accept();
557  return;
558  }
559 
560  // Filter key-events if EchoMode is normal and
561  // completion mode is not set to CompletionNone
562  if (echoMode() == QLineEdit::Normal &&
564  if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
565  const bool trap = (d->completionBox && d->completionBox->isVisible());
566  const bool stopEvent = (trap || (d->trapReturnKeyEvents &&
567  (e->modifiers() == Qt::NoButton ||
568  e->modifiers() == Qt::KeypadModifier)));
569 
570  if (stopEvent) {
572  e->accept();
573  }
574 
575  emit returnPressed(displayText());
576 
577  if (trap) {
578  d->completionBox->hide();
579  deselect();
580  setCursorPosition(text().length());
581  }
582 
583  // Eat the event if the user asked for it, or if a completionbox was visible
584  if (stopEvent) {
585  return;
586  }
587  }
588 
589  const KeyBindingMap keys = keyBindingMap();
591  const bool noModifier = (e->modifiers() == Qt::NoButton ||
592  e->modifiers() == Qt::ShiftModifier ||
593  e->modifiers() == Qt::KeypadModifier);
594 
595  if ((mode == KCompletion::CompletionAuto ||
597  mode == KCompletion::CompletionMan) && noModifier) {
598  if (!d->userSelection && hasSelectedText() &&
599  (e->key() == Qt::Key_Right || e->key() == Qt::Key_Left) &&
600  e->modifiers() == Qt::NoButton) {
601  const QString old_txt = text();
602  d->disableRestoreSelection = true;
603  const int start = selectionStart();
604 
605  deselect();
607  const int cPosition = cursorPosition();
608  setText(old_txt);
609 
610  // keep cursor at cPosition
611  setSelection(old_txt.length(), cPosition - old_txt.length());
612  if (e->key() == Qt::Key_Right && cPosition > start) {
613  //the user explicitly accepted the autocompletion
614  d->updateUserText(text());
615  }
616 
617  d->disableRestoreSelection = false;
618  return;
619  }
620 
621  if (e->key() == Qt::Key_Escape) {
622  if (hasSelectedText() && !d->userSelection) {
623  del();
624  setUserSelection(true);
625  }
626 
627  // Don't swallow the Escape press event for the case
628  // of dialogs, which have Escape associated to Cancel
629  e->ignore();
630  return;
631  }
632 
633  }
634 
635  if ((mode == KCompletion::CompletionAuto ||
636  mode == KCompletion::CompletionMan) && noModifier) {
637  const QString keycode = e->text();
638  if (!keycode.isEmpty() && (keycode.unicode()->isPrint() ||
639  e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete)) {
640  const bool hasUserSelection = d->userSelection;
641  const bool hadSelection = hasSelectedText();
642 
643  bool cursorNotAtEnd = false;
644 
645  const int start = selectionStart();
646  const int cPos = cursorPosition();
647 
648  // When moving the cursor, we want to keep the autocompletion as an
649  // autocompletion, so we want to process events at the cursor position
650  // as if there was no selection. After processing the key event, we
651  // can set the new autocompletion again.
652  if (hadSelection && !hasUserSelection && start > cPos) {
653  del();
654  setCursorPosition(cPos);
655  cursorNotAtEnd = true;
656  }
657 
658  d->disableRestoreSelection = true;
660  d->disableRestoreSelection = false;
661 
662  QString txt = text();
663  int len = txt.length();
664  if (!hasSelectedText() && len /*&& cursorPosition() == len */) {
665  if (e->key() == Qt::Key_Backspace) {
666  if (hadSelection && !hasUserSelection && !cursorNotAtEnd) {
667  backspace();
668  txt = text();
669  len = txt.length();
670  }
671 
672  if (!d->s_backspacePerformsCompletion || !len) {
673  d->autoSuggest = false;
674  }
675  }
676 
677  if (e->key() == Qt::Key_Delete) {
678  d->autoSuggest = false;
679  }
680 
681  doCompletion(txt);
682 
683  if ((e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete)) {
684  d->autoSuggest = true;
685  }
686 
687  e->accept();
688  }
689 
690  return;
691  }
692 
693  } else if ((mode == KCompletion::CompletionPopup ||
695  noModifier && !e->text().isEmpty()) {
696  const QString old_txt = text();
697  const bool hasUserSelection = d->userSelection;
698  const bool hadSelection = hasSelectedText();
699  bool cursorNotAtEnd = false;
700 
701  const int start = selectionStart();
702  const int cPos = cursorPosition();
703  const QString keycode = e->text();
704 
705  // When moving the cursor, we want to keep the autocompletion as an
706  // autocompletion, so we want to process events at the cursor position
707  // as if there was no selection. After processing the key event, we
708  // can set the new autocompletion again.
709  if (hadSelection && !hasUserSelection && start > cPos &&
710  ((!keycode.isEmpty() && keycode.unicode()->isPrint()) ||
711  e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete)) {
712  del();
713  setCursorPosition(cPos);
714  cursorNotAtEnd = true;
715  }
716 
717  const int selectedLength = selectedText().length();
718 
719  d->disableRestoreSelection = true;
721  d->disableRestoreSelection = false;
722 
723  if ((selectedLength != selectedText().length()) && !hasUserSelection) {
724  d->_k_restoreSelectionColors(); // and set userSelection to true
725  }
726 
727  QString txt = text();
728  int len = txt.length();
729  if ((txt != old_txt || txt != e->text()) && len/* && ( cursorPosition() == len || force )*/ &&
730  ((!keycode.isEmpty() && keycode.unicode()->isPrint()) ||
731  e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete)) {
732  if (e->key() == Qt::Key_Backspace) {
733  if (hadSelection && !hasUserSelection && !cursorNotAtEnd) {
734  backspace();
735  txt = text();
736  len = txt.length();
737  }
738 
739  if (!d->s_backspacePerformsCompletion) {
740  d->autoSuggest = false;
741  }
742  }
743 
744  if (e->key() == Qt::Key_Delete) {
745  d->autoSuggest = false;
746  }
747 
748  if (d->completionBox) {
749  d->completionBox->setCancelledText(txt);
750  }
751 
752  doCompletion(txt);
753 
754  if ((e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete) &&
756  d->autoSuggest = true;
757  }
758 
759  e->accept();
760  } else if (!len && d->completionBox && d->completionBox->isVisible()) {
761  d->completionBox->hide();
762  }
763 
764  return;
765  } else if (mode == KCompletion::CompletionShell) {
766  // Handles completion.
768  if (keys[TextCompletion].isEmpty()) {
770  } else {
771  cut = keys[TextCompletion];
772  }
773 
774  if (cut.contains(key)) {
775  // Emit completion if the completion mode is CompletionShell
776  // and the cursor is at the end of the string.
777  const QString txt = text();
778  const int len = txt.length();
779  if (cursorPosition() == len && len != 0) {
780  doCompletion(txt);
781  return;
782  }
783  } else if (d->completionBox) {
784  d->completionBox->hide();
785  }
786  }
787 
788  // handle rotation
789  // Handles previous match
791  if (keys[PrevCompletionMatch].isEmpty()) {
793  } else {
794  cut = keys[PrevCompletionMatch];
795  }
796 
797  if (cut.contains(key)) {
798  if (emitSignals()) {
800  }
801  if (handleSignals()) {
803  }
804  return;
805  }
806 
807  // Handles next match
808  if (keys[NextCompletionMatch].isEmpty()) {
810  } else {
811  cut = keys[NextCompletionMatch];
812  }
813 
814  if (cut.contains(key)) {
815  if (emitSignals()) {
817  }
818  if (handleSignals()) {
820  }
821  return;
822  }
823 
824  // substring completion
825  if (compObj()) {
827  if (keys[SubstringCompletion].isEmpty()) {
829  } else {
830  cut = keys[SubstringCompletion];
831  }
832 
833  if (cut.contains(key)) {
834  if (emitSignals()) {
835  emit substringCompletion(text());
836  }
837  if (handleSignals()) {
839  e->accept();
840  }
841  return;
842  }
843  }
844  }
845  const int selectedLength = selectedText().length();
846 
847  // Let QLineEdit handle any other keys events.
849 
850  if (selectedLength != selectedText().length()) {
851  d->_k_restoreSelectionColors(); // and set userSelection to true
852  }
853 }
854 
856 {
857  Q_D(KLineEdit);
858  if (e->button() == Qt::LeftButton) {
859  d->possibleTripleClick = true;
861  SLOT(_k_tripleClickTimeout()));
862  }
864 }
865 
867 {
868  Q_D(KLineEdit);
869  if (e->button() == Qt::LeftButton && d->possibleTripleClick) {
870  selectAll();
871  e->accept();
872  return;
873  }
874 
875  // if middle clicking and if text is present in the clipboard then clear the selection
876  // to prepare paste operation
877  if (e->button() == Qt::MidButton) {
878  if (hasSelectedText() && !isReadOnly()) {
880  backspace();
881  }
882  }
883  }
884 
886 }
887 
889 {
890  Q_D(KLineEdit);
892 
893  if (QApplication::clipboard()->supportsSelection()) {
894  if (e->button() == Qt::LeftButton) {
895  // Fix copying of squeezed text if needed
896  d->copySqueezedText(false);
897  }
898  }
899 }
900 
901 void KLineEditPrivate::_k_tripleClickTimeout()
902 {
903  possibleTripleClick = false;
904 }
905 
907 {
908  Q_D(KLineEdit);
910 
911  if (!isReadOnly()) {
912  // FIXME: This code depends on Qt's action ordering.
913  const QList<QAction *> actionList = popup->actions();
914  enum { UndoAct, RedoAct, Separator1, CutAct, CopyAct, PasteAct, DeleteAct, ClearAct,
915  Separator2, SelectAllAct, NCountActs
916  };
917  QAction *separatorAction = nullptr;
918  // separator we want is right after Delete right now.
919  const int idx = actionList.indexOf(actionList[DeleteAct]) + 1;
920  if (idx < actionList.count()) {
921  separatorAction = actionList.at(idx);
922  }
923  if (separatorAction) {
924  QAction *clearAllAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear")), tr("C&lear", "@action:inmenu"), this);
926  connect(clearAllAction, &QAction::triggered, this, &QLineEdit::clear);
927  if (text().isEmpty()) {
928  clearAllAction->setEnabled(false);
929  }
930  popup->insertAction(separatorAction, clearAllAction);
931  }
932  }
933 
934  // If a completion object is present and the input
935  // widget is not read-only, show the Text Completion
936  // menu item.
937  if (compObj() && !isReadOnly() && KAuthorized::authorize(QStringLiteral("lineedit_text_completion"))) {
938  QMenu *subMenu = popup->addMenu(QIcon::fromTheme(QStringLiteral("text-completion")), tr("Text Completion", "@title:menu"));
939  connect(subMenu, SIGNAL(triggered(QAction*)),
940  this, SLOT(_k_completionMenuActivated(QAction*)));
941 
942  popup->addSeparator();
943 
944  QActionGroup *ag = new QActionGroup(this);
945  d->noCompletionAction = ag->addAction(tr("None", "@item:inmenu Text Completion"));
946  d->shellCompletionAction = ag->addAction(tr("Manual", "@item:inmenu Text Completion"));
947  d->autoCompletionAction = ag->addAction(tr("Automatic", "@item:inmenu Text Completion"));
948  d->popupCompletionAction = ag->addAction(tr("Dropdown List", "@item:inmenu Text Completion"));
949  d->shortAutoCompletionAction = ag->addAction(tr("Short Automatic", "@item:inmenu Text Completion"));
950  d->popupAutoCompletionAction = ag->addAction(tr("Dropdown List && Automatic", "@item:inmenu Text Completion"));
951  subMenu->addActions(ag->actions());
952 
953  //subMenu->setAccel( KStandardShortcut::completion(), ShellCompletion );
954 
955  d->shellCompletionAction->setCheckable(true);
956  d->noCompletionAction->setCheckable(true);
957  d->popupCompletionAction->setCheckable(true);
958  d->autoCompletionAction->setCheckable(true);
959  d->shortAutoCompletionAction->setCheckable(true);
960  d->popupAutoCompletionAction->setCheckable(true);
961 
962  d->shellCompletionAction->setEnabled(!d->disableCompletionMap[ KCompletion::CompletionShell ]);
963  d->noCompletionAction->setEnabled(!d->disableCompletionMap[ KCompletion::CompletionNone ]);
964  d->popupCompletionAction->setEnabled(!d->disableCompletionMap[ KCompletion::CompletionPopup ]);
965  d->autoCompletionAction->setEnabled(!d->disableCompletionMap[ KCompletion::CompletionAuto ]);
966  d->shortAutoCompletionAction->setEnabled(!d->disableCompletionMap[ KCompletion::CompletionMan ]);
967  d->popupAutoCompletionAction->setEnabled(!d->disableCompletionMap[ KCompletion::CompletionPopupAuto ]);
968 
970  d->noCompletionAction->setChecked(mode == KCompletion::CompletionNone);
971  d->shellCompletionAction->setChecked(mode == KCompletion::CompletionShell);
972  d->popupCompletionAction->setChecked(mode == KCompletion::CompletionPopup);
973  d->autoCompletionAction->setChecked(mode == KCompletion::CompletionAuto);
974  d->shortAutoCompletionAction->setChecked(mode == KCompletion::CompletionMan);
975  d->popupAutoCompletionAction->setChecked(mode == KCompletion::CompletionPopupAuto);
976 
978  if (mode != defaultMode && !d->disableCompletionMap[ defaultMode ]) {
979  subMenu->addSeparator();
980  d->defaultAction = subMenu->addAction(tr("Default", "@item:inmenu Text Completion"));
981  }
982  }
983 
984  return popup;
985 }
986 
988 {
990  return;
991  }
992  QMenu *popup = createStandardContextMenu();
993 
994  // ### do we really need this? Yes, Please do not remove! This
995  // allows applications to extend the popup menu without having to
996  // inherit from this class! (DA)
997  emit aboutToShowContextMenu(popup);
998 
999  popup->exec(e->globalPos());
1000  delete popup;
1001 }
1002 
1003 void KLineEditPrivate::_k_completionMenuActivated(QAction *act)
1004 {
1005  Q_Q(KLineEdit);
1006  KCompletion::CompletionMode oldMode = q->completionMode();
1007 
1008  if (act == noCompletionAction) {
1009  q->setCompletionMode(KCompletion::CompletionNone);
1010  } else if (act == shellCompletionAction) {
1011  q->setCompletionMode(KCompletion::CompletionShell);
1012  } else if (act == autoCompletionAction) {
1013  q->setCompletionMode(KCompletion::CompletionAuto);
1014  } else if (act == popupCompletionAction) {
1015  q->setCompletionMode(KCompletion::CompletionPopup);
1016  } else if (act == shortAutoCompletionAction) {
1017  q->setCompletionMode(KCompletion::CompletionMan);
1018  } else if (act == popupAutoCompletionAction) {
1019  q->setCompletionMode(KCompletion::CompletionPopupAuto);
1020  } else if (act == defaultAction) {
1021  q->setCompletionMode(KCompletion::CompletionPopup);
1022  } else {
1023  return;
1024  }
1025 
1026  if (oldMode != q->completionMode()) {
1027  if ((oldMode == KCompletion::CompletionPopup ||
1028  oldMode == KCompletion::CompletionPopupAuto) &&
1030  completionBox->hide();
1031  }
1032  emit q->completionModeChanged(q->completionMode());
1033  }
1034 }
1035 
1037 {
1038  Q_D(KLineEdit);
1039  KCursor::autoHideEventFilter(this, ev);
1040  if (ev->type() == QEvent::ShortcutOverride) {
1041  QKeyEvent *e = static_cast<QKeyEvent *>(ev);
1042  if (d->overrideShortcut(e)) {
1043  ev->accept();
1044  }
1045  } else if (ev->type() == QEvent::ApplicationPaletteChange
1046  || ev->type() == QEvent::PaletteChange) {
1047  // Assume the widget uses the application's palette
1049  d->previousHighlightedTextColor = p.color(QPalette::Normal, QPalette::HighlightedText);
1050  d->previousHighlightColor = p.color(QPalette::Normal, QPalette::Highlight);
1051  setUserSelection(d->userSelection);
1052  } else if (ev->type() == QEvent::ChildAdded) {
1053  QObject *obj = static_cast<QChildEvent*>(ev)->child();
1054  if (obj) {
1055  connect(obj, &QObject::objectNameChanged, this, [this, obj] {
1056  if (obj->objectName() == QLatin1String("_q_qlineeditclearaction")) {
1057  QAction *action = qobject_cast<QAction*>(obj);
1059  }
1060  });
1061  }
1062  }
1063 
1064  return QLineEdit::event(ev);
1065 }
1066 
1067 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 0)
1069 {
1070  Q_D(KLineEdit);
1071  if (enable && !d->handleURLDrops) {
1072  installEventFilter(d->urlDropEventFilter);
1073  d->handleURLDrops = true;
1074  } else if (!enable && d->handleURLDrops) {
1075  removeEventFilter(d->urlDropEventFilter);
1076  d->handleURLDrops = false;
1077  }
1078 }
1079 #endif
1080 
1081 bool KLineEdit::urlDropsEnabled() const
1082 {
1083  Q_D(const KLineEdit);
1084  return d->handleURLDrops;
1085 }
1086 
1088 {
1089  Q_D(KLineEdit);
1090  d->trapReturnKeyEvents = trap;
1091 }
1092 
1094 {
1095  Q_D(const KLineEdit);
1096  return d->trapReturnKeyEvents;
1097 }
1098 
1099 void KLineEdit::setUrl(const QUrl &url)
1100 {
1101  setText(url.toDisplayString());
1102 }
1103 
1105 {
1106  Q_D(KLineEdit);
1107  if (d->completionBox) {
1108  return;
1109  }
1110 
1111  d->completionBox = box;
1112  if (handleSignals()) {
1113  connect(d->completionBox, SIGNAL(currentTextChanged(QString)),
1114  SLOT(_k_completionBoxTextChanged(QString)));
1115  connect(d->completionBox, &KCompletionBox::userCancelled,
1116  this, &KLineEdit::userCancelled);
1117  connect(d->completionBox, SIGNAL(activated(QString)),
1118  SIGNAL(completionBoxActivated(QString)));
1119  connect(d->completionBox, SIGNAL(activated(QString)),
1120  SIGNAL(textEdited(QString)));
1121  }
1122 }
1123 
1124 /*
1125  * Set the line edit text without changing the modified flag. By default
1126  * calling setText resets the modified flag to false.
1127  */
1128 static void setEditText(KLineEdit *edit, const QString &text)
1129 {
1130  if (!edit) {
1131  return;
1132  }
1133 
1134  const bool wasModified = edit->isModified();
1135  edit->setText(text);
1136  edit->setModified(wasModified);
1137 }
1138 
1139 void KLineEdit::userCancelled(const QString &cancelText)
1140 {
1141  Q_D(KLineEdit);
1143  setEditText(this, cancelText);
1144  } else if (hasSelectedText()) {
1145  if (d->userSelection) {
1146  deselect();
1147  } else {
1148  d->autoSuggest = false;
1149  const int start = selectionStart();
1150  const QString s = text().remove(selectionStart(), selectedText().length());
1151  setEditText(this, s);
1152  setCursorPosition(start);
1153  d->autoSuggest = true;
1154  }
1155  }
1156 }
1157 
1158 bool KLineEditPrivate::overrideShortcut(const QKeyEvent *e)
1159 {
1160  Q_Q(KLineEdit);
1161  QList<QKeySequence> scKey;
1162 
1163  const int key = e->key() | e->modifiers();
1164  const KLineEdit::KeyBindingMap keys = q->keyBindingMap();
1165 
1166  if (keys[KLineEdit::TextCompletion].isEmpty()) {
1168  } else {
1169  scKey = keys[KLineEdit::TextCompletion];
1170  }
1171 
1172  if (scKey.contains(key)) {
1173  return true;
1174  }
1175 
1176  if (keys[KLineEdit::NextCompletionMatch].isEmpty()) {
1178  } else {
1179  scKey = keys[KLineEdit::NextCompletionMatch];
1180  }
1181 
1182  if (scKey.contains(key)) {
1183  return true;
1184  }
1185 
1186  if (keys[KLineEdit::PrevCompletionMatch].isEmpty()) {
1188  } else {
1189  scKey = keys[KLineEdit::PrevCompletionMatch];
1190  }
1191 
1192  if (scKey.contains(key)) {
1193  return true;
1194  }
1195 
1196  // Override all the text manupilation accelerators...
1197  if (KStandardShortcut::copy().contains(key)) {
1198  return true;
1199  } else if (KStandardShortcut::paste().contains(key)) {
1200  return true;
1201  } else if (KStandardShortcut::cut().contains(key)) {
1202  return true;
1203  } else if (KStandardShortcut::undo().contains(key)) {
1204  return true;
1205  } else if (KStandardShortcut::redo().contains(key)) {
1206  return true;
1207  } else if (KStandardShortcut::deleteWordBack().contains(key)) {
1208  return true;
1209  } else if (KStandardShortcut::deleteWordForward().contains(key)) {
1210  return true;
1211  } else if (KStandardShortcut::forwardWord().contains(key)) {
1212  return true;
1213  } else if (KStandardShortcut::backwardWord().contains(key)) {
1214  return true;
1215  } else if (KStandardShortcut::beginningOfLine().contains(key)) {
1216  return true;
1217  } else if (KStandardShortcut::endOfLine().contains(key)) {
1218  return true;
1219  }
1220 
1221  // Shortcut overrides for shortcuts that QLineEdit handles
1222  // but doesn't dare force as "stronger than kaction shortcuts"...
1223  else if (e->matches(QKeySequence::SelectAll)) {
1224  return true;
1225  } else if (qApp->platformName() == QLatin1String("xcb") && (key == Qt::CTRL + Qt::Key_E || key == Qt::CTRL + Qt::Key_U)) {
1226  return true;
1227  }
1228 
1230  const int key = e->key();
1231  const Qt::KeyboardModifiers modifiers = e->modifiers();
1232  if ((key == Qt::Key_Backtab || key == Qt::Key_Tab) &&
1233  (modifiers == Qt::NoModifier || (modifiers & Qt::ShiftModifier))) {
1234  return true;
1235  }
1236  }
1237 
1238  return false;
1239 }
1240 
1242 {
1243  Q_D(KLineEdit);
1244  QString txt;
1245  if (d->completionBox && d->completionBox->isVisible()) {
1246  // The popup is visible already - do the matching on the initial string,
1247  // not on the currently selected one.
1248  txt = completionBox()->cancelledText();
1249  } else {
1250  txt = text();
1251  }
1252 
1253  if (!items.isEmpty() &&
1254  !(items.count() == 1 && txt == items.first())) {
1255  // create completion box if non-existent
1256  completionBox();
1257 
1258  if (d->completionBox->isVisible()) {
1259  QListWidgetItem *currentItem = d->completionBox->currentItem();
1260 
1261  QString currentSelection;
1262  if (currentItem != nullptr) {
1263  currentSelection = currentItem->text();
1264  }
1265 
1266  d->completionBox->setItems(items);
1267 
1268  const QList<QListWidgetItem *> matchedItems = d->completionBox->findItems(currentSelection, Qt::MatchExactly);
1269  QListWidgetItem *matchedItem = matchedItems.isEmpty() ? nullptr : matchedItems.first();
1270 
1271  if (matchedItem) {
1272  const bool blocked = d->completionBox->blockSignals(true);
1273  d->completionBox->setCurrentItem(matchedItem);
1274  d->completionBox->blockSignals(blocked);
1275  } else {
1276  d->completionBox->setCurrentRow(-1);
1277  }
1278  } else { // completion box not visible yet -> show it
1279  if (!txt.isEmpty()) {
1280  d->completionBox->setCancelledText(txt);
1281  }
1282  d->completionBox->setItems(items);
1283  d->completionBox->popup();
1284  }
1285 
1286  if (d->autoSuggest && autoSuggest) {
1287  const int index = items.first().indexOf(txt);
1288  const QString newText = items.first().mid(index);
1289  setUserSelection(false); // can be removed? setCompletedText sets it anyway
1290  setCompletedText(newText, true);
1291  }
1292  } else {
1293  if (d->completionBox && d->completionBox->isVisible()) {
1294  d->completionBox->hide();
1295  }
1296  }
1297 }
1298 
1300 {
1301  Q_D(KLineEdit);
1302  if (create && !d->completionBox) {
1303  setCompletionBox(new KCompletionBox(this));
1304  d->completionBox->setObjectName(QStringLiteral("completion box"));
1305  d->completionBox->setFont(font());
1306  }
1307 
1308  return d->completionBox;
1309 }
1310 
1312 {
1313  KCompletion *oldComp = compObj();
1314  if (oldComp && handleSignals())
1315  disconnect(oldComp, SIGNAL(matches(QStringList)),
1316  this, SLOT(setCompletedItems(QStringList)));
1317 
1318  if (comp && handle)
1319  connect(comp, SIGNAL(matches(QStringList)),
1320  this, SLOT(setCompletedItems(QStringList)));
1321 
1323 }
1324 
1325 void KLineEdit::setUserSelection(bool userSelection)
1326 {
1327  Q_D(KLineEdit);
1328  //if !d->userSelection && userSelection we are accepting a completion,
1329  //so trigger an update
1330 
1331  if (!d->userSelection && userSelection) {
1332  d->updateUserText(text());
1333  }
1334 
1335  QPalette p = palette();
1336 
1337  if (userSelection) {
1338  p.setColor(QPalette::Highlight, d->previousHighlightColor);
1339  p.setColor(QPalette::HighlightedText, d->previousHighlightedTextColor);
1340  } else {
1344  p.setColor(QPalette::Highlight, color);
1345  }
1346 
1347  d->userSelection = userSelection;
1348  setPalette(p);
1349 }
1350 
1351 void KLineEditPrivate::_k_restoreSelectionColors()
1352 {
1353  Q_Q(KLineEdit);
1354  if (disableRestoreSelection) {
1355  return;
1356  }
1357 
1358  q->setUserSelection(true);
1359 }
1360 
1361 void KLineEditPrivate::_k_completionBoxTextChanged(const QString &text)
1362 {
1363  Q_Q(KLineEdit);
1364  if (!text.isEmpty()) {
1365  q->setText(text);
1366  q->setModified(true);
1367  q->end(false); // force cursor at end
1368  }
1369 }
1370 
1372 {
1373  Q_D(const KLineEdit);
1374  if (d->enableSqueezedText && isReadOnly()) {
1375  return d->squeezedText;
1376  }
1377 
1378  return text();
1379 }
1380 
1382 {
1383  Q_D(const KLineEdit);
1384  return d->userText;
1385 }
1386 
1388 {
1389  Q_D(const KLineEdit);
1390  return d->autoSuggest;
1391 }
1392 
1393 void KLineEdit::paintEvent(QPaintEvent *ev)
1394 {
1395  Q_D(KLineEdit);
1396  if (echoMode() == Password && d->threeStars) {
1397  // ### hack alert!
1398  // QLineEdit has currently no hooks to modify the displayed string.
1399  // When we call setText(), an update() is triggered and we get
1400  // into an infinite recursion.
1401  // Qt offers the setUpdatesEnabled() method, but when we re-enable
1402  // them, update() is triggered, and we get into the same recursion.
1403  // To work around this problem, we set/clear the internal Qt flag which
1404  // marks the updatesDisabled state manually.
1406  blockSignals(true);
1407  const QString oldText = text();
1408  const bool isModifiedState = isModified(); // save modified state because setText resets it
1409  setText(oldText + oldText + oldText);
1411  setText(oldText);
1412  setModified(isModifiedState);
1413  blockSignals(false);
1415  } else {
1417  }
1418 }
1419 
1420 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 0)
1422 {
1423  setPlaceholderText(msg);
1424 }
1425 #endif
1426 
1427 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(4, 5)
1429 {
1431 }
1432 #endif
1433 
1434 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(4, 5)
1436 {
1438 }
1439 #endif
1440 
1442 {
1443  Q_D(KLineEdit);
1444  if (passwordMode) {
1445  KConfigGroup cg(KSharedConfig::openConfig(), "Passwords");
1446  const QString val = cg.readEntry("EchoMode", "OneStar");
1447  if (val == QLatin1String("NoEcho")) {
1449  } else {
1450  d->threeStars = (val == QLatin1String("ThreeStars"));
1452  }
1453  } else {
1455  }
1456 }
1457 
1458 bool KLineEdit::passwordMode() const
1459 {
1460  return echoMode() == NoEcho || echoMode() == Password;
1461 }
1462 
1464 {
1465  Q_D(KLineEdit);
1466  if (emitSignals()) {
1467  emit completion(text); // emit when requested...
1468  }
1469  d->completionRunning = true;
1470  if (handleSignals()) {
1471  makeCompletion(text); // handle when requested...
1472  }
1473  d->completionRunning = false;
1474 }
1475 
1476 #include "moc_klineedit.cpp"
void setCompletionModeDisabled(KCompletion::CompletionMode mode, bool disable=true)
Disables completion modes by makeing them non-checkable.
Definition: klineedit.cpp:216
void undo()
void setCompletedItems(const QStringList &items, bool autoSuggest=true) override
Same as the above function except it allows you to temporarily turn off text completion in Completion...
Definition: klineedit.cpp:1241
KeypadModifier
void triggered(bool checked)
Qt::KeyboardModifiers modifiers() const const
const QList< QKeySequence > & redo()
ShortcutOverride
const QList< QKeySequence > & deleteWordBack()
QEvent::Type type() const const
int doubleClickInterval()
const QPalette & palette() const const
QString toDisplayString(QUrl::FormattingOptions options) const const
Lists all possible matches in a popup list box to choose from.
Definition: kcompletion.h:149
void create(WId window, bool initializeWindow, bool destroyOldWindow)
int width() const const
void keyPressEvent(QKeyEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:489
MatchExactly
void setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color)
void setPasswordMode(bool passwordMode=true)
set the line edit in password mode.
Definition: klineedit.cpp:1441
void clear()
QString text() const const
A helper widget for "completion-widgets" (KLineEdit, KComboBox))
void addActions(QList< QAction * > actions)
QMenu * createStandardContextMenu()
Reimplemented for internal reasons.
Definition: klineedit.cpp:906
virtual bool event(QEvent *e) override
void paste()
Text is automatically filled in whenever possible.
Definition: kcompletion.h:137
const QColor & color(QPalette::ColorGroup group, QPalette::ColorRole role) const const
const T & at(int i) const const
QString userText() const
Returns the text as given by the user (i.e.
Definition: klineedit.cpp:1381
Switch to next completion (by default Ctrl-Down).
KLineEdit(const QString &string, QWidget *parent=nullptr)
Constructs a KLineEdit object with a default text, a parent, and a name.
Definition: klineedit.cpp:128
bool hasSelectedText() const const
bool isVisible() const const
void setCompletionBox(KCompletionBox *box)
Set the completion-box to be used in completion mode CompletionPopup.
Definition: klineedit.cpp:1104
QMenu * createStandardContextMenu()
NoButton
bool isSqueezedTextEnabled() const
Returns true if text squeezing is enabled.
Definition: klineedit.cpp:353
bool trapReturnKey() const
Definition: klineedit.cpp:1093
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QLineEdit::EchoMode echoMode() const const
bool autoSuggest() const
Whether in current state text should be auto-suggested.
Definition: klineedit.cpp:1387
QAction * addAction(QAction *action)
KCompletion * compObj() const
Returns a pointer to the completion object.
QString clickMessage() const
bool event(QEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:1036
void textRotation(KCompletionBase::KeyBindingType)
Emitted when the text rotation key-bindings are pressed.
QString & remove(int position, int n)
Text completion (by default Ctrl-E).
const QList< QKeySequence > & cut()
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
const QPoint & globalPos() const const
void showText(const QPoint &pos, const QString &text, QWidget *w)
QString tr(const char *sourceText, const char *disambiguation, int n)
QString selectedText() const const
virtual void keyPressEvent(QKeyEvent *event) override
QAction * addAction(const QString &text)
virtual KCompletionBox * completionBox(bool create=true)
Definition: klineedit.cpp:1299
DefaultContextMenu
void cursorWordBackward(bool mark)
static void autoHideEventFilter(QObject *, QEvent *)
bool isModified() const const
void setUrl(const QUrl &url)
Sets url into the lineedit.
Definition: klineedit.cpp:1099
KSharedConfigPtr config()
KCompletion::CompletionMode completionMode() const
Returns the current completion mode.
void userCancelled(const QString &cancelText)
Resets the current displayed text.
Definition: klineedit.cpp:1139
int indexOf(const T &value, int from) const const
void clear()
void contextMenuEvent(QContextMenuEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:987
void clearButtonClicked()
Emitted when the user clicked on the clear button.
const QList< QKeySequence > & deleteWordForward()
virtual void setContextMenuEnabled(bool showMenu)
Enables/disables the popup (context) menu.
Definition: klineedit.cpp:1428
void setUrlDropsEnabled(bool enable)
Enables/Disables handling of URL drops.
Definition: klineedit.cpp:1068
bool isPrint() const const
KeyBindingMap keyBindingMap() const
Returns a key binding map.
const QList< QKeySequence > & beginningOfLine()
void mouseDoubleClickEvent(QMouseEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:855
void setCompletionMode(KCompletion::CompletionMode mode) override
Reimplemented from KCompletionBase for internal reasons.
Definition: klineedit.cpp:184
void insertAction(QAction *before, QAction *action)
const QList< QKeySequence > & undo()
int count(const T &value) const const
void mouseReleaseEvent(QMouseEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:888
const QList< QKeySequence > & forwardWord()
void setShortcuts(const QList< QKeySequence > &shortcuts)
void ignore()
Substring completion (by default Ctrl-T).
void substringCompletion(const QString &)
Emitted when the shortcut for substring completion is pressed.
void installEventFilter(QObject *filterObj)
Switch to previous completion (by default Ctrl-Up).
void completion(const QString &)
Emitted when the completion key is pressed.
const QList< QKeySequence > & copy()
No completion is used.
Definition: kcompletion.h:133
bool passwordMode() const
bool urlDropsEnabled() const
Returns true when decoded URL drops are enabled.
QStringRef leftRef(int n) const const
QPalette palette()
QStringList allMatches()
Returns a list of all items matching the last completed string.
Qt::MouseButton button() const const
QPalette::ColorRole backgroundRole() const const
CompletionMode
This enum describes the completion mode used for by the KCompletion class.
Definition: kcompletion.h:129
bool isEmpty() const const
bool isEmpty() const const
QString trimmed() const const
A generic class for completing QStrings.
Definition: kcompletion.h:115
void setUserSelection(bool userSelection)
Sets the widget in userSelection mode or in automatic completion selection mode.
Definition: klineedit.cpp:1325
QString text() const const
void setSqueezedTextEnabled(bool enable)
Enable text squeezing whenever the supplied text is too long.
Definition: klineedit.cpp:347
QStringRef rightRef(int n) const const
void setClearButtonEnabled(bool enable)
bool handleSignals() const
Returns true if the object handles the signals.
T & first()
QAction * addSeparator()
void hide()
void aboutToShowContextMenu(QMenu *contextMenu)
Emitted before the context menu is displayed.
void selectAll()
const QList< QKeySequence > & pasteSelection()
QString placeholderText() const const
void setSqueezedText(const QString &text)
Squeezes text into the line edit.
Definition: klineedit.cpp:341
QAction * exec()
virtual void paintEvent(QPaintEvent *) override
void backspace()
bool matches(QKeySequence::StandardKey key) const const
void redo()
void completionBoxActivated(const QString &)
Emitted whenever the completion box is activated.
void setCompletedText(const QString &) override
See KCompletionBase::setCompletedText.
Definition: klineedit.cpp:243
virtual void mouseReleaseEvent(QMouseEvent *e) override
int selectionStart() const const
QString originalText() const
Returns the original text if text squeezing is enabled.
Definition: klineedit.cpp:1371
static void setAutoHideCursor(QWidget *w, bool enable, bool customEventFilter=false)
int key() const const
void textEdited(const QString &text)
void setCompletionObject(KCompletion *, bool handle=true) override
Reimplemented for internal reasons, the API is not affected.
Definition: klineedit.cpp:1311
void accept()
void insert(const QString &newText)
bool blockSignals(bool block)
void setTrapReturnKey(bool trap)
By default, KLineEdit recognizes Key_Return and Key_Enter and emits the returnPressed() signals...
Definition: klineedit.cpp:1087
const QFont & font() const const
Qt::ContextMenuPolicy contextMenuPolicy() const const
bool contains(const T &value) const const
QString nextMatch()
Returns the next item from the list of matching items.
void resizeEvent(QResizeEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:479
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QSize clearButtonUsedSize() const
Definition: klineedit.cpp:167
const QList< QKeySequence > & endOfLine()
void mousePressEvent(QMouseEvent *) override
Reimplemented for internal reasons.
Definition: klineedit.cpp:866
virtual void setCompletionMode(KCompletion::CompletionMode mode)
Sets the type of completion to be used.
bool isContextMenuEnabled() const
Returns true when the context menu is enabled.
Definition: klineedit.cpp:1435
WA_UpdatesDisabled
void returnPressed()
QString text(QClipboard::Mode mode) const const
const QChar * unicode() const const
QList< QAction * > actions() const const
Completes text much in the same way as a typical *nix shell would.
Definition: kcompletion.h:145
void end(bool mark)
virtual QString makeCompletion(const QString &string)
Attempts to find an item in the list of available completions that begins with string.
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:140
void cut()
PM_DefaultFrameWidth
QString mid(int position, int n) const const
void del()
const QList< QKeySequence > & backwardWord()
void objectNameChanged(const QString &objectName)
void home(bool mark)
void cursorWordForward(bool mark)
virtual void copy() const
Reimplemented for internal reasons, the API is not affected.
Definition: klineedit.cpp:438
void selectionChanged()
QAction * addMenu(QMenu *menu)
KCONFIGCORE_EXPORT bool authorize(const QString &action)
bool isClearButtonShown() const
Definition: klineedit.cpp:161
int length() const const
bool emitSignals() const
Returns true if the object emits the signals.
KeyBindingType
Constants that represent the items whose shortcut key binding is programmable.
void userCancelled(const QString &)
Emitted whenever the user chooses to ignore the available selections and closes this box...
~KLineEdit() override
Destructor.
Definition: klineedit.cpp:142
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:154
QIcon fromTheme(const QString &name)
QList< QKeySequence > keyBindings(QKeySequence::StandardKey key)
void setText(const QString &text, QClipboard::Mode mode)
QSize size(int flags, const QString &text, int tabStops, int *tabArray) const const
const QList< QKeySequence > & shortcut(StandardShortcut id)
void show()
void setClickMessage(const QString &msg)
This makes the line edit display a grayed-out hinting text as long as the user didn&#39;t enter any text...
Definition: klineedit.cpp:1421
Lists all possible matches in a popup list box to choose from, and automatically fills the result whe...
Definition: kcompletion.h:154
virtual void setCompletionObject(KCompletion *completionObject, bool handleSignals=true)
Sets up the completion object to be used.
const QList< QKeySequence > & paste()
virtual void resizeEvent(QResizeEvent *event)
void copy() const const
void setBackgroundRole(QPalette::ColorRole role)
void setCursorPosition(int)
virtual void makeCompletion(const QString &)
Completes the remaining text with a matching one from a given list.
Definition: klineedit.cpp:275
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QList< QAction * > actions() const const
T qobject_cast(QObject *object)
virtual void setReadOnly(bool)
Sets the lineedit to read-only.
Definition: klineedit.cpp:314
QObject * parent() const const
virtual void setText(const QString &)
Reimplemented to enable text squeezing.
Definition: klineedit.cpp:359
virtual void mouseDoubleClickEvent(QMouseEvent *e) override
void deselect()
T readEntry(const QString &key, const T &aDefault) const
QClipboard * clipboard()
Same as automatic, but shortest match is used for completion.
Definition: kcompletion.h:141
Key_Return
void setEnabled(bool)
QString text() const const
QString previousMatch()
Returns the next item from the list of matching items.
void rotateText(KCompletionBase::KeyBindingType type)
Iterates through all possible matches of the completed text or the history list.
Definition: klineedit.cpp:253
void removeEventFilter(QObject *obj)
virtual void mousePressEvent(QMouseEvent *e) override
int height() const const
void doCompletion(const QString &text)
Do completion now.
Definition: klineedit.cpp:1463
void setSelection(int start, int length)
QString displayText() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 22:55:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.