void DeleteCommand::undo() { KoTextCommandBase::undo(); UndoRedoFinalizer finalizer(this); // Look at KoTextCommandBase documentation // KoList updateListChanges(); // KoTextRange KoTextRangeManager *rangeManager = KoTextDocument(m_document).textRangeManager(); foreach (KoTextRange *range, m_rangesToRemove) { rangeManager->insert(range); }
void TestKoTextEditor::testRemoveSelectedText() { TestDocument doc; KoTextEditor *editor = doc.textEditor(); KoTextRangeManager *rangeManager = &doc.m_rangeManager; // enter some lorem ipsum editor->insertText(lorem); QTextCursor cur(doc.m_document); cur.setPosition(editor->position()); KoBookmark *bookmark = new KoBookmark(cur); bookmark->setName("start!"); bookmark->setPositionOnlyMode(false); // we want it to be several chars long rangeManager->insert(bookmark); editor->insertText(lorem); bookmark->setRangeEnd(editor->position()); QCOMPARE(bookmark->rangeStart(), lorem.length()); QCOMPARE(bookmark->rangeEnd(), lorem.length() * 2); Q_ASSERT(rangeManager->textRanges().length() == 1); // select all text editor->setPosition(0, QTextCursor::MoveAnchor); editor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor); Q_ASSERT(editor->hasSelection()); // remove the text + the bookmark from the document editor->deleteChar(); // check whether the bookmark has gone. Q_ASSERT(rangeManager->textRanges().length() == 0); }
void TestKoBookmark::testRoundtrip() { const QString lorem( "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor" "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud" "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla" "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia" "deserunt mollit anim id est laborum.\n" ); { // Get the words part and create a document KWDocument *doc = new KWDocument(new KWPart(0)); Q_ASSERT(doc); doc->setAutoSave(0); doc->initEmpty(); // get the main text frame KWTextFrameSet *mainFrameSet = doc->mainFrameSet(); Q_ASSERT(mainFrameSet); QTextDocument *textDocument = mainFrameSet->document(); Q_ASSERT(textDocument); // Insert some text a bookmark pair KoTextDocument koTextDocument(textDocument); KoTextEditor *editor = koTextDocument.textEditor(); editor->insertText(lorem); KoTextRangeManager *rangeManager = koTextDocument.textRangeManager(); QTextCursor cursor(editor->document()); KoBookmark *mark = new KoBookmark(cursor); mark->setName("TESTMARK"); rangeManager->insert(mark); editor->insertTable(5,10); const QTextTable *table = editor->currentTable(); Q_ASSERT(table); editor->setPosition(table->lastPosition()); mark->setRangeEnd(editor->position()); editor->insertText(lorem); // Save the document KUrl url(QString(FILES_OUTPUT_DIR) + "/bookmark_roundtrip.odt"); doc->documentPart()->saveAs(url); // check the number of bookmarks QCOMPARE(rangeManager->bookmarkManager()->bookmarkNameList().length(), 1); QCOMPARE(rangeManager->textRanges().length(), 1); delete doc; } { // Load the document KWDocument *doc = new KWDocument(new KWPart(0)); Q_ASSERT(doc); doc->setAutoSave(0); KUrl url(QString(FILES_OUTPUT_DIR) + "/bookmark_roundtrip.odt"); // this also creates a view... bool result = doc->openUrl(url); Q_ASSERT(result); // get the main text frame KWTextFrameSet *mainFrameSet = doc->mainFrameSet(); Q_ASSERT(mainFrameSet); QTextDocument *textDocument = mainFrameSet->document(); Q_ASSERT(textDocument); KoTextDocument koTextDocument(textDocument); // check the number of bookmarks KoTextRangeManager *rangeManager = koTextDocument.textRangeManager(); QCOMPARE(rangeManager->bookmarkManager()->bookmarkNameList().length(), 1); QCOMPARE(rangeManager->textRanges().length(), 1); delete doc; } }