lfpport_qte_textfield.cpp
来自「This is a resource based on j2me embedde」· C++ 代码 · 共 664 行 · 第 1/2 页
CPP
664 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * * This source file is specific for Qt-based configurations. */#include <lfpport_form.h>#include "lfpport_qte_textfield.h"#include <moc_lfpport_qte_textfield.cpp>#include "lfpport_qte_util.h"#include "lfpport_qte_mscreen.h"#include <qregexp.h>#include <stdio.h>/** The number of visible lines of a long ANY TextField */#define LONG_ANY_TEXTFIELD_LINES 6/** The number of visible lines of a long URL TextField */#define LONG_URL_TEXTFIELD_LINES 2/** Constructor */TextFieldBody::TextFieldBody(QWidget *parent) : QMultiLineEdit(parent) { connect(this, SIGNAL(textChanged()), SLOT(notifyStateChanged()));}/** Set cursor position in one dimension manner. */void TextFieldBody::setCursorPosition(int position) { if (position < 0) { home(FALSE); } else if (position > length()) { end(FALSE); } else { /* Iterate each line's length */ for (int line = 0; line < numLines(); line++) { int numChars = lineLength(line); if (position <= numChars) { QMultiLineEdit::setCursorPosition(line, position); return; } position -= numChars + 1; /* EOL is counted as one char */ } } /* Should not reach here */}/** * Override QMultiLineEdit to notify Java peer of traversal out. * * @param keyEvent key event to handle */void TextFieldBody::keyPressEvent(QKeyEvent *key){ int k = key->key(); // always handle select event because it switches // between the modal and non-modal modes#ifdef QT_KEYPAD_MODE if (k == Qt::Key_Select) { QMultiLineEdit::keyPressEvent(key); } else if (isModalEditing()) {#endif if (isReadOnly()) { if ((k == Qt::Key_Up && rowIsVisible(0)) || (k == Qt::Key_Down && rowIsVisible(numRows() - 1))) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyPressEvent(key); } else { QMultiLineEdit::keyPressEvent(key); } } else { int line; int col; QMultiLineEdit::getCursorPosition(&line, &col); if ((k == Qt::Key_Up && line == 0) || (k == Qt::Key_Down && (line == numLines() - 1))){ PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyPressEvent(key); } else { QMultiLineEdit::keyPressEvent(key); } }#ifdef QT_KEYPAD_MODE } else { // not handle events while it's in not modal state key->ignore(); }#endif}/** * Override QMultiLineEdit to notify Java peer of traversal out. * * @param keyEvent key event to handle */void TextFieldBody::keyReleaseEvent(QKeyEvent *key){ int k = key->key(); // always handle select event because it switches // between the modal and non-modal modes#ifdef QT_KEYPAD_MODE if (k == Qt::Key_Select) { QMultiLineEdit::keyReleaseEvent(key); } else if (isModalEditing()) {#endif int line; int col; QMultiLineEdit::getCursorPosition(&line, &col); if (k == Qt::Key_Up && line == 0) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyReleaseEvent(key); } else if (k == Qt::Key_Down && line == numLines() - 1) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyReleaseEvent(key); } else { QMultiLineEdit::keyReleaseEvent(key); }#ifdef QT_KEYPAD_MODE } else { // not handle events while it's in not modal state key->ignore(); }#endif}/** Get cursor position in one dimension manner. */int TextFieldBody::getCursorPosition() { int position; if (atBeginning()) { position = 0; } else if (atEnd()) { position = length(); } else { int line, col; QMultiLineEdit::getCursorPosition(&line, &col); /* Iterate each line's length */ for (position = col, line--; line >= 0; line--) { position += lineLength(line) + 1; /* EOL is counted as one char */ } } return position;}/** Validate a string against current constraint */bool TextFieldBody::validate(const QString &s, int line, int col) { bool ok = false; switch ((MidpConstraint)(constraints & MIDP_CONSTRAINT_MASK)) { case MIDP_CONSTRAINT_NUMERIC: { QRegExp numbers("^-?[0-9]*$"); if (numbers.match(s) >= 0) { if (length() == 0) { ok = true; } else if (s[0] == '-') { /* only allow one '-' & at beginning */ if (text()[0] != '-' && line == 0 && col == 0) { ok = true; } } else { /* number(s) being entered */ if (text()[0] != '-') { /* positive integer */ ok = true; } else { /* negative integer-allow digits only after '-' */ if (col > 0 || line > 0) { ok = true; } } } } } break; case MIDP_CONSTRAINT_DECIMAL: { QRegExp decimals("^-?[0-9]*[.]?[0-9]*$"); if (decimals.match(s) >= 0) { if (length() == 0) { ok = true; } else { if (s[0] == '-') { /* only allow one '-' & at beginning */ if (text()[0] != '-' && line == 0 && col == 0) { ok = true; } } else { /* number(s) being entered */ if (text()[0] != '-') { /* positive decimal */ ok = true; } else { /* negative decimal-allow digits only after '-' */ if (col > 0 || line > 0) { ok = true; } } } /* Only allow one dot(.) */ if (ok) { if (s.find('.') >= 0 && text().find('.') >= 0) { ok = false; } } } } } break; case MIDP_CONSTRAINT_PHONENUMBER: { /* the phone number has to accept the '+' at the start of the text, any digits, '#' and '*' */ QRegExp numbers("^[\053]?[0-9\\s\052\043]*$"); if (numbers.match(s) >= 0) { ok = true; } } break; case MIDP_CONSTRAINT_ANY: case MIDP_CONSTRAINT_EMAILADDR: case MIDP_CONSTRAINT_URL: ok = true; break; } /* end of switch(constraints) */ return ok;}/** * Method format phone string * @param s string */QString TextFieldBody::getStringForPhoneNumber(QString& s) { int res = s.find(" ",0); while (res != -1) { s.remove(res,1); res = s.find(" ",0); } switch (s.length()) { case 5: case 6: case 7: s.insert(3," "); break; case 8: case 9: case 10: case 12: case 13: s.insert(3," "); s.insert(6," "); break; case 11: s.insert(1," "); s.insert(5," "); s.insert(9," "); break; } return s;}/** Override to validate constraint against new insertion */void TextFieldBody::insertAt(const QString &s, int line, int col, bool mark) { QString str; if (validate(s, line, col)) { switch ((MidpConstraint)(constraints & MIDP_CONSTRAINT_MASK)) { case MIDP_CONSTRAINT_PHONENUMBER: str = QMultiLineEdit::textLine(line); str.insert(col,s); QMultiLineEdit::removeLine(line); str = getStringForPhoneNumber(str); QMultiLineEdit::insertAt(str,line,0,mark); QMultiLineEdit::setCursorPosition(line,col + 2,mark); break; default: QMultiLineEdit::insertAt(s, line, col, mark); } }}/** * Override QMultLineEdit::backspace to format phone string */void TextFieldBody::backspace() { QString str; int line, col; QMultiLineEdit::backspace(); 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);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?