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) 2011-2012 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 
98 {
99  delete m_stroke;
100 }
101 
103 {
104  assert(stroke);
105 
106  delete m_stroke;
107 
108  m_stroke = stroke->clone();
109 
110  updateUi();
111 }
112 
114 {
115  return m_stroke->clone();
116 }
117 
119 {
120  // Color
122  te::se::GetColor(m_stroke, rgba);
123  m_color = QColor(rgba.getRgba());
124  m_color.setAlpha(rgba.getAlpha());
125  updateUiStrokeColor();
126 
127  // Opacity
128  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setValue(m_opacityProperty, m_color.alphaF() * 100);
129 
130  // Width
131  const te::se::SvgParameter* width = m_stroke->getWidth();
132  if(width)
134 
135  // Dash style
136  const te::se::SvgParameter* dasharray = m_stroke->getDashArray();
137  if(dasharray)
138  {
139  std::string str = te::se::GetString(dasharray);
140  for(size_t t = 0; t < m_dashes.size(); ++t)
141  {
142  if(str == m_dashes[t])
143  {
145  break;
146  }
147  }
148  }
149  else
150  {
152  }
153 
154  // Join Style
155  const te::se::SvgParameter* join = m_stroke->getLineJoin();
156  if(join)
157  {
158  std::string joinStr = te::se::GetString(join);
159 
160  std::map<int, std::string>::iterator it = m_joinMap.begin();
161 
162  while(it != m_joinMap.end())
163  {
164  if(it->second == joinStr)
165  {
166  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_joinProperty, it->first);
167  }
168 
169  ++it;
170  }
171  }
172  else
173  {
174  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_joinProperty, 0); //TE_SE_MITRE_JOIN
175  }
176 
177  // Cap Style
178  const te::se::SvgParameter* cap = m_stroke->getLineCap();
179  if(cap)
180  {
181  std::string capStr = te::se::GetString(cap);
182 
183  std::map<int, std::string>::iterator it = m_capMap.begin();
184 
185  while(it != m_capMap.end())
186  {
187  if(it->second == capStr)
188  {
189  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_capProperty, it->first);
190  }
191 
192  ++it;
193  }
194  }
195  else
196  {
197  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_capProperty, 0); //TE_SE_BUTT_CAP
198  }
199 }
200 
202 {
203  te::qt::widgets::AbstractPropertyManager::getInstance().m_colorManager->setValue(m_colorProperty, m_color);
204 }
205 
207 {
208  if(p == m_opacityProperty)
209  {
210  double opacity = value / 100.0;
211 
212  m_color.setAlpha(opacity * 255);
213  updateUiStrokeColor();
214 
215  // Updating stroke opacity
216  m_stroke->setOpacity(QString::number(opacity, 'g', 2).toStdString());
217  emit strokeChanged();
218  }
219  else if(p == m_dashProperty)
220  {
221  if(value == -1)
222  return;
223 
224  std::string pattern = m_dashes[value];
225  m_stroke->setDashArray(pattern);
226  emit strokeChanged();
227  }
228  else if(p == m_joinProperty)
229  {
230  m_stroke->setLineJoin(m_joinMap[value]);
231 
232  emit strokeChanged();
233  }
234  else if(p == m_capProperty)
235  {
236  m_stroke->setLineCap(m_capMap[value]);
237 
238  emit strokeChanged();
239  }
240 }
241 
243 {
244  if(p == m_widthProperty)
245  {
246  QString str;
247  str.setNum(value);
248 
249  m_stroke->setWidth(str.toStdString());
250  emit strokeChanged();
251  }
252 }
253 
254 void te::qt::widgets::BasicStrokePropertyItem::valueChanged(QtProperty *p, const QColor &value)
255 {
256  if(p == m_colorProperty)
257  {
258  // The new stroke color
259  m_color.setRgb(value.red(), value.green(), value.blue(), m_color.alpha());
260 
261  updateUiStrokeColor();
262 
263  // Updating stroke color
264  m_stroke->setColor(m_color.name().toStdString());
265  emit strokeChanged();
266  }
267 }
268 
270 {
271  QMap<int, QIcon> map;
272 
273  // Dash graphical representation size
274  QSize size(32, 16);
275 
276  // Line that will be draw with the dashe styles
277  QLine line(0, size.height() * 0.5, size.width(), size.height() * 0.5);
278 
279  // Setuping...
280  QPen pen;
281  pen.setWidth(2);
282 
283  for(size_t t = 0; t < m_dashes.size(); ++t)
284  {
285  QPixmap pixmap(size);
286  pixmap.fill(Qt::transparent);
287 
288  QPainter painter(&pixmap);
289 
290  std::string pattern = m_dashes[t];
291  std::vector<double> dasharray;
292  te::map::GetDashStyle(pattern, dasharray);
293 
294  QVector<qreal> qdasharray = QVector<qreal>::fromStdVector(dasharray);
295 
296  pen.setDashPattern(qdasharray);
297  painter.setPen(pen);
298  painter.drawLine(line);
299 
300  QIcon icon(pixmap);
301 
302  map[t] = icon;
303  }
304 
305  return map;
306 }
307 
309 {
310  QMap<int, QIcon> map;
311 
312  map[0] = QIcon::fromTheme("stroke-join-miter").pixmap(16,16);
313  map[1] = QIcon::fromTheme("stroke-join-round").pixmap(16,16);
314  map[2] = QIcon::fromTheme("stroke-join-bevel").pixmap(16,16);
315 
316  return map;
317 }
318 
320 {
321  QMap<int, QIcon> map;
322 
323  map[0] = QIcon::fromTheme("stroke-cap-butt").pixmap(16,16);
324  map[1] = QIcon::fromTheme("stroke-cap-round").pixmap(16,16);
325  map[2] = QIcon::fromTheme("stroke-cap-square").pixmap(16,16);
326 
327  return map;
328 }
329 
331 {
332  m_dashes.push_back("");
333  m_dashes.push_back("1 2");
334  m_dashes.push_back("4 2");
335  m_dashes.push_back("4 2 1 2");
336  m_dashes.push_back("4 2 1 2 1 2");
337 
338  QStringList l;
339 
340  for(size_t t = 0; t < m_dashes.size(); ++t)
341  {
342  l << m_dashes[t].c_str();
343  }
344 
345  return l;
346 }
347 
349 {
350  m_joinMap.insert(std::map<int, std::string>::value_type(0, TE_SE_MITRE_JOIN));
351  m_joinMap.insert(std::map<int, std::string>::value_type(1, TE_SE_ROUND_JOIN));
352  m_joinMap.insert(std::map<int, std::string>::value_type(2, TE_SE_BEVEL_JOIN));
353 
354  QStringList l;
355 
356  l << m_joinMap[0].c_str();
357  l << m_joinMap[1].c_str();
358  l << m_joinMap[2].c_str();
359 
360  return l;
361 }
362 
364 {
365  m_capMap.insert(std::map<int, std::string>::value_type(0, TE_SE_BUTT_CAP));
366  m_capMap.insert(std::map<int, std::string>::value_type(1, TE_SE_ROUND_CAP));
367  m_capMap.insert(std::map<int, std::string>::value_type(2, TE_SE_SQUARE_CAP));
368 
369  QStringList l;
370 
371  l << m_capMap[0].c_str();
372  l << m_capMap[1].c_str();
373  l << m_capMap[2].c_str();
374 
375  return l;
376 }
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