1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "XMLColumnRowGroupExport.hxx"
21 #include "xmlexprt.hxx"
22 #include <xmloff/nmspmap.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <xmloff/xmlnmspe.hxx>
25 
26 #include <algorithm>
27 
28 using namespace xmloff::token;
29 
ScMyColumnRowGroup()30 ScMyColumnRowGroup::ScMyColumnRowGroup()
31     : nField(0)
32     , nLevel(0)
33     , bDisplay(false)
34 {
35 }
36 
operator <(const ScMyColumnRowGroup & rGroup) const37 bool ScMyColumnRowGroup::operator<(const ScMyColumnRowGroup& rGroup) const
38 {
39     if (rGroup.nField > nField)
40         return true;
41     else
42         if (rGroup.nField == nField && rGroup.nLevel > nLevel)
43             return true;
44         else
45             return false;
46 }
47 
ScMyOpenCloseColumnRowGroup(ScXMLExport & rTempExport,sal_uInt32 nToken)48 ScMyOpenCloseColumnRowGroup::ScMyOpenCloseColumnRowGroup(ScXMLExport& rTempExport, sal_uInt32 nToken)
49     : rExport(rTempExport),
50     rName(rExport.GetNamespaceMap().GetQNameByKey(XML_NAMESPACE_TABLE, GetXMLToken(XMLTokenEnum(nToken)))),
51     aTableStart(),
52     aTableEnd()
53 {
54 }
55 
~ScMyOpenCloseColumnRowGroup()56 ScMyOpenCloseColumnRowGroup::~ScMyOpenCloseColumnRowGroup()
57 {
58 }
59 
NewTable()60 void ScMyOpenCloseColumnRowGroup::NewTable()
61 {
62     aTableStart.clear();
63     aTableEnd.clear();
64 }
65 
AddGroup(const ScMyColumnRowGroup & aGroup,const sal_Int32 nEndField)66 void ScMyOpenCloseColumnRowGroup::AddGroup(const ScMyColumnRowGroup& aGroup, const sal_Int32 nEndField)
67 {
68     aTableStart.push_back(aGroup);
69     aTableEnd.push_back(nEndField);
70 }
71 
IsGroupStart(const sal_Int32 nField)72 bool ScMyOpenCloseColumnRowGroup::IsGroupStart(const sal_Int32 nField)
73 {
74     bool bGroupStart(false);
75     if (!aTableStart.empty())
76     {
77         //  when used to find repeated rows at the beginning of a group,
78         //  aTableStart may contain entries before nField. They must be skipped here
79         //  (they will be used for OpenGroups later in the right order).
80 
81         ScMyColumnRowGroupVec::iterator aItr = std::find_if_not(aTableStart.begin(), aTableStart.end(),
82             [&nField](const ScMyColumnRowGroup& rGroup) { return rGroup.nField < nField; });
83         bGroupStart = (aItr != aTableStart.end()) && (aItr->nField == nField);
84     }
85     return bGroupStart;
86 }
87 
OpenGroup(const ScMyColumnRowGroup & rGroup)88 void ScMyOpenCloseColumnRowGroup::OpenGroup(const ScMyColumnRowGroup& rGroup)
89 {
90     if (!rGroup.bDisplay)
91         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DISPLAY, XML_FALSE);
92     rExport.StartElement( rName, true);
93 }
94 
OpenGroups(const sal_Int32 nField)95 void ScMyOpenCloseColumnRowGroup::OpenGroups(const sal_Int32 nField)
96 {
97     ScMyColumnRowGroupVec::iterator aItr(aTableStart.begin());
98     bool bReady(false);
99     while(!bReady && aItr != aTableStart.end())
100     {
101         if (aItr->nField == nField)
102         {
103             OpenGroup(*aItr);
104             aItr = aTableStart.erase(aItr);
105         }
106         else
107             bReady = true;
108     }
109 }
110 
IsGroupEnd(const sal_Int32 nField)111 bool ScMyOpenCloseColumnRowGroup::IsGroupEnd(const sal_Int32 nField)
112 {
113     return (!aTableEnd.empty()) && (aTableEnd.front() == nField);
114 }
115 
CloseGroups(const sal_Int32 nField)116 void ScMyOpenCloseColumnRowGroup::CloseGroups(const sal_Int32 nField)
117 {
118     ScMyFieldGroupVec::iterator aItr(aTableEnd.begin());
119     bool bReady(false);
120     while(!bReady && aItr != aTableEnd.end())
121     {
122         if (*aItr == nField)
123         {
124             rExport.EndElement( rName, true );
125             aItr = aTableEnd.erase(aItr);
126         }
127         else
128             bReady = true;
129     }
130 }
131 
GetLast()132 sal_Int32 ScMyOpenCloseColumnRowGroup::GetLast()
133 {
134     sal_Int32 maximum(-1);
135     if (!aTableEnd.empty())
136     {
137         ScMyFieldGroupVec::iterator i(std::max_element(aTableEnd.begin(), aTableEnd.end()));
138         if (*i > maximum)
139             maximum = *i;
140     }
141     return maximum;
142 }
143 
Sort()144 void ScMyOpenCloseColumnRowGroup::Sort()
145 {
146     std::sort(aTableStart.begin(), aTableStart.end());
147     std::sort(aTableEnd.begin(), aTableEnd.end());
148 }
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
151