void tst_Q3RichText::cursorPosition() { Q3TextEdit textedit; textedit.setText( "This is a test" ); textedit.setCursorPosition( textedit.paragraphs() - 1, textedit.paragraphLength( textedit.paragraphs() - 1 ) ); int para; int index; textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 14 ); textedit.setText( "This is a test\nThis is a test\nThis is a test" ); textedit.setCursorPosition( textedit.paragraphs() - 1, textedit.paragraphLength( textedit.paragraphs() - 1 ) ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 14 ); }
void QUimTextUtil::Q3TextEditPositionBackward( int *cursor_para, int *cursor_index ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); int preedit_len, preedit_cursor_pos; int para, index; int current_para, current_index; current_para = *cursor_para; current_index = *cursor_index; if ( ! mPreeditSaved ) { preedit_len = mIc->getPreeditString().length(); preedit_cursor_pos = mIc->getPreeditCursorPosition(); } else { preedit_len = 0; preedit_cursor_pos = 0; } edit->getCursorPosition( ¶, &index ); if ( current_para == para && current_index > ( index - preedit_cursor_pos ) && ( current_index <= ( index - preedit_cursor_pos + preedit_len ) ) ) current_index = index - preedit_cursor_pos; if ( current_index > 0 ) current_index--; else { if ( current_para > 0 ) { current_para--; current_index = edit->paragraphLength( current_para ); } } *cursor_para = current_para; *cursor_index = current_index; }
void tst_Q3RichText::keyPressEvent() { // Still needs to test Key_Prior and Key_Next int para, index; Q3TextEdit textedit; textedit.show(); textedit.setText( "This is a test" ); qWarning( "Consider using QtTestCase::keyEvent() for sending key events" ); QKeyEvent ke( QEvent::KeyPress, Qt::Key_Right, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 1 ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Right, 0, Qt::ControlModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 5 ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Left, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 4 ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Left, 0, Qt::ControlModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 0 ); // Test that the text is removed when Enter/Return is pressed first textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("\n") ); textedit.setText( "This is a test" ); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("\n") ); // Now test if the line-break is added in rich text mode textedit.setTextFormat( Qt::RichText ); textedit.setText( "This is a test" ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, 0, Qt::ControlModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); QVERIFY( textedit.text().contains( "<br />" ) ); textedit.setText( "This is a test" ); textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::ControlModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); QVERIFY( textedit.text().contains( "<br />" ) ); textedit.setText( "This is a test" ); textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Return, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QString es = QString::fromLatin1("<p dir=\"ltr\">"); QVERIFY( textedit.text().count( es ) == 2 ); textedit.setTextFormat( Qt::AutoText ); textedit.setText( "This is a test" ); QApplication::clipboard()->setText(""); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("") ); #if defined (Q_WS_WIN) textedit.setText( "This is a test" ); QApplication::clipboard()->setText(""); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::ShiftModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("") ); QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") ); #endif textedit.setText( "This is a test" ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Delete, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("his is a test") ); bool nativeClipboardWorking = true; #if defined (Q_WS_MAC) PasteboardRef pasteboard; OSStatus status = PasteboardCreate(0, &pasteboard); if (status == noErr) CFRelease(pasteboard); nativeClipboardWorking = status == noErr; #endif if (nativeClipboardWorking) { textedit.setText( "This is a test" ); QApplication::clipboard()->setText(" and this is another test"); textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Insert, 0, Qt::ShiftModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("This is a test and this is another test") ); } #if defined (Q_WS_WIN) textedit.setText( "This is a test" ); QApplication::clipboard()->setText(""); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Insert, 0, Qt::ControlModifier ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") ); #endif textedit.setText( "This is a test" ); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Backspace, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("") ); textedit.setText( "This is a test" ); textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE ); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_Backspace, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString( "This is a tes" ) ); if (nativeClipboardWorking) { textedit.setText( "This is a test" ); QApplication::clipboard()->setText(""); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F16, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") ); textedit.setText( "This is a test" ); textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE ); QApplication::clipboard()->setText(" and this is another test"); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F18, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("This is a test and this is another test") ); textedit.setText( "This is a test" ); QApplication::clipboard()->setText(""); textedit.selectAll(); ke = QKeyEvent( QEvent::KeyPress, Qt::Key_F20, 0, Qt::NoButton ); QApplication::sendEvent( textedit.viewport(), &ke ); QCOMPARE( textedit.text(), QString("") ); QCOMPARE( QApplication::clipboard()->text(), QString("This is a test") ); } }
void tst_Q3RichText::moveCursor() { // Still needs to test for MovePageUp and MovePageDown int para, index; Q3TextEdit textedit; textedit.show(); textedit.setText( "This is a test" ); textedit.moveCursor( Q3TextEdit::MoveEnd, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 14 ); textedit.moveCursor( Q3TextEdit::MoveBackward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 13 ); textedit.moveCursor( Q3TextEdit::MoveWordBackward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 10 ); textedit.moveCursor( Q3TextEdit::MoveHome, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 0 ); textedit.moveCursor( Q3TextEdit::MoveForward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 1 ); textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 5 ); textedit.moveCursor( Q3TextEdit::MoveLineStart, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 0 ); textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 14 ); textedit.setText( "This is a test\nThis is a test\nThis is a test" ); textedit.moveCursor( Q3TextEdit::MoveEnd, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 14 ); textedit.moveCursor( Q3TextEdit::MoveBackward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 13 ); textedit.moveCursor( Q3TextEdit::MoveWordBackward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 10 ); textedit.moveCursor( Q3TextEdit::MoveHome, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 0 ); textedit.moveCursor( Q3TextEdit::MoveForward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 1 ); textedit.moveCursor( Q3TextEdit::MoveWordForward, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 0 ); QCOMPARE( index, 5 ); textedit.moveCursor( Q3TextEdit::MoveDown, FALSE ); textedit.moveCursor( Q3TextEdit::MoveDown, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 5 ); textedit.moveCursor( Q3TextEdit::MoveLineStart, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 0 ); textedit.moveCursor( Q3TextEdit::MoveLineEnd, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 2 ); QCOMPARE( index, 14 ); textedit.moveCursor( Q3TextEdit::MoveUp, FALSE ); textedit.getCursorPosition( ¶, &index ); QCOMPARE( para, 1 ); QCOMPARE( index, 14 ); }
int QUimTextUtil::acquireSelectionTextInQ3TextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len, char **former, char **latter ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); QString text; int len, offset, newline; int start_para, start_index, end_para, end_index; int para, index; bool cursor_at_beginning = false; Qt::TextFormat format; if ( ! edit->hasSelectedText() ) return -1; format = edit->textFormat(); edit->setTextFormat( Qt::PlainText ); edit->getCursorPosition( ¶, &index ); edit->getSelection(&start_para, &start_index, &end_para, &end_index, 0 ); if ( para == start_para && index == start_index ) cursor_at_beginning = true; text = edit->selectedText(); len = text.length(); if ( origin == UTextOrigin_Beginning || ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) { *former = 0; offset = 0; if ( latter_req_len >= 0 ) { if ( len > latter_req_len ) offset = len - latter_req_len; } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) { edit->setTextFormat( format ); return -1; } if ( latter_req_len == UTextExtent_Line && ( ( newline = text.indexOf( '\n' ) ) != -1 ) ) offset = len - newline; } *latter = strdup( text.left( len - offset ).toUtf8().data() ); } else if ( origin == UTextOrigin_End || ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) { offset = 0; if ( former_req_len >= 0 ) { if ( len > former_req_len ) offset = len - former_req_len; } else { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) { edit->setTextFormat( format ); return -1; } if ( former_req_len == UTextExtent_Line && ( ( newline = text.lastIndexOf( '\n' ) ) != -1 ) ) offset = newline + 1; } *former = strdup( text.mid( offset, len - offset ).toUtf8().data() ); *latter = 0; } else { edit->setTextFormat( format ); return -1; } edit->setTextFormat( format ); return 0; }
int QUimTextUtil::acquirePrimaryTextInQ3TextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len, char **former, char **latter ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); QString text; int start_para, start_index, end_para, end_index, para, index; int n_para; int preedit_len, preedit_cursor_pos; int sel_start_para, sel_start_index, sel_end_para, sel_end_index; Qt::TextFormat format; format = edit->textFormat(); edit->setTextFormat( Qt::PlainText ); edit->getCursorPosition( ¶, &index ); // including preedit string // keep current selection edit->getSelection( &sel_start_para, &sel_start_index, &sel_end_para, &sel_end_index, 0 ); preedit_len = mIc->getPreeditString().length(); preedit_cursor_pos = mIc->getPreeditCursorPosition(); n_para = edit->paragraphs(); switch ( origin ) { case UTextOrigin_Cursor: start_index = index - preedit_cursor_pos; start_para = para; end_index = start_index + preedit_len; end_para = para; if ( former_req_len >= 0 ) { for ( int i = 0; i < former_req_len; i++ ) Q3TextEditPositionBackward( &start_para, &start_index ); } else { if ( former_req_len == UTextExtent_Line ) start_index = 0; else if ( former_req_len == UTextExtent_Full ) { start_para = 0; start_index = 0; } else { edit->setTextFormat( format ); return -1; } } edit->setSelection( start_para, start_index, para, index - preedit_cursor_pos, 0 ); *former = strdup( edit->selectedText().toUtf8().data() ); if ( latter_req_len >= 0 ) { for ( int i = 0; i < latter_req_len; i++ ) Q3TextEditPositionForward( &end_para, &end_index ); } else { if ( latter_req_len == UTextExtent_Line ) { end_index = edit->paragraphLength( end_para ); } else if ( latter_req_len == UTextExtent_Full ) { end_para = n_para - 1; end_index = edit->paragraphLength( end_para ); } else { edit->setTextFormat( format ); return -1; } } edit->setSelection( para, index - preedit_cursor_pos + preedit_len, end_para, end_index, 0 ); *latter = strdup( edit->selectedText().toUtf8().data() ); break; case UTextOrigin_Beginning: *former = 0; start_para = 0; start_index = 0; end_para = start_para; end_index = start_index; if ( latter_req_len >= 0 ) { for ( int i = 0; i < latter_req_len; i++ ) Q3TextEditPositionForward( &end_para, &end_index ); } else { if ( latter_req_len == UTextExtent_Line ) end_index = edit->paragraphLength( end_para ); else if ( latter_req_len == UTextExtent_Full ) { end_para = n_para - 1; end_index = edit->paragraphLength( end_para ); } else { edit->setTextFormat( format ); return -1; } } if ( end_para < para || ( end_para == para && end_index <= ( index - preedit_cursor_pos ) ) ) { edit->setSelection( start_para, start_index, end_para, end_index, 0 ); text = edit->selectedText(); } else { edit->setSelection( start_para, start_index, para, index - preedit_cursor_pos, 0 ); text = edit->selectedText(); edit->setSelection( para, index - preedit_cursor_pos + preedit_len, end_para, end_index, 0 ); text += edit->selectedText(); } *latter = strdup( text.toUtf8().data() ); break; case UTextOrigin_End: end_para = n_para - 1; end_index = edit->paragraphLength( end_para ); start_para = end_para; start_index = end_index; if ( former_req_len >= 0 ) { for ( int i = 0; i < former_req_len; i++ ) Q3TextEditPositionBackward( &start_para, &start_index ); } else { if ( former_req_len == UTextExtent_Line ) start_index = 0; else if ( former_req_len == UTextExtent_Full ) { start_para = 0; start_index = 0; } else { edit->setTextFormat( format ); return -1; } } if ( start_para > para || ( start_para == para && start_index >= ( index - preedit_cursor_pos + preedit_len ) ) ) { edit->setSelection( start_para, start_index, end_para, end_index, 0 ); text = edit->selectedText(); } else { edit->setSelection( start_para, start_index, para, index - preedit_cursor_pos, 0 ); text = edit->selectedText(); edit->setSelection( para, index - preedit_cursor_pos + preedit_len, end_para, end_index, 0 ); text += edit->selectedText(); } *former = strdup( text.toUtf8().data() ); *latter = 0; break; case UTextOrigin_Unspecified: default: edit->setTextFormat( format ); return -1; } if ( sel_start_para != -1 && sel_start_index != -1 && sel_end_para != -1 && sel_end_index != -1 ) edit->setSelection( sel_start_index, sel_start_index, sel_end_para, sel_end_index, 0 ); else edit->removeSelection( 0 ); edit->setCursorPosition( para, index ); edit->setTextFormat( format ); return 0; }
int QUimTextUtil::deleteSelectionTextInQ3TextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); QString text; int len, newline; int para, index; int sel_para_from, sel_index_from, sel_para_to, sel_index_to; int start_para, start_index, end_para, end_index; bool cursor_at_beginning = false; if ( ! edit->hasSelectedText() ) return -1; edit->getCursorPosition( ¶, &index ); edit->getSelection( &sel_para_from, &sel_index_from, &sel_para_to, &sel_index_to, 0 ); if ( para == sel_para_from && index == sel_index_from ) cursor_at_beginning = true; text = edit->selectedText(); len = text.length(); start_para = sel_para_from; start_index = sel_index_from; end_para = sel_para_to; end_index = sel_index_to; if ( origin == UTextOrigin_Beginning || ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) { edit->setCursorPosition( sel_para_from, sel_index_from ); if ( latter_req_len >= 0 ) { if ( len > latter_req_len ) { end_para = sel_para_from; end_index = sel_index_from; for ( int i = 0; i < latter_req_len; i++) Q3TextEditPositionForward( &end_para, &end_index ); } } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; if ( latter_req_len == UTextExtent_Line && ( ( newline = text.indexOf('\n') ) != -1 ) ) { end_para = sel_para_from; end_index = sel_index_from + newline; } } } else if ( origin == UTextOrigin_End || ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) { if ( former_req_len >= 0 ) { if ( len > former_req_len ) { start_para = sel_para_to; start_index = sel_index_to; for ( int i = 0; i < former_req_len; i++) Q3TextEditPositionBackward( &start_para, &start_index ); } } else { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; if ( former_req_len == UTextExtent_Line && ( ( newline = text.lastIndexOf( '\n' ) ) != -1 ) ) { start_para = sel_para_to; start_index = 0; } } } else { return -1; } edit->setSelection( start_para, start_index, end_para, end_index, 1 ); edit->removeSelectedText( 1 ); return 0; }
int QUimTextUtil::deletePrimaryTextInQ3TextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); int start_para, start_index, end_para, end_index, para, index; int n_para; savePreedit(); edit->getCursorPosition( ¶, &index ); n_para = edit->paragraphs(); switch ( origin ) { case UTextOrigin_Cursor: start_index = index; start_para = para; end_index = start_index; end_para = para; if ( former_req_len >= 0 ) { for ( int i = 0; i < former_req_len; i++ ) Q3TextEditPositionBackward( &start_para, &start_index ); } else { if ( former_req_len == UTextExtent_Line ) { start_index = 0; } else if ( former_req_len == UTextExtent_Full ) { start_para = 0; start_index = 0; } else { restorePreedit(); return -1; } } if ( latter_req_len >= 0 ) { for ( int i = 0; i < latter_req_len; i++ ) Q3TextEditPositionForward( &end_para, &end_index ); } else { if ( latter_req_len == UTextExtent_Line ) { end_index = edit->paragraphLength( end_para ); } else if ( latter_req_len == UTextExtent_Full ) { end_para = n_para - 1; end_index = edit->paragraphLength( end_para ); } else { restorePreedit(); return -1; } } break; case UTextOrigin_Beginning: start_para = 0; start_index = 0; end_para = start_para; end_index = start_index; if ( latter_req_len >= 0 ) { for ( int i = 0; i < latter_req_len; i++ ) Q3TextEditPositionForward( &end_para, &end_index ); } else { if ( latter_req_len == UTextExtent_Line ) { end_index = edit->paragraphLength( end_para ); } else if ( latter_req_len == UTextExtent_Full ) { end_para = n_para - 1; end_index = edit->paragraphLength( end_para ); } else { restorePreedit(); return -1; } } break; case UTextOrigin_End: end_para = n_para - 1; end_index = edit->paragraphLength( end_para ); start_para = end_para; start_index = end_index; if ( former_req_len >= 0 ) { for ( int i = 0; i < former_req_len; i++ ) Q3TextEditPositionBackward( &start_para, &start_index ); } else { if ( former_req_len == UTextExtent_Line ) start_index = 0; else if ( former_req_len == UTextExtent_Full ) { start_para = 0; start_index = 0; } else { restorePreedit(); return -1; } } break; case UTextOrigin_Unspecified: default: restorePreedit(); return -1; } edit->setSelection( start_para, start_index, end_para, end_index, 1 ); edit->removeSelectedText( 1 ); edit->setCursorPosition( start_para, start_index ); restorePreedit(); return 0; }