libodsstream
Library for mass spectrometry
Loading...
Searching...
No Matches
odscolorscale.cpp
Go to the documentation of this file.
1/**
2 * \file odsstream/writer/options/odscolorscale.cpp
3 * \date 10/03/2018
4 * \author Olivier Langella
5 * \brief structure to apply a color scale in ODS sheets
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2013 Olivier Langella <Olivier.Langella@u-psud.fr>.
10 *
11 * This file is part of the libodsstream library.
12 *
13 * libodsstream is a library to read and write ODS documents as streams
14 * Copyright (C) 2013 Olivier Langella <Olivier.Langella@u-psud.fr>
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published
18 *by the Free Software Foundation, either version 3 of the License, or (at your
19 *option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *
29 * Contributors:
30 * Olivier Langella <Olivier.Langella@u-psud.fr> - initial API and
31 *implementation
32 ******************************************************************************/
33
34#include "odscolorscale.h"
35#include "../../odsexception.h"
36#include "../structure/metaxml.h"
37#include <QDebug>
38#include <QRegularExpression>
39
40
41OdsColorScale::OdsColorScale(const QString &cell_start_position,
42 const QString &cell_stop_position)
43{
44 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
45 << cell_start_position;
46
47 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
48 << cell_stop_position;
49 setCellRange(cell_start_position, cell_stop_position);
50}
51
61
65
66/*
67 *
68</table:table-row>
69
70 <calcext:conditional-formats>
71</calcext:conditional-formats>
72
73</table:table>
74*/
75bool
76OdsColorScale::isInSheet(const QString &sheet_name)
77{
78 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
79 << sheet_name << " " << _sheet_name;
80 if(_sheet_name == sheet_name)
81 {
82 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " OK";
83 return true;
84 }
85 return false;
86}
87void
88OdsColorScale::writeConditionalFormat(QXmlStreamWriter *p_writer)
89{
90
91 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
92 QString sheet_name = _sheet_name;
93 if(sheet_name.contains(" "))
94 {
95 sheet_name = QString("&apos;%1&apos;").arg(sheet_name);
96 }
97
98
99 //<calcext:conditional-format
100 //calcext:target-range-address="Feuille1.C2:Feuille1.D4">
101 p_writer->writeStartElement(MetaXml::getNamespaceURI("calcext"),
102 "conditional-format");
103 p_writer->device()->write(
104 QString(" calcext:target-range-address=\"%1.%2:%1.%3\" ")
105 .arg(sheet_name)
108 .toUtf8());
109 // p_writer->writeAttribute(MetaXml::getNamespaceURI("calcext"),
110 // "target-range-address",QString("%1:%2").arg(cell_start_position).arg(cell_stop_position));
111
112 //<calcext:color-scale>
113 p_writer->writeStartElement(MetaXml::getNamespaceURI("calcext"),
114 "color-scale");
115 //<calcext:color-scale-entry calcext:value="0" calcext:type="minimum"
116 //calcext:color="#ff0000"/>
117 p_writer->writeStartElement(MetaXml::getNamespaceURI("calcext"),
118 "color-scale-entry");
119 p_writer->writeAttribute(MetaXml::getNamespaceURI("calcext"), "value", "0");
120 p_writer->writeAttribute(
121 MetaXml::getNamespaceURI("calcext"), "type", "minimum");
122 p_writer->writeAttribute(
123 MetaXml::getNamespaceURI("calcext"), "color", _minimum_color.name());
124 p_writer->writeEndElement();
125 //<calcext:color-scale-entry calcext:value="50" calcext:type="percentile"
126 //calcext:color="#ffffff"/>
127 p_writer->writeStartElement(MetaXml::getNamespaceURI("calcext"),
128 "color-scale-entry");
129 p_writer->writeAttribute(MetaXml::getNamespaceURI("calcext"), "value", "50");
130 p_writer->writeAttribute(
131 MetaXml::getNamespaceURI("calcext"), "type", "percentile");
132 p_writer->writeAttribute(
133 MetaXml::getNamespaceURI("calcext"), "color", _percentile_color.name());
134 p_writer->writeEndElement();
135 //<calcext:color-scale-entry calcext:value="0" calcext:type="maximum"
136 //calcext:color="#0000ff"/>
137 p_writer->writeStartElement(MetaXml::getNamespaceURI("calcext"),
138 "color-scale-entry");
139 p_writer->writeAttribute(MetaXml::getNamespaceURI("calcext"), "value", "0");
140 p_writer->writeAttribute(
141 MetaXml::getNamespaceURI("calcext"), "type", "maximum");
142 p_writer->writeAttribute(
143 MetaXml::getNamespaceURI("calcext"), "color", _maximum_color.name());
144 p_writer->writeEndElement();
145 //</calcext:color-scale>
146 p_writer->writeEndElement();
147 //</calcext:conditional-format>
148 p_writer->writeEndElement();
149 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
150}
151
152
153std::tuple<QString, QString>
154OdsColorScale::parseCellRange(const QString &cell_position)
155{
156 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
157 if(cell_position.isEmpty())
158 {
159 throw OdsException(
160 QObject::tr("error parsing cell range :\n empty cell position"));
161 }
162 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
163 << cell_position;
164 QRegularExpression reg_exp("^(.*)\\.([A-Z]+[0-9]+)$");
165 QRegularExpressionMatch reg_exp_match = reg_exp.match(cell_position);
166 if(reg_exp_match.hasMatch())
167 {
168 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
169 << cell_position;
170 return std::make_tuple(reg_exp_match.capturedTexts().at(1),
171 reg_exp_match.capturedTexts().at(2));
172 }
173 else
174 {
175 throw OdsException(
176 QObject::tr("error parsing cell range :\n cell_position %1 does not "
177 "contains pattern ^(.*).([A-Z]+[0-9]+)$")
178 .arg(cell_position));
179 }
180}
181
182void
183OdsColorScale::setCellRange(const QString &cell_start_position,
184 const QString &cell_stop_position)
185{
186 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
187 if(cell_start_position.isEmpty() && cell_stop_position.isEmpty())
188 return; // this is not an ods doc writer
189 QString sheet_name;
190 std::tie(sheet_name, _cell_start_position) =
191 parseCellRange(cell_start_position);
192 _sheet_name = sheet_name;
193
194 std::tie(sheet_name, _cell_stop_position) =
195 parseCellRange(cell_stop_position);
196 //"Feuille1.C2:Feuille1.D4"
197 if(sheet_name != _sheet_name)
198 {
199 throw OdsException(
200 QObject::tr("error in OdsColorScale::setCellRange :\n start %1 and "
201 "stop %2 are not on the same sheet")
202 .arg(cell_start_position)
203 .arg(cell_stop_position));
204 }
205 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
206}
207void
208OdsColorScale::setMinimumColor(const QColor &minimum_color)
209{
210 _minimum_color = minimum_color;
211}
212void
213OdsColorScale::setMaximumColor(const QColor &maximum_color)
214{
215 _maximum_color = maximum_color;
216}
static const QString & getNamespaceURI(const QString &xml_namespace)
Definition metaxml.cpp:32
QColor _maximum_color
bool isInSheet(const QString &sheet_name)
std::tuple< QString, QString > parseCellRange(const QString &cell_position)
OdsColorScale(const QString &cell_start_position, const QString &cell_stop_position)
QString _sheet_name
void setCellRange(const QString &cell_start_position, const QString &cell_stop_position)
set cell range to apply color scale
QString _cell_stop_position
void setMaximumColor(const QColor &maximum_color)
set maximum color of the color scale
QString _cell_start_position
void writeConditionalFormat(QXmlStreamWriter *p_writer)
QColor _percentile_color
virtual ~OdsColorScale()
QColor _minimum_color
void setMinimumColor(const QColor &minimum_color)
set minimum color of the color scale
structure to apply a color scale in ODS sheets