sc: kit chart looks come from svx ChartHelper The look code 0020 added to sc moved to svx (0028); Calc calls it there. diff --git a/sc/inc/ChartTools.hxx b/sc/inc/ChartTools.hxx index 2e49612..4a22141 100644 --- a/sc/inc/ChartTools.hxx +++ b/sc/inc/ChartTools.hxx @@ -13,8 +13,6 @@ #include #include -#include -#include #include #include #include "types.hxx" @@ -24,7 +22,6 @@ class ScDocShell; class ScDocument; class SdrOle2Obj; -namespace tools { class JsonWriter; } namespace sctools { @@ -55,14 +52,6 @@ std::vector getAllPivotChartsConnectedTo(std::u16string_view sPivot /// `rText`'s ranges, when every part names existing cells. std::optional chartRanges(const ScDocument& rDoc, std::u16string_view rText); -/// Whether a kit client's chart look parses. -bool isChartLook(std::u16string_view rJson); -/// Sets type, title, legend and axis titles from JSON. -bool setChartLook(const css::uno::Reference& xChart, - std::u16string_view rJson); -/// Writes the same look as JSON members. -void writeChartLook(::tools::JsonWriter& rJson, - const css::uno::Reference& xChart); } // end sctools diff --git a/sc/source/ui/docshell/docsh4.cxx b/sc/source/ui/docshell/docsh4.cxx index 46f3547..78810ce 100644 --- a/sc/source/ui/docshell/docsh4.cxx +++ b/sc/source/ui/docshell/docsh4.cxx @@ -96,6 +96,7 @@ #include #include #include +#include #include #include #include @@ -1729,7 +1730,7 @@ void ScDocShell::ExecuteChartSource(SfxRequest& rReq) // A kit client's look; it must parse before anything changes. const SfxStringItem* pLook = rReq.GetArg(FN_PARAM_3); if (pLook - && (!bColInit || !bRowInit || !sctools::isChartLook(pLook->GetValue()) + && (!bColInit || !bRowInit || !ChartHelper::IsChartLook(pLook->GetValue()) || !sctools::chartRanges(rDoc, aRangeName))) { SAL_WARN("sc.lok", "chart look needs headers and must parse: " << pLook->GetValue()); @@ -1800,7 +1801,7 @@ void ScDocShell::ExecuteChartSource(SfxRequest& rReq) pUndoManager->EnterListAction(ScResId(STR_UNDO_CHARTDATA), u""_ustr, 0, pViewSh->GetViewShellId()); auto xChart = m_pDocument->GetChartByName(aChartName); ChartState aOld = lcl_ChartState(xChart); - sctools::setChartLook(xChart, pLook->GetValue()); + ChartHelper::SetChartLook(xChart, pLook->GetValue()); if (bUndo) pUndoManager->AddUndoAction(std::make_unique( *this, aChartName, std::move(aOld), lcl_ChartState(xChart))); diff --git a/sc/source/ui/drawfunc/fuins2.cxx b/sc/source/ui/drawfunc/fuins2.cxx index 78e4a9c..b0169d0 100644 --- a/sc/source/ui/drawfunc/fuins2.cxx +++ b/sc/source/ui/drawfunc/fuins2.cxx @@ -63,6 +63,7 @@ #include #include #include +#include #include #include #include @@ -424,7 +425,7 @@ FuInsertChart::FuInsertChart(ScTabViewShell& rViewSh, vcl::Window* pWin, ScDrawV // A kit client's look must parse before anything lands. const SfxStringItem* pLook = rReq.GetArg(FN_PARAM_3); - if (pLook && !sctools::isChartLook(pLook->GetValue())) + if (pLook && !ChartHelper::IsChartLook(pLook->GetValue())) { SAL_WARN("sc.lok", "chart look does not parse: " << pLook->GetValue()); return; @@ -643,7 +644,7 @@ FuInsertChart::FuInsertChart(ScTabViewShell& rViewSh, vcl::Window* pWin, ScDrawV // No wizard: one undo step, the look from the request. pView->AddUndo(std::make_unique(*pObj)); if (pLook) - sctools::setChartLook(xChartModel, pLook->GetValue()); + ChartHelper::SetChartLook(xChartModel, pLook->GetValue()); if (xChartModel.is()) xChartModel->unlockControllers(); } diff --git a/sc/source/ui/unoobj/ChartTools.cxx b/sc/source/ui/unoobj/ChartTools.cxx index caba334..5fb13a4 100644 --- a/sc/source/ui/unoobj/ChartTools.cxx +++ b/sc/source/ui/unoobj/ChartTools.cxx @@ -13,18 +13,8 @@ #include #include -#include -#include -#include -#include #include -#include -#include -#include #include -#include -#include -#include #include #include #include @@ -181,76 +171,6 @@ std::vector getAllPivotChartsConnectedTo(std::u16string_view sPivot return aObjects; } -namespace -{ -/// Kit names and the diagram services they map to. -constexpr std::pair aChartTypes[] - = { { "column", u"com.sun.star.chart.BarDiagram" }, - { "bar", u"com.sun.star.chart.BarDiagram" }, - { "line", u"com.sun.star.chart.LineDiagram" }, - { "scatter", u"com.sun.star.chart.XYDiagram" }, - { "pie", u"com.sun.star.chart.PieDiagram" } }; - -/// A parsed look; "other" keeps the diagram. -struct ChartLook -{ - std::string_view aType; - OUString aTitle, aXTitle, aYTitle; - bool bLegend = true; -}; - -std::optional lcl_ParseLook(std::u16string_view rJson) -{ - boost::property_tree::ptree aTree; - std::stringstream aStream{ std::string(OUString(rJson).toUtf8()) }; - try - { - boost::property_tree::read_json(aStream, aTree); - } - catch (const boost::property_tree::json_parser_error&) - { - return std::nullopt; - } - const std::string aType = aTree.get("type", ""); - ChartLook aLook; - if (aType == "other") - aLook.aType = "other"; - for (const auto& [rName, rService] : aChartTypes) - if (rName == aType) - aLook.aType = rName; - if (aLook.aType.empty()) - return std::nullopt; - aLook.aTitle = OUString::fromUtf8(aTree.get("title", "")); - aLook.aXTitle = OUString::fromUtf8(aTree.get("xtitle", "")); - aLook.aYTitle = OUString::fromUtf8(aTree.get("ytitle", "")); - aLook.bLegend = aTree.get("legend", true); - return aLook; -} - -/// Shows a title with this text; empty hides it. -void lcl_SetTitle(const uno::Reference& xOwner, const OUString& rHas, - const std::function()>& rTitle, - const OUString& rText) -{ - xOwner->setPropertyValue(rHas, uno::Any(!rText.isEmpty())); - if (rText.isEmpty()) - return; - uno::Reference xTitle(rTitle(), uno::UNO_QUERY_THROW); - xTitle->setPropertyValue(u"String"_ustr, uno::Any(rText)); -} - -OUString lcl_Title(const uno::Reference& xOwner, const OUString& rHas, - const std::function()>& rTitle) -{ - OUString aText; - if (!xOwner->getPropertyValue(rHas).get()) - return aText; - uno::Reference xTitle(rTitle(), uno::UNO_QUERY_THROW); - xTitle->getPropertyValue(u"String"_ustr) >>= aText; - return aText; -} -} - std::optional chartRanges(const ScDocument& rDoc, std::u16string_view rText) { ScRangeList aRanges; @@ -264,71 +184,6 @@ std::optional chartRanges(const ScDocument& rDoc, std::u16string_vi return aRanges; } -bool isChartLook(std::u16string_view rJson) { return lcl_ParseLook(rJson).has_value(); } - -bool setChartLook(const uno::Reference& xChart, std::u16string_view rJson) -{ - const std::optional oLook = lcl_ParseLook(rJson); - uno::Reference xDoc(xChart, uno::UNO_QUERY); - if (!oLook || !xDoc.is()) - return false; - uno::Reference xDiagram = xDoc->getDiagram(); - const auto pType = std::find_if(std::begin(aChartTypes), std::end(aChartTypes), - [&oLook](const auto& rPair) { return rPair.first == oLook->aType; }); - if (pType != std::end(aChartTypes)) - { - if (xDiagram->getDiagramType() != pType->second) - { - uno::Reference xFactory(xDoc, uno::UNO_QUERY_THROW); - xDiagram.set(xFactory->createInstance(OUString(pType->second)), uno::UNO_QUERY_THROW); - xDoc->setDiagram(xDiagram); - } - uno::Reference xProps(xDiagram, uno::UNO_QUERY_THROW); - if (pType->second == u"com.sun.star.chart.BarDiagram") - xProps->setPropertyValue(u"Vertical"_ustr, uno::Any(oLook->aType == "bar")); - if (oLook->aType == "scatter") - xProps->setPropertyValue(u"Lines"_ustr, uno::Any(false)); - } - uno::Reference xDocProps(xDoc, uno::UNO_QUERY_THROW); - lcl_SetTitle(xDocProps, u"HasMainTitle"_ustr, [&xDoc] { return xDoc->getTitle(); }, - oLook->aTitle); - xDocProps->setPropertyValue(u"HasLegend"_ustr, uno::Any(oLook->bLegend)); - // A pie has no axes. - if (xDiagram->getDiagramType() == u"com.sun.star.chart.PieDiagram") - return true; - uno::Reference xX(xDiagram, uno::UNO_QUERY_THROW); - uno::Reference xY(xDiagram, uno::UNO_QUERY_THROW); - uno::Reference xProps(xDiagram, uno::UNO_QUERY_THROW); - lcl_SetTitle(xProps, u"HasXAxisTitle"_ustr, [&xX] { return xX->getXAxisTitle(); }, - oLook->aXTitle); - lcl_SetTitle(xProps, u"HasYAxisTitle"_ustr, [&xY] { return xY->getYAxisTitle(); }, - oLook->aYTitle); - return true; -} - -void writeChartLook(::tools::JsonWriter& rJson, const uno::Reference& xChart) -{ - uno::Reference xDoc(xChart, uno::UNO_QUERY_THROW); - uno::Reference xDiagram = xDoc->getDiagram(); - uno::Reference xProps(xDiagram, uno::UNO_QUERY_THROW); - const OUString aService = xDiagram->getDiagramType(); - std::string_view aType = "other"; - for (const auto& [rName, rService] : aChartTypes) - if (rService == aService && aType == "other") - aType = rName; - if (aType == "column" && xProps->getPropertyValue(u"Vertical"_ustr).get()) - aType = "bar"; - rJson.put("type", aType); - uno::Reference xDocProps(xDoc, uno::UNO_QUERY_THROW); - rJson.put("title", lcl_Title(xDocProps, u"HasMainTitle"_ustr, [&xDoc] { return xDoc->getTitle(); })); - rJson.put("legend", xDocProps->getPropertyValue(u"HasLegend"_ustr).get()); - uno::Reference xX(xDiagram, uno::UNO_QUERY_THROW); - uno::Reference xY(xDiagram, uno::UNO_QUERY_THROW); - const bool bAxes = aType != "pie"; - rJson.put("xtitle", bAxes ? lcl_Title(xProps, u"HasXAxisTitle"_ustr, [&xX] { return xX->getXAxisTitle(); }) : OUString()); - rJson.put("ytitle", bAxes ? lcl_Title(xProps, u"HasYAxisTitle"_ustr, [&xY] { return xY->getYAxisTitle(); }) : OUString()); -} - } // end sctools /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/unoobj/docuno.cxx b/sc/source/ui/unoobj/docuno.cxx index b850504..cf42499 100644 --- a/sc/source/ui/unoobj/docuno.cxx +++ b/sc/source/ui/unoobj/docuno.cxx @@ -154,6 +154,7 @@ #include #include #include +#include #include #include #include @@ -947,7 +948,7 @@ void lcl_Chart(tools::JsonWriter& rJson, ScDocument& rDoc, ScViewData& rData) rJson.put("range", aRange); rJson.put("colHeaders", bColHeaders); rJson.put("rowHeaders", bRowHeaders); - sctools::writeChartLook(rJson, rDoc.GetChartByName(aName)); + ChartHelper::WriteChartLook(rJson, rDoc.GetChartByName(aName)); } /// A repeated row or column span, 1-based.