diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2021-07-05 10:57:41 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2021-07-26 05:55:58 +0200 |
commit | 6e155959de6f46afbe0b68057200c3da822d81f9 (patch) | |
tree | 1b5d069b4026c5afa18e5aa1646eed9b90bd6de2 /sw | |
parent | 2a151d1d5bc055d5e0011460b6ec42ea9f34f880 (diff) |
indexing: search result locator to return the rect of the result
Returns the rectangle(s) where the search result is located in the
document.
Change-Id: Ib2333584fbc460cc16b1bf205fc3d674a1c06957
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118668
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'sw')
-rw-r--r-- | sw/CppunitTest_sw_indexingexport.mk | 1 | ||||
-rw-r--r-- | sw/Library_sw.mk | 1 | ||||
-rw-r--r-- | sw/qa/extras/indexing/SearchResultLocatorTest.cxx | 72 | ||||
-rw-r--r-- | sw/source/core/inc/SearchResultLocator.hxx | 45 | ||||
-rw-r--r-- | sw/source/core/model/SearchResultLocator.cxx | 47 |
5 files changed, 166 insertions, 0 deletions
diff --git a/sw/CppunitTest_sw_indexingexport.mk b/sw/CppunitTest_sw_indexingexport.mk index a3884fee8fb6..7210fbe33042 100644 --- a/sw/CppunitTest_sw_indexingexport.mk +++ b/sw/CppunitTest_sw_indexingexport.mk @@ -15,6 +15,7 @@ $(eval $(call gb_CppunitTest_use_common_precompiled_header,sw_indexingexport)) $(eval $(call gb_CppunitTest_add_exception_objects,sw_indexingexport, \ sw/qa/extras/indexing/IndexingExportTest \ + sw/qa/extras/indexing/SearchResultLocatorTest \ )) $(eval $(call gb_CppunitTest_use_libraries,sw_indexingexport, \ diff --git a/sw/Library_sw.mk b/sw/Library_sw.mk index 02d12d171295..8f9398d2ab8b 100644 --- a/sw/Library_sw.mk +++ b/sw/Library_sw.mk @@ -353,6 +353,7 @@ $(eval $(call gb_Library_add_exception_objects,sw,\ sw/source/core/layout/virtoutp \ sw/source/core/layout/wsfrm \ sw/source/core/model/ModelTraverser \ + sw/source/core/model/SearchResultLocator \ sw/source/core/objectpositioning/anchoredobjectposition \ sw/source/core/objectpositioning/ascharanchoredobjectposition \ sw/source/core/objectpositioning/environmentofanchoredobject \ diff --git a/sw/qa/extras/indexing/SearchResultLocatorTest.cxx b/sw/qa/extras/indexing/SearchResultLocatorTest.cxx new file mode 100644 index 000000000000..933c96159a51 --- /dev/null +++ b/sw/qa/extras/indexing/SearchResultLocatorTest.cxx @@ -0,0 +1,72 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <memory> +#include <string_view> +#include <swmodeltestbase.hxx> +#include <docsh.hxx> +#include <unotxdoc.hxx> + +#include <SearchResultLocator.hxx> + +namespace +{ +constexpr OUStringLiteral DATA_DIRECTORY = u"sw/qa/extras/indexing/data/"; +} + +class SearchResultLocatorTest : public SwModelTestBase +{ +private: + SwDoc* createDoc(const char* pName = nullptr); + +public: + void testSearchResultLocator(); + + CPPUNIT_TEST_SUITE(SearchResultLocatorTest); + CPPUNIT_TEST(testSearchResultLocator); + CPPUNIT_TEST_SUITE_END(); +}; + +SwDoc* SearchResultLocatorTest::createDoc(const char* pName) +{ + if (!pName) + loadURL("private:factory/swriter", nullptr); + else + load(DATA_DIRECTORY, pName); + + SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get()); + CPPUNIT_ASSERT(pTextDoc); + return pTextDoc->GetDocShell()->GetDoc(); +} + +void SearchResultLocatorTest::testSearchResultLocator() +{ +#ifndef MACOSX + SwDoc* pDoc = createDoc("IndexingExport_VariousParagraphs.odt"); + CPPUNIT_ASSERT(pDoc); + + sw::SearchResultLocator aLocator(pDoc); + sw::SearchIndexData aData; + aData.nNodeIndex = 14; + + sw::LocationResult aResult = aLocator.find(aData); + CPPUNIT_ASSERT_EQUAL(size_t(1), aResult.maRectangles.size()); + 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); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(9638.0, aRectangle.getWidth(), 1e-4); + CPPUNIT_ASSERT_DOUBLES_EQUAL(276.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 new file mode 100644 index 000000000000..f9e3aab6929b --- /dev/null +++ b/sw/source/core/inc/SearchResultLocator.hxx @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#pragma once + +#include <swdllapi.h> +#include <doc.hxx> +#include <basegfx/range/b2drange.hxx> + +namespace sw +{ +struct SearchIndexData +{ + sal_uInt32 nNodeIndex; +}; + +struct LocationResult +{ + bool mbFound = false; + std::vector<basegfx::B2DRange> maRectangles; +}; + +class SW_DLLPUBLIC SearchResultLocator +{ + SwDoc* mpDocument; + +public: + SearchResultLocator(SwDoc* pDoc) + : mpDocument(pDoc) + { + } + + LocationResult find(SearchIndexData const& rSearchIndexData); +}; + +} // end sw namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/model/SearchResultLocator.cxx b/sw/source/core/model/SearchResultLocator.cxx new file mode 100644 index 000000000000..09aa12ffeb85 --- /dev/null +++ b/sw/source/core/model/SearchResultLocator.cxx @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <SearchResultLocator.hxx> +#include <node.hxx> +#include <drawdoc.hxx> +#include <frame.hxx> +#include <cntfrm.hxx> +#include <viewsh.hxx> +#include <IDocumentLayoutAccess.hxx> + +namespace sw +{ +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]; + + 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(); + + aResult.mbFound = true; + aResult.maRectangles.emplace_back(rArea.Left(), rArea.Top(), rArea.Left() + rArea.Width(), + rArea.Top() + rArea.Height()); + } + + return aResult; +} + +} // end sw namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |