lfpport_qte_textfield.cpp

来自「This is a resource based on j2me embedde」· C++ 代码 · 共 664 行 · 第 1/2 页

CPP
664
字号
            break;        default:            break;    }}/** * Override QMultLineEdit::del to format phone string */void TextFieldBody::del() {    QString str;    int line, col;    QMultiLineEdit::del();    QMultiLineEdit::getCursorPosition(&line, &col);    switch ((MidpConstraint)(constraints & MIDP_CONSTRAINT_MASK)) {        case MIDP_CONSTRAINT_PHONENUMBER:            str = QMultiLineEdit::textLine(line);            QMultiLineEdit::removeLine(line);            str = getStringForPhoneNumber(str);            QMultiLineEdit::insertAt(str,line,0,FALSE);            break;        default:            break;    }}/** Override to notify Form focus change */void TextFieldBody::focusInEvent(QFocusEvent *event) {    /* Notify Java if this is caused by user action */    if (event->reason() != QFocusEvent::Other) {	MidpFormFocusChanged(parent());    }    /* Continue with focus activation */    QMultiLineEdit::focusInEvent(event);}/** Listening for text changes to notify Java Peer */void TextFieldBody::notifyStateChanged() {    MidpFormItemPeerStateChanged(parent(), 0);}/** Wrapper function to avoid triggering Java notification event */void TextFieldBody::setTextQuietly(const QString &s) {    bool wasBlocked = signalsBlocked();    if (!wasBlocked) {	/* Prevent setText() call from emitting textChanged() signal */	blockSignals(true);    }    setText(s);    /* Restore blocking state */    if (!wasBlocked) {	blockSignals(false);    }}/** * Construct a TextField native peer with label and body. */TextField::TextField(QWidget *parent, const QString &label, int layout,		     const QString &text, int maxSize,		     int constraints, const QString &inputMode)    : Item(parent, label, layout){    /* Suppress unused-parameter warning */    (void)inputMode;    qedit = new TextFieldBody(this);    qedit->setMaxLength(maxSize);    setConstraints(constraints);        /* The text will be validated by constraints above */    setString(text);     setFocusPolicy(QWidget::StrongFocus);    /* Delegate focus to MultiLineEdit */    setFocusProxy(qedit);}/** * Construct a TextField native peer with label and body. */TextField::~TextField() {    delete qedit;}/** Implement virtual function (defined in lfpport_qte_item.h) */void TextField::bodyResize(int w, int h) {    // keep the old cursor position    int oldPos = qedit->getCursorPosition();    qedit->resize(w, h);    qedit->setWordWrap(QMultiLineEdit::WidgetWidth);    // try to set the cursor at the old position    qedit->setCursorPosition(oldPos);}void TextField::bodyRelocate(int x, int y) {  qedit->move(x, y);}/** * Get preferred height with the given width. * If input constraint is NOT ANY or URL, we prefer single line. * Otherwise, if the maximum size is less than a single * line can fit, we still prefer single line. Else, * we prefer to have 6 visible lines. */int TextField::bodyHeightForWidth(int *takenWidth, int w) {    int visibleLines;    /* Deduct frame width on both ends */    int border = qedit->frameWidth()*2;    /* Maximum number of chars on a single line, plus a cursor */    int maxWidth = (qedit->maxLength()+1)*qedit->fontMetrics().width('W');    if (border + maxWidth <= w) {        visibleLines = 1;	*takenWidth  = border + maxWidth;    } else {	/* Decide preference basing on constraint */	switch ((MidpConstraint)(qedit->constraints & MIDP_CONSTRAINT_MASK)) {	case MIDP_CONSTRAINT_ANY:	    visibleLines = LONG_ANY_TEXTFIELD_LINES;	    break;	case MIDP_CONSTRAINT_URL:	    visibleLines = LONG_URL_TEXTFIELD_LINES;	    break;	default:	    visibleLines = 1;	}	/* Need the full width */	*takenWidth = w;    }    return (border + qedit->fontMetrics().lineSpacing()*visibleLines);}int TextField::bodyWidthForHeight(int *takenHeight, int h) {    /* Suppress unused-parameter warning */    (void)h;    *takenHeight = 0;    return 0;}bool TextField::bodyCanBeOnSameLine(int bodyHeight) {  bool res = Item::bodyCanBeOnSameLine(bodyHeight);  return res && (qedit->numLines() == 1);}/** * Set the text content of current TextField. */MidpErrorTextField::setString(const QString &text) {    qedit->setTextQuietly(text);    qedit->setCursorPosition(qedit->isReadOnly() ? 0 : qedit->length());    return KNI_OK;}/** * Get the text content of current TextField. */MidpErrorTextField::getString(QString &text) {    text = qedit->text();    return KNI_OK;}/** * Set the maximum text length of current TextField. */MidpErrorTextField::setMaxSize(int maxSize) {    qedit->setMaxLength(maxSize);    /*     * Workaround Qt feature in QMultiLineEdit::setMaxLength()     * Truncate existing text if it is larger than the new maxSize.     */    if (maxSize < qedit->length()) {	QString text = qedit->text();	text.truncate(maxSize);	qedit->setTextQuietly(text);    }    return KNI_OK;}/** * Get the insertion point of current TextField. */MidpErrorTextField::getCaretPosition(int &position) {    position = qedit->getCursorPosition();    return KNI_OK;}/** * Set input constraint and modifiers as defined in MIDP Spec. * Handle modifiers here. Constraint will be handled in insertAt(). */MidpErrorTextField::setConstraints(int constraints) {    /* Uneditable Modifier */    if (constraints & MIDP_MODIFIER_UNEDITABLE) {	qedit->setReadOnly(true);    } else {	qedit->setReadOnly(false);    }    /* Password Modifier */    if (constraints & MIDP_MODIFIER_PASSWORD) {	qedit->setEchoMode(QMultiLineEdit::Password);    } else {	qedit->setEchoMode(QMultiLineEdit::Normal);    }    /* Check the rest constraints in TextFieldBody::validate() */    qedit->constraints = constraints;    return KNI_OK;}/** * Create a TextField Qt peer without showing it yet. * Upon successful return, fields in *itemPtr should be set properly. */extern "C" MidpErrorlfpport_textfield_create(MidpItem* itemPtr, MidpDisplayable* formPtr,			 const pcsl_string* label, int layout,			 const pcsl_string* text, int maxSize,			 int constraints, const pcsl_string* inputMode) {    QString qlabel, qtext, qinputMode;    pcsl_string2QString(*label, qlabel);    pcsl_string2QString(*text, qtext);    pcsl_string2QString(*inputMode, qinputMode);    /* Fill in MidpItem structure */    itemPtr->widgetPtr =       new TextField((formPtr == INVALID_NATIVE_ID                     ? 0 : (QWidget *)formPtr->frame.widgetPtr),		    qlabel,		    layout,		    qtext,		    maxSize,		    constraints,		    qinputMode);    initItemPtr(itemPtr, formPtr);    return KNI_OK;}/** * Notifies native peer of a content change in the corresponding TextField. * @param text - the new string set */extern "C" MidpErrorlfpport_textfield_set_string(MidpItem* itemPtr, const pcsl_string* text){    QString qtext;    pcsl_string2QString(*text, qtext);        return ((TextField *)itemPtr->widgetPtr)->setString(qtext);}/** * Query native peer for current text content. * @param text - pointer to the returned content text * @param newChange - pointer to the returned flag for whether new changes *			has been made since last call of this function */extern "C" MidpErrorlfpport_textfield_get_string(pcsl_string* text, jboolean* newChange,			     MidpItem* itemPtr) {    QString qtext;    MidpError err = ((TextField *)itemPtr->widgetPtr)->getString(qtext);    if (err == KNI_OK) {	err = QString2pcsl_string(qtext, *text);	*newChange = KNI_TRUE;    }    return err;}/** * Notifies native peer of a maximum size change in the corresponding TextField. * @param maxSize - the new maximum size */extern "C" MidpErrorlfpport_textfield_set_max_size(MidpItem* itemPtr, int maxSize) {    return ((TextField *)itemPtr->widgetPtr)->setMaxSize(maxSize);}/** * Gets the current input position. * @return the current caret position, <code>0</code> if at the beginning */extern "C" MidpErrorlfpport_textfield_get_caret_position(int *position, MidpItem* itemPtr) {    return ((TextField *)itemPtr->widgetPtr)->getCaretPosition(*position);}/** * Notifies native peer that constraints have to be changed. * @param constraints - the new input constraints */extern "C" MidpErrorlfpport_textfield_set_constraints(MidpItem* itemPtr, int constraints) {    return ((TextField *)itemPtr->widgetPtr)->setConstraints(constraints);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?