summaryrefslogtreecommitdiffstats
path: root/sc/source/filter/oox/SparklineFragment.cxx
blob: 54ef0df3c6ec8f680bed6e59f8d2f1c0024a0479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- 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 <SparklineFragment.hxx>
#include <oox/core/contexthandler.hxx>
#include <oox/token/tokens.hxx>
#include <oox/token/namespaces.hxx>
#include <oox/helper/helper.hxx>
#include <oox/helper/attributelist.hxx>
#include <document.hxx>
#include <rangeutl.hxx>
#include <Sparkline.hxx>
#include <themebuffer.hxx>

using ::oox::core::ContextHandlerRef;

namespace oox::xls
{
namespace
{
// TODO: deduplicate with importOOXColor
::Color getColor(const AttributeList& rAttribs, ThemeBuffer const& rThemeBuffer)
{
    if (rAttribs.hasAttribute(XML_rgb))
    {
        return ::Color(ColorAlpha, rAttribs.getIntegerHex(XML_rgb, sal_Int32(API_RGB_TRANSPARENT)));
    }
    else if (rAttribs.hasAttribute(XML_theme))
    {
        sal_uInt32 nThemeIndex = rAttribs.getUnsigned(XML_theme, 0);

        // Excel has a bug in the mapping of index 0, 1, 2 and 3.
        if (nThemeIndex == 0)
            nThemeIndex = 1;
        else if (nThemeIndex == 1)
            nThemeIndex = 0;
        else if (nThemeIndex == 2)
            nThemeIndex = 3;
        else if (nThemeIndex == 3)
            nThemeIndex = 2;

        ::Color aColor = rThemeBuffer.getColorByIndex(nThemeIndex);
        double nTint = rAttribs.getDouble(XML_tint, 0.0);

        if (nTint > 0.0)
            aColor.ApplyTintOrShade(nTint * 10000);
        return aColor;
    }

    return ::Color();
}

void addColorsToSparklineGroup(sc::SparklineGroup& rSparklineGroup, sal_Int32 nElement,
                               const AttributeList& rAttribs, const ThemeBuffer& rThemeBuffer)
{
    switch (nElement)
    {
        case XLS14_TOKEN(colorSeries):
            rSparklineGroup.m_aColorSeries = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorNegative):
            rSparklineGroup.m_aColorNegative = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorAxis):
            rSparklineGroup.m_aColorAxis = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorMarkers):
            rSparklineGroup.m_aColorMarkers = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorFirst):
            rSparklineGroup.m_aColorFirst = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorLast):
            rSparklineGroup.m_aColorLast = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorHigh):
            rSparklineGroup.m_aColorHigh = getColor(rAttribs, rThemeBuffer);
            break;
        case XLS14_TOKEN(colorLow):
            rSparklineGroup.m_aColorLow = getColor(rAttribs, rThemeBuffer);
            break;
        default:
            break;
    }
}

void addAttributesToSparklineGroup(sc::SparklineGroup& rSparklineGroup,
                                   const AttributeList& rAttribs)
{
    auto oManualMax = rAttribs.getDouble(XML_manualMax);
    auto oManualMin = rAttribs.getDouble(XML_manualMin);

    rSparklineGroup.m_fLineWeight = rAttribs.getDouble(XML_lineWeight, 0.75);

    rSparklineGroup.m_sType = rAttribs.getString(XML_type, "line");

    rSparklineGroup.m_bDateAxis = rAttribs.getBool(XML_dateAxis, false);

    rSparklineGroup.m_sDisplayEmptyCellsAs = rAttribs.getString(XML_displayEmptyCellsAs, "zero");

    rSparklineGroup.m_bMarkers = rAttribs.getBool(XML_markers, false);
    rSparklineGroup.m_bHigh = rAttribs.getBool(XML_high, false);
    rSparklineGroup.m_bLow = rAttribs.getBool(XML_low, false);
    rSparklineGroup.m_bFirst = rAttribs.getBool(XML_first, false);
    rSparklineGroup.m_bLast = rAttribs.getBool(XML_last, false);
    rSparklineGroup.m_bNegative = rAttribs.getBool(XML_negative, false);
    rSparklineGroup.m_bDisplayXAxis = rAttribs.getBool(XML_displayXAxis, false);
    rSparklineGroup.m_bDisplayHidden = rAttribs.getBool(XML_displayHidden, false);

    rSparklineGroup.m_sMinAxisType = rAttribs.getString(XML_minAxisType, "individual");
    rSparklineGroup.m_sMaxAxisType = rAttribs.getString(XML_maxAxisType, "individual");

    rSparklineGroup.m_bRightToLeft = rAttribs.getBool(XML_rightToLeft, false);

    rSparklineGroup.m_sUID = rAttribs.getString(XML_uid, OUString());

    if (rSparklineGroup.m_sMaxAxisType == "custom")
        rSparklineGroup.m_aManualMax = oManualMax.get();
    if (rSparklineGroup.m_sMinAxisType == "custom")
        rSparklineGroup.m_aManualMin = oManualMin.get();
}

} // end anonymous namespace

SparklineGroupsContext::SparklineGroupsContext(WorksheetContextBase& rFragment)
    : WorksheetContextBase(rFragment)
{
}

ContextHandlerRef SparklineGroupsContext::onCreateContext(sal_Int32 nElement,
                                                          const AttributeList& rAttribs)
{
    switch (nElement)
    {
        case XLS14_TOKEN(sparklineGroup):
        {
            auto& rLastGroup = m_aSparklineGroups.emplace_back();
            addAttributesToSparklineGroup(*rLastGroup.getSparklineGroup(), rAttribs);
            return this;
        }
        case XLS14_TOKEN(colorSeries):
        case XLS14_TOKEN(colorNegative):
        case XLS14_TOKEN(colorAxis):
        case XLS14_TOKEN(colorMarkers):
        case XLS14_TOKEN(colorFirst):
        case XLS14_TOKEN(colorLast):
        case XLS14_TOKEN(colorHigh):
        case XLS14_TOKEN(colorLow):
        {
            auto& rLastGroup = m_aSparklineGroups.back();
            addColorsToSparklineGroup(*rLastGroup.getSparklineGroup(), nElement, rAttribs,
                                      getTheme());
            return this;
        }
        case XLS14_TOKEN(sparklines):
        {
            return this;
        }
        case XLS14_TOKEN(sparkline):
        {
            auto& rLastGroup = m_aSparklineGroups.back();
            rLastGroup.getSparklines().emplace_back();
            return this;
        }
    }
    return this;
}

void SparklineGroupsContext::onStartElement(const AttributeList&) {}

void SparklineGroupsContext::onCharacters(const OUString& rChars)
{
    if (getCurrentElement() == XM_TOKEN(sqref) || getCurrentElement() == XM_TOKEN(f))
    {
        ScDocument& rDocument = getScDocument();
        auto& rLastGroup = m_aSparklineGroups.back();
        auto& rLastSparkline = rLastGroup.getSparklines().back();
        ScRangeList aRange;
        if (ScRangeStringConverter::GetRangeListFromString(aRange, rChars, rDocument,
                                                           formula::FormulaGrammar::CONV_XL_OOX))
        {
            if (!aRange.empty())
            {
                if (getCurrentElement() == XM_TOKEN(sqref))
                {
                    rLastSparkline.m_aTargetRange = aRange;

                    // Need to set the current sheet index to the range as
                    // it is assumed that the address string refers to
                    // the current sheet and is not defined in the string.
                    for (auto& rRange : rLastSparkline.m_aTargetRange)
                    {
                        rRange.aStart.SetTab(getSheetIndex());
                        rRange.aEnd.SetTab(getSheetIndex());
                    }
                }
                else if (getCurrentElement() == XM_TOKEN(f))
                    rLastSparkline.m_aInputRange = aRange;
            }
        }
    }
}

void SparklineGroupsContext::onEndElement()
{
    if (getCurrentElement() == XLS14_TOKEN(sparklineGroup))
    {
        auto& rLastGroup = m_aSparklineGroups.back();
        for (Sparkline& rSparkline : rLastGroup.getSparklines())
        {
            insertSparkline(rLastGroup, rSparkline);
        }
    }
}

void SparklineGroupsContext::insertSparkline(SparklineGroup& rSparklineGroup, Sparkline& rSparkline)
{
    auto& rDocument = getScDocument();
    if (rSparkline.m_aTargetRange.size() == 1)
    {
        auto& rRange = rSparkline.m_aTargetRange[0];
        if (rRange.aStart == rRange.aEnd)
        {
            auto pSparklineGroup = rSparklineGroup.getSparklineGroup();
            auto* pCreated = rDocument.CreateSparkline(rRange.aStart, pSparklineGroup);
            pCreated->setInputRange(rSparkline.m_aInputRange);
        }
    }
}

} //namespace oox::xls

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */