29 #include "klineedit_p.h"
46 #include <QtCore/QTimer>
47 #include <QtGui/QClipboard>
48 #include <QtGui/QStyleOption>
49 #include <QtGui/QToolTip>
53 class KLineEditPrivate
60 handleURLDrops =
true;
61 grabReturnKeyEvents =
false;
65 disableRestoreSelection =
false;
66 enableSqueezedText =
false;
68 enableClickMsg =
false;
70 completionRunning =
false;
73 s_backspacePerformsCompletion =
config.readEntry(
"Backspace performs completion",
false);
79 wideEnoughForClear =
true;
86 QString metaMsg =
i18nc(
"Italic placeholder text in line edits: 0 no, 1 yes",
"1");
87 italicizePlaceholder = (metaMsg.trimmed() !=
QString(
'0'));
97 void _k_slotSettingsChanged(
int category)
106 void _k_textChanged(
const QString &txt)
110 if (!completionRunning && (txt != userText)) {
112 #ifndef KDE_NO_DEPRECATED
113 emit q->userTextChanged(txt);
120 void _k_updateUserText(
const QString &txt)
122 if (!completionRunning && (txt != userText)) {
124 q->setModified(
true);
125 #ifndef KDE_NO_DEPRECATED
126 emit q->userTextChanged(txt);
128 emit q->textEdited(txt);
129 emit q->textChanged(txt);
137 void adjustForReadOnly()
139 if (style && style.data()->m_overlap) {
140 style.data()->m_overlap = 0;
150 bool overrideShortcut(
const QKeyEvent* e);
152 static bool s_initialized;
153 static bool s_backspacePerformsCompletion;
155 QColor previousHighlightColor;
156 QColor previousHighlightedTextColor;
158 bool userSelection: 1;
159 bool autoSuggest : 1;
160 bool disableRestoreSelection: 1;
161 bool handleURLDrops:1;
162 bool grabReturnKeyEvents:1;
163 bool enableSqueezedText:1;
164 bool completionRunning:1;
168 QPalette::ColorRole bgRole;
173 bool enableClickMsg:1;
176 bool possibleTripleClick :1;
179 bool wideEnoughForClear:1;
180 KLineEditButton *clearButton;
181 QWeakPointer<KLineEditStyle> style;
186 bool italicizePlaceholder:1;
188 QAction *noCompletionAction, *shellCompletionAction, *autoCompletionAction, *popupCompletionAction, *shortAutoCompletionAction, *popupAutoCompletionAction, *defaultAction;
194 QStyle *KLineEditStyle::style()
const
197 return m_subStyle.data();
200 return KdeUiProxyStyle::style();
203 QRect KLineEditStyle::subElementRect(SubElement element,
const QStyleOption *option,
const QWidget *widget)
const
205 if (element == SE_LineEditContents) {
206 KLineEditStyle *unconstThis =
const_cast<KLineEditStyle *
>(
this);
210 unconstThis->m_subStyle.clear();
213 unconstThis->m_sentinel =
true;
214 QStyle *s = m_subStyle ? m_subStyle.data() : style();
215 QRect rect = s->subElementRect(SE_LineEditContents, option, widget);
216 unconstThis->m_sentinel =
false;
218 if (option->direction == Qt::LeftToRight) {
219 return rect.adjusted(0, 0, -m_overlap, 0);
221 return rect.adjusted(m_overlap, 0, 0, 0);
225 return KdeUiProxyStyle::subElementRect(element, option, widget);
228 bool KLineEditPrivate::s_backspacePerformsCompletion =
false;
229 bool KLineEditPrivate::s_initialized =
false;
233 :
QLineEdit( string, parent ), d(new KLineEditPrivate(this))
239 :
QLineEdit( parent ), d(new KLineEditPrivate(this))
250 void KLineEdit::init()
252 d->possibleTripleClick =
false;
253 d->bgRole = backgroundRole();
256 QLineEdit::setContextMenuPolicy( Qt::DefaultContextMenu );
263 connect(
this, SIGNAL(selectionChanged()),
this, SLOT(slotRestoreSelectionColors()));
265 connect(
KGlobalSettings::self(), SIGNAL(settingsChanged(
int)),
this, SLOT(_k_slotSettingsChanged(
int)));
267 const QPalette p = palette();
268 if ( !d->previousHighlightedTextColor.isValid() )
269 d->previousHighlightedTextColor=p.color(QPalette::Normal,QPalette::HighlightedText);
270 if ( !d->previousHighlightColor.isValid() )
271 d->previousHighlightColor=p.color(QPalette::Normal,QPalette::Highlight);
274 setStyle(d->style.data());
276 connect(
this, SIGNAL(textChanged(
QString)),
this, SLOT(_k_textChanged(
QString)));
281 return d->clickMessage;
287 if (d->clearButton) {
291 d->clearButton =
new KLineEditButton(
this);
292 d->clearButton->setObjectName(
"KLineEditButton");
293 d->clearButton->setCursor( Qt::ArrowCursor );
294 d->clearButton->setToolTip(
i18nc(
"@action:button Clear current text in the line edit",
"Clear text" ) );
296 updateClearButtonIcon(text());
298 connect(
this, SIGNAL(textChanged(
QString)),
this, SLOT(updateClearButtonIcon(
QString)));
300 disconnect(
this, SIGNAL(textChanged(
QString)),
this, SLOT(updateClearButtonIcon(
QString)));
301 delete d->clearButton;
303 d->clickInClear =
false;
305 d->style.data()->m_overlap = 0;
312 return d->clearButton != 0;
318 if (d->clearButton) {
319 const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0,
this);
320 s = d->clearButton->sizeHint();
321 s.rwidth() += frameWidth;
327 void KLineEdit::updateClearButtonIcon(
const QString& text)
329 if (!d->clearButton) {
333 d->adjustForReadOnly();
338 if (d->clearButton->pixmap().isNull()) {
340 if (layoutDirection() == Qt::LeftToRight) {
341 d->clearButton->setPixmap(
SmallIcon(
"edit-clear-locationbar-rtl", 0, clearButtonState));
343 d->clearButton->setPixmap(
SmallIcon(
"edit-clear-locationbar-ltr", 0, clearButtonState));
348 if (d->wideEnoughForClear && text.length() > 0) {
349 d->clearButton->animateVisible(
true);
351 d->clearButton->animateVisible(
false);
356 void KLineEdit::updateClearButton()
358 if (!d->clearButton) {
362 d->adjustForReadOnly();
366 const QSize geom = size();
367 const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,
this);
368 const int buttonWidth = d->clearButton->sizeHint().width();
369 const QSize newButtonSize(buttonWidth, geom.height());
370 const QFontMetrics fm(font());
371 const int em = fm.width(
"m");
375 const bool wideEnough = geom.width() > 4 * em + buttonWidth + frameWidth;
377 if (newButtonSize != d->clearButton->size()) {
378 d->clearButton->resize(newButtonSize);
382 d->style.data()->m_overlap = wideEnough ? buttonWidth + frameWidth : 0;
385 if (layoutDirection() == Qt::LeftToRight ) {
386 d->clearButton->move(geom.width() - frameWidth - buttonWidth - 1, 0);
388 d->clearButton->move(frameWidth + 1, 0);
391 if (wideEnough != d->wideEnoughForClear) {
395 d->wideEnoughForClear = wideEnough;
396 updateClearButtonIcon(text());
406 d->completionBox && d->completionBox->isVisible() )
407 d->completionBox->hide();
411 if ( echoMode() != QLineEdit::Normal )
420 d->autoSuggest =
true;
422 d->autoSuggest =
false;
429 d->disableCompletionMap[ mode ] = disable;
434 if ( !d->autoSuggest )
443 setSelection(t.length(), txt.length()-t.length());
476 if ( input.isEmpty() || input == displayText() )
495 if ( match.isEmpty() )
497 if ( d->completionBox )
499 d->completionBox->hide();
500 d->completionBox->clear();
510 if ( match.isEmpty() || match == text )
516 if ( d->autoSuggest )
524 if (readOnly == isReadOnly ()) {
528 QLineEdit::setReadOnly(readOnly);
531 d->bgRole = backgroundRole();
532 setBackgroundRole(QPalette::Window);
533 if (d->enableSqueezedText && d->squeezedText.isEmpty()) {
534 d->squeezedText = text();
538 if (d->clearButton) {
539 d->clearButton->animateVisible(
false);
540 d->adjustForReadOnly();
543 if (!d->squeezedText.isEmpty()) {
545 d->squeezedText.clear();
548 setBackgroundRole(d->bgRole);
561 d->enableSqueezedText = enable;
566 return d->enableSqueezedText;
571 if( d->enableClickMsg )
575 if( d->enableSqueezedText && isReadOnly() )
577 d->squeezedText = text;
582 QLineEdit::setText( text );
585 void KLineEdit::setSqueezedText()
587 d->squeezedStart = 0;
589 const QString fullText = d->squeezedText;
590 const int fullLength = fullText.length();
591 const QFontMetrics fm(fontMetrics());
592 const int labelWidth = size().width() - 2*style()->pixelMetric(QStyle::PM_DefaultFrameWidth) - 2;
593 const int textWidth = fm.width(fullText);
595 if (textWidth > labelWidth)
599 int squeezedWidth = fm.width(squeezedText);
602 int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2;
603 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
604 squeezedWidth = fm.width(squeezedText);
606 if (squeezedWidth < labelWidth)
613 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
614 squeezedWidth = fm.width(squeezedText);
615 }
while (squeezedWidth < labelWidth && letters <= fullLength / 2);
617 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
619 else if (squeezedWidth > labelWidth)
626 squeezedText = fullText.left(letters) +
"..." + fullText.right(letters);
627 squeezedWidth = fm.width(squeezedText);
628 }
while (squeezedWidth > labelWidth && letters >= 5);
634 QLineEdit::setText(fullText);
638 QLineEdit::setText(squeezedText);
639 d->squeezedStart = letters;
640 d->squeezedEnd = fullText.length() - letters;
643 setToolTip( fullText );
648 QLineEdit::setText(fullText);
650 this->setToolTip(
"" );
651 QToolTip::showText(pos(),
QString());
654 setCursorPosition(0);
659 if( !copySqueezedText(
true))
663 bool KLineEdit::copySqueezedText(
bool clipboard)
const
665 if (!d->squeezedText.isEmpty() && d->squeezedStart)
668 if (!that->hasSelectedText())
670 int start = selectionStart(),
end = start + selectedText().length();
671 if (start >= d->squeezedStart+3)
672 start = start - 3 - d->squeezedStart + d->squeezedEnd;
673 else if (start > d->squeezedStart)
674 start = d->squeezedStart;
675 if (
end >= d->squeezedStart+3)
676 end =
end - 3 - d->squeezedStart + d->squeezedEnd;
677 else if (
end > d->squeezedStart)
678 end = d->squeezedEnd;
682 t = t.mid(start,
end - start);
683 disconnect( QApplication::clipboard(), SIGNAL(selectionChanged()),
this, 0);
684 QApplication::clipboard()->setText( t, clipboard ? QClipboard::Clipboard : QClipboard::Selection );
685 connect( QApplication::clipboard(), SIGNAL(selectionChanged()),
this,
686 SLOT(_q_clipboardChanged()) );
694 if (!d->squeezedText.isEmpty())
698 QLineEdit::resizeEvent(ev);
704 const int key = e->key() | e->modifiers();
722 QString text = QApplication::clipboard()->text( QClipboard::Selection);
748 cursorWordBackward(
true);
749 if ( hasSelectedText() )
758 cursorWordForward(
true);
759 if ( hasSelectedText() )
767 cursorWordBackward(
false);
773 cursorWordForward(
false);
793 if ( echoMode() == QLineEdit::Normal &&
796 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
797 const bool trap = (d->completionBox && d->completionBox->isVisible());
798 const bool stopEvent = (trap || (d->grabReturnKeyEvents &&
799 (e->modifiers() == Qt::NoButton ||
800 e->modifiers() == Qt::KeypadModifier)));
803 emit QLineEdit::returnPressed();
810 d->completionBox->hide();
812 setCursorPosition(text().length());
823 const bool noModifier = (e->modifiers() == Qt::NoButton ||
824 e->modifiers() == Qt::ShiftModifier ||
825 e->modifiers() == Qt::KeypadModifier);
831 if ( !d->userSelection && hasSelectedText() &&
832 ( e->key() == Qt::Key_Right || e->key() == Qt::Key_Left ) &&
833 e->modifiers()==Qt::NoButton )
835 const QString old_txt = text();
836 d->disableRestoreSelection =
true;
837 const int start = selectionStart();
840 QLineEdit::keyPressEvent ( e );
841 const int cPosition=cursorPosition();
845 setSelection(old_txt.length(), cPosition - old_txt.length());
846 if (e->key() == Qt::Key_Right && cPosition > start )
849 d->_k_updateUserText(text());
852 d->disableRestoreSelection =
false;
856 if ( e->key() == Qt::Key_Escape )
858 if (hasSelectedText() && !d->userSelection )
875 const QString keycode = e->text();
876 if ( !keycode.isEmpty() && (keycode.unicode()->isPrint() ||
877 e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete ) )
879 const bool hasUserSelection=d->userSelection;
880 const bool hadSelection=hasSelectedText();
882 bool cursorNotAtEnd=
false;
884 const int start = selectionStart();
885 const int cPos = cursorPosition();
891 if ( hadSelection && !hasUserSelection && start>cPos )
894 setCursorPosition(cPos);
898 d->disableRestoreSelection =
true;
899 QLineEdit::keyPressEvent ( e );
900 d->disableRestoreSelection =
false;
903 int len = txt.length();
904 if ( !hasSelectedText() && len )
906 if ( e->key() == Qt::Key_Backspace )
908 if ( hadSelection && !hasUserSelection && !cursorNotAtEnd )
915 if (!d->s_backspacePerformsCompletion || !len) {
916 d->autoSuggest =
false;
920 if (e->key() == Qt::Key_Delete )
921 d->autoSuggest=
false;
925 if( (e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete) )
938 noModifier && !e->text().isEmpty() )
940 const QString old_txt = text();
941 const bool hasUserSelection=d->userSelection;
942 const bool hadSelection=hasSelectedText();
943 bool cursorNotAtEnd=
false;
945 const int start = selectionStart();
946 const int cPos = cursorPosition();
947 const QString keycode = e->text();
953 if (hadSelection && !hasUserSelection && start>cPos &&
954 ( (!keycode.isEmpty() && keycode.unicode()->isPrint()) ||
955 e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete ) )
958 setCursorPosition(cPos);
962 const int selectedLength=selectedText().length();
964 d->disableRestoreSelection =
true;
965 QLineEdit::keyPressEvent ( e );
966 d->disableRestoreSelection =
false;
968 if (( selectedLength != selectedText().length() ) && !hasUserSelection )
969 slotRestoreSelectionColors();
972 int len = txt.length();
973 if ( ( txt != old_txt || txt != e->text() ) && len &&
974 ( (!keycode.isEmpty() && keycode.unicode()->isPrint()) ||
975 e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete) )
977 if ( e->key() == Qt::Key_Backspace )
979 if ( hadSelection && !hasUserSelection && !cursorNotAtEnd )
986 if (!d->s_backspacePerformsCompletion) {
987 d->autoSuggest =
false;
991 if (e->key() == Qt::Key_Delete )
992 d->autoSuggest=
false;
994 if ( d->completionBox )
995 d->completionBox->setCancelledText( txt );
999 if ( (e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete ) &&
1001 d->autoSuggest=
true;
1005 else if (!len && d->completionBox && d->completionBox->isVisible())
1006 d->completionBox->hide();
1025 const int len = txt.length();
1026 if ( cursorPosition() == len && len != 0 )
1032 else if ( d->completionBox )
1033 d->completionBox->hide();
1093 const int selectedLength = selectedText().length();
1096 QLineEdit::keyPressEvent ( e );
1098 if ( selectedLength != selectedText().length() )
1099 slotRestoreSelectionColors();
1104 if ( e->button() == Qt::LeftButton )
1106 d->possibleTripleClick=
true;
1107 QTimer::singleShot( QApplication::doubleClickInterval(),
this,
1108 SLOT(tripleClickTimeout()) );
1110 QLineEdit::mouseDoubleClickEvent( e );
1115 if ( (e->button() == Qt::LeftButton ||
1116 e->button() == Qt::MidButton ) &&
1118 d->clickInClear = ( d->clearButton == childAt(e->pos()) || d->clearButton->underMouse() );
1120 if ( d->clickInClear ) {
1121 d->possibleTripleClick =
false;
1125 if ( e->button() == Qt::LeftButton && d->possibleTripleClick ) {
1133 if ( e->button() == Qt::MidButton ) {
1134 if ( hasSelectedText() && !isReadOnly() ) {
1135 if ( QApplication::clipboard()->text( QClipboard::Selection ).length() >0 ) {
1141 QLineEdit::mousePressEvent( e );
1146 if ( d->clickInClear ) {
1147 if ( d->clearButton == childAt(e->pos()) || d->clearButton->underMouse() ) {
1149 if ( e->button() == Qt::MidButton ) {
1150 newText = QApplication::clipboard()->text( QClipboard::Selection );
1153 setSelection(0, text().size());
1157 emit textChanged( newText );
1160 d->clickInClear =
false;
1165 QLineEdit::mouseReleaseEvent( e );
1167 if (QApplication::clipboard()->supportsSelection() ) {
1168 if ( e->button() == Qt::LeftButton ) {
1170 copySqueezedText(
false );
1175 void KLineEdit::tripleClickTimeout()
1177 d->possibleTripleClick=
false;
1182 QMenu *popup = QLineEdit::createStandardContextMenu();
1188 enum { UndoAct, RedoAct, Separator1, CutAct, CopyAct, PasteAct, DeleteAct, ClearAct,
1189 Separator2, SelectAllAct, NCountActs };
1190 QAction *separatorAction = 0L;
1192 const int idx = actionList.indexOf( actionList[DeleteAct] ) + 1;
1193 if ( idx < actionList.count() )
1194 separatorAction = actionList.at( idx );
1195 if ( separatorAction )
1198 if ( text().isEmpty() )
1199 clearAllAction->setEnabled(
false );
1200 popup->insertAction( separatorAction, clearAllAction );
1211 QMenu *subMenu = popup->addMenu(
KIcon(
"text-completion"),
i18nc(
"@title:menu",
"Text Completion") );
1212 connect( subMenu, SIGNAL(triggered(
QAction*)),
1213 this, SLOT(completionMenuActivated(
QAction*)) );
1215 popup->addSeparator();
1217 QActionGroup* ag =
new QActionGroup(
this );
1218 d->noCompletionAction = ag->addAction(
i18nc(
"@item:inmenu Text Completion",
"None"));
1219 d->shellCompletionAction = ag->addAction(
i18nc(
"@item:inmenu Text Completion",
"Manual") );
1220 d->autoCompletionAction = ag->addAction(
i18nc(
"@item:inmenu Text Completion",
"Automatic") );
1221 d->popupCompletionAction = ag->addAction(
i18nc(
"@item:inmenu Text Completion",
"Dropdown List") );
1222 d->shortAutoCompletionAction = ag->addAction(
i18nc(
"@item:inmenu Text Completion",
"Short Automatic") );
1223 d->popupAutoCompletionAction = ag->addAction(
i18nc(
"@item:inmenu Text Completion",
"Dropdown List && Automatic"));
1224 subMenu->addActions( ag->actions() );
1228 d->shellCompletionAction->setCheckable(
true );
1229 d->noCompletionAction->setCheckable(
true );
1230 d->popupCompletionAction->setCheckable(
true );
1231 d->autoCompletionAction->setCheckable(
true );
1232 d->shortAutoCompletionAction->setCheckable(
true );
1233 d->popupAutoCompletionAction->setCheckable(
true );
1251 if ( mode != defaultMode && !d->disableCompletionMap[ defaultMode ] )
1253 subMenu->addSeparator();
1254 d->defaultAction = subMenu->addAction(
i18nc(
"@item:inmenu Text Completion",
"Default") );
1263 if ( QLineEdit::contextMenuPolicy() != Qt::DefaultContextMenu )
1272 popup->exec(e->globalPos());
1276 void KLineEdit::completionMenuActivated(
QAction *act)
1280 if( act == d->noCompletionAction )
1284 else if( act == d->shellCompletionAction)
1288 else if( act == d->autoCompletionAction)
1292 else if( act == d->popupCompletionAction)
1296 else if( act == d->shortAutoCompletionAction)
1300 else if( act == d->popupAutoCompletionAction)
1304 else if( act == d->defaultAction )
1315 d->completionBox && d->completionBox->isVisible() )
1316 d->completionBox->hide();
1323 if( d->handleURLDrops )
1326 if ( !urlList.isEmpty() )
1339 KUrl::List::ConstIterator it;
1340 for( it = urlList.begin() ; it != urlList.end() ; ++it )
1342 if(!dropText.isEmpty())
1345 dropText += (*it).prettyUrl();
1349 setCursorPosition(dropText.length());
1355 QLineEdit::dropEvent(e);
1361 if ( ev->type() == QEvent::ShortcutOverride )
1363 QKeyEvent *e =
static_cast<QKeyEvent *
>( ev );
1364 if (d->overrideShortcut(e)) {
1367 }
else if (ev->type() == QEvent::ApplicationPaletteChange
1368 || ev->type() == QEvent::PaletteChange) {
1370 QPalette p = QApplication::palette();
1371 d->previousHighlightedTextColor=p.color(QPalette::Normal,QPalette::HighlightedText);
1372 d->previousHighlightColor=p.color(QPalette::Normal,QPalette::Highlight);
1374 }
else if (ev->type() == QEvent::StyleChange) {
1381 if (!qobject_cast<KLineEditStyle *>(style()) &&
1382 qstrcmp(style()->metaObject()->className(),
"QStyleSheetStyle") != 0 &&
1383 QLatin1String(style()->metaObject()->className()) != d->lastStyleClass) {
1389 kleStyle->m_subStyle = style();
1392 d->lastStyleClass = QLatin1String(style()->metaObject()->className());
1394 d->lastStyleClass.clear();
1396 }
else if (ev->type() == QEvent::ApplicationLayoutDirectionChange
1397 || ev->type() == QEvent::LayoutDirectionChange) {
1398 updateClearButtonIcon(text());
1399 updateClearButton();
1402 return QLineEdit::event( ev );
1408 d->handleURLDrops=enable;
1413 return d->handleURLDrops;
1418 d->grabReturnKeyEvents = grab;
1423 return d->grabReturnKeyEvents;
1433 if ( d->completionBox )
1436 d->completionBox = box;
1439 connect( d->completionBox, SIGNAL(currentTextChanged(
QString)),
1440 SLOT(_k_slotCompletionBoxTextChanged(
QString)) );
1443 connect( d->completionBox, SIGNAL(activated(
QString)),
1445 connect( d->completionBox, SIGNAL(activated(
QString)),
1446 SIGNAL(textEdited(
QString)) );
1460 const bool wasModified = edit->isModified();
1462 edit->setModified(wasModified);
1471 else if (hasSelectedText() )
1473 if (d->userSelection)
1477 d->autoSuggest=
false;
1478 const int start = selectionStart() ;
1479 const QString s = text().remove(selectionStart(), selectedText().length());
1481 setCursorPosition(start);
1482 d->autoSuggest=
true;
1487 bool KLineEditPrivate::overrideShortcut(
const QKeyEvent* e)
1491 const int key = e->key() | e->modifiers();
1552 if (completionBox && completionBox->isVisible ())
1554 const int key = e->key();
1555 const Qt::KeyboardModifiers modifiers = e->modifiers();
1556 if ((key == Qt::Key_Backtab || key == Qt::Key_Tab) &&
1557 (modifiers == Qt::NoModifier || (modifiers & Qt::ShiftModifier)))
1570 if ( d->completionBox && d->completionBox->isVisible() ) {
1578 if ( !items.isEmpty() &&
1579 !(items.count() == 1 && txt == items.first()) )
1584 if ( d->completionBox->isVisible() )
1586 QListWidgetItem* currentItem = d->completionBox->currentItem();
1589 if ( currentItem != 0 ) {
1590 currentSelection = currentItem->text();
1593 d->completionBox->setItems( items );
1596 QListWidgetItem* matchedItem = matchedItems.isEmpty() ? 0 : matchedItems.first();
1599 const bool blocked = d->completionBox->blockSignals(
true );
1600 d->completionBox->setCurrentItem( matchedItem );
1601 d->completionBox->blockSignals( blocked );
1603 d->completionBox->setCurrentRow(-1);
1608 if ( !txt.isEmpty() )
1609 d->completionBox->setCancelledText( txt );
1610 d->completionBox->setItems( items );
1611 d->completionBox->popup();
1614 if ( d->autoSuggest && autoSuggest )
1616 const int index = items.first().indexOf( txt );
1617 const QString newText = items.first().mid( index );
1624 if ( d->completionBox && d->completionBox->isVisible() )
1625 d->completionBox->hide();
1631 if ( create && !d->completionBox ) {
1633 d->completionBox->setObjectName(
"completion box");
1634 d->completionBox->setFont(font());
1637 return d->completionBox;
1644 disconnect( oldComp, SIGNAL(matches(
QStringList)),
1666 if (!d->userSelection && userSelection)
1668 d->_k_updateUserText(text());
1671 QPalette p = palette();
1675 p.setColor(QPalette::Highlight, d->previousHighlightColor);
1676 p.setColor(QPalette::HighlightedText, d->previousHighlightedTextColor);
1680 QColor color=p.color(QPalette::Disabled, QPalette::Text);
1681 p.setColor(QPalette::HighlightedText, color);
1682 color=p.color(QPalette::Active, QPalette::Base);
1683 p.setColor(QPalette::Highlight, color);
1686 d->userSelection=userSelection;
1690 void KLineEdit::slotRestoreSelectionColors()
1692 if (d->disableRestoreSelection)
1703 void KLineEdit::_k_slotCompletionBoxTextChanged(
const QString& text )
1705 if (!text.isEmpty())
1715 if ( d->enableSqueezedText && isReadOnly() )
1716 return d->squeezedText;
1728 return d->autoSuggest;
1733 if (echoMode() == Password && d->threeStars) {
1742 setAttribute(Qt::WA_UpdatesDisabled,
true);
1744 const QString oldText = text();
1745 const bool isModifiedState = isModified();
1746 setText(oldText + oldText + oldText);
1747 QLineEdit::paintEvent(ev);
1749 setModified(isModifiedState);
1750 blockSignals(
false);
1751 setAttribute(Qt::WA_UpdatesDisabled,
false);
1753 QLineEdit::paintEvent( ev );
1756 if (d->enableClickMsg && text().isEmpty()) {
1759 f.setItalic(d->italicizePlaceholder);
1762 QColor color(palette().color(foregroundRole()));
1763 color.setAlphaF(0.5);
1766 QStyleOptionFrame opt;
1767 initStyleOption(&opt);
1768 QRect cr = style()->subElementRect(QStyle::SE_LineEditContents, &opt,
this);
1771 const int verticalMargin(1);
1772 const int horizontalMargin(2);
1774 int left, top, right, bottom;
1775 getTextMargins( &left, &top, &right, &bottom );
1776 cr.adjust( left, top, -right, -bottom );
1780 QFontMetrics fm = fontMetrics();
1781 Qt::Alignment va = alignment() & Qt::AlignVertical_Mask;
1783 switch (va & Qt::AlignVertical_Mask)
1785 case Qt::AlignBottom:
1786 vscroll = cr.y() + cr.height() - fm.height() - verticalMargin;
1790 vscroll = cr.y() + verticalMargin;
1794 vscroll = cr.y() + (cr.height() - fm.height() + 1) / 2;
1799 QRect lineRect(cr.x() + horizontalMargin, vscroll, cr.width() - 2*horizontalMargin, fm.height());
1800 p.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, d->clickMessage);
1807 QLineEdit::focusInEvent( ev );
1812 QLineEdit::focusOutEvent( ev );
1817 d->enableClickMsg = !msg.isEmpty();
1818 d->clickMessage = msg;
1822 #ifndef KDE_NO_DEPRECATED
1825 QLineEdit::setContextMenuPolicy( showMenu ? Qt::DefaultContextMenu : Qt::NoContextMenu );
1829 #ifndef KDE_NO_DEPRECATED
1832 return ( contextMenuPolicy() == Qt::DefaultContextMenu );
1842 if (val ==
"NoEcho")
1843 setEchoMode(NoEcho);
1845 d->threeStars = (val ==
"ThreeStars");
1846 setEchoMode(Password);
1851 setEchoMode( Normal );
1857 return echoMode() == NoEcho || echoMode() == Password;
1865 d->completionRunning =
true;
1869 d->completionRunning =
false;
1872 #include "klineedit.moc"
1873 #include "klineedit_p.moc"
bool contains(const QKeySequence &needle) const
Returns whether at least one of the key sequences is equal to needle.
virtual void mouseDoubleClickEvent(QMouseEvent *)
Re-implemented for internal reasons.
const KShortcut & deleteWordForward()
Delete a word forward from mouse/cursor position.
KAction * cut(const QObject *recvr, const char *slot, QObject *parent)
Cut selected area and store it in the clipboard.
KeyBindingMap getKeyBindings() const
Returns a key-binding map.
static void setEditText(KLineEdit *edit, const QString &text)
void doCompletion(const QString &txt)
Do completion now.
KAction * undo(const QObject *recvr, const char *slot, QObject *parent)
Undo the last operation.
const KShortcut & forwardWord()
ForwardWord.
void aboutToShowContextMenu(QMenu *menu)
Emitted before the context menu is displayed.
virtual void create(WId=0, bool initializeWindow=true, bool destroyOldWindow=true)
Reimplemented for internal reasons, the API is not affected.
A helper widget for "completion-widgets" (KLineEdit, KComboBox))
void setUrl(const KUrl &url)
Sets url into the lineedit.
QMenu * createStandardContextMenu()
Re-implemented for internal reasons.
const KShortcut & redo()
Redo.
KAction * copy(const QObject *recvr, const char *slot, QObject *parent)
Copy the selected area into the clipboard.
const KShortcut & shortcut(StandardShortcut id)
Returns the keybinding for accel.
const KShortcut & undo()
Undo last operation.
virtual void setCompletionObject(KCompletion *, bool hsig=true)
Reimplemented for internal reasons, the API is not affected.
virtual void setCompletedText(const QString &)
See KCompletionBase::setCompletedText.
QString userText() const
Returns the text as given by the user (i.e.
Switch to next completion (by default Ctrl-Down).
void setCompletionBox(KCompletionBox *box)
Set the completion-box to be used in completion mode KGlobalSettings::CompletionPopup.
virtual void focusOutEvent(QFocusEvent *ev)
bool isSqueezedTextEnabled() const
Returns true if text squeezing is enabled.
bool trapReturnKey() const
bool autoSuggest() const
Whether in current state text should be auto-suggested.
KCompletion * compObj() const
Returns a pointer to the completion object.
QString clickMessage() const
KAction * deselect(const QObject *recvr, const char *slot, QObject *parent)
Deselect any selected elements in the current document.
void textRotation(KCompletionBase::KeyBindingType)
Emitted when the text rotation key-bindings are pressed.
Text completion (by default Ctrl-E).
static KGlobalSettings * self()
Return the KGlobalSettings singleton.
virtual void contextMenuEvent(QContextMenuEvent *)
Re-implemented for internal reasons.
KAction * paste(const QObject *recvr, const char *slot, QObject *parent)
Paste the contents of clipboard at the current mouse or cursor position.
KCompletionBox * completionBox(bool create=true)
static void autoHideEventFilter(QObject *, QEvent *)
KCursor has to install an eventFilter over the widget you want to auto-hide.
KGuiItem del()
Returns the 'Delete' gui item.
virtual void clear()
Reimplemented to workaround a buggy QLineEdit::clear() (changing the clipboard to the text we just ha...
QString i18nc(const char *ctxt, const char *text)
virtual void keyPressEvent(QKeyEvent *)
Re-implemented for internal reasons.
KSharedConfigPtr config()
void userCancelled(const QString &cancelText)
Resets the current displayed text.
Represents a keyboard shortcut.
void clearButtonClicked()
Emitted when the user clicked on the clear button.
virtual void mousePressEvent(QMouseEvent *)
Re-implemented for internal reasons.
virtual void setContextMenuEnabled(bool showMenu)
Enables/disables the popup (context) menu.
void setUrlDropsEnabled(bool enable)
Enables/Disables handling of URL drops.
virtual void dropEvent(QDropEvent *)
Re-implemented to handle URI drops.
Substring completion (by default Ctrl-T).
void substringCompletion(const QString &)
Emitted when the shortcut for substring completion is pressed.
Switch to previous completion (by default Ctrl-Up).
void completion(const QString &)
Emitted when the completion key is pressed.
static KUrl::List fromMimeData(const QMimeData *mimeData, KUrl::MetaDataMap *metaData=0)
bool passwordMode() const
const KShortcut & insert()
Toggle insert/overwrite (with visual feedback, e.g.
bool urlDropsEnabled() const
Returns true when decoded URL drops are enabled.
QStringList allMatches()
Returns a list of all items matching the last completed string.
const KShortcut & endOfLine()
Goto end of current line.
virtual void setCompletionMode(KGlobalSettings::Completion mode)
Sets the type of completion to be used.
A generic class for completing QStrings.
Text is automatically filled in whenever possible.
void setUserSelection(bool userSelection)
Sets the widget in userSelection mode or in automatic completion selection mode.
virtual void resizeEvent(QResizeEvent *)
Re-implemented for internal reasons.
const KShortcut & backwardWord()
BackwardWord.
A wrapper around QIcon that provides KDE icon features.
const KShortcut & beginningOfLine()
Goto beginning of current line.
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
Clear the content of the focus widget.
void setSqueezedTextEnabled(bool enable)
Enable text squeezing whenever the supplied text is too long.
KAction * home(const QObject *recvr, const char *slot, QObject *parent)
Go to the "Home" position or document.
virtual ~KLineEdit()
Destructor.
bool handleSignals() const
Returns true if the object handles the signals.
KAction * redo(const QObject *recvr, const char *slot, QObject *parent)
Redo the last operation.
virtual void setCompletionMode(KGlobalSettings::Completion mode)
Re-implemented from KCompletionBase for internal reasons.
void setSqueezedText(const QString &text)
Squeezes text into the line edit.
const KShortcut & copy()
Copy selected area into the clipboard.
void completionBoxActivated(const QString &)
Emitted whenever the completion box is activated.
QString originalText() const
Returns the original text if text squeezing is enabled.
static void setAutoHideCursor(QWidget *w, bool enable, bool customEventFilter=false)
Sets auto-hiding the cursor for widget w.
void setTrapReturnKey(bool trap)
By default, KLineEdit recognizes Key_Return and Key_Enter and emits the returnPressed() signals...
QString nextMatch()
Returns the next item from the matching-items-list.
QSize clearButtonUsedSize() const
virtual void mouseReleaseEvent(QMouseEvent *)
Re-implemented for internal reasons.
const KShortcut & cut()
Cut selected area and store it in the clipboard.
Lists all possible matches in a popup list-box to choose from.
bool isContextMenuEnabled() const
Returns true when the context menu is enabled.
virtual void focusInEvent(QFocusEvent *ev)
Same as automatic except shortest match is used for completion.
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.
const KShortcut & deleteWordBack()
Delete a word back from mouse/cursor position.
KLineEdit(const QString &string, QWidget *parent=0)
Constructs a KLineEdit object with a default text, a parent, and a name.
const KShortcut & pasteSelection()
Paste the selection at mouse/cursor position.
virtual void copy() const
Reimplemented for internal reasons, the API is not affected.
Class to encapsulate user-driven action or event.
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.
bool isClearButtonShown() const
Completion
This enum describes the completion mode used for by the KCompletion class.
virtual void setCompletionObject(KCompletion *compObj, bool hsig=true)
Sets up the completion object to be used.
QPixmap SmallIcon(const QString &name, int force_size, int state, const QStringList &overlays)
virtual bool event(QEvent *)
Re-implemented for internal reasons.
virtual void paintEvent(QPaintEvent *ev)
bool emitSignals() const
Returns true if the object emits the signals.
KAction * selectAll(const QObject *recvr, const char *slot, QObject *parent)
Select all elements in the current document.
KeyBindingType
Constants that represent the items whose short-cut key-binding is programmable.
void setPasswordMode(bool b=true)
set the line edit in password mode.
bool authorize(const QString &genericAction)
void completionModeChanged(KGlobalSettings::Completion)
Emitted when the user changed the completion mode by using the popupmenu.
void setClearButtonShown(bool show)
This makes the line edit display an icon on one side of the line edit which, when clicked...
friend class KLineEditStyle
Complete text much in the same way as a typical *nix shell would.
void setClickMessage(const QString &msg)
This makes the line edit display a grayed-out hinting text as long as the user didn't enter any text...
static GraphicEffects graphicEffectsLevel()
This function determines the desired level of effects on the GUI.
static Completion completionMode()
Returns the preferred completion mode setting.
static void assignIconsToContextMenu(ContextMenus type, QList< QAction * > actions)
Assigns standard icons to the various standard text edit context menus.
virtual void makeCompletion(const QString &)
Completes the remaining text with a matching one from a given list.
void returnPressed(const QString &)
Emitted when the user presses the return key.
virtual void setReadOnly(bool)
Sets the lineedit to read-only.
virtual void setText(const QString &)
Re-implemented to enable text squeezing.
void setCompletedItems(const QStringList &items, bool autoSuggest=true)
Same as the above function except it allows you to temporarily turn off text completion in Completion...
const KShortcut & end()
Goto end of the document.
T readEntry(const QString &key, const T &aDefault) const
GUI with simple animations enabled.
QString prettyUrl(AdjustPathOption trailing=LeaveTrailingSlash) const
KGlobalSettings::Completion completionMode() const
Returns the current completion mode.
QString previousMatch()
Returns the next item from the matching-items-list.
void rotateText(KCompletionBase::KeyBindingType type)
Iterates through all possible matches of the completed text or the history list.
Lists all possible matches in a popup list-box to choose from, and automatically fill the result when...
void setCompletionModeDisabled(KGlobalSettings::Completion mode, bool disable=true)
Disables completion modes by makeing them non-checkable.
const KShortcut & paste()
Paste contents of clipboard at mouse/cursor position.