All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BasicStrokeWidget.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/BasicStrokeWidget.cpp
22 
23  \brief A widget used to build a basic stroke element.
24 */
25 
26 // TerraLib
27 #include "../../../maptools/Utils.h"
28 #include "../../../maptools/Enums.h"
29 #include "../../../se/Stroke.h"
30 #include "../../../se/SvgParameter.h"
31 #include "../utils/ColorPickerToolButton.h"
32 #include "BasicStrokeWidget.h"
33 #include "ui_BasicStrokeWidgetForm.h"
34 
35 // Qt
36 #include <QtGui/QPainter>
37 
38 // STL
39 #include <cassert>
40 
41 te::qt::widgets::BasicStrokeWidget::BasicStrokeWidget(QWidget* parent, Qt::WindowFlags f)
42  : QWidget(parent, f),
43  m_ui(new Ui::BasicStrokeWidgetForm),
44  m_stroke(new te::se::Stroke)
45 {
46  m_ui->setupUi(this);
47 
48  // Color Picker
50  m_colorPicker->setFixedSize(70, 24);
51 
52  // Adjusting...
53  QGridLayout* layout = new QGridLayout(m_ui->m_colorPickerFrame);
54  layout->setContentsMargins(0, 0, 0, 0);
55  layout->setSizeConstraint(QLayout::SetFixedSize);
56  layout->addWidget(m_colorPicker);
57 
58  initialize();
59 
60  // Signals & slots
61  connect(m_colorPicker, SIGNAL(colorChanged(const QColor&)), SLOT(onColorChanged(const QColor&)));
62  connect(m_ui->m_strokeOpacitySlider, SIGNAL(valueChanged(int)), SLOT(onStrokeOpacitySliderValueChanged(int)));
63  connect(m_ui->m_strokeWidthDoubleSpinBox, SIGNAL(valueChanged(const QString&)), SLOT(onStrokeWidthDoubleSpinBoxValueChanged(const QString&)));
64  connect(m_ui->m_strokeDashComboBox, SIGNAL(currentIndexChanged(int)), SLOT(onStrokeDashComboBoxCurrentIndexChanged(int)));
65  connect(m_strokeJoinStyleButtonGroup, SIGNAL(buttonClicked(int)), SLOT(onStrokeJoinStyleChanged(int)));
66  connect(m_strokeCapStyleButtonGroup, SIGNAL(buttonClicked(int)), SLOT(onStrokeCapStyleChanged(int)));
67 }
68 
70 {
71  delete m_stroke;
72 }
73 
75 {
76  assert(stroke);
77 
78  delete m_stroke;
79 
80  m_stroke = stroke->clone();
81 
82  updateUi();
83 }
84 
86 {
87  return m_stroke->clone();
88 }
89 
91 {
92  // Temp: Bulding predefined dash styles
93  m_dashes.push_back("");
94  m_dashes.push_back("1 2");
95  m_dashes.push_back("4 2");
96  m_dashes.push_back("4 2 1 2");
97  m_dashes.push_back("4 2 1 2 1 2");
98  // more?!
99 
100  // Default stroke color
101  m_color = QColor(TE_SE_DEFAULT_STROKE_BASIC_COLOR);
102  updateUiStrokeColor();
103 
104  // Dash styles selection
105  fillStrokeDashStyleComboBox();
106 
107  // Controlling Join Style Buttons
108  m_strokeJoinStyleButtonGroup = new QButtonGroup(this);
109  m_strokeJoinStyleButtonGroup->addButton(m_ui->m_miterJoinPushButton, te::map::MiterJoin);
110  m_strokeJoinStyleButtonGroup->addButton(m_ui->m_roundJoinPushButton, te::map::RoundJoin);
111  m_strokeJoinStyleButtonGroup->addButton(m_ui->m_bevelJoinPushButton, te::map::BevelJoin);
112 
113  // Controlling Cap Style Buttons
114  m_strokeCapStyleButtonGroup = new QButtonGroup(this);
115  m_strokeCapStyleButtonGroup->addButton(m_ui->m_buttCapPushButton, te::map::ButtCap);
116  m_strokeCapStyleButtonGroup->addButton(m_ui->m_roundCapPushButton, te::map::RoundCap);
117  m_strokeCapStyleButtonGroup->addButton(m_ui->m_squareCapPushButton, te::map::SquareCap);
118 
119  // Setup icons
120  m_ui->m_miterJoinPushButton->setIcon(QIcon::fromTheme("stroke-join-miter"));
121  m_ui->m_roundJoinPushButton->setIcon(QIcon::fromTheme("stroke-join-round"));
122  m_ui->m_bevelJoinPushButton->setIcon(QIcon::fromTheme("stroke-join-bevel"));
123 
124  m_ui->m_buttCapPushButton->setIcon(QIcon::fromTheme("stroke-cap-butt"));
125  m_ui->m_roundCapPushButton->setIcon(QIcon::fromTheme("stroke-cap-round"));
126  m_ui->m_squareCapPushButton->setIcon(QIcon::fromTheme("stroke-cap-square"));
127 }
128 
130 {
131  // Color
133  te::map::GetColor(m_stroke, rgba);
134  m_color = QColor(rgba.getRgba());
135  m_color.setAlpha(rgba.getAlpha());
136  updateUiStrokeColor();
137 
138  // Opacity
139  m_ui->m_strokeOpacitySlider->setValue(m_color.alphaF() * 100);
140 
141  // Width
142  const te::se::SvgParameter* width = m_stroke->getWidth();
143  if(width)
144  m_ui->m_strokeWidthDoubleSpinBox->setValue(te::map::GetDouble(width));
145 
146  // Dash style
147  const te::se::SvgParameter* dasharray = m_stroke->getDashArray();
148  if(dasharray)
149  updateUiDashStyle(te::map::GetString(dasharray));
150 
151  // Join Style
152  const te::se::SvgParameter* join = m_stroke->getLineJoin();
153  if(join)
154  updateUiJoinStyle(te::map::GetString(join));
155 
156  // Cap Style
157  const te::se::SvgParameter* cap = m_stroke->getLineCap();
158  if(cap)
159  updateUiCapStyle(te::map::GetString(cap));
160 }
161 
163 {
164  m_colorPicker->setColor(m_color);
165 }
166 
168 {
169  int i = 0;
170  std::vector<std::string>::iterator it;
171  for(it = m_dashes.begin(); it != m_dashes.end(); ++it) // Try find the given pattern
172  {
173  if(pattern == *it)
174  {
175  m_ui->m_strokeDashComboBox->setCurrentIndex(i);
176  return;
177  }
178  ++i;
179  }
180 
181  if(it == m_dashes.end()) // Not found
182  {
183  m_dashes.push_back(pattern); // Adding on dash styles...
184  fillStrokeDashStyleComboBox(); // Updating dash combo box...
185  m_ui->m_strokeDashComboBox->setCurrentIndex(i); // Select it!
186  }
187 }
188 
190 {
191  // Default TE_SE_MITRE
192  if(style == TE_SE_ROUND_JOIN)
193  m_ui->m_roundJoinPushButton->setChecked(true);
194  else if(style == TE_SE_BEVEL_JOIN)
195  m_ui->m_bevelJoinPushButton->setChecked(true);
196 }
197 
199 {
200  // Default TE_SE_BUTT
201  if(style == TE_SE_ROUND_CAP)
202  m_ui->m_roundCapPushButton->setChecked(true);
203  else if(style == TE_SE_SQUARE_CAP)
204  m_ui->m_squareCapPushButton->setChecked(true);
205 }
206 
208 {
209  // Cleaning...
210  m_ui->m_strokeDashComboBox->clear();
211 
212  // Dash graphical representation size
213  QSize size(150, 5);
214 
215  // Adjusts the dash combo box to comport the graphical representation
216  m_ui->m_strokeDashComboBox->setIconSize(size);
217 
218  // Line that will be draw with the dashe styles
219  QLine line(0, size.height() * 0.5, size.width(), size.height() * 0.5);
220 
221  // Setuping...
222  QPen pen;
223  pen.setWidth(2);
224 
225  // Draw!
226  std::vector<std::string>::iterator it = m_dashes.begin();
227  for(it = m_dashes.begin(); it != m_dashes.end(); ++it) // for each dash style
228  {
229  QPixmap pixmap(size);
230  pixmap.fill(Qt::transparent);
231 
232  QPainter painter(&pixmap);
233 
234  std::string pattern = *it;
235  std::vector<double> dasharray;
236  te::map::GetDashStyle(pattern, dasharray);
237 
238  QVector<qreal> qdasharray = QVector<qreal>::fromStdVector(dasharray);
239 
240  pen.setDashPattern(qdasharray);
241  painter.setPen(pen);
242  painter.drawLine(line);
243 
244  m_ui->m_strokeDashComboBox->addItem(pixmap, "");
245  }
246 }
247 
249 {
250  // The new stroke color
251  m_color.setRgb(color.red(), color.green(), color.blue(), m_color.alpha());
252 
253  updateUiStrokeColor();
254 
255  // Updating stroke color
256  m_stroke->setColor(m_color.name().toStdString());
257  emit strokeChanged();
258 }
259 
261 {
262  double opacity = value / 100.0;
263 
264  m_color.setAlpha(opacity * 255);
265  updateUiStrokeColor();
266 
267  // Updating stroke opacity
268  m_stroke->setOpacity(QString::number(opacity, 'g', 2).toStdString());
269  emit strokeChanged();
270 }
271 
273 {
274  m_stroke->setWidth(text.toStdString());
275  emit strokeChanged();
276 }
277 
279 {
280  if(index == -1)
281  return;
282 
283  std::string pattern = m_dashes[index];
284  m_stroke->setDashArray(pattern);
285  emit strokeChanged();
286 }
287 
289 {
290  switch(style)
291  {
292  case te::map::MiterJoin:
293  m_stroke->setLineJoin(TE_SE_MITRE_JOIN);
294  break;
295 
296  case te::map::RoundJoin:
297  m_stroke->setLineJoin(TE_SE_ROUND_JOIN);
298  break;
299 
300  case te::map::BevelJoin:
301  m_stroke->setLineJoin(TE_SE_BEVEL_JOIN);
302  break;
303  }
304 
305  emit strokeChanged();
306 }
307 
309 {
310  switch(style)
311  {
312  case te::map::ButtCap:
313  m_stroke->setLineCap(TE_SE_BUTT_CAP);
314  break;
315 
316  case te::map::RoundCap:
317  m_stroke->setLineCap(TE_SE_ROUND_CAP);
318  break;
319 
320  case te::map::SquareCap:
321  m_stroke->setLineCap(TE_SE_SQUARE_CAP);
322  break;
323  }
324 
325  emit strokeChanged();
326 }
void onColorChanged(const QColor &color)
TEMAPEXPORT void GetColor(const te::se::Stroke *stroke, te::color::RGBAColor &color)
Gets the RGBA color from Stroke element.
Definition: Utils.cpp:83
A widget used to build a basic stroke element.
#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
void fillStrokeDashStyleComboBox()
Fills the widget form element used to visualize the stroke cap style.
void onStrokeWidthDoubleSpinBoxValueChanged(const QString &text)
te::se::Stroke * getStroke() const
Gets the configured stroke element.
ColorPickerToolButton * m_colorPicker
Widget used to pick a color.
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
QButtonGroup * m_strokeJoinStyleButtonGroup
Button group used to control the join styles.
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.
BasicStrokeWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a basic stroke widget which is a child of parent, with widget flags set to f...
#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
void onStrokeDashComboBoxCurrentIndexChanged(int index)
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void initialize()
Internal method to initialize the widget (e.g.: color, combos, icons, etc.)
void updateUiDashStyle(const std::string &pattern)
Updates the widget form element used to visualize the stroke dash style.
std::auto_ptr< Ui::BasicStrokeWidgetForm > m_ui
Widget form.
Custom tool button used to pick a color.
void updateUiJoinStyle(const std::string &style)
Updates the widget form element used to visualize the stroke join style.
void updateUiCapStyle(const std::string &style)
Updates the widget form element used to visualize the stroke cap style.
void updateUi()
Updates the widget form based on internal stroke element.
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
QButtonGroup * m_strokeCapStyleButtonGroup
Button group used to control the cap styles.