[Libreoffice-commits] core.git: desktop/qa sw/qa sw/source

Tomaž Vajngerl (via logerrit) logerrit at kemper.freedesktop.org
Tue Aug 10 05:54:43 UTC 2021


 desktop/qa/desktop_lib/test_desktop_lib.cxx       |    2 
 sw/qa/extras/indexing/SearchResultLocatorTest.cxx |   50 +++++++++++++----
 sw/source/core/inc/SearchResultLocator.hxx        |   13 +++-
 sw/source/core/model/SearchResultLocator.cxx      |   62 +++++++++++++++++-----
 sw/source/uibase/uno/unotxdoc.cxx                 |    9 ++-
 5 files changed, 105 insertions(+), 31 deletions(-)

New commits:
commit ea1818b8ba34378b777b8706069d28fade2cc924
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Fri Jul 9 19:36:58 2021 +0900
Commit:     Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Tue Aug 10 07:54:06 2021 +0200

    indexing: add support for SdrObjects in SearchResultLocator
    
    Also add (node) "type" parameter because we need to differentiate
    between Writer nodes and SdrObject nodes.
    
    Change-Id: I590695ae71781f64c22bdd7e1df01d69e3376e67
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118671
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 927f24fa26ca..f23f9709416e 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -3117,7 +3117,7 @@ void DesktopLOKTest::testRenderSearchResult()
     Scheduler::ProcessEventsToIdle();
 
     unsigned char* pBuffer = nullptr;
-    OString aJSON = "{ \"node_index\" : 19 }";
+    OString aJSON = "{ \"type\" : 1, \"node_index\" : 19 }";
 
     int nWidth = 0;
     int nHeight = 0;
diff --git a/sw/qa/extras/indexing/SearchResultLocatorTest.cxx b/sw/qa/extras/indexing/SearchResultLocatorTest.cxx
index 9b8474911fbd..99f33422a065 100644
--- a/sw/qa/extras/indexing/SearchResultLocatorTest.cxx
+++ b/sw/qa/extras/indexing/SearchResultLocatorTest.cxx
@@ -17,27 +17,24 @@
 
 namespace
 {
-#if !defined MACOSX
 constexpr OUStringLiteral DATA_DIRECTORY = u"sw/qa/extras/indexing/data/";
-#endif
 }
 
 class SearchResultLocatorTest : public SwModelTestBase
 {
 private:
-#if !defined MACOSX
     SwDoc* createDoc(const char* pName = nullptr);
-#endif
 
 public:
     void testSearchResultLocator();
+    void testSearchResultLocatorForSdrObjects();
 
     CPPUNIT_TEST_SUITE(SearchResultLocatorTest);
     CPPUNIT_TEST(testSearchResultLocator);
+    CPPUNIT_TEST(testSearchResultLocatorForSdrObjects);
     CPPUNIT_TEST_SUITE_END();
 };
 
-#if !defined MACOSX
 SwDoc* SearchResultLocatorTest::createDoc(const char* pName)
 {
     if (!pName)
@@ -49,25 +46,27 @@ SwDoc* SearchResultLocatorTest::createDoc(const char* pName)
     CPPUNIT_ASSERT(pTextDoc);
     return pTextDoc->GetDocShell()->GetDoc();
 }
-#endif
 
 void SearchResultLocatorTest::testSearchResultLocator()
 {
-#if !defined(_WIN32) && !defined(MACOSX)
     if (!IsDefaultDPI())
         return;
 
     SwDoc* pDoc = createDoc("IndexingExport_VariousParagraphs.odt");
     CPPUNIT_ASSERT(pDoc);
 
-    sw::SearchResultLocator aLocator(pDoc);
-    sw::SearchIndexData aData;
+    sw::search::SearchResultLocator aLocator(pDoc);
+    sw::search::SearchIndexData aData;
+    aData.eType = sw::search::NodeType::WriterNode;
     aData.nNodeIndex = 14;
 
-    sw::LocationResult aResult = aLocator.find(aData);
+    sw::search::LocationResult aResult = aLocator.find(aData);
     CPPUNIT_ASSERT_EQUAL(size_t(1), aResult.maRectangles.size());
-    auto aRectangle = aResult.maRectangles[0];
 
+    // skip asserting exact values for macOS and Windows because of
+    // inconsistent results
+#if !defined(_WIN32) && !defined(MACOSX)
+    auto aRectangle = aResult.maRectangles[0];
     CPPUNIT_ASSERT_DOUBLES_EQUAL(1418.0, aRectangle.getMinX(), 1e-4);
     CPPUNIT_ASSERT_DOUBLES_EQUAL(4444.0, aRectangle.getMinY(), 1e-4);
 
@@ -76,6 +75,35 @@ void SearchResultLocatorTest::testSearchResultLocator()
 #endif
 }
 
+void SearchResultLocatorTest::testSearchResultLocatorForSdrObjects()
+{
+    if (!IsDefaultDPI())
+        return;
+
+    SwDoc* pDoc = createDoc("IndexingExport_Shapes.odt");
+    CPPUNIT_ASSERT(pDoc);
+
+    sw::search::SearchResultLocator aLocator(pDoc);
+    sw::search::SearchIndexData aData;
+    aData.eType = sw::search::NodeType::SdrObject;
+    aData.aObjectName = u"Circle";
+    aData.nNodeIndex = 1;
+
+    sw::search::LocationResult aResult = aLocator.find(aData);
+    CPPUNIT_ASSERT_EQUAL(size_t(1), aResult.maRectangles.size());
+
+    // skip asserting exact values for macOS and Windows because of
+    // inconsistent results
+#if !defined(_WIN32) && !defined(MACOSX)
+    auto aRectangle = aResult.maRectangles[0];
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(1478.0, aRectangle.getMinX(), 1e-4);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(3223.0, aRectangle.getMinY(), 1e-4);
+
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(2059.0, aRectangle.getWidth(), 1e-4);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(2059.0, aRectangle.getHeight(), 1e-4);
+#endif
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(SearchResultLocatorTest);
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/inc/SearchResultLocator.hxx b/sw/source/core/inc/SearchResultLocator.hxx
index f9e3aab6929b..5621a397b85c 100644
--- a/sw/source/core/inc/SearchResultLocator.hxx
+++ b/sw/source/core/inc/SearchResultLocator.hxx
@@ -14,11 +14,20 @@
 #include <doc.hxx>
 #include <basegfx/range/b2drange.hxx>
 
-namespace sw
+namespace sw::search
 {
+enum class NodeType
+{
+    Undefined,
+    WriterNode,
+    SdrObject
+};
+
 struct SearchIndexData
 {
-    sal_uInt32 nNodeIndex;
+    NodeType eType = NodeType::Undefined;
+    OUString aObjectName;
+    sal_uInt32 nNodeIndex = 0;
 };
 
 struct LocationResult
diff --git a/sw/source/core/model/SearchResultLocator.cxx b/sw/source/core/model/SearchResultLocator.cxx
index 09aa12ffeb85..9b22c61fa441 100644
--- a/sw/source/core/model/SearchResultLocator.cxx
+++ b/sw/source/core/model/SearchResultLocator.cxx
@@ -14,29 +14,63 @@
 #include <frame.hxx>
 #include <cntfrm.hxx>
 #include <viewsh.hxx>
+#include <IDocumentDrawModelAccess.hxx>
 #include <IDocumentLayoutAccess.hxx>
 
-namespace sw
+#include <svx/svdpage.hxx>
+#include <svx/svdobj.hxx>
+
+namespace sw::search
 {
 LocationResult SearchResultLocator::find(SearchIndexData const& rSearchIndexData)
 {
     LocationResult aResult;
-    SwNodes const& rNodes = mpDocument->GetNodes();
-    if (rSearchIndexData.nNodeIndex >= rNodes.Count())
-        return aResult;
-    SwNode* pNode = rNodes[rSearchIndexData.nNodeIndex];
+    if (rSearchIndexData.eType == NodeType::WriterNode)
+    {
+        SwNodes const& rNodes = mpDocument->GetNodes();
+        if (rSearchIndexData.nNodeIndex >= rNodes.Count())
+            return aResult;
+        SwNode* pNode = rNodes[rSearchIndexData.nNodeIndex];
 
-    auto* pContentNode = pNode->GetContentNode();
-    auto* pShell = mpDocument->getIDocumentLayoutAccess().GetCurrentViewShell();
+        auto* pContentNode = pNode->GetContentNode();
+        auto* pShell = mpDocument->getIDocumentLayoutAccess().GetCurrentViewShell();
 
-    if (pContentNode && pShell)
-    {
-        const SwFrame* pFrame = pContentNode->getLayoutFrame(pShell->GetLayout(), nullptr, nullptr);
-        SwRect const& rArea = pFrame->getFrameArea();
+        if (pContentNode && pShell)
+        {
+            const SwFrame* pFrame
+                = pContentNode->getLayoutFrame(pShell->GetLayout(), nullptr, nullptr);
+            SwRect const& rArea = pFrame->getFrameArea();
 
-        aResult.mbFound = true;
-        aResult.maRectangles.emplace_back(rArea.Left(), rArea.Top(), rArea.Left() + rArea.Width(),
-                                          rArea.Top() + rArea.Height());
+            aResult.mbFound = true;
+            aResult.maRectangles.emplace_back(rArea.Left(), rArea.Top(),
+                                              rArea.Left() + rArea.Width(),
+                                              rArea.Top() + rArea.Height());
+        }
+    }
+    else if (rSearchIndexData.eType == NodeType::SdrObject)
+    {
+        IDocumentDrawModelAccess& rDrawModelAccess = mpDocument->getIDocumentDrawModelAccess();
+        auto* pModel = rDrawModelAccess.GetDrawModel();
+        for (sal_uInt16 nPage = 0; nPage < pModel->GetPageCount(); ++nPage)
+        {
+            SdrPage* pPage = pModel->GetPage(nPage);
+            for (size_t nObject = 0; nObject < pPage->GetObjCount(); ++nObject)
+            {
+                SdrObject* pObject = pPage->GetObj(nObject);
+                if (pObject)
+                {
+                    if (pObject->GetName() == rSearchIndexData.aObjectName)
+                    {
+                        auto aRect = o3tl::convert(pObject->GetLogicRect(), o3tl::Length::mm100,
+                                                   o3tl::Length::twip);
+                        aResult.mbFound = true;
+                        aResult.maRectangles.emplace_back(aRect.Left(), aRect.Top(),
+                                                          aRect.Left() + aRect.GetWidth(),
+                                                          aRect.Top() + aRect.GetHeight());
+                    }
+                }
+            }
+        }
     }
 
     return aResult;
diff --git a/sw/source/uibase/uno/unotxdoc.cxx b/sw/source/uibase/uno/unotxdoc.cxx
index 8ea3697192b4..2940c52b8256 100644
--- a/sw/source/uibase/uno/unotxdoc.cxx
+++ b/sw/source/uibase/uno/unotxdoc.cxx
@@ -3401,14 +3401,17 @@ SwXTextDocument::getSearchResultRectangles(const char* pPayload)
     std::stringstream aStream(pPayload);
     boost::property_tree::read_json(aStream, aTree);
 
-    sw::SearchIndexData aData;
+    sw::search::SearchIndexData aData;
+
+    int nType = aTree.get<int>("type");
 
     aData.nNodeIndex = sal_uInt32(aTree.get<int>("node_index"));
+    aData.eType = sw::search::NodeType(nType);
 
     SwDoc* pDoc = m_pDocShell->GetDoc();
 
-    sw::SearchResultLocator aLocator(pDoc);
-    sw::LocationResult aResult = aLocator.find(aData);
+    sw::search::SearchResultLocator aLocator(pDoc);
+    sw::search::LocationResult aResult = aLocator.find(aData);
     if (aResult.mbFound)
         aRectangles = aResult.maRectangles;
 


More information about the Libreoffice-commits mailing list