Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
ColorFilterSettings.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "CmdMediator.h"
8 #include "ColorConstants.h"
9 #include "ColorFilterSettings.h"
10 #include "ColorFilterSettingsStrategyAbstractBase.h"
11 #include "ColorFilterSettingsStrategyForeground.h"
12 #include "ColorFilterSettingsStrategyHue.h"
13 #include "ColorFilterSettingsStrategyIntensity.h"
14 #include "ColorFilterSettingsStrategySaturation.h"
15 #include "ColorFilterSettingsStrategyValue.h"
16 #include "DocumentSerialize.h"
17 #include "EngaugeAssert.h"
18 #include "GridCoordDisable.h"
19 #include "Logger.h"
20 #include <QTextStream>
21 #include <QXmlStreamWriter>
22 #include "Xml.h"
23 
25  m_colorFilterMode (COLOR_FILTER_MODE_INTENSITY),
26  m_intensityLow (INTENSITY_LOW_DEFAULT),
27  m_intensityHigh (INTENSITY_HIGH_DEFAULT),
28  m_foregroundLow (FOREGROUND_LOW_DEFAULT),
29  m_foregroundHigh (FOREGROUND_HIGH_DEFAULT),
30  m_hueLow (HUE_LOW_DEFAULT),
31  m_hueHigh (HUE_HIGH_DEFAULT),
32  m_saturationLow (SATURATION_LOW_DEFAULT),
33  m_saturationHigh (SATURATION_HIGH_DEFAULT),
34  m_valueLow (VALUE_LOW_DEFAULT),
35  m_valueHigh (VALUE_HIGH_DEFAULT)
36 {
37  createStrategies ();
38 }
39 
40 ColorFilterSettings::ColorFilterSettings(ColorFilterMode colorFilterMode,
41  int intensityLow,
42  int intensityHigh,
43  int foregroundLow,
44  int foregroundHigh,
45  int hueLow,
46  int hueHigh,
47  int saturationLow,
48  int saturationHigh,
49  int valueLow,
50  int valueHigh) :
51  m_colorFilterMode (colorFilterMode),
52  m_intensityLow (intensityLow),
53  m_intensityHigh (intensityHigh),
54  m_foregroundLow (foregroundLow),
55  m_foregroundHigh (foregroundHigh),
56  m_hueLow (hueLow),
57  m_hueHigh (hueHigh),
58  m_saturationLow (saturationLow),
59  m_saturationHigh (saturationHigh),
60  m_valueLow (valueLow),
61  m_valueHigh (valueHigh)
62 {
63  createStrategies ();
64 }
65 
67  m_colorFilterMode (other.colorFilterMode()),
68  m_intensityLow (other.intensityLow()),
69  m_intensityHigh (other.intensityHigh()),
70  m_foregroundLow (other.foregroundLow()),
71  m_foregroundHigh (other.foregroundHigh()),
72  m_hueLow (other.hueLow()),
73  m_hueHigh (other.hueHigh()),
74  m_saturationLow (other.saturationLow()),
75  m_saturationHigh (other.saturationHigh()),
76  m_valueLow (other.valueLow()),
77  m_valueHigh (other.valueHigh())
78 {
79  createStrategies ();
80 }
81 
83 {
84  loadXml(reader);
85  createStrategies ();
86 }
87 
89 {
90  m_colorFilterMode = other.colorFilterMode();
91  m_intensityLow = other.intensityLow();
92  m_intensityHigh = other.intensityHigh();
93  m_foregroundLow = other.foregroundLow();
94  m_foregroundHigh = other.foregroundHigh();
95  m_hueLow = other.hueLow();
96  m_hueHigh = other.hueHigh();
97  m_saturationLow = other.saturationLow();
98  m_saturationHigh = other.saturationHigh();
99  m_valueLow = other.valueLow();
100  m_valueHigh = other.valueHigh();
101 
102  createStrategies ();
103 
104  return *this;
105 }
106 
108 {
109  return m_colorFilterMode;
110 }
111 
112 void ColorFilterSettings::createStrategies ()
113 {
114  m_strategies [COLOR_FILTER_MODE_FOREGROUND] = new ColorFilterSettingsStrategyForeground ();
115  m_strategies [COLOR_FILTER_MODE_HUE ] = new ColorFilterSettingsStrategyHue ();
116  m_strategies [COLOR_FILTER_MODE_INTENSITY ] = new ColorFilterSettingsStrategyIntensity ();
117  m_strategies [COLOR_FILTER_MODE_SATURATION] = new ColorFilterSettingsStrategySaturation ();
118  m_strategies [COLOR_FILTER_MODE_VALUE ] = new ColorFilterSettingsStrategyValue ();
119 }
120 
122 {
123  return ColorFilterSettings ();
124 }
125 
127 {
128  return m_foregroundHigh;
129 }
130 
132 {
133  return m_foregroundLow;
134 }
135 
137 {
138  if (m_strategies.contains (m_colorFilterMode)) {
139  return m_strategies [m_colorFilterMode]->high (*this);
140  } else {
141  ENGAUGE_ASSERT (false);
142  return m_strategies [COLOR_FILTER_MODE_INTENSITY]->high (*this);
143  }
144 }
145 
147 {
148  return m_hueHigh;
149 }
150 
152 {
153  return m_hueLow;
154 }
155 
157 {
158  return m_intensityHigh;
159 }
160 
162 {
163  return m_intensityLow;
164 }
165 
166 void ColorFilterSettings::loadXml(QXmlStreamReader &reader)
167 {
168  LOG4CPP_INFO_S ((*mainCat)) << "ColorFilterSettings::loadXml";
169 
170  bool success = true;
171 
172  QXmlStreamAttributes attributes = reader.attributes();
173 
174  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_MODE) &&
175  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_INTENSITY_LOW) &&
176  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_INTENSITY_HIGH) &&
177  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_FOREGROUND_LOW) &&
178  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_FOREGROUND_HIGH) &&
179  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_HUE_LOW) &&
180  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_HUE_HIGH) &&
181  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_SATURATION_LOW) &&
182  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_SATURATION_HIGH) &&
183  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_VALUE_LOW) &&
184  attributes.hasAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_VALUE_HIGH)) {
185 
186  setColorFilterMode ((ColorFilterMode) attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_MODE).toInt());
187  setIntensityLow (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_INTENSITY_LOW).toInt());
188  setIntensityHigh ((GridCoordDisable) attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_INTENSITY_HIGH).toInt());
189  setForegroundLow (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_FOREGROUND_LOW).toInt());
190  setForegroundHigh (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_FOREGROUND_HIGH).toInt());
191  setHueLow (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_HUE_LOW).toInt());
192  setHueHigh (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_HUE_HIGH).toInt());
193  setSaturationLow ((GridCoordDisable) attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_SATURATION_LOW).toInt());
194  setSaturationHigh (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_SATURATION_HIGH).toInt());
195  setValueLow (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_VALUE_LOW).toInt());
196  setValueHigh (attributes.value(DOCUMENT_SERIALIZE_COLOR_FILTER_VALUE_HIGH).toInt());
197 
198  // Read until end of this subtree
199  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
200  (reader.name() != DOCUMENT_SERIALIZE_COLOR_FILTER)){
201  loadNextFromReader(reader);
202 
203  if (reader.atEnd()) {
204  success = false;
205  break;
206  }
207  }
208  }
209 
210  if (!success) {
211  reader.raiseError (QObject::tr ("Cannot read curve filter data"));
212  }
213 }
214 
216 {
217  if (m_strategies.contains (m_colorFilterMode)) {
218  return m_strategies [m_colorFilterMode]->low (*this);
219  } else {
220  ENGAUGE_ASSERT (false);
221  return m_strategies [COLOR_FILTER_MODE_INTENSITY]->low (*this);
222  }
223 }
224 
225 void ColorFilterSettings::printStream (QString indentation,
226  QTextStream &str) const
227 {
228  str << indentation << "ColorFilterSettings\n";
229 
230  indentation += INDENTATION_DELTA;
231 
232  if (m_strategies.contains (m_colorFilterMode)) {
233  return m_strategies [m_colorFilterMode]->printStream (*this,
234  indentation,
235  str);
236  }
237 }
238 
240 {
241  return m_saturationHigh;
242 }
243 
245 {
246  return m_saturationLow;
247 }
248 
249 void ColorFilterSettings::saveXml(QXmlStreamWriter &writer,
250  const QString &curveName) const
251 {
252  LOG4CPP_INFO_S ((*mainCat)) << "ColorFilterSettings::saveXml";
253 
254  writer.writeStartElement(DOCUMENT_SERIALIZE_COLOR_FILTER);
255  writer.writeAttribute(DOCUMENT_SERIALIZE_CURVE_NAME, curveName);
256  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_MODE, QString::number (m_colorFilterMode));
257  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_MODE_STRING, colorFilterModeToString (m_colorFilterMode));
258  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_INTENSITY_LOW, QString::number (m_intensityLow));
259  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_INTENSITY_HIGH, QString::number (m_intensityHigh));
260  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_FOREGROUND_LOW, QString::number (m_foregroundLow));
261  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_FOREGROUND_HIGH, QString::number (m_foregroundHigh));
262  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_HUE_LOW, QString::number (m_hueLow));
263  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_HUE_HIGH, QString::number (m_hueHigh));
264  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_SATURATION_LOW, QString::number (m_saturationLow));
265  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_SATURATION_HIGH, QString::number (m_saturationHigh));
266  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_VALUE_LOW, QString::number (m_valueLow));
267  writer.writeAttribute(DOCUMENT_SERIALIZE_COLOR_FILTER_VALUE_HIGH, QString::number (m_valueHigh));
268  writer.writeEndElement();
269 }
270 
271 void ColorFilterSettings::setColorFilterMode(ColorFilterMode colorFilterMode)
272 {
273  m_colorFilterMode = colorFilterMode;
274 }
275 
276 void ColorFilterSettings::setForegroundHigh (int foregroundHigh)
277 {
278  ENGAUGE_ASSERT (FOREGROUND_MIN <= foregroundHigh && foregroundHigh <= FOREGROUND_MAX);
279  m_foregroundHigh = foregroundHigh;
280 }
281 
283 {
284  ENGAUGE_ASSERT (FOREGROUND_MIN <= foregroundLow && foregroundLow <= FOREGROUND_MAX);
285  m_foregroundLow = foregroundLow;
286 }
287 
288 void ColorFilterSettings::setHigh (double s0To1)
289 {
290  if (m_strategies.contains (m_colorFilterMode)) {
291  return m_strategies [m_colorFilterMode]->setHigh (*this,
292  s0To1);
293  } else {
294  ENGAUGE_ASSERT (false);
295  }
296 }
297 
299 {
300  ENGAUGE_ASSERT (HUE_MIN <= hueHigh && hueHigh <= HUE_MAX);
301  m_hueHigh = hueHigh;
302 }
303 
305 {
306  ENGAUGE_ASSERT (HUE_MIN <= hueLow && hueLow <= HUE_MAX);
307  m_hueLow = hueLow;
308 }
309 
311 {
312  ENGAUGE_ASSERT (INTENSITY_MIN <= intensityHigh && intensityHigh <= INTENSITY_MAX);
313  m_intensityHigh = intensityHigh;
314 }
315 
317 {
318  ENGAUGE_ASSERT (INTENSITY_MIN <= intensityLow && intensityLow <= INTENSITY_MAX);
319  m_intensityLow = intensityLow;
320 }
321 
322 void ColorFilterSettings::setLow (double s0To1)
323 {
324  if (m_strategies.contains (m_colorFilterMode)) {
325  return m_strategies [m_colorFilterMode]->setLow (*this,
326  s0To1);
327  } else {
328  ENGAUGE_ASSERT (false);
329  }
330 }
331 
332 void ColorFilterSettings::setSaturationHigh (int saturationHigh)
333 {
334  ENGAUGE_ASSERT (SATURATION_MIN <= saturationHigh && saturationHigh <= SATURATION_MAX);
335  m_saturationHigh = saturationHigh;
336 }
337 
339 {
340  ENGAUGE_ASSERT (SATURATION_MIN <= saturationLow && saturationLow <= SATURATION_MAX);
341  m_saturationLow = saturationLow;
342 }
343 
345 {
346  ENGAUGE_ASSERT (VALUE_MIN <= valueHigh && valueHigh <= VALUE_MAX);
347  m_valueHigh = valueHigh;
348 }
349 
351 {
352  ENGAUGE_ASSERT (VALUE_MIN <= valueLow && valueLow <= VALUE_MAX);
353  m_valueLow = valueLow;
354 }
355 
357 {
358  return m_valueHigh;
359 }
360 
362 {
363  return m_valueLow;
364 }
void setSaturationLow(int saturationLow)
Set method for saturation low.
void setLow(double s0To1)
Set the low value for the current filter mode.
void setHueLow(int hueLow)
Set method for hue lower bound.
int saturationLow() const
Get method for saturation lower bound.
Leaf class for hue strategy for ColorFilterSettings.
Color filter parameters for one curve. For a class, this is handled the same as LineStyle and PointSt...
Leaf class for saturation strategy for ColorFilterSettings.
void setColorFilterMode(ColorFilterMode colorFilterMode)
Set method for filter mode.
int hueLow() const
Get method for hue lower bound.
int foregroundHigh() const
Get method for foreground higher bound.
int hueHigh() const
Get method for hue higher bound.
int saturationHigh() const
Get method for saturation higher bound.
ColorFilterSettings & operator=(const ColorFilterSettings &other)
Assignment operator.
void setForegroundLow(int foregroundLow)
Set method for foreground lower bound.
void setHigh(double s0To1)
Set the high value for the current filter mode.
int foregroundLow() const
Get method for foreground lower bound.
void setHueHigh(int hueHigh)
Set method for hue higher bound.
ColorFilterMode colorFilterMode() const
Get method for filter mode.
int valueLow() const
Get method for value low.
void setForegroundHigh(int foregroundHigh)
Set method for foreground higher bound.
Leaf class for value strategy for ColorFilterSettings.
void saveXml(QXmlStreamWriter &writer, const QString &curveName) const
Save curve filter to stream.
static ColorFilterSettings defaultFilter()
Initial default for any Curve.
void setIntensityHigh(int intensityHigh)
Set method for intensity higher bound.
Leaf class for intensity strategy for ColorFilterSettings.
int intensityHigh() const
Get method for intensity higher bound.
void setValueHigh(int valueHigh)
Set method for value high.
void setIntensityLow(int intensityLow)
Set method for intensity lower bound.
int valueHigh() const
Get method for value high.
double low() const
Low value of foreground, hue, intensity, saturation or value according to current filter mode...
void setValueLow(int valueLow)
Set method for value low.
void setSaturationHigh(int saturationHigh)
Set method for saturation high.
void loadXml(QXmlStreamReader &reader)
Load curve filter to stream.
Leaf class for foreground strategy for ColorFilterSettings.
double high() const
High value of foreground, hue, intensity, saturation or value according to current filter mode...
ColorFilterSettings()
Default constructor only for use when this class is being stored by a container that requires the def...
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
int intensityLow() const
Get method for intensity lower bound.