diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2021-02-11 16:37:00 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2021-02-11 18:50:08 +0100 |
commit | b11db88d0174fbad25f7ee04726ae27ceb1488c3 (patch) | |
tree | 42166c5bfa038246b796d478c21f83eea4e4cb4f /sw | |
parent | 8180bdc75c2bf3c1f813469eabd38db0612f1c91 (diff) |
sw tooltip on bibliography fields: add the actual tooltip functionality
- Similar to e.g. SwMacroField::GetMacro(), add a new
SwAuthorityField::GetAuthority() that returns a string which is similar
to the one-liner text node in the bibliography table for a given
bibliography reference.
- Base this on the recently added
SwAuthorityFieldType::CreateTOXInternational() and
SwTOXAuthority::GetText() to share code with
sw::ToxTextGenerator::GenerateText() and SwTOXBaseSection::Update().
- Finally extend SwEditWin::RequestHelp() to actually provide the
tooltip on mouse hover.
Change-Id: I33a58076c6d141566298259e7e4681541fac1055
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110765
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
Diffstat (limited to 'sw')
-rw-r--r-- | sw/CppunitTest_sw_core_fields.mk | 76 | ||||
-rw-r--r-- | sw/Module_sw.mk | 1 | ||||
-rw-r--r-- | sw/inc/authfld.hxx | 8 | ||||
-rw-r--r-- | sw/qa/core/fields/fields.cxx | 76 | ||||
-rw-r--r-- | sw/source/core/fields/authfld.cxx | 48 | ||||
-rw-r--r-- | sw/source/uibase/docvw/edtwin2.cxx | 9 |
6 files changed, 216 insertions, 2 deletions
diff --git a/sw/CppunitTest_sw_core_fields.mk b/sw/CppunitTest_sw_core_fields.mk new file mode 100644 index 000000000000..c8c982da4cc8 --- /dev/null +++ b/sw/CppunitTest_sw_core_fields.mk @@ -0,0 +1,76 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +#************************************************************************* +# +# 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/. +# +#************************************************************************* + +$(eval $(call gb_CppunitTest_CppunitTest,sw_core_fields)) + +$(eval $(call gb_CppunitTest_use_common_precompiled_header,sw_core_fields)) + +$(eval $(call gb_CppunitTest_add_exception_objects,sw_core_fields, \ + sw/qa/core/fields/fields \ +)) + +$(eval $(call gb_CppunitTest_use_libraries,sw_core_fields, \ + comphelper \ + cppu \ + cppuhelper \ + sal \ + sfx \ + sw \ + swqahelper \ + test \ + unotest \ + utl \ + vcl \ + svt \ + tl \ + svl \ + svxcore \ +)) + +$(eval $(call gb_CppunitTest_use_externals,sw_core_fields,\ + boost_headers \ + libxml2 \ +)) + +$(eval $(call gb_CppunitTest_set_include,sw_core_fields,\ + -I$(SRCDIR)/sw/inc \ + -I$(SRCDIR)/sw/source/core/inc \ + -I$(SRCDIR)/sw/source/uibase/inc \ + -I$(SRCDIR)/sw/qa/inc \ + $$(INCLUDE) \ +)) + +$(eval $(call gb_CppunitTest_use_api,sw_core_fields,\ + udkapi \ + offapi \ + oovbaapi \ +)) + +$(eval $(call gb_CppunitTest_use_ure,sw_core_fields)) +$(eval $(call gb_CppunitTest_use_vcl,sw_core_fields)) + +$(eval $(call gb_CppunitTest_use_rdb,sw_core_fields,services)) + +$(eval $(call gb_CppunitTest_use_custom_headers,sw_core_fields,\ + officecfg/registry \ +)) + +$(eval $(call gb_CppunitTest_use_configuration,sw_core_fields)) + +$(eval $(call gb_CppunitTest_use_uiconfigs,sw_core_fields, \ + modules/swriter \ + svt \ + svx \ +)) + +$(eval $(call gb_CppunitTest_use_more_fonts,sw_core_fields)) + +# vim: set noet sw=4 ts=4: diff --git a/sw/Module_sw.mk b/sw/Module_sw.mk index 123b7acf5321..fc4498ca29be 100644 --- a/sw/Module_sw.mk +++ b/sw/Module_sw.mk @@ -120,6 +120,7 @@ $(eval $(call gb_Module_add_slowcheck_targets,sw,\ CppunitTest_sw_uibase_uno \ CppunitTest_sw_core_accessibilitycheck \ CppunitTest_sw_core_layout \ + CppunitTest_sw_core_fields \ CppunitTest_sw_core_frmedt \ CppunitTest_sw_core_txtnode \ CppunitTest_sw_core_objectpositioning \ diff --git a/sw/inc/authfld.hxx b/sw/inc/authfld.hxx index 7b7bdd8518be..6436a17bb5f0 100644 --- a/sw/inc/authfld.hxx +++ b/sw/inc/authfld.hxx @@ -30,6 +30,7 @@ #include <vector> class SwTOXInternational; +class SwTextAttr; class SwAuthEntry final : public salhelper::SimpleReferenceObject { @@ -148,7 +149,7 @@ public: one of the instances with the same m_nHandle is actually in the document, they're all cloned via CopyField()... */ -class SAL_DLLPUBLIC_RTTI SwAuthorityField final : public SwField +class SW_DLLPUBLIC SwAuthorityField final : public SwField { rtl::Reference<SwAuthEntry> m_xAuthEntry; mutable sal_IntPtr m_nTempSequencePos; @@ -162,7 +163,7 @@ public: OUString ConditionalExpandAuthIdentifier(SwRootFrame const* pLayout) const; //To handle Citation - SW_DLLPUBLIC OUString ExpandCitation(ToxAuthorityField eField, SwRootFrame const* pLayout) const; + OUString ExpandCitation(ToxAuthorityField eField, SwRootFrame const* pLayout) const; SwAuthorityField(SwAuthorityFieldType* pType, const OUString& rFieldContents); SwAuthorityField(SwAuthorityFieldType* pType, SwAuthEntry* pAuthEntry); @@ -179,6 +180,9 @@ public: SwAuthEntry* GetAuthEntry() const { return m_xAuthEntry.get(); } virtual OUString GetDescription() const override; + + /// Returns the line matching the source's default row in the ToX. + OUString GetAuthority(const SwTextAttr* pTextAttr, const SwRootFrame* pLayout) const; }; inline OUString const & SwAuthEntry::GetAuthorField(ToxAuthorityField ePos) const diff --git a/sw/qa/core/fields/fields.cxx b/sw/qa/core/fields/fields.cxx new file mode 100644 index 000000000000..8d82344a24af --- /dev/null +++ b/sw/qa/core/fields/fields.cxx @@ -0,0 +1,76 @@ +/* -*- 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 <swmodeltestbase.hxx> + +#include <com/sun/star/style/PageStyleLayout.hpp> + +#include <comphelper/propertyvalue.hxx> +#include <svx/svdpage.hxx> +#include <vcl/gdimtf.hxx> + +#include <IDocumentDrawModelAccess.hxx> +#include <IDocumentLayoutAccess.hxx> +#include <IDocumentState.hxx> +#include <authfld.hxx> +#include <docsh.hxx> +#include <drawdoc.hxx> +#include <rootfrm.hxx> +#include <unotxdoc.hxx> +#include <wrtsh.hxx> +#include <ndtxt.hxx> + +namespace +{ +/// Covers sw/source/core/fields/ fixes. +class Test : public SwModelTestBase +{ +}; + +CPPUNIT_TEST_FIXTURE(Test, testAuthorityTooltip) +{ + // Create a document with a bibliography reference in it. + SwDoc* pDoc = createSwDoc(); + uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference<beans::XPropertySet> xField( + xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aFields = { + comphelper::makePropertyValue("Identifier", OUString("ARJ00")), + comphelper::makePropertyValue("Author", OUString("Ar, J")), + comphelper::makePropertyValue("Title", OUString("mytitle")), + comphelper::makePropertyValue("Year", OUString("2020")), + }; + xField->setPropertyValue("Fields", uno::makeAny(aFields)); + uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XText> xText = xTextDocument->getText(); + uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor(); + uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); + + // Get the tooltip of the field. + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 1, /*bBasicCall=*/false); + SwPaM* pCursor = pWrtShell->GetCursor(); + auto pField = dynamic_cast<SwAuthorityField*>( + SwCursorShell::GetFieldAtCursor(pCursor, /*bIncludeInputFieldAtStart=*/true)); + CPPUNIT_ASSERT(pField); + SwTextNode* pTextNode = pCursor->GetNode().GetTextNode(); + const SwTextAttr* pTextAttr = pTextNode->GetSwpHints().Get(0); + const SwRootFrame* pLayout = pWrtShell->GetLayout(); + OUString aTooltip = pField->GetAuthority(pTextAttr, pLayout); + + // Without the accompanying fix in place, generating this tooltip text was not possible without + // first inserting an empty bibliography table into the document. + CPPUNIT_ASSERT_EQUAL(OUString("ARJ00: Ar, J, mytitle, 2020"), aTooltip); +} +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/fields/authfld.cxx b/sw/source/core/fields/authfld.cxx index 53867c809673..4728e48fd60b 100644 --- a/sw/source/core/fields/authfld.cxx +++ b/sw/source/core/fields/authfld.cxx @@ -554,6 +554,54 @@ OUString SwAuthorityField::GetDescription() const return SwResId(STR_AUTHORITY_ENTRY); } +OUString SwAuthorityField::GetAuthority(const SwTextAttr* pTextAttr, + const SwRootFrame* pLayout) const +{ + OUString aText; + + SwForm aForm(TOX_AUTHORITIES); + if (!pTextAttr) + { + return aText; + } + + auto& rFormatField = const_cast<SwFormatField&>(pTextAttr->GetFormatField()); + SwTextField* pTextField = rFormatField.GetTextField(); + if (!pTextField) + { + return aText; + } + + const SwTextNode& rNode = pTextField->GetTextNode(); + const auto pFieldType = static_cast<const SwAuthorityFieldType*>(GetTyp()); + std::unique_ptr<SwTOXInternational> pIntl(pFieldType->CreateTOXInternational()); + SwTOXAuthority aAuthority(rNode, rFormatField, *pIntl); + sal_uInt16 nLevel = aAuthority.GetLevel(); + SwFormTokens aPattern = aForm.GetPattern(nLevel); + aAuthority.InitText(pLayout); + for (const auto& rToken : aPattern) + { + switch (rToken.eTokenType) + { + case TOKEN_TEXT: + { + aText += rToken.sText; + break; + } + case TOKEN_AUTHORITY: + { + sal_uInt16 eField = rToken.nAuthorityField; + aText += aAuthority.GetText(eField, pLayout); + break; + } + default: + break; + } + } + + return aText; +} + const char* const aFieldNames[] = { "Identifier", diff --git a/sw/source/uibase/docvw/edtwin2.cxx b/sw/source/uibase/docvw/edtwin2.cxx index 5770afbd4c05..f3b975dfb546 100644 --- a/sw/source/uibase/docvw/edtwin2.cxx +++ b/sw/source/uibase/docvw/edtwin2.cxx @@ -52,6 +52,7 @@ #include <ndtxt.hxx> #include <FrameControlsManager.hxx> #include <comphelper/lok.hxx> +#include <authfld.hxx> static OUString lcl_GetRedlineHelp( const SwRangeRedline& rRedl, bool bBalloon ) { @@ -340,6 +341,14 @@ void SwEditWin::RequestHelp(const HelpEvent &rEvt) } break; } + case SwFieldIds::TableOfAuthorities: + { + const auto pAuthorityField + = static_cast<const SwAuthorityField*>(pField); + sText = pAuthorityField->GetAuthority(aContentAtPos.pFndTextAttr, + rSh.GetLayout()); + break; + } default: break; } |