📄 qlineedit.cpp
字号:
} if (unknown) event->ignore(); else event->accept();}/*! This function is not intended as polymorphic usage. Just a shared code fragment that calls QInputContext::mouseHandler for this class.*/bool QLineEditPrivate::sendMouseEventToInputContext( QMouseEvent *e ){#if !defined QT_NO_IM Q_Q(QLineEdit); if ( composeMode() ) { int tmp_cursor = xToPos(e->pos().x()); int mousePos = tmp_cursor - cursor; if ( mousePos < 0 || mousePos > textLayout.preeditAreaText().length() ) { mousePos = -1; // don't send move events outside the preedit area if ( e->type() == QEvent::MouseMove ) return true; } QInputContext *qic = q->inputContext(); if ( qic ) // may be causing reset() in some input methods qic->mouseHandler(mousePos, e); if (!textLayout.preeditAreaText().isEmpty()) return true; }#else Q_UNUSED(e);#endif return false;}/*! \reimp */void QLineEdit::inputMethodEvent(QInputMethodEvent *e){ Q_D(QLineEdit); if (d->readOnly) { e->ignore(); return; }#ifdef Q_WS_QWS if(echoMode() == PasswordEchoOnEdit) { setEchoMode(Normal); clear(); d->resumePassword = true; }#endif#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) setEditFocus(true);#endif int priorState = d->undoState; d->removeSelectedText(); int c = d->cursor; // cursor position after insertion of commit string if (e->replacementStart() <= 0) c += e->commitString().length() + qMin(-e->replacementStart(), e->replacementLength()); d->cursor += e->replacementStart(); // insert commit string if (e->replacementLength()) { d->selstart = d->cursor; d->selend = d->selstart + e->replacementLength(); d->removeSelectedText(); } if (!e->commitString().isEmpty()) d->insert(e->commitString()); d->cursor = c; d->textLayout.setPreeditArea(d->cursor, e->preeditString()); d->preeditCursor = e->preeditString().length(); d->hideCursor = false; QList<QTextLayout::FormatRange> formats; for (int i = 0; i < e->attributes().size(); ++i) { const QInputMethodEvent::Attribute &a = e->attributes().at(i); if (a.type == QInputMethodEvent::Cursor) { d->preeditCursor = a.start; d->hideCursor = !a.length; } else if (a.type == QInputMethodEvent::TextFormat) { QTextCharFormat f = qvariant_cast<QTextFormat>(a.value).toCharFormat(); if (f.isValid()) { QTextLayout::FormatRange o; o.start = a.start + d->cursor; o.length = a.length; o.format = f; formats.append(o); } } } d->textLayout.setAdditionalFormats(formats); d->updateTextLayout(); update(); if (!e->commitString().isEmpty()) d->emitCursorPositionChanged(); d->finishChange(priorState);}/*!\reimp*/QVariant QLineEdit::inputMethodQuery(Qt::InputMethodQuery property) const{ Q_D(const QLineEdit); switch(property) { case Qt::ImMicroFocus: return d->cursorRect(); case Qt::ImFont: return font(); case Qt::ImCursorPosition: return QVariant(d->cursor); case Qt::ImSurroundingText: return QVariant(d->text); case Qt::ImCurrentSelection: return QVariant(selectedText()); default: return QVariant(); }}/*!\reimp*/void QLineEdit::focusInEvent(QFocusEvent *e){ Q_D(QLineEdit); if (e->reason() == Qt::TabFocusReason || e->reason() == Qt::BacktabFocusReason || e->reason() == Qt::ShortcutFocusReason) if (d->maskData) d->moveCursor(d->nextMaskBlank(0)); else if (!d->hasSelectedText()) selectAll(); if (!d->cursorTimer) { int cft = QApplication::cursorFlashTime(); d->cursorTimer = cft ? startTimer(cft/2) : -1; } QStyleOptionFrame opt = d->getStyleOption(); if((!hasSelectedText() && d->textLayout.preeditAreaText().isEmpty()) || style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this)) d->setCursorVisible(true);#ifdef Q_WS_MAC if (d->echoMode == Password || d->echoMode == NoEcho) qt_mac_secure_keyboard(true);#endif#ifdef QT_KEYPAD_NAVIGATION d->origText = d->text;#endif update();}/*!\reimp*/void QLineEdit::focusOutEvent(QFocusEvent *e){ Q_D(QLineEdit);#ifdef Q_WS_QWS if(d->resumePassword){ setEchoMode(PasswordEchoOnEdit); d->resumePassword = false; }#endif Qt::FocusReason reason = e->reason(); if (reason != Qt::ActiveWindowFocusReason && reason != Qt::PopupFocusReason) deselect(); d->setCursorVisible(false); if (d->cursorTimer > 0) killTimer(d->cursorTimer); d->cursorTimer = 0; if (reason != Qt::PopupFocusReason && !(QApplication::activePopupWidget() && QApplication::activePopupWidget()->parentWidget() == this)) { if (!d->emitingEditingFinished) emit editingFinished();#ifdef QT3_SUPPORT emit lostFocus();#endif }#ifdef Q_WS_MAC if (d->echoMode == Password || d->echoMode == NoEcho) qt_mac_secure_keyboard(false);#endif#ifdef QT_KEYPAD_NAVIGATION d->origText = QString();#endif update();}/*!\reimp*/void QLineEdit::paintEvent(QPaintEvent *){ Q_D(QLineEdit); QPainter p(this); QRect r = rect(); const QPalette &pal = palette(); QStyleOptionFrame panel = d->getStyleOption(); style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this); if (d->frame) { int frameWidth = panel.lineWidth; r.adjust(frameWidth, frameWidth, -frameWidth, -frameWidth); p.setClipRect(r); } QFontMetrics fm = fontMetrics(); QRect lineRect(r.x() + horizontalMargin, r.y() + (r.height() - fm.height() + 1) / 2, r.width() - 2*horizontalMargin, fm.height()); QTextLine line = d->textLayout.lineAt(0); int cursor = d->cursor; if (d->preeditCursor != -1) cursor += d->preeditCursor; // locate cursor position int cix = qRound(line.cursorToX(cursor)); // horizontal scrolling int minLB = qMax(0, -fm.minLeftBearing()); int minRB = qMax(0, -fm.minRightBearing()); int widthUsed = qRound(line.naturalTextWidth()) + 1 + minRB; if ((minLB + widthUsed) <= lineRect.width()) { Qt::Alignment va = QStyle::visualAlignment(layoutDirection(), QFlag(d->alignment)); va &= ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask); switch (va) { case Qt::AlignRight: d->hscroll = widthUsed - lineRect.width() + 1; break; case Qt::AlignHCenter: d->hscroll = (widthUsed - lineRect.width()) / 2; break; default: // Left d->hscroll = 0; break; } d->hscroll -= minLB; } else if (cix - d->hscroll >= lineRect.width()) { d->hscroll = cix - lineRect.width() + 1; } else if (cix - d->hscroll < 0) { d->hscroll = cix; } else if (widthUsed - d->hscroll < lineRect.width()) { d->hscroll = widthUsed - lineRect.width() + 1; } // the y offset is there to keep the baseline constant in case we have script changes in the text. QPoint topLeft = lineRect.topLeft() - QPoint(d->hscroll, d->ascent-fm.ascent()); // draw text, selections and cursors p.setPen(pal.text().color()); QVector<QTextLayout::FormatRange> selections; if (d->selstart < d->selend || (d->cursorVisible && d->maskData)) { QTextLayout::FormatRange o; const QPalette &pal = palette(); if (d->selstart < d->selend) { o.start = d->selstart; o.length = d->selend - d->selstart; o.format.setBackground(pal.brush(QPalette::Highlight)); o.format.setForeground(pal.brush(QPalette::HighlightedText)); } else { // mask selection o.start = d->cursor; o.length = 1; o.format.setBackground(pal.brush(QPalette::Text)); o.format.setForeground(pal.brush(QPalette::Background)); } selections.append(o); } // Asian users see an IM selection text as cursor on candidate // selection phase of input method, so the ordinary cursor should be // invisible if we have a preedit string. d->textLayout.draw(&p, topLeft, selections, r); if (d->cursorVisible && !d->readOnly && !d->hideCursor) d->textLayout.drawCursor(&p, topLeft, cursor);}#ifndef QT_NO_DRAGANDDROP/*!\reimp*/void QLineEdit::dragMoveEvent(QDragMoveEvent *e){ Q_D(QLineEdit); if (!d->readOnly && e->mimeData()->hasFormat("text/plain")) { e->acceptProposedAction(); d->cursor = d->xToPos(e->pos().x()); d->cursorVisible = true; update(); d->emitCursorPositionChanged(); }}/*!\reimp */void QLineEdit::dragEnterEvent(QDragEnterEvent * e){ QLineEdit::dragMoveEvent(e);}/*!\reimp */void QLineEdit::dragLeaveEvent(QDragLeaveEvent *){ Q_D(QLineEdit); if (d->cursorVisible) { d->cursorVisible = false; update(); }}/*!\reimp */void QLineEdit::dropEvent(QDropEvent* e){ Q_D(QLineEdit); QString str = e->mimeData()->text(); if (!str.isNull() && !d->readOnly) { if (e->source() == this && e->dropAction() == Qt::CopyAction) deselect(); d->cursor =d->xToPos(e->pos().x()); int selStart = d->cursor; int oldSelStart = d->selstart; int oldSelEnd = d->selend; d->cursorVisible = false; e->acceptProposedAction(); insert(str); if (e->source() == this) { if (e->dropAction() == Qt::MoveAction) { if (selStart > oldSelStart && selStart <= oldSelEnd) setSelection(oldSelStart, str.length()); else if (selStart > oldSelEnd) setSelection(selStart - str.length(), str.length()); else setSelection(selStart, str.length()); } else { setSelection(selStart, str.length()); } } } else { e->ignore(); update(); }}void QLineEditPrivate::drag(){ Q_Q(QLineEdit); dndTimer.stop(); QMimeData *data = new QMimeData; data->setText(q->selectedText()); QDrag *drag = new QDrag(q); drag->setMimeData(data); Qt::DropAction action = drag->start(); if (action == Qt::MoveAction && !readOnly && drag->target() != q) { int priorState = undoState; removeSelectedText(); finishChange(priorState); }}#endif // QT_NO_DRAGANDDROP#ifndef QT_NO_MENU/*! Shows the standard context menu created with createStandardContextMenu(). If you do not want the line edit to have a context menu, you can set its \l contextMenuPolicy to Qt::NoContextMenu. If you want to customize the context menu, reimplement this function. If you want to extend the standard context menu, reimplement this function, call createStandardContextMenu() and extend the menu returned. \code void LineEdit::contextMenuEvent(QContextMenuEvent *event) { QMenu *menu = createStandardContextMenu(); menu->addAction(tr("My Menu Item")); //... menu->exec(event->globalPos()); delete menu; } \endcode The \a event parameter is used to obtain the position where the mouse cursor was when the event was generated. \sa setContextMenuPolicy()*/void QLineEdit::contextMenuEvent(QContextMenuEvent *event){ QPointer<QMenu> menu = createStandardContextMenu(); menu->exec(event->globalPos()); delete menu;}/*! This function creates the standard context menu which is shown when the user clicks on the line edit with the right mouse button. It is called from the default contextMenuEvent() handler. The popup menu's ownership is transferred to the caller.*/QMenu *QLineEdit::createStandardContextMenu(){ Q_D(QLineEdit); d->actions[QLineEditPrivate::UndoAct]->setEnabled(d->isUndoAvailable()); d->actions[QLineEditPrivate::RedoAct]->setEnabled(d->isRedoAvailable());#ifndef QT_NO_CLIPBOARD d->actions[QLineEditPrivate::CutAct]->setEnabled(!d->readOnly && d->hasSelectedText()); d->actions[QLineEditPrivate::CopyAct]->setEnabled(d->hasSelectedText()); d->actions[QLineEditPrivate::PasteAct]->setEnabled(!d->readOnly && !QApplication::clipboard()->text().isEmpty());#else d->actions[QLineEditPrivate::CutAct]->setEnabled(false); d->actions[QLineEditPrivate::CopyAct]->setEnabled(false); d->actions[QLineEditPrivate::PasteAct]->setEnabled(false);#endif d->actions[QLineEditPrivate::ClearAct]->setEnabled(!d->readOnly && !d->text.isEmpty() && d->hasSelectedText()); d->actions[QLineEditPrivate::SelectAllAct]->setEnabled(!d->text.isEmpty() && !d->allSelected()); QMenu *popup = new QMenu(this); popup->setObjectName(QLatin1String("qt_edit_menu")); popup->addAction(d->actions[QLineEditPrivate::UndoAct]); popup->addAction(d->actions[QLineEditPrivate::RedoAct]); popup->addSeparator(); popup->addAction(d->actions[QLineEditPrivate::CutAct]); popup->addAction(d->actions[QLineEditPrivate::CopyAct]); popup->addAction(d->actions[QLineEditPrivate::PasteAct]); popup->addAction(d->actions[QLineEditPrivate::ClearAct]); popup->addSeparator(); popup->addAction(d->actions[QLineEditPrivate::SelectAllAct]);#if !defined(QT_NO_IM) QInputContext *qic = inputContext(); if (qic) { QList<QAction *> imActions = qic->actions(); for (int i = 0; i < imActions.size(); ++i) popup->addAction(imActions.at(i)); }#endif if (!d->readOnly) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -