All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
28 
29 #include "../../../se/Config.h"
30 #include "../../../se/Stroke.h"
31 #include "../../../se/SvgParameter.h"
32 #include "../../../maptools/Utils.h"
33 #include "../../../maptools/Enums.h"
34 
35 // Qt
36 #include <QtGui/QPainter>
37 #include <QtGui/QIcon>
38 #include "../../../../../third-party/qt/propertybrowser/qtpropertybrowser.h"
39 
40 // STL
41 #include <cassert>
42 
43 
44 te::qt::widgets::BasicStrokePropertyItem::BasicStrokePropertyItem(QtTreePropertyBrowser* pb, QColor c) : te::qt::widgets::AbstractPropertyItem(pb, c),
45  m_stroke(new te::se::Stroke)
46 {
47  //build property browser basic stroke
48  QtProperty* basicStrokeProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_groupManager->addProperty(tr("Basic Stroke"));
49 
50  //color
51  m_colorProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_colorManager->addProperty(tr("Color"));
53  basicStrokeProperty->addSubProperty(m_colorProperty);
54 
55  //opacity
56  m_opacityProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->addProperty(tr("Opacity"));
59  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setMaximum(m_opacityProperty, 100);
60  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setSingleStep(m_opacityProperty, 10);
61  basicStrokeProperty->addSubProperty(m_opacityProperty);
62 
63  //width
64  m_widthProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_doubleManager->addProperty(tr("Width"));
70  basicStrokeProperty->addSubProperty(m_widthProperty);
71 
72  //dash
73  m_dashProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->addProperty(tr("Dash"));
76  basicStrokeProperty->addSubProperty(m_dashProperty);
77 
78  //join
79  m_joinProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->addProperty(tr("Join"));
82  basicStrokeProperty->addSubProperty(m_joinProperty);
83 
84  //cap
85  m_capProperty = te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->addProperty(tr("Cap"));
88  basicStrokeProperty->addSubProperty(m_capProperty);
89 
90  addProperty(basicStrokeProperty, tr("Basic Stroke"), QColor(175, 255, 175));
91 }
92 
94 {
95  delete m_stroke;
96 }
97 
99 {
100  assert(stroke);
101 
102  delete m_stroke;
103 
104  m_stroke = stroke->clone();
105 
106  updateUi();
107 }
108 
110 {
111  return m_stroke->clone();
112 }
113 
115 {
116  // Color
118  te::map::GetColor(m_stroke, rgba);
119  m_color = QColor(rgba.getRgba());
120  m_color.setAlpha(rgba.getAlpha());
121  updateUiStrokeColor();
122 
123  // Opacity
124  te::qt::widgets::AbstractPropertyManager::getInstance().m_intSliderManager->setValue(m_opacityProperty, m_color.alphaF() * 100);
125 
126  // Width
127  const te::se::SvgParameter* width = m_stroke->getWidth();
128  if(width)
130 
131  // Dash style
132  const te::se::SvgParameter* dasharray = m_stroke->getDashArray();
133  if(dasharray)
134  {
135  std::string str = te::map::GetString(dasharray);
136  for(size_t t = 0; t < m_dashes.size(); ++t)
137  {
138  if(str == m_dashes[t])
139  {
141  break;
142  }
143  }
144  }
145  else
146  {
148  }
149 
150  // Join Style
151  const te::se::SvgParameter* join = m_stroke->getLineJoin();
152  if(join)
153  {
154  std::string joinStr = te::map::GetString(join);
155 
156  std::map<int, std::string>::iterator it = m_joinMap.begin();
157 
158  while(it != m_joinMap.end())
159  {
160  if(it->second == joinStr)
161  {
162  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_joinProperty, it->first);
163  }
164 
165  ++it;
166  }
167  }
168  else
169  {
170  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_joinProperty, 0); //TE_SE_MITRE_JOIN
171  }
172 
173  // Cap Style
174  const te::se::SvgParameter* cap = m_stroke->getLineCap();
175  if(cap)
176  {
177  std::string capStr = te::map::GetString(cap);
178 
179  std::map<int, std::string>::iterator it = m_capMap.begin();
180 
181  while(it != m_capMap.end())
182  {
183  if(it->second == capStr)
184  {
185  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_capProperty, it->first);
186  }
187 
188  ++it;
189  }
190  }
191  else
192  {
193  te::qt::widgets::AbstractPropertyManager::getInstance().m_enumManager->setValue(m_capProperty, 0); //TE_SE_BUTT_CAP
194  }
195 }
196 
198 {
199  te::qt::widgets::AbstractPropertyManager::getInstance().m_colorManager->setValue(m_colorProperty, m_color);
200 }
201 
203 {
204  if(p == m_opacityProperty)
205  {
206  double opacity = value / 100.0;
207 
208  m_color.setAlpha(opacity * 255);
209  updateUiStrokeColor();
210 
211  // Updating stroke opacity
212  m_stroke->setOpacity(QString::number(opacity, 'g', 2).toStdString());
213  emit strokeChanged();
214  }
215  else if(p == m_dashProperty)
216  {
217  if(value == -1)
218  return;
219 
220  std::string pattern = m_dashes[value];
221  m_stroke->setDashArray(pattern);
222  emit strokeChanged();
223  }
224  else if(p == m_joinProperty)
225  {
226  m_stroke->setLineJoin(m_joinMap[value]);
227 
228  emit strokeChanged();
229  }
230  else if(p == m_capProperty)
231  {
232  m_stroke->setLineCap(m_capMap[value]);
233 
234  emit strokeChanged();
235  }
236 }
237 
239 {
240  if(p == m_widthProperty)
241  {
242  QString str;
243  str.setNum(value);
244 
245  m_stroke->setWidth(str.toStdString());
246  emit strokeChanged();
247  }
248 }
249 
250 void te::qt::widgets::BasicStrokePropertyItem::valueChanged(QtProperty *p, const QColor &value)
251 {
252  if(p == m_colorProperty)
253  {
254  // The new stroke color
255  m_color.setRgb(value.red(), value.green(), value.blue(), m_color.alpha());
256 
257  updateUiStrokeColor();
258 
259  // Updating stroke color
260  m_stroke->setColor(m_color.name().toStdString());
261  emit strokeChanged();
262  }
263 }
264 
266 {
267  QMap<int, QIcon> map;
268 
269  // Dash graphical representation size
270  QSize size(32, 16);
271 
272  // Line that will be draw with the dashe styles
273  QLine line(0, size.height() * 0.5, size.width(), size.height() * 0.5);
274 
275  // Setuping...
276  QPen pen;
277  pen.setWidth(2);
278 
279  for(size_t t = 0; t < m_dashes.size(); ++t)
280  {
281  QPixmap pixmap(size);
282  pixmap.fill(Qt::transparent);
283 
284  QPainter painter(&pixmap);
285 
286  std::string pattern = m_dashes[t];
287  std::vector<double> dasharray;
288  te::map::GetDashStyle(pattern, dasharray);
289 
290  QVector<qreal> qdasharray = QVector<qreal>::fromStdVector(dasharray);
291 
292  pen.setDashPattern(qdasharray);
293  painter.setPen(pen);
294  painter.drawLine(line);
295 
296  QIcon icon(pixmap);
297 
298  map[t] = icon;
299  }
300 
301  return map;
302 }
303 
305 {
306  QMap<int, QIcon> map;
307 
308  map[0] = QIcon::fromTheme("stroke-join-miter").pixmap(16,16);
309  map[1] = QIcon::fromTheme("stroke-join-round").pixmap(16,16);
310  map[2] = QIcon::fromTheme("stroke-join-bevel").pixmap(16,16);
311 
312  return map;
313 }
314 
316 {
317  QMap<int, QIcon> map;
318 
319  map[0] = QIcon::fromTheme("stroke-cap-butt").pixmap(16,16);
320  map[1] = QIcon::fromTheme("stroke-cap-round").pixmap(16,16);
321  map[2] = QIcon::fromTheme("stroke-cap-square").pixmap(16,16);
322 
323  return map;
324 }
325 
327 {
328  m_dashes.push_back("");
329  m_dashes.push_back("1 2");
330  m_dashes.push_back("4 2");
331  m_dashes.push_back("4 2 1 2");
332  m_dashes.push_back("4 2 1 2 1 2");
333 
334  QStringList l;
335 
336  for(size_t t = 0; t < m_dashes.size(); ++t)
337  {
338  l << m_dashes[t].c_str();
339  }
340 
341  return l;
342 }
343 
345 {
346  m_joinMap.insert(std::map<int, std::string>::value_type(0, TE_SE_MITRE_JOIN));
347  m_joinMap.insert(std::map<int, std::string>::value_type(1, TE_SE_ROUND_JOIN));
348  m_joinMap.insert(std::map<int, std::string>::value_type(2, TE_SE_BEVEL_JOIN));
349 
350  QStringList l;
351 
352  l << m_joinMap[0].c_str();
353  l << m_joinMap[1].c_str();
354  l << m_joinMap[2].c_str();
355 
356  return l;
357 }
358 
360 {
361  m_capMap.insert(std::map<int, std::string>::value_type(0, TE_SE_BUTT_CAP));
362  m_capMap.insert(std::map<int, std::string>::value_type(1, TE_SE_ROUND_CAP));
363  m_capMap.insert(std::map<int, std::string>::value_type(2, TE_SE_SQUARE_CAP));
364 
365  QStringList l;
366 
367  l << m_capMap[0].c_str();
368  l << m_capMap[1].c_str();
369  l << m_capMap[2].c_str();
370 
371  return l;
372 }
TEMAPEXPORT void GetColor(const te::se::Stroke *stroke, te::color::RGBAColor &color)
Gets the RGBA color from Stroke element.
Definition: Utils.cpp:83
virtual void valueChanged(QtProperty *p, int value)
#define TE_SE_ROUND_CAP
It specifies the value &quot;round&quot; for stroke-linecap parameter.
Definition: Config.h:128
#define TE_SE_ROUND_JOIN
It specifies the value &quot;round&quot; for stroke-linejoin parameter.
Definition: Config.h:149
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
Definition: RGBAColor.h:315
#define TE_SE_BUTT_CAP
It specifies the value &quot;butt&quot; for stroke-linecap parameter.
Definition: Config.h:121
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
TEMAPEXPORT double GetDouble(const te::se::ParameterValue *param)
Gets the parameter value as double.
Definition: Utils.cpp:127
Stroke * clone() const
It creates a new copy of this object.
Definition: Stroke.cpp:158
void setStroke(const te::se::Stroke *stroke)
Sets a stroke element to this widget.
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:227
void updateUiStrokeColor()
Updates the widget form element used to visualize the stroke color.
#define TE_SE_SQUARE_CAP
It specifies the value &quot;square&quot; for stroke-linecap parameter.
Definition: Config.h:135
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TEMAPEXPORT std::string GetString(const te::se::ParameterValue *param)
Gets the parameter value as string.
Definition: Utils.cpp:132
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
Definition: Config.h:86
#define TE_SE_BEVEL_JOIN
It specifies the value &quot;bevel&quot; for stroke-linejoin parameter.
Definition: Config.h:156
#define TE_SE_MITRE_JOIN
It specifies the value &quot;mitre&quot; for stroke-linejoin parameter.
Definition: Config.h:142
A widget used to define the basic fill se object.
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
Definition: Config.h:79
void updateUi()
Updates the widget form based on internal stroke element.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
A widget used to define the main property items that can be used to describe a se object...
te::se::Stroke * getStroke() const
Gets the configured stroke element.
void addProperty(QtProperty *property, const QString &id, QColor c)
static AbstractPropertyManager & getInstance()
It returns a reference to the singleton instance.
A SvgParameter refers to an SVG/CSS graphical-formatting parameter.
Definition: SvgParameter.h:48
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
BasicStrokePropertyItem(QtTreePropertyBrowser *pb, QColor c=QColor())
Constructor.