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