View Issue Details

IDProjectCategoryView StatusLast Update
0016348ScribusStory Editor / Text Framespublic2022-12-08 14:39
Reportersreerajp Assigned To 
PrioritynoneSeverityfeatureReproducibilityN/A
Status newResolutionopen 
PlatformWindowsOS10OS Version1909
Product Version1.5.7.svn 
Summary0016348: Paste without formatting for Scribus
DescriptionWe have done a paste without formatting while pasting content from clipboard. Please find the code below. If useful this can be incorporated in Scribus. Only issue is that Redo is not proper

void NewsPaper::pasteFromClipboard()
{
    UndoManager* const undoMgr = UndoManager::instance();
    undoMgr->setUndoEnabled(true);
    UndoTransaction activeTransaction;
    UndoTransaction activeDelTransaction;
    if(!checkDoc()) return;
    ScribusDoc *doc = ScCore->primaryMainWindow()->doc;
    if (!doc->inAnEditMode()) return;

        ScribusMainWindow* win = ScCore->primaryMainWindow();
        if (win->ClipB == nullptr) {
                return;
        }
        QString newText = win->ClipB->text();
    if (newText.size() == 0) return;

    PageItem *item = selectedItem();

    if (item == nullptr) return;
    int pos = item->itemText.cursorPosition();
    int firstSelected = item->itemText.startOfSelection();
    int lastSelected = item->itemText.endOfSelection();

    int n = newText.size();
    if (lastSelected < 0) {
        /* Start of changes by Sreeraj P on 17th November 2020 - Applying paragraph style from cursor position */
        int stylePos = firstSelected;
        if (pos > 0)
        {
            stylePos = pos-1;
        }
        else {
            stylePos = pos;
        }
        const CharStyle cstyle = item->itemText.charStyle(stylePos);
        const ParagraphStyle pstyle = item->itemText.paragraphStyle(stylePos);
        if (UndoManager::undoEnabled())
        {
            PageItem* currItem = ScCore->primaryMainWindow()->doc->m_Selection->itemAt(0);
            QString targetName = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::SelectionGroup : currItem->getUName();
            QPixmap* targetPixmap = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::IGroup : currItem->getUPixmap();
            activeTransaction = undoMgr->beginTransaction(targetName, targetPixmap, Um::InsertText, "Inserting text", nullptr);
            //SimpleState* ss = dynamic_cast<SimpleState*>(undoMgr->getLastUndo());
            ScItemState<CharStyle>* ss = new ScItemState<CharStyle>(Um::InsertText, "", Um::ICreate);
            //ss = new SimpleState(Um::InsertText, "", Um::ICreate);
            ss->set("INSERT_FRAMETEXT");
            ss->set("ETEA", QString("insert_frametext"));
            ss->set("TEXT_STR", newText);
            ss->set("START", pos);
            ss->setItem(cstyle);
            undoMgr->action(currItem, ss);
            
        }
        item->itemText.insertChars(pos, newText, false);
        item->itemText.select(pos, newText.length(), true);
        if (activeTransaction)
            activeTransaction.commit();
        undoMgr->setUndoEnabled(false);
        if (&cstyle != nullptr)
        {
            item->itemText.setCharStyle(pos, newText.length(), cstyle);
        }
        item->itemText.select(pos, newText.length(), true);
        if (&pstyle != nullptr)
        {
            doc->itemSelection_ApplyParagraphStyle(pstyle);
        }
        item->itemText.setCursorPosition(pos + n);
        /* End of changes by Sreeraj P on 17th November 2020 - Applying paragraph style from cursor position */
        undoMgr->setUndoEnabled(true);
    }
    else {
        /* Start of changes by Sreeraj P on 17th November 2020 - Crashing while trying to apply character style */
        const CharStyle cstyle = item->itemText.charStyle(firstSelected);
        const ParagraphStyle pstyle = item->itemText.paragraphStyle(firstSelected); //(firstSelected + lastSelected) / 2
        PageItem* currItem = ScCore->primaryMainWindow()->doc->m_Selection->itemAt(0);
        QString targetName = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::SelectionGroup : currItem->getUName();
        QPixmap* targetPixmap = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::IGroup : currItem->getUPixmap();
        if (UndoManager::undoEnabled())
        {
            activeDelTransaction = undoMgr->beginTransaction(targetName, targetPixmap, Um::ReplaceText, "Replacing text", nullptr);
            ScItemState<CharStyle>* is = new ScItemState<CharStyle>(Um::DeleteText, "", Um::IDelete);
            is->set("DELETE_FRAMETEXT");
            is->set("ETEA", QString("delete_frametext"));
            is->set("TEXT_STR", item->itemText.selectedText());
            is->set("START", firstSelected);
            is->setItem(cstyle);
            undoMgr->action(currItem, is);

            //activeTransaction = undoMgr->beginTransaction(targetName, targetPixmap, Um::InsertText, "Inserting text", nullptr);
            //SimpleState* ss = dynamic_cast<SimpleState*>(undoMgr->getLastUndo());
            ScItemState<CharStyle>* ss = new ScItemState<CharStyle>(Um::InsertText, "", Um::ICreate);
            //ss = new SimpleState(Um::InsertText, "", Um::ICreate);
            ss->set("INSERT_FRAMETEXT");
            ss->set("ETEA", QString("insert_frametext"));
            ss->set("TEXT_STR", newText);
            ss->set("START", firstSelected);
            ss->setItem(cstyle);
            undoMgr->action(currItem, ss);
        }
        
        item->itemText.replaceSelection(newText);
        item->itemText.select(firstSelected, newText.length(), true);
        if (activeDelTransaction)
            activeDelTransaction.commit();
        undoMgr->setUndoEnabled(false);
         if (&cstyle != nullptr)
        {
            item->itemText.setCharStyle(firstSelected, n, cstyle);
        }
         item->itemText.select(firstSelected, newText.length(), true);
        if (&pstyle != nullptr)
        {
            doc->itemSelection_ApplyParagraphStyle(pstyle);
        }
        /* End of changes by Sreeraj P on 17th November 2020 - Crashing while trying to apply character style */
        item->itemText.setCursorPosition(pos+n);
        undoMgr->setUndoEnabled(true);
    }
    item->itemText.invalidateAll();
    item->update();
    //-----------------------------------------
    ScCore->primaryMainWindow()->toggleColumnBorders();
    ScCore->primaryMainWindow()->toggleColumnBorders();
    return;
}
TagsNo tags attached.
PatchYes

Relationships

related to 0011814 confirmed Managing styles when pasting an object coming from another document 
related to 0011574 confirmed Usability : Pasting text should not paste the paragraph style 
related to 0005320 confirmed Copy/Paste between docs may create unneccessary line styles 
related to 0003345 new Paste text without formatting 

Activities

sreerajp

2020-11-23 11:51

reporter   ~0048476

Updated the source code. Previous one caused crashing of Scribus when we paste text to a selection with multiple paragraph


void NewsPaper::pasteFromClipboard()
{
    UndoManager* const undoMgr = UndoManager::instance();
    undoMgr->setUndoEnabled(true);
    UndoTransaction activeTransaction;
    UndoTransaction activeDelTransaction;
    if(!checkDoc()) return;
    ScribusDoc *doc = ScCore->primaryMainWindow()->doc;
    if (!doc->inAnEditMode()) return;

        ScribusMainWindow* win = ScCore->primaryMainWindow();
        if (win->ClipB == nullptr) {
                return;
        }
        QString newText = win->ClipB->text();
    if (newText.size() == 0) return;

    PageItem *item = selectedItem();

    if (item == nullptr) return;
    int pos = item->itemText.cursorPosition();
    int firstSelected = item->itemText.startOfSelection();
    int lastSelected = item->itemText.endOfSelection();

    int n = newText.size();
    if (lastSelected < 0) {
        /* Start of changes by Sreeraj P on 17th November 2020 - Applying paragraph style from cursor position */
        int stylePos = firstSelected;
        if (pos > 0)
        {
            stylePos = pos-1;
        }
        else {
            stylePos = pos;
        }
        const CharStyle cstyle = item->itemText.charStyle(stylePos);
        const ParagraphStyle pstyle = item->itemText.paragraphStyle(stylePos);
        if (UndoManager::undoEnabled())
        {
            PageItem* currItem = ScCore->primaryMainWindow()->doc->m_Selection->itemAt(0);
            QString targetName = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::SelectionGroup : currItem->getUName();
            QPixmap* targetPixmap = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::IGroup : currItem->getUPixmap();
            activeTransaction = undoMgr->beginTransaction(targetName, targetPixmap, Um::InsertText, "Inserting text", nullptr);
            //SimpleState* ss = dynamic_cast<SimpleState*>(undoMgr->getLastUndo());
            ScItemState<CharStyle>* ss = new ScItemState<CharStyle>(Um::InsertText, "", Um::ICreate);
            //ss = new SimpleState(Um::InsertText, "", Um::ICreate);
            ss->set("INSERT_FRAMETEXT");
            ss->set("ETEA", QString("insert_frametext"));
            ss->set("TEXT_STR", newText);
            ss->set("START", pos);
            ss->setItem(cstyle);
            undoMgr->action(currItem, ss);
            
        }
        item->itemText.insertChars(pos, newText, false);
        item->itemText.select(pos, newText.length(), true);
        if (activeTransaction)
            activeTransaction.commit();
        undoMgr->setUndoEnabled(false);
        if (&cstyle != nullptr)
        {
            item->itemText.setCharStyle(pos, newText.length(), cstyle);
        }
        item->itemText.select(pos, newText.length(), true);
        if (&pstyle != nullptr)
        {
            doc->itemSelection_ApplyParagraphStyle(pstyle);
        }
        item->itemText.setCursorPosition(pos + n);
        /* End of changes by Sreeraj P on 17th November 2020 - Applying paragraph style from cursor position */
        undoMgr->setUndoEnabled(true);
    }
    else {
        /* Start of changes by Sreeraj P on 17th November 2020 - Crashing while trying to apply character style */
        const CharStyle cstyle = item->itemText.charStyle(firstSelected+1);
        const ParagraphStyle pstyle = item->itemText.paragraphStyle(firstSelected); //(firstSelected + lastSelected) / 2
        PageItem* currItem = ScCore->primaryMainWindow()->doc->m_Selection->itemAt(0);
        QString targetName = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::SelectionGroup : currItem->getUName();
        QPixmap* targetPixmap = (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1) ? Um::IGroup : currItem->getUPixmap();
        if (UndoManager::undoEnabled())
        {
            activeDelTransaction = undoMgr->beginTransaction(targetName, targetPixmap, Um::ReplaceText, "Replacing text", nullptr);
            ScItemState<CharStyle>* is = new ScItemState<CharStyle>(Um::DeleteText, "", Um::IDelete);
            is->set("DELETE_FRAMETEXT");
            is->set("ETEA", QString("delete_frametext"));
            is->set("TEXT_STR", item->itemText.selectedText());
            is->set("START", firstSelected);
            is->setItem(cstyle);
            undoMgr->action(currItem, is);

            //activeTransaction = undoMgr->beginTransaction(targetName, targetPixmap, Um::InsertText, "Inserting text", nullptr);
            //SimpleState* ss = dynamic_cast<SimpleState*>(undoMgr->getLastUndo());
            ScItemState<CharStyle>* ss = new ScItemState<CharStyle>(Um::InsertText, "", Um::ICreate);
            //ss = new SimpleState(Um::InsertText, "", Um::ICreate);
            ss->set("INSERT_FRAMETEXT");
            ss->set("ETEA", QString("insert_frametext"));
            ss->set("TEXT_STR", newText);
            ss->set("START", firstSelected);
            ss->setItem(cstyle);
            undoMgr->action(currItem, ss);
        }
        
        item->itemText.replaceSelection(newText);
        item->itemText.select(firstSelected, newText.length(), true);
        if (activeDelTransaction)
            activeDelTransaction.commit();
        undoMgr->setUndoEnabled(false);
         /*if (&cstyle != nullptr)
        {
            item->itemText.setCharStyle(firstSelected, n, cstyle);
        }*/
         item->itemText.select(firstSelected, newText.length(), true);
        if (&pstyle != nullptr)
        {
            doc->itemSelection_ApplyParagraphStyle(pstyle);
        }
        /* End of changes by Sreeraj P on 17th November 2020 - Crashing while trying to apply character style */
        item->itemText.setCursorPosition(pos+n);
        undoMgr->setUndoEnabled(true);
    }
    item->itemText.invalidateAll();
    item->update();
    //-----------------------------------------
    ScCore->primaryMainWindow()->toggleColumnBorders();
    ScCore->primaryMainWindow()->toggleColumnBorders();
    return;
}

Issue History

Date Modified Username Field Change
2020-11-20 06:41 sreerajp New Issue
2020-11-20 08:11 JLuc Relationship added related to 0011814
2020-11-20 08:12 JLuc Relationship added related to 0011574
2020-11-20 08:12 JLuc Relationship added related to 0005320
2020-11-21 15:01 jghali Severity text => feature
2020-11-23 11:51 sreerajp Note Added: 0048476
2022-12-08 14:37 JLuc Relationship added related to 0003345
2022-12-08 14:39 JLuc Patch No => Yes