Exemplo n.º 1
0
void TestChangeTrackedDelete::testPrefixMerge()
{
    TextTool *textTool = new TextTool(new MockCanvas);
    KoTextEditor *textEditor = textTool->textEditor();
    QVERIFY(textEditor);
    QTextDocument *document = textEditor->document();
    KTextDocumentLayout *layout = qobject_cast<KTextDocumentLayout*>(document->documentLayout());
    QTextCursor *cursor = textEditor->cursor();
    cursor->insertText("Hello World");
    cursor->setPosition(3);
    ChangeTrackedDeleteCommand *delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    QCOMPARE(document->characterAt(3).unicode(), (ushort)(QChar::ObjectReplacementCharacter));
    cursor->setPosition(4);
    delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    // This is wierd. Without this loop present the succeeding call to inlineTextObject returs NULL. Why ??????
    for (int i=0; i<document->characterCount(); i++) {
        cursor->setPosition(i);
    }
    cursor->setPosition(4);
    KDeleteChangeMarker *testMarker = dynamic_cast<KDeleteChangeMarker*>(layout->inlineTextObjectManager()->inlineTextObject(*cursor));
    QTextDocumentFragment deleteData =  KTextDocument(document).changeTracker()->elementById(testMarker->changeId())->deleteData();
    QCOMPARE(deleteData.toPlainText(), QString("lo"));
    delete textTool;
}
Exemplo n.º 2
0
void TestChangeTrackedDelete::testListDelete()
{
    TextTool *textTool = new TextTool(new MockCanvas);
    KoTextEditor *textEditor = textTool->textEditor();
    QVERIFY(textEditor);
    QTextDocument *document = textEditor->document();
    KTextDocumentLayout *layout = qobject_cast<KTextDocumentLayout*>(document->documentLayout());
    QTextCursor *cursor = textEditor->cursor();
    insertSampleList(document);

    cursor->setPosition(16);
    cursor->setPosition(152, QTextCursor::KeepAnchor);
    ChangeTrackedDeleteCommand *delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    QCOMPARE(document->characterAt(16).unicode(), (ushort)(QChar::ObjectReplacementCharacter));

    // This is wierd. Without this loop present the succeeding call to inlineTextObject returs NULL. Why ??????
    for (int i=0; i<document->characterCount(); i++) {
        cursor->setPosition(i);
    }

    cursor->setPosition(17);
    KDeleteChangeMarker *testMarker = dynamic_cast<KDeleteChangeMarker*>(layout->inlineTextObjectManager()->inlineTextObject(*cursor));
    QTextDocumentFragment deleteData =  KTextDocument(document).changeTracker()->elementById(testMarker->changeId())->deleteData();

    QTextDocument deleteDocument;
    QTextCursor deleteCursor(&deleteDocument);

    deleteCursor.insertFragment(deleteData);
    bool listFound = false;

    for (int i=0; i < deleteDocument.characterCount(); i++) {
        deleteCursor.setPosition(i);
        if (deleteCursor.currentList()) {
            listFound = true;
            continue;
        }
    }

    QVERIFY(listFound == true);
    QTextList *deletedList = deleteCursor.currentList();
    bool deletedListStatus = deletedList->format().boolProperty(KDeleteChangeMarker::DeletedList);
    QVERIFY (deletedListStatus == true);
    bool deletedListItemStatus;
    deletedListItemStatus  = deletedList->item(0).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(1).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(2).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(3).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(4).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    delete textTool;
}
Exemplo n.º 3
0
void TextPasteCommand::redo()
{
    if (m_document.isNull()) return;

    KoTextDocument textDocument(m_document);
    KoTextEditor *editor = textDocument.textEditor();

    if (!m_first) {
        KUndo2Command::redo();
    } else {
        editor->beginEditBlock(); //this is needed so Qt does not merge successive paste actions together
        m_first = false;
        if (editor->hasSelection()) { //TODO
            editor->addCommand(new DeleteCommand(DeleteCommand::NextChar, m_document.data(), m_shapeController, this));
        }

        // check for mime type
        if (m_mimeData->hasFormat(KoOdf::mimeType(KoOdf::Text))
                        || m_mimeData->hasFormat(KoOdf::mimeType(KoOdf::OpenOfficeClipboard)) ) {
            KoOdf::DocumentType odfType = KoOdf::Text;
            if (!m_mimeData->hasFormat(KoOdf::mimeType(odfType))) {
                odfType = KoOdf::OpenOfficeClipboard;
            }

            if (editor->blockFormat().hasProperty(KoParagraphStyle::HiddenByTable)) {
                editor->insertText("");
            }

            if (m_pasteAsText) {
                editor->insertText(m_mimeData->text());
            } else {

                QSharedPointer<Soprano::Model> rdfModel;
#ifdef SHOULD_BUILD_RDF
                if(!m_rdf) {
                    rdfModel = QSharedPointer<Soprano::Model>(Soprano::createModel());
                } else {
                    rdfModel = m_rdf->model();
                }
#endif

                KoTextPaste paste(editor, m_shapeController, rdfModel, m_canvas, this);
                paste.paste(odfType, m_mimeData);

#ifdef SHOULD_BUILD_RDF
                if (m_rdf) {
                    m_rdf->updateInlineRdfStatements(editor->document());
                }
#endif
            }
        } else if (m_pasteAsText || m_mimeData->hasText()) {
            editor->insertText(m_mimeData->text());
        }
        editor->endEditBlock(); //see above beginEditBlock
    }
}
void InsertInlineObjectActionBase::activated()
{
    Q_ASSERT(m_canvas);
    KoTextEditor *editor = KoTextEditor::getTextEditorFromCanvas(m_canvas);
    if (editor) {
        KoInlineObject *obj = createInlineObject();
        if (obj) {
            editor->insertInlineObject(obj);
        }
    }
}
void InsertInlineObjectActionBase::activated()
{
    Q_ASSERT(m_canvas->toolProxy());
    KoTextEditor *handler = qobject_cast<KoTextEditor*> (m_canvas->toolProxy()->selection());
    if (handler) {
        KoInlineObject *obj = createInlineObject();
        if (obj)
            handler->insertInlineObject(obj);
    } else {
        kWarning(32500) << "InsertVariableAction: No texttool selected while trying to insert variable";
    }
}
Exemplo n.º 6
0
bool KTextShapeData::loadOdf(const KXmlElement &element, KShapeLoadingContext &context, KDocumentRdfBase *rdfData, KShape *shape)
{
    Q_UNUSED(rdfData);
    KTextLoader loader(context, shape);

    QTextCursor cursor(document());
    loader.loadBody(element, cursor);   // now let's load the body from the ODF KXmlElement.
    KoTextEditor *editor = KTextDocument(document()).textEditor();
    if (editor) { // at one point we have to get the position from the odf doc instead.
        editor->setPosition(0);
        editor->finishedLoading();
    }

    return true;
}
Exemplo n.º 7
0
void TestChangeTrackedDelete::testDeleteNextChar()
{
    TextTool *textTool = new TextTool(new MockCanvas);
    KoTextEditor *textEditor = textTool->textEditor();
    QVERIFY(textEditor);
    QTextDocument *document = textEditor->document();
    QTextCursor *cursor = textEditor->cursor();
    cursor->insertText("Hello World");
    cursor->setPosition(4);
    ChangeTrackedDeleteCommand *delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    QCOMPARE(document->characterAt(4).unicode(), (ushort)(QChar::ObjectReplacementCharacter));
    delCommand->undo();
    QCOMPARE(document->characterAt(4), QChar('o'));
    delete textTool;
}
void KoRdfSemanticItemViewSite::select(KoCanvasBase *host)
{
    Q_ASSERT(d->m_semItem);
    Q_ASSERT(d->m_semItem->documentRdf());
    Q_ASSERT(host);
    KoTextEditor *editor = KoDocumentRdf::ensureTextTool(host);
    KoResourceManager *provider = host->resourceManager();
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    QPair<int, int> p = p = rdf->findExtent(d->m_xmlid);
    int startpos = p.first;
    int endpos = p.second + 1;
    if (!endpos) {
        kDebug(30015) << "No end point found for semantic item:" << d->m_semItem->name();
        kDebug(30015) << "xmlid:" << d->m_xmlid;
        return;
    }
    kDebug(30015) << "xmlid:" << d->m_xmlid;
    kDebug(30015) << "start:" << startpos << " endpos:" << endpos;
    selectRange(provider, startpos, endpos);
    kDebug(30015) << "selected text:" << editor->selectedText();
}
Exemplo n.º 9
0
void TestKoTextEditor::checkSectionFormattingLevel(
    TestDocument *doc,
    int neededBlockCount,
    const QVector< QVector<QString> > &needStartings,
    const QVector< QVector<QString> > &needEndings)
{
    // Assuming here that we can check names of the sections
    // instead of actual pointers. This seems to be true for now.
    QCOMPARE(needStartings.size(), neededBlockCount);
    QCOMPARE(needEndings.size(), neededBlockCount);

    KoTextEditor *editor = doc->textEditor();
    editor->movePosition(QTextCursor::Start);

    QCOMPARE(doc->m_document->blockCount(), neededBlockCount);
    for (int i = 0; i < doc->m_document->blockCount(); i++) {
        if (!checkStartings(needStartings[i], editor)
                || !checkEndings(needEndings[i], editor)) {
            QFAIL("Wrong section information.");
        }
        editor->movePosition(QTextCursor::NextBlock);
    }
}
Exemplo n.º 10
0
void TestKoTextEditor::formSectionTestDocument(TestDocument *doc)
{
    // Here we are going to create next document with nested sections:
    //      ** offset  ** block num
    // [ 0P     0           0
    // [ 1P     4           1
    // [ 2P     8           2
    // 2 ]P     12          3
    // 1 ]P     16          4
    // [ 3P     20          5
    // 3 ]P     24          6
    // [ 4P     28          7
    // 4 ]P     32          8
    // 0 ]P     36          9
    // (**empty_block**)    10
    //
    // Sections will receive names "0", "1", etc.
    // [ and ] is actual text, not a sign!

    KoTextEditor *editor = doc->textEditor();

    const int TOTAL_SECTIONS = 5;
    KoSection *sec[TOTAL_SECTIONS];
    KoSectionEnd *secEnd[TOTAL_SECTIONS];

    sec[0] = doc->sectionModel()->createSection(editor->constCursor(), 0, QString::number(0));
    pushSectionStart(0, sec[0], editor);

    sec[1] = doc->sectionModel()->createSection(editor->constCursor(), sec[0], QString::number(1));
    pushSectionStart(1, sec[1], editor);

    sec[2] = doc->sectionModel()->createSection(editor->constCursor(), sec[1], QString::number(2));
    pushSectionStart(2, sec[2], editor);

    secEnd[2] = doc->sectionModel()->createSectionEnd(sec[2]);
    pushSectionEnd(2, secEnd[2], editor);

    secEnd[1] = doc->sectionModel()->createSectionEnd(sec[1]);
    pushSectionEnd(1, secEnd[1], editor);

    sec[3] = doc->sectionModel()->createSection(editor->constCursor(), sec[0], QString::number(3));
    pushSectionStart(3, sec[3], editor);

    secEnd[3] = doc->sectionModel()->createSectionEnd(sec[3]);
    pushSectionEnd(3, secEnd[3], editor);

    sec[4] = doc->sectionModel()->createSection(editor->constCursor(), sec[0], QString::number(4));
    pushSectionStart(4, sec[4], editor);

    secEnd[4] = doc->sectionModel()->createSectionEnd(sec[4]);
    pushSectionEnd(4, secEnd[4], editor);

    secEnd[0] = doc->sectionModel()->createSectionEnd(sec[0]);
    pushSectionEnd(0, secEnd[0], editor);

    doc->sectionModel()->allowMovingEndBound();
}
Exemplo n.º 11
0
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);
}
Exemplo n.º 12
0
QString RdfTest::insertTableWithSemItem(KoTextEditor &editor,
                               KoDocumentRdf &rdfDoc,
                               const QString name)
{
    editor.insertTable(5,10);
#define TABLESIZE (5*10)
    const QTextTable *table = editor.currentTable();

    QTextCursor cur(editor.document());
    cur.setPosition(table->firstPosition());
    KoBookmark *bookmark = new KoBookmark(cur);
    bookmark->setPositionOnlyMode(false); // we want it to be several chars long

    KoTextInlineRdf *inlineRdf(new KoTextInlineRdf(editor.document(), bookmark));
    QString newId = inlineRdf->createXmlId();
    inlineRdf->setXmlId(newId);

    bookmark->setName(newId);
    bookmark->setInlineRdf(inlineRdf);
    KoTextDocument(editor.document()).textRangeManager()->insert(bookmark);

    editor.setPosition(table->firstPosition());
    editor.movePosition(QTextCursor::PreviousCharacter);

    hTestSemanticItem testItem(new TestSemanticItem(0, &rdfDoc));
    testItem->setName(name);
    Soprano::Statement st(
                testItem->linkingSubject(), // subject
                Soprano::Node::createResourceNode(QUrl("http://docs.oasis-open.org/ns/office/1.2/meta/pkg#idref")), // predicate
                Soprano::Node::createLiteralNode(newId), // object
                rdfDoc.manifestRdfNode()); // manifest datastore
    rdfDoc.model()->addStatement(st);
    rdfDoc.rememberNewInlineRdfObject(inlineRdf);

    Q_ASSERT(rdfDoc.model()->statementCount() > 0);

    bookmark->setRangeEnd(table->lastPosition());

    return newId;
}
Exemplo n.º 13
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;
    }

}
Exemplo n.º 14
0
void TestChangeTrackedDelete::testTableDelete()
{
    TextTool *textTool = new TextTool(new MockCanvas);
    KoTextEditor *textEditor = textTool->textEditor();
    QVERIFY(textEditor);
    QTextDocument *document = textEditor->document();
    KTextDocumentLayout *layout = qobject_cast<KTextDocumentLayout*>(document->documentLayout());
    QTextCursor *cursor = textEditor->cursor();
    insertSampleTable(document);

    cursor->setPosition(13);
    cursor->setPosition(102, QTextCursor::KeepAnchor);
    ChangeTrackedDeleteCommand *delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    QCOMPARE(document->characterAt(13).unicode(), (ushort)(QChar::ObjectReplacementCharacter));

    // This is wierd. Without this loop present the succeeding call to inlineTextObject returs NULL. Why ??????
    for (int i=0; i<document->characterCount(); i++) {
        cursor->setPosition(i);
    }

    cursor->setPosition(14);
    KDeleteChangeMarker *testMarker = dynamic_cast<KDeleteChangeMarker*>(layout->inlineTextObjectManager()->inlineTextObject(*cursor));
    QTextDocumentFragment deleteData =  KTextDocument(document).changeTracker()->elementById(testMarker->changeId())->deleteData();

    QTextDocument deleteDocument;
    QTextCursor deleteCursor(&deleteDocument);

    deleteCursor.insertFragment(deleteData);
    bool tableFound = false;

    for (int i=0; i < deleteDocument.characterCount(); i++) {
        deleteCursor.setPosition(i);
        if (deleteCursor.currentTable()) {
            tableFound = true;
            break;
        }
    }
    QVERIFY(tableFound == true);
    QTextTable *table = deleteCursor.currentTable();
    QVERIFY(table->rows() == 3);
    QVERIFY(table->columns() == 3);

    tableFound = false;
    for (int i=0; i < document->characterCount(); i++) {
        deleteCursor.setPosition(i);
        if (deleteCursor.currentTable()) {
            tableFound = true;
            break;
        }
    }
    QVERIFY(tableFound == false);

    delCommand->undo();
    tableFound = false;
    for (int i=0; i < document->characterCount(); i++) {
        deleteCursor.setPosition(i);
        if (deleteCursor.currentTable()) {
            tableFound = true;
            break;
        }
    }
    QVERIFY(tableFound == true);
    delete textTool;
}
Exemplo n.º 15
0
void TextPasteCommand::redo()
{
    KoTextEditor *editor = KoTextDocument(m_tool->m_textShapeData->document()).textEditor();
    if (!m_first) {
        QUndoCommand::redo();
    } else {
        //kDebug() << "begin paste command";
        editor->cursor()->beginEditBlock();
        m_first = false;
        if (editor->hasSelection()) { //TODO
            if (m_tool->m_actionShowChanges->isChecked())
                editor->addCommand(new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, m_tool));
            else
                editor->addCommand(new DeleteCommand(DeleteCommand::NextChar, m_tool));
        }

        // check for mime type
        const QMimeData *data = QApplication::clipboard()->mimeData(m_mode);

        if (data->hasFormat("application/vnd.oasis.opendocument.text")) {

            bool weOwnRdfModel = true;
            Soprano::Model *rdfModel = 0;
#ifdef SHOULD_BUILD_RDF
            rdfModel = Soprano::createModel();
            if (KoDocumentRdf *rdf = KoDocumentRdf::fromResourceManager(m_tool->canvas())) {
                if (rdfModel) {
                    delete rdfModel;
                }
                rdfModel = rdf->model();
                weOwnRdfModel = false;
            }
#endif

            //kDebug() << "pasting odf text";
            KoTextPaste paste(m_tool->m_textShapeData, *editor->cursor(),
                              m_tool->canvas(), rdfModel);
            paste.paste(KoOdf::Text, data);
            //kDebug() << "done with pasting odf";

#ifdef SHOULD_BUILD_RDF
            if (KoDocumentRdf *rdf = KoDocumentRdf::fromResourceManager(m_tool->canvas())) {
                KoTextEditor *e = KoDocumentRdf::ensureTextTool(m_tool->canvas());
                rdf->updateInlineRdfStatements(e->document());
            }
            if (weOwnRdfModel && rdfModel) {
                delete rdfModel;
            }
#endif
        } else if (data->hasHtml()) {
            //kDebug() << "pasting html";
            editor->cursor()->insertHtml(data->html());
            //kDebug() << "done with pasting";
        } else if (data->hasText()) {
            //kDebug() << "pasting text";
            editor->cursor()->insertText(data->text());
            //kDebug() << "done with pasting";
        }
        editor->cursor()->endEditBlock();
    }
}