From 7b2170f6239f0c4f16a1cbd3ec54a861405aa07a Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sun, 31 May 2020 13:28:36 +0200 Subject: sd: add support to search backwards in PDF search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I2c7f75d16a430dcfa892d28fb6b4f64118705ad2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95459 Tested-by: Tomaž Vajngerl Reviewed-by: Tomaž Vajngerl --- sd/qa/unit/tiledrendering/LOKitSearchTest.cxx | 104 +++++++++++++++++++++++++- sd/source/ui/view/Outliner.cxx | 21 +++++- 2 files changed, 120 insertions(+), 5 deletions(-) diff --git a/sd/qa/unit/tiledrendering/LOKitSearchTest.cxx b/sd/qa/unit/tiledrendering/LOKitSearchTest.cxx index 2c1e1c0426a3..8c2a41eeca9e 100644 --- a/sd/qa/unit/tiledrendering/LOKitSearchTest.cxx +++ b/sd/qa/unit/tiledrendering/LOKitSearchTest.cxx @@ -56,6 +56,7 @@ public: void testSearchInPDFNonExisting(); void testSearchInPDF(); void testSearchInPDFInMultiplePages(); + void testSearchInPDFInMultiplePagesBackwards(); void testSearchIn2MixedObjects(); void testSearchIn6MixedObjects(); @@ -69,6 +70,7 @@ public: CPPUNIT_TEST(testSearchInPDFNonExisting); CPPUNIT_TEST(testSearchInPDF); CPPUNIT_TEST(testSearchInPDFInMultiplePages); + CPPUNIT_TEST(testSearchInPDFInMultiplePagesBackwards); CPPUNIT_TEST(testSearchIn2MixedObjects); CPPUNIT_TEST(testSearchIn6MixedObjects); CPPUNIT_TEST_SUITE_END(); @@ -123,14 +125,14 @@ LOKitSearchTest::createDoc(const char* pName, const uno::Sequence aPropertyValues(comphelper::InitPropertySequence({ { "SearchItem.SearchString", uno::makeAny(rKey) }, - { "SearchItem.Backward", uno::makeAny(false) }, + { "SearchItem.Backward", uno::makeAny(bBackwards) }, { "SearchItem.Command", uno::makeAny(sal_uInt16(eSearch)) }, })); @@ -422,6 +424,104 @@ void LOKitSearchTest::testSearchInPDFInMultiplePages() mpCallbackRecorder->m_aSearchResultSelection[0]); } +void LOKitSearchTest::testSearchInPDFInMultiplePagesBackwards() +{ + SdXImpressDocument* pXImpressDocument = createDoc("PDFSearch.pdf"); + sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell(); + CPPUNIT_ASSERT(pViewShell); + mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase()); + + SdPage* pPage = pViewShell->GetActualPage(); + CPPUNIT_ASSERT(pPage); + + { + SdrObject* pObject = pPage->GetObj(0); + CPPUNIT_ASSERT(pObject); + + SdrGrafObj* pGraphicObject = dynamic_cast(pObject); + CPPUNIT_ASSERT(pGraphicObject); + + Graphic aGraphic = pGraphicObject->GetGraphic(); + auto const& pVectorGraphicData = aGraphic.getVectorGraphicData(); + CPPUNIT_ASSERT(pVectorGraphicData); + CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf, + pVectorGraphicData->getVectorGraphicDataType()); + } + + // Expected for backwards search is: + // - Start with Page 1 + // + search backwards through objects + // + inside objects search backwards through text + // - Switch to Page 2 + // + search backwards through objects + // + inside objects search backwards through text + + // Search for "him" + lcl_search("him", /*FindAll*/ false, /*Backwards*/ true); + + CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound); + CPPUNIT_ASSERT_EQUAL(1, mpCallbackRecorder->m_nSearchResultCount); + + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size()); + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size()); + + CPPUNIT_ASSERT_EQUAL(0, mpCallbackRecorder->m_aSearchResultPart[0]); + CPPUNIT_ASSERT_EQUAL(OString("5592, 5038, 1100, 499"), + mpCallbackRecorder->m_aSearchResultSelection[0]); + + // Search for "him" + lcl_search("him", /*FindAll*/ false, /*Backwards*/ true); + + CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound); + CPPUNIT_ASSERT_EQUAL(2, mpCallbackRecorder->m_nSearchResultCount); + + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size()); + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size()); + + CPPUNIT_ASSERT_EQUAL(0, mpCallbackRecorder->m_aSearchResultPart[0]); + CPPUNIT_ASSERT_EQUAL(OString("9463, 3382, 1099, 499"), + mpCallbackRecorder->m_aSearchResultSelection[0]); + + // Search for "him" + lcl_search("him", /*FindAll*/ false, /*Backwards*/ true); + + CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound); + CPPUNIT_ASSERT_EQUAL(3, mpCallbackRecorder->m_nSearchResultCount); + + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size()); + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size()); + + CPPUNIT_ASSERT_EQUAL(1, mpCallbackRecorder->m_aSearchResultPart[0]); + CPPUNIT_ASSERT_EQUAL(OString("5592, 2964, 1100, 499"), + mpCallbackRecorder->m_aSearchResultSelection[0]); + + // Search for "him" + lcl_search("him", /*FindAll*/ false, /*Backwards*/ true); + + CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound); + CPPUNIT_ASSERT_EQUAL(4, mpCallbackRecorder->m_nSearchResultCount); + + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size()); + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size()); + + CPPUNIT_ASSERT_EQUAL(1, mpCallbackRecorder->m_aSearchResultPart[0]); + CPPUNIT_ASSERT_EQUAL(OString("9463, 1308, 1099, 499"), + mpCallbackRecorder->m_aSearchResultSelection[0]); + + // Search for "him" - back to start + lcl_search("him", /*FindAll*/ false, /*Backwards*/ true); + + CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound); + CPPUNIT_ASSERT_EQUAL(5, mpCallbackRecorder->m_nSearchResultCount); + + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size()); + CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size()); + + CPPUNIT_ASSERT_EQUAL(0, mpCallbackRecorder->m_aSearchResultPart[0]); + CPPUNIT_ASSERT_EQUAL(OString("5592, 5038, 1100, 499"), + mpCallbackRecorder->m_aSearchResultSelection[0]); +} + // Test searching in document with mixed objects. // We have 2 objects: 1. Text Object, 2. Graphic Object with PDF void LOKitSearchTest::testSearchIn2MixedObjects() diff --git a/sd/source/ui/view/Outliner.cxx b/sd/source/ui/view/Outliner.cxx index 44ecd9182e6b..3843cd692872 100644 --- a/sd/source/ui/view/Outliner.cxx +++ b/sd/source/ui/view/Outliner.cxx @@ -826,7 +826,15 @@ bool SdOutliner::SearchAndReplaceOnce(std::vector* pSelecti if (mpImpl->mbCurrentIsVectorGraphic) { - if (mpImpl->mpVectorGraphicSearch->next()) + bool bBackwards = mpSearchItem->GetBackward(); + + bool bResult = false; + if (bBackwards) + bResult = mpImpl->mpVectorGraphicSearch->previous(); + else + bResult = mpImpl->mpVectorGraphicSearch->next(); + + if (bResult) { nMatchCount = 1; @@ -1227,12 +1235,19 @@ void SdOutliner::ProvideNextTextObject() // contains a vector graphic auto* pGraphicObject = static_cast(mpObj); OUString const & rString = mpSearchItem->GetSearchString(); + bool bBackwards = mpSearchItem->GetBackward(); + SearchStartPosition eSearchStartPosition = bBackwards ? SearchStartPosition::End : SearchStartPosition::Begin; mpImpl->mpVectorGraphicSearch = std::make_unique(pGraphicObject->GetGraphic()); - bool bResult = mpImpl->mpVectorGraphicSearch->search(rString); + bool bResult = mpImpl->mpVectorGraphicSearch->search(rString, eSearchStartPosition); if (bResult) - bResult = mpImpl->mpVectorGraphicSearch->next(); + { + if (bBackwards) + bResult = mpImpl->mpVectorGraphicSearch->previous(); + else + bResult = mpImpl->mpVectorGraphicSearch->next(); + } if (bResult) { -- cgit v1.2.3