sd/source/ui/view/drviews2.cxx |  206 +++++++++++++++++++++++++++++++++--------
 1 file changed, 169 insertions(+), 37 deletions(-)

New commits:
commit 5c06f466dd5856c6fa5952ff777632768ccdeec5
Author:     Attila Szűcs <attila.sz...@collabora.com>
AuthorDate: Wed Feb 5 13:38:41 2025 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Wed Feb 12 09:36:58 2025 +0100

    SD: fillapi-slide: format text parts
    
    Implemented way to select partial text in a text object, and use
    uno commands to format them.
    
    Tested several uno commands.
    
    to avoid some JSON validity issue:
    "Transforms"
    "SlideCommands"
    "EditTextObject."
    can have array value also, not just object values (as previously)
    
    New useable commands:
    { "EditTextObject.1": [   // start to edit the object-1 on the actual slide
    // multiple commands can be inside of it
    
    New useable commands inside of "EditTextObject.1"
    {"SelectParagraph": 1},   //select the paragraph-1 text in the edited 
textobject
    {"SelectText": [0,1,0,3] },  //select text
    // 0.paragraph,1.character - 0.paragraph 3.character
    // can be used with less parameter
    // [] = select all text (=[0,0,*,*])
    // [1] = select the full paragraph 1 text (=[1,0,1,*])
    // [1,2] = position cursor to paragraph 1, char 2.. no selection 
(=[1,2,1,2])
    // [1,2,3] = select the text between 1. para 2. char - 3.para last char 
(=[1,2,3,*])
    {"InsertText": "-Inserted-"},  //insert text "-Inserted-" into the actual 
textobject
    // to the selected place. It can insert multiple paragraps. "--
++" = 2 paragraph
    // if a text is selected, it will replace that,
    // if cursor was set without selection, then it will extend the text there.
    // It will select the newly inserted tex, so it can be formatted right away
    {"UnoCommand": ".uno:Bold"},   //run the uno command ".uno:Bold" that 
change the
    // selected text format to bold.
    
    several basic formatting unoCommands that worked:
    
    ".uno:Bold", ".uno:Italic", ".uno:Strikeout", ".uno:Shadowed", 
.uno:Underline
    ".uno:DefaultBullet", .uno:DefaultNumbering
    ".uno:SuperScript", ".uno:SubScript"
    // all previous commands are toggle command
    // useing them on a bold text will make it not-bold
    
    ".uno:CenterPara", ".uno:RightPara", ".uno:LeftPara", ".uno:JustifyPara"
    // these commands set the horizontal alignment of whole paragraphs.
    
    ".uno:Color {\"Color.Color\":{\"type\":\"long\",\"value\":2777241}}"
    // set the selected text color to: 2777241 it is a blue like color
    ".uno:CharBackColor 
{\"CharBackColor.Color\":{\"type\":\"long\",\"value\":6710886}}"
    // set the background color of the selected text to: 6710886 it is a gray 
color
    
    a sample transform:
    {
        "Transforms": [
            { "SlideCommands": [
                {"JumpToSlide": 0},
                {"EditTextObject.1": [
                    {"SelectParagraph": 1},
                    {"InsertText": "----
++++"},
                    {"UnoCommand": ".uno:DefaultNumbering"},
                    {"SelectText": [0,1,0,3] },
                    {"InsertText": "-Inserted-"},
                    {"UnoCommand": ".uno:Underline"},
                    {"UnoCommand": ".uno:Bold"},
                    {"UnoCommand": ".uno:Italic"},
                    {"UnoCommand": ".uno:Strikeout"},
                    {"UnoCommand": ".uno:Shadowed"},
                    {"UnoCommand": ".uno:JustifyPara"},
                    {"UnoCommand": ".uno:DefaultBullet"},
                    {"UnoCommand": ".uno:SuperScript"},
                    {"SelectText": [0,14,0,17] },
                    {"UnoCommand": ".uno:SubScript"},
                    {"UnoCommand": ".uno:Color 
{\"Color.Color\":{\"type\":\"long\",\"value\":2777241}}"},
                    {"UnoCommand": ".uno:CharBackColor 
{\"CharBackColor.Color\":{\"type\":\"long\",\"value\":6710886}}"},
                    {"SelectParagraph": 1},
                    {"UnoCommand": ".uno:CenterPara"},
                    {"SelectParagraph": 2},
                    {"UnoCommand": ".uno:RightPara"},
                    {"SelectParagraph": 3},
                    {"UnoCommand": ".uno:LeftPara"}
                ]  }
            ] }
        ]
    }
    
    Change-Id: I90eba60f8b31e48c681283fa27a6c7daa1886141
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181171
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sd/source/ui/view/drviews2.cxx b/sd/source/ui/view/drviews2.cxx
index 7803fe605f02..7626fb6427e3 100644
--- a/sd/source/ui/view/drviews2.cxx
+++ b/sd/source/ui/view/drviews2.cxx
@@ -79,6 +79,7 @@
 #include <svx/hlnkitem.hxx>
 #include <svx/imapdlg.hxx>
 #include <svx/sdtagitm.hxx>
+#include <svx/svdetc.hxx>
 #include <svx/svdograf.hxx>
 #include <svx/svdoole2.hxx>
 #include <svx/svdpagv.hxx>
@@ -203,6 +204,7 @@
 #include <iostream>
 #include <boost/property_tree/json_parser.hpp>
 #include <rtl/uri.hxx>
+#include <editeng/editeng.hxx>
 
 
 using namespace ::com::sun::star;
@@ -257,6 +259,55 @@ void lcl_LogWarning(const std::string& rWarning)
     LOK_WARN("sd.transform", rWarning);
 }
 
+void lcl_UnoCommand(const std::string& rText)
+{
+    if (rText.size() > 0)
+    {
+        OUString aCmd;
+        std::vector<beans::PropertyValue> aArg;
+        std::size_t nSpace = rText.find(' ');
+        if (nSpace != std::string::npos)
+        {
+            aCmd = OStringToOUString(rText.substr(0, nSpace), 
RTL_TEXTENCODING_UTF8);
+            std::string aArgText = rText.substr(nSpace + 1);
+
+            aArg = comphelper::JsonToPropertyValues(aArgText);
+        }
+        else
+        {
+            aCmd = OStringToOUString(rText, RTL_TEXTENCODING_UTF8);
+        }
+
+        OUString aCmdSub;
+        if (aCmd.startsWith(".uno:"))
+        {
+            aCmdSub = aCmd.subView(5);
+        }
+        else
+        {
+            lcl_LogWarning("FillApi SlideCmd: uno command not recognized'" + 
rText + "'");
+            return;
+        }
+
+        // Check if the uno command is allowed
+        const std::map<std::u16string_view, KitUnoCommand>& rUnoCommandList
+            = GetKitUnoCommandList();
+        auto aCmdData = rUnoCommandList.find(aCmdSub);
+        if (aCmdData != rUnoCommandList.end())
+        {
+            // Make the uno command synchron
+            aArg.push_back(comphelper::makePropertyValue("SynchronMode", 
true));
+
+            // Todo: check why it does not work on my windows system
+            comphelper::dispatchCommand(aCmd, 
comphelper::containerToSequence(aArg));
+        }
+        else
+        {
+            lcl_LogWarning("FillApi SlideCmd: uno command not recognized'" + 
rText + "'");
+        }
+    }
+}
+
 class ClassificationCommon
 {
 protected:
@@ -778,8 +829,13 @@ void DrawViewShell::FuTemporary(SfxRequest& rReq)
                 if (aItem.first == "Transforms")
                 {
                     // Handle all transformations
-                    for (const auto& aItem2 : aItem.second)
+                    for (const auto& aItem2Obj : aItem.second)
                     {
+                        // handle `"Transforms": { `  and `"Transforms": [` 
cases as well
+                        // if an element is an object `{...}`, then get the 
first element of the object
+                        const auto& aItem2
+                            = aItem2Obj.first == "" ? 
*aItem2Obj.second.ordered_begin() : aItem2Obj;
+
                         //jump to slide
                         if (aItem2.first == "SlideCommands")
                         {
@@ -787,7 +843,10 @@ void DrawViewShell::FuTemporary(SfxRequest& rReq)
                             int nNextPageId = 0;
                             for (const auto& aItem3Obj : aItem2.second)
                             {
-                                const auto& aItem3 = 
*aItem3Obj.second.ordered_begin();
+                                // It accept direct property, or object as well
+                                const auto& aItem3 = aItem3Obj.first == ""
+                                                         ? 
*aItem3Obj.second.ordered_begin()
+                                                         : aItem3Obj;
 
                                 sal_uInt16 nPageCount
                                     = 
GetDoc()->GetSdPageCount(PageKind::Standard);
@@ -1142,8 +1201,7 @@ void DrawViewShell::FuTemporary(SfxRequest& rReq)
                                          || aItem3.first == "UnMarkObject")
                                 {
                                     bool bUnMark = aItem3.first == 
"UnMarkObject";
-                                    int nObjId
-                                        = 
static_cast<AutoLayout>(aItem3.second.get_value<int>());
+                                    int nObjId = 
aItem3.second.get_value<int>();
 
                                     SdPage* pPageStandard
                                         = GetDoc()->GetSdPage(nActPageId, 
PageKind::Standard);
@@ -1170,48 +1228,122 @@ void DrawViewShell::FuTemporary(SfxRequest& rReq)
                                             + "' (Object Count = " + 
std::to_string(nObjId));
                                     }
                                 }
-                                else if (aItem3.first == "UnoCommand")
+
+                                else if 
(aItem3.first.starts_with("EditTextObject."))
                                 {
-                                    std::string aText = 
aItem3.second.get_value<std::string>();
-                                    if (aText.size() > 0)
+                                    int nObjId = stoi(aItem3.first.substr(15));
+                                    SdPage* pPageStandard
+                                        = GetDoc()->GetSdPage(nActPageId, 
PageKind::Standard);
+                                    int nObjCount = 
pPageStandard->GetObjCount();
+                                    if (nObjId < 0)
                                     {
-                                        OUString aCmd;
-                                        std::vector<beans::PropertyValue> aArg;
-                                        std::size_t nSpace = aText.find(' ');
-                                        if (nSpace != std::string::npos)
-                                        {
-                                            aCmd = 
OStringToOUString(aText.substr(0, nSpace),
-                                                                     
RTL_TEXTENCODING_UTF8);
-                                            std::string aArgText = 
aText.substr(nSpace + 1);
-
-                                            aArg = 
comphelper::JsonToPropertyValues(aArgText);
-                                        }
-                                        else
-                                        {
-                                            aCmd = OStringToOUString(aText, 
RTL_TEXTENCODING_UTF8);
-                                        }
-
-                                        // Check if the uno command is allowed
-                                        const std::map<std::u16string_view, 
KitUnoCommand>& rUnoCommandList = GetKitUnoCommandList();
-                                        const bool bSupportedCmd = 
rUnoCommandList.find(aCmd) != rUnoCommandList.end();
-                                        if (bSupportedCmd)
+                                        lcl_LogWarning(
+                                            "FillApi SlideCmd EditTextObject: 
Object idx < 0. '"
+                                            + aItem3.first + "'");
+                                    }
+                                    else if (nObjId >= nObjCount)
+                                    {
+                                        lcl_LogWarning(
+                                            "FillApi SlideCmd EditTextObject: 
Object idx >= "
+                                            "Object Count. '"
+                                            + aItem3.first
+                                            + "' (Object Count = " + 
std::to_string(nPageCount));
+                                    }
+                                    else
+                                    {
+                                        SdrObject* pSdrObj = 
pPageStandard->GetObj(nObjId);
+                                        if (!pSdrObj->IsSdrTextObj())
                                         {
-                                            // Make the uno command synchron
-                                            
aArg.push_back(comphelper::makePropertyValue(
-                                                "SynchronMode", true));
-
-                                            // Todo: check why it does not 
work on my windows system
-                                            comphelper::dispatchCommand(
-                                                aCmd, 
comphelper::containerToSequence(aArg));
+                                            lcl_LogWarning(
+                                                "FillApi SlideCmd 
EditTextObject: Object is "
+                                                "not a TextObject. '"
+                                                + aItem3.first + "'");
                                         }
                                         else
                                         {
-                                            lcl_LogWarning(
-                                                "FillApi SlideCmd: uno command 
not recognized'"
-                                                + aText + "'");
+                                            SdrTextObj* pSdrTxt = 
static_cast<SdrTextObj*>(pSdrObj);
+                                            SdrView* pView1 = GetView();
+                                            pView1->MarkObj(pSdrTxt, 
pView1->GetSdrPageView());
+                                            pView1->SdrBeginTextEdit(pSdrTxt);
+                                            EditView& rEditView
+                                                = 
pView1->GetTextEditOutlinerView()->GetEditView();
+                                            for (const auto& aItem4Obj : 
aItem3.second)
+                                            {
+                                                const auto& aItem4
+                                                    = aItem4Obj.first == ""
+                                                          ? 
*aItem4Obj.second.ordered_begin()
+                                                          : aItem4Obj;
+
+                                                if (aItem4.first == 
"SelectText")
+                                                {
+                                                    std::vector<int> aValues;
+                                                    for (const auto& aItem5 : 
aItem4.second)
+                                                    {
+                                                        //if == last?
+                                                        
aValues.push_back(aItem5.second.get_value<int>());
+                                                    }
+                                                    if (aValues.size() == 0)
+                                                    {
+                                                        //select the whole text
+                                                        aValues.push_back(0);
+                                                        aValues.push_back(0);
+                                                        
aValues.push_back(EE_PARA_MAX_COUNT);
+                                                        
aValues.push_back(EE_TEXTPOS_MAX_COUNT);
+                                                    }
+                                                    else if (aValues.size() == 
1)
+                                                    {
+                                                        //select the paragraph
+                                                        aValues.push_back(0);
+                                                        
aValues.push_back(aValues[0]);
+                                                        
aValues.push_back(EE_TEXTPOS_MAX_COUNT);
+                                                    }
+                                                    else if (aValues.size() == 
2)
+                                                    {
+                                                        // set the cursor 
without selecting anything
+                                                        
aValues.push_back(aValues[0]);
+                                                        
aValues.push_back(aValues[1]);
+                                                    }
+                                                    else if (aValues.size() == 
3)
+                                                    {
+                                                        
aValues.push_back(EE_TEXTPOS_MAX_COUNT);
+                                                    }
+
+                                                    const ESelection 
rNewSel(aValues[0], aValues[1],
+                                                                             
aValues[2], aValues[3]);
+                                                    
rEditView.SetSelection(rNewSel);
+                                                }
+                                                else if (aItem4.first == 
"SelectParagraph")
+                                                {
+                                                    int nParaId = 
aItem4.second.get_value<int>();
+
+                                                    const ESelection 
rNewSel(nParaId, 0, nParaId,
+                                                                             
EE_TEXTPOS_MAX_COUNT);
+                                                    
rEditView.SetSelection(rNewSel);
+                                                }
+                                                else if (aItem4.first == 
"InsertText")
+                                                {
+                                                    OUString aText = 
OStringToOUString(
+                                                        
aItem4.second.get_value<std::string>(),
+                                                        RTL_TEXTENCODING_UTF8);
+                                                    // It select the inserted 
text also
+                                                    
rEditView.InsertText(aText, true);
+                                                }
+                                                else if (aItem4.first == 
"UnoCommand")
+                                                {
+                                                    std::string aText
+                                                        = 
aItem4.second.get_value<std::string>();
+                                                    lcl_UnoCommand(aText);
+                                                }
+                                            }
+                                            pView1->SdrEndTextEdit();
                                         }
                                     }
                                 }
+                                else if (aItem3.first == "UnoCommand")
+                                {
+                                    std::string aText = 
aItem3.second.get_value<std::string>();
+                                    lcl_UnoCommand(aText);
+                                }
                             }
                         }
                     }

Reply via email to