All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BasicStrokePropertyItem.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/se/BasicStrokePropertyItem.cpp
22 
23  \brief A widget used to define the basic fill se object.
24 */
25 
26 // TerraLib
27 #include "../../../se/Config.h"
28 #include "../../../se/Stroke.h"
29 #include "../../../se/SvgParameter.h"
30 #include "../../../se/Utils.h"
31 #include "../../../maptools/Utils.h"
32 #include "../../../maptools/Enums.h"
33 #include "../propertybrowser/AbstractPropertyManager.h"
35 
36 // Qt
37 #include <QPainter>
38 #include <QIcon>
39 
40 // QtPropertyBrowser
41 #include <QtPropertyBrowser/QtTreePropertyBrowser>
42 #include <QtPropertyBrowser/QtVariantPropertyManager>
43 
44 // STL
45 #include <cassert>
46 
47 
48 te::qt::widgets::BasicStrokePropertyItem::BasicStrokePropertyItem(QtTreePropertyBrowser* pb, QColor c) : te::qt::widgets::AbstractPropertyItem(pb, c),
49  m_stroke(new te::se::Stroke)
50 {
51  //build property browser basic stroke
52  QtProperty* basicStrokeProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_groupManager->addProperty(tr("Basic Stroke"));
53 
54  //color
55  m_colorProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_colorManager->addProperty(tr("Color"));
57  basicStrokeProperty->addSubProperty(m_colorProperty);
58 
59  //opacity
60  m_opacityProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->addProperty(tr("Opacity"));
63  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setMaximum(m_opacityProperty, 100);
64  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setSingleStep(m_opacityProperty, 10);
65  basicStrokeProperty->addSubProperty(m_opacityProperty);
66 
67  //width
68  m_widthProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_doubleManager->addProperty(tr("Width"));
74  basicStrokeProperty->addSubProperty(m_widthProperty);
75 
76  //dash
77  m_dashProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->addProperty(tr("Dash"));
80  basicStrokeProperty->addSubProperty(m_dashProperty);
81 
82  //join
83  m_joinProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->addProperty(tr("Join"));
86  basicStrokeProperty->addSubProperty(m_joinProperty);
87 
88  //cap
89  m_capProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->addProperty(tr("Cap"));
92  basicStrokeProperty->addSubProperty(m_capProperty);
93 
94  addProperty(basicStrokeProperty, tr("Basic Stroke"), QColor(175, 255, 175));
95 
96  m_update = true;
97 }
98 
100 {
101  delete m_stroke;
102 }
103 
105 {
106  assert(stroke);
107 
108  delete m_stroke;
109 
110  m_stroke = stroke->clone();
111 
112  updateUi();
113 }
114 
116 {
117  return m_stroke->clone();
118 }
119 
121 {
122  // Color
124  te::se::GetColor(m_stroke, rgba);
125  m_color = QColor(rgba.getRgba());
126  m_color.setAlpha(rgba.getAlpha());
127  updateUiStrokeColor();
128 
129  // Opacity
130  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setValue(m_opacityProperty, m_color.alphaF() * 100);
131 
132  // Width
133  const te::se::SvgParameter* width = m_stroke->getWidth();
134  if(width)
136 
137  // Dash style
138  const te::se::SvgParameter* dasharray = m_stroke->getDashArray();
139  if(dasharray)
140  {
141  std::string str = te::se::GetString(dasharray);
142  for(size_t t = 0; t < m_dashes.size(); ++t)
143  {
144  if(str == m_dashes[t])
145  {
147  break;
148  }
149  }
150  }
151  else
152  {
154  }
155 
156  // Join Style
157  const te::se::SvgParameter* join = m_stroke->getLineJoin();
158  if(join)
159  {
160  std::string joinStr = te::se::GetString(join);
161 
162  std::map<int, std::string>::iterator it = m_joinMap.begin();
163 
164  while(it != m_joinMap.end())
165  {
166  if(it->second == joinStr)
167  {
168  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_joinProperty, it->first);
169  }
170 
171  ++it;
172  }
173  }
174  else
175  {
176  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_joinProperty, 0); //TE_SE_MITRE_JOIN
177  }
178 
179  // Cap Style
180  const te::se::SvgParameter* cap = m_stroke->getLineCap();
181  if(cap)
182  {
183  std::string capStr = te::se::GetString(cap);
184 
185  std::map<int, std::string>::iterator it = m_capMap.begin();
186 
187  while(it != m_capMap.end())
188  {
189  if(it->second == capStr)
190  {
191  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_capProperty, it->first);
192  }
193 
194  ++it;
195  }
196  }
197  else
198  {
199  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_capProperty, 0); //TE_SE_BUTT_CAP
200  }
201 }
202 
204 {
205  te::qt::widgets::AbstractPropertyManager::getInstance().m_colorManager->setValue(m_colorProperty, m_color);
206 }
207 
209 {
210  if(!m_update)
211  return;
212 
213  m_update = false;
214 
215  if(p == m_opacityProperty)
216  {
217  double opacity = value / 100.0;
218 
219  m_color.setAlpha(opacity * 255);
220  updateUiStrokeColor();
221 
222  // Updating stroke opacity
223  m_stroke->setOpacity(QString::number(opacity, 'g', 2).toStdString());
224  emit strokeChanged();
225  }
226  else if(p == m_dashProperty)
227  {
228  if(value == -1)
229  return;
230 
231  std::string pattern = m_dashes[value];
232  m_stroke->setDashArray(pattern);
233  emit strokeChanged();
234  }
235  else if(p == m_joinProperty)
236  {
237  m_stroke->setLineJoin(m_joinMap[value]);
238 
239  emit strokeChanged();
240  }
241  else if(p == m_capProperty)
242  {
243  m_stroke->setLineCap(m_capMap[value]);
244 
245  emit strokeChanged();
246  }
247 
248  m_update = true;
249 }
250 
252 {
253  if(!m_update)
254  return;
255 
256  m_update = false;
257 
258  if(p == m_widthProperty)
259  {
260  QString str;
261  str.setNum(value);
262 
263  m_stroke->setWidth(str.toStdString());
264  emit strokeChanged();
265  }
266 
267  m_update = true;
268 }
269 
270 void te::qt::widgets::BasicStrokePropertyItem::valueChanged(QtProperty *p, const QColor &value)
271 {
272  if(!m_update)
273  return;
274 
275  m_update = false;
276 
277  if(p == m_colorProperty)
278  {
279  // The new stroke color
280  m_color.setRgb(value.red(), value.green(), value.blue(), value.alpha());
281 
282  int opacity = (value.alpha() / 255.) * 100.;
283 
284  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setValue(m_opacityProperty, opacity);
285 
286  updateUiStrokeColor();
287 
288  // Updating stroke color
289  m_stroke->setColor(m_color.name().toStdString());
290  m_stroke->setOpacity(QString::number(opacity, 'g', 2).toStdString());
291  emit strokeChanged();
292  }
293 
294  m_update = true;
295 }
296 
298 {
299  QMap<int, QIcon> map;
300 
301  // Dash graphical representation size
302  QSize size(32, 16);
303 
304  // Line that will be draw with the dashe styles
305  QLine line(0, size.height() * 0.5, size.width(), size.height() * 0.5);
306 
307  // Setuping...
308  QPen pen;
309  pen.setWidth(2);
310 
311  for(size_t t = 0; t < m_dashes.size(); ++t)
312  {
313  QPixmap pixmap(size);
314  pixmap.fill(Qt::transparent);
315 
316  QPainter painter(&pixmap);
317 
318  std::string pattern = m_dashes[t];
319  std::vector<double> dasharray;
320  te::map::GetDashStyle(pattern, dasharray);
321 
322  QVector<qreal> qdasharray = QVector<qreal>::fromStdVector(dasharray);
323 
324  pen.setDashPattern(qdasharray);
325  painter.setPen(pen);
326  painter.drawLine(line);
327 
328  QIcon icon(pixmap);
329 
330  map[t] = icon;
331  }
332 
333  return map;
334 }
335 
337 {
338  QMap<int, QIcon> map;
339 
340  map[0] = QIcon::fromTheme("stroke-join-miter").pixmap(16,16);
341  map[1] = QIcon::fromTheme("stroke-join-round").pixmap(16,16);
342  map[2] = QIcon::fromTheme("stroke-join-bevel").pixmap(16,16);
343 
344  return map;
345 }
346 
348 {
349  QMap<int, QIcon> map;
350 
351  map[0] = QIcon::fromTheme("stroke-cap-butt").pixmap(16,16);
352  map[1] = QIcon::fromTheme("stroke-cap-round").pixmap(16,16);
353  map[2] = QIcon::fromTheme("stroke-cap-square").pixmap(16,16);
354 
355  return map;
356 }
357 
359 {
360  m_dashes.push_back("");
361  m_dashes.push_back("1 2");
362  m_dashes.push_back("4 2");
363  m_dashes.push_back("4 2 1 2");
364  m_dashes.push_back("4 2 1 2 1 2");
365 
366  QStringList l;
367 
368  for(size_t t = 0; t < m_dashes.size(); ++t)
369  {
370  l << m_dashes[t].c_str();
371  }
372 
373  return l;
374 }
375 
377 {
378  m_joinMap.insert(std::map<int, std::string>::value_type(0, TE_SE_MITRE_JOIN));
379  m_joinMap.insert(std::map<int, std::string>::value_type(1, TE_SE_ROUND_JOIN));
380  m_joinMap.insert(std::map<int, std::string>::value_type(2, TE_SE_BEVEL_JOIN));
381 
382  QStringList l;
383 
384  l << m_joinMap[0].c_str();
385  l << m_joinMap[1].c_str();
386  l << m_joinMap[2].c_str();
387 
388  return l;
389 }
390 
392 {
393  m_capMap.insert(std::map<int, std::string>::value_type(0, TE_SE_BUTT_CAP));
394  m_capMap.insert(std::map<int, std::string>::value_type(1, TE_SE_ROUND_CAP));
395  m_capMap.insert(std::map<int, std::string>::value_type(2, TE_SE_SQUARE_CAP));
396 
397  QStringList l;
398 
399  l << m_capMap[0].c_str();
400  l << m_capMap[1].c_str();
401  l << m_capMap[2].c_str();
402 
403  return l;
404 }
Stroke * clone() const
It creates a new copy of this object.
Definition: Stroke.cpp:158
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
Definition: RGBAColor.h:315
void updateUiStrokeColor()
Updates the widget form element used to visualize the stroke color.
TESEEXPORT void GetColor(const te::se::Stroke *stroke, te::color::RGBAColor &color)
It gets the RGBA color from the Stroke element.
Definition: Utils.cpp:404
A widget used to define the main property items that can be used to describe a se object...
#define TE_SE_MITRE_JOIN
It specifies the value "mitre" for stroke-linejoin parameter.
Definition: Config.h:142
TESEEXPORT double GetDouble(const te::se::ParameterValue *param)
It gets the parameter value as a double.
Definition: Utils.cpp:448
BasicStrokePropertyItem(QtTreePropertyBrowser *pb, QColor c=QColor())
Constructor.
void updateUi()
Updates the widget form based on internal stroke element.
#define TE_SE_BUTT_CAP
It specifies the value "butt" for stroke-linecap parameter.
Definition: Config.h:121
#define TE_SE_BEVEL_JOIN
It specifies the value "bevel" for stroke-linejoin parameter.
Definition: Config.h:156
TEMAPEXPORT void GetDashStyle(const std::string &dasharray, std::vector< double > &style)
Converts a dasharray pattern coded by a string to a vector of double.
Definition: Utils.cpp:155
#define TE_SE_SQUARE_CAP
It specifies the value "square" for stroke-linecap parameter.
Definition: Config.h:135
static AbstractPropertyManager & getInstance()
It returns a reference to the singleton instance.
A widget used to define the basic fill se object.
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
Definition: Config.h:86
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
#define TE_SE_ROUND_JOIN
It specifies the value "round" for stroke-linejoin parameter.
Definition: Config.h:149
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
te::se::Stroke * getStroke() const
Gets the configured stroke element.
#define TE_SE_ROUND_CAP
It specifies the value "round" for stroke-linecap parameter.
Definition: Config.h:128
void addProperty(QtProperty *property, const QString &id, QColor c)
virtual void valueChanged(QtProperty *p, int value)
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
Definition: Config.h:79
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A SvgParameter refers to an SVG/CSS graphical-formatting parameter.
Definition: SvgParameter.h:48
void setStroke(const te::se::Stroke *stroke)
Sets a stroke element to this widget.
TESEEXPORT std::string GetString(const te::se::ParameterValue *param)
It gets the parameter value as a string.
Definition: Utils.cpp:453