writerfilter: extend SdtHelper for better support control types

There is much more different controls which must be supprorted
in future. So approach with isInsideDropDownControl() and
validateDateFormat() is not sufficient. Use enum to determine
and store control type.

Change-Id: Ibea7fde117c059053195be213a3ae821644f277c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125471
Tested-by: Jenkins
Reviewed-by: Vasily Melenchuk <vasily.melenchuk@cib.de>
This commit is contained in:
Vasily Melenchuk
2021-12-01 13:33:15 +01:00
parent 4437071c3f
commit fe1da0ce2e
3 changed files with 66 additions and 62 deletions
+50 -47
View File
@@ -1078,10 +1078,17 @@ void DomainMapper::lcl_attribute(Id nName, Value & val)
else
m_pImpl->setSdtEndDeferred(true);
if (m_pImpl->m_pSdtHelper->isInsideDropDownControl())
m_pImpl->m_pSdtHelper->createDropDownControl();
else if (m_pImpl->m_pSdtHelper->validateDateFormat())
m_pImpl->m_pSdtHelper->createDateContentControl();
switch (m_pImpl->m_pSdtHelper->getControlType())
{
case SdtControlType::dropDown:
m_pImpl->m_pSdtHelper->createDropDownControl();
break;
case SdtControlType::datePicker:
m_pImpl->m_pSdtHelper->createDateContentControl();
break;
case SdtControlType::unknown:
default:;
}
break;
case NS_ooxml::LN_CT_SdtListItem_displayText:
// TODO handle when this is != value
@@ -2638,7 +2645,7 @@ void DomainMapper::sprmWithProps( Sprm& rSprm, const PropertyMapPtr& rContext )
case NS_ooxml::LN_CT_SdtPr_dropDownList:
case NS_ooxml::LN_CT_SdtPr_comboBox:
{
m_pImpl->m_pSdtHelper->setInsideDropDownControl(true);
m_pImpl->m_pSdtHelper->setControlType(SdtControlType::dropDown);
writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
if (pProperties)
pProperties->resolve(*this);
@@ -2661,6 +2668,7 @@ void DomainMapper::sprmWithProps( Sprm& rSprm, const PropertyMapPtr& rContext )
break;
case NS_ooxml::LN_CT_SdtPr_date:
{
m_pImpl->m_pSdtHelper->setControlType(SdtControlType::datePicker);
resolveSprmProps(*this, rSprm);
m_pImpl->m_pSdtHelper->setDateFieldStartRange(GetCurrentTextRange()->getEnd());
}
@@ -3518,7 +3526,7 @@ void DomainMapper::lcl_utext(const sal_uInt8 * data_, size_t len)
}
bool bNewLine = len == 1 && (sText[0] == 0x0d || sText[0] == 0x07);
if (m_pImpl->m_pSdtHelper->isInsideDropDownControl())
if (m_pImpl->m_pSdtHelper->getControlType() == SdtControlType::dropDown)
{
if (bNewLine)
// Dropdown control has single-line texts, so in case of newline, create the control.
@@ -3529,44 +3537,48 @@ void DomainMapper::lcl_utext(const sal_uInt8 * data_, size_t len)
return;
}
}
else if (!m_pImpl->m_pSdtHelper->isInteropGrabBagEmpty())
else if (m_pImpl->m_pSdtHelper->getControlType() == SdtControlType::datePicker)
{
// Ignore grabbag when we have a date field, it can conflict during export
if(m_pImpl->m_pSdtHelper->validateDateFormat())
if (!m_pImpl->m_pSdtHelper->isInteropGrabBagEmpty())
{
m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear();
}
else
else if (IsInHeaderFooter() && m_pImpl->IsDiscardHeaderFooter())
{
// there are unsupported SDT properties in the document
// save them in the paragraph interop grab bag
if (m_pImpl->IsDiscardHeaderFooter())
{
// Unless we're supposed to ignore this header/footer.
m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear();
return;
}
if((m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_checkbox") ||
m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_text") ||
m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_dataBinding") ||
m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_citation") ||
(m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_id") &&
m_pImpl->m_pSdtHelper->getInteropGrabBagSize() == 1)) && !m_pImpl->m_pSdtHelper->isOutsideAParagraph())
{
PropertyMapPtr pContext = m_pImpl->GetTopContextOfType(CONTEXT_CHARACTER);
if (m_pImpl->IsOpenField())
// We have a field, insert the SDT properties to the field's grab-bag, so they won't be lost.
pContext = m_pImpl->GetTopFieldContext()->getProperties();
pContext->Insert(PROP_SDTPR, uno::makeAny(m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear()), true, CHAR_GRAB_BAG);
}
else
m_pImpl->GetTopContextOfType(CONTEXT_PARAGRAPH)->Insert(PROP_SDTPR,
uno::makeAny(m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear()), true, PARA_GRAB_BAG);
m_pImpl->m_pSdtHelper->getDateFormat().truncate();
m_pImpl->m_pSdtHelper->getLocale().truncate();
return;
}
}
else if (!m_pImpl->m_pSdtHelper->isInteropGrabBagEmpty())
{
// there are unsupported SDT properties in the document
// save them in the paragraph interop grab bag
if (m_pImpl->IsDiscardHeaderFooter())
{
// Unless we're supposed to ignore this header/footer.
m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear();
return;
}
if((m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_checkbox") ||
m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_text") ||
m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_dataBinding") ||
m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_citation") ||
(m_pImpl->m_pSdtHelper->containedInInteropGrabBag("ooxml:CT_SdtPr_id") &&
m_pImpl->m_pSdtHelper->getInteropGrabBagSize() == 1)) && !m_pImpl->m_pSdtHelper->isOutsideAParagraph())
{
PropertyMapPtr pContext = m_pImpl->GetTopContextOfType(CONTEXT_CHARACTER);
if (m_pImpl->IsOpenField())
// We have a field, insert the SDT properties to the field's grab-bag, so they won't be lost.
pContext = m_pImpl->GetTopFieldContext()->getProperties();
pContext->Insert(PROP_SDTPR, uno::makeAny(m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear()), true, CHAR_GRAB_BAG);
}
else
m_pImpl->GetTopContextOfType(CONTEXT_PARAGRAPH)->Insert(PROP_SDTPR,
uno::makeAny(m_pImpl->m_pSdtHelper->getInteropGrabBagAndClear()), true, PARA_GRAB_BAG);
}
else if (len == 1 && sText[0] == 0x03)
{
// This is the uFtnEdnSep, remember that the document has a separator.
@@ -3591,15 +3603,6 @@ void DomainMapper::lcl_utext(const sal_uInt8 * data_, size_t len)
return;
}
}
else if (m_pImpl->m_pSdtHelper->validateDateFormat())
{
if(IsInHeaderFooter() && m_pImpl->IsDiscardHeaderFooter())
{
m_pImpl->m_pSdtHelper->getDateFormat().truncate();
m_pImpl->m_pSdtHelper->getLocale().truncate();
return;
}
}
if (!m_pImpl->hasTableManager())
return;
@@ -4186,7 +4189,7 @@ void DomainMapper::HandleRedline( Sprm& rSprm )
void DomainMapper::finishParagraph(const bool bRemove, const bool bNoNumbering)
{
if (m_pImpl->m_pSdtHelper->validateDateFormat())
if (m_pImpl->m_pSdtHelper->getControlType() == SdtControlType::datePicker)
m_pImpl->m_pSdtHelper->createDateContentControl();
m_pImpl->finishParagraph(m_pImpl->GetTopContextOfType(CONTEXT_PARAGRAPH), bRemove, bNoNumbering);
}
+5 -8
View File
@@ -67,7 +67,7 @@ static awt::Size lcl_getOptimalWidth(const StyleSheetTablePtr& pStyleSheet,
SdtHelper::SdtHelper(DomainMapper_Impl& rDM_Impl)
: m_rDM_Impl(rDM_Impl)
, m_bInsideDropDownControl(false)
, m_aControlType(SdtControlType::unknown)
, m_bHasElements(false)
, m_bOutsideAParagraph(false)
{
@@ -77,7 +77,7 @@ SdtHelper::~SdtHelper() = default;
void SdtHelper::createDropDownControl()
{
assert(m_bInsideDropDownControl);
assert(getControlType() == SdtControlType::dropDown);
const bool bDropDown
= officecfg::Office::Writer::Filter::Import::DOCX::ImportComboBoxAsDropDown::get();
@@ -132,12 +132,7 @@ void SdtHelper::createDropDownControl()
// clean up
m_aDropDownItems.clear();
m_bInsideDropDownControl = false;
}
bool SdtHelper::validateDateFormat()
{
return !m_sDateFormat.toString().isEmpty() && !m_sLocale.toString().isEmpty();
setControlType(SdtControlType::unknown);
}
void SdtHelper::createDateContentControl()
@@ -199,6 +194,8 @@ void SdtHelper::createDateContentControl()
sFullDate = sFullDate.copy(0, nTimeSep);
xNameCont->insertByName(ODF_FORMDATE_CURRENTDATE, uno::makeAny(sFullDate));
}
setControlType(SdtControlType::unknown);
}
void SdtHelper::createControlShape(awt::Size aSize,
+11 -7
View File
@@ -27,6 +27,13 @@ namespace writerfilter::dmapper
{
class DomainMapper_Impl;
enum class SdtControlType
{
datePicker,
dropDown,
unknown
};
/**
* Helper to create form controls from w:sdt tokens.
*
@@ -39,8 +46,8 @@ class SdtHelper final : public virtual SvRefBase
/// Items of the drop-down control.
std::vector<OUString> m_aDropDownItems;
/// Indicator of a drop-down control
bool m_bInsideDropDownControl;
/// Type of sdt control
SdtControlType m_aControlType;
/// Pieces of the default text -- currently used only by the dropdown control.
OUStringBuffer m_aSdtTexts;
/// Date ISO string contained in the w:date element, used by the date control.
@@ -79,9 +86,6 @@ public:
m_xDateFieldStartRange = xStartRange;
}
/// Decides if we have enough information to create a date control.
bool validateDateFormat();
OUStringBuffer& getLocale() { return m_sLocale; }
/// If createControlShape() was ever called.
bool hasElements() const { return m_bHasElements; }
@@ -93,8 +97,8 @@ public:
bool isOutsideAParagraph() const { return m_bOutsideAParagraph; }
bool isInsideDropDownControl() const { return m_bInsideDropDownControl; }
void setInsideDropDownControl(bool bInside) { m_bInsideDropDownControl = bInside; }
SdtControlType getControlType() { return m_aControlType; }
void setControlType(SdtControlType aType) { m_aControlType = aType; }
/// Create drop-down control from w:sdt's w:dropDownList.
void createDropDownControl();