tdf#132853 - UI: fix different sizes in Image tabs Type & Crop
[LibreOffice.git] / docmodel / source / color / ComplexColorJSON.cxx
blob7d5688d84af5d8bf7a2ed26fbb0fbbff9585dec2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <docmodel/color/ComplexColorJSON.hxx>
12 #include <boost/property_tree/json_parser.hpp>
13 #include <sstream>
14 #include <utility>
15 #include <sal/log.hxx>
16 #include <tools/json_writer.hxx>
18 namespace model::color
20 bool convertFromJSON(OString const& rJsonString, model::ComplexColor& rComplexColor)
22 model::ComplexColor aComplexColor;
24 try
26 std::stringstream aStream((std::string(rJsonString)));
27 boost::property_tree::ptree aRootTree;
28 boost::property_tree::read_json(aStream, aRootTree);
30 sal_Int32 nThemeType = aRootTree.get<sal_Int32>("ThemeIndex", -1);
31 aComplexColor.setThemeColor(model::convertToThemeColorType(nThemeType));
32 boost::property_tree::ptree aTransformTree = aRootTree.get_child("Transformations");
33 for (const auto& rEachTransformationNode :
34 boost::make_iterator_range(aTransformTree.equal_range("")))
36 auto const& rTransformationTree = rEachTransformationNode.second;
37 std::string sType = rTransformationTree.get<std::string>("Type", "");
38 sal_Int16 nValue = rTransformationTree.get<sal_Int16>("Value", 0);
40 auto eType = model::TransformationType::Undefined;
41 if (sType == "LumOff")
42 eType = model::TransformationType::LumOff;
43 else if (sType == "LumMod")
44 eType = model::TransformationType::LumMod;
45 else if (sType == "Tint")
46 eType = model::TransformationType::Tint;
47 else if (sType == "Shade")
48 eType = model::TransformationType::Shade;
50 if (eType != model::TransformationType::Undefined)
51 aComplexColor.addTransformation({ eType, nValue });
54 catch (const boost::property_tree::json_parser_error& /*exception*/)
56 return false;
59 rComplexColor = std::move(aComplexColor);
60 return true;
63 void convertToJSONTree(tools::JsonWriter& rTree, model::ComplexColor const& rComplexColor)
65 rTree.put("ThemeIndex", sal_Int16(rComplexColor.getThemeColorType()));
66 auto aTransformationsList = rTree.startArray("Transformations");
68 for (auto const& rTransformation : rComplexColor.getTransformations())
70 std::string aType;
71 switch (rTransformation.meType)
73 case model::TransformationType::LumMod:
74 aType = "LumMod";
75 break;
76 case model::TransformationType::LumOff:
77 aType = "LumOff";
78 break;
79 case model::TransformationType::Tint:
80 aType = "Tint";
81 break;
82 case model::TransformationType::Shade:
83 aType = "Shade";
84 break;
85 default:
86 break;
88 if (!aType.empty())
90 auto aChild = rTree.startStruct();
91 rTree.put("Type", aType);
92 rTree.put("Value", rTransformation.mnValue);
97 OString convertToJSON(model::ComplexColor const& rComplexColor)
99 tools::JsonWriter aTree;
100 convertToJSONTree(aTree, rComplexColor);
101 return aTree.finishAndGetAsOString();
104 } // end model::theme
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */