All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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 QtUtils.cpp
22 
23  \brief This file contains utility functions for dealing with Qt controls.
24  */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../maptools/MarkRendererManager.h"
29 #include "../../maptools/Canvas.h"
30 #include "../../se/Fill.h"
31 #include "../../se/Mark.h"
32 #include "../../se/Stroke.h"
33 #include "../../se/Utils.h"
34 #include "Utils.h"
35 
36 // Qt
37 #include <QApplication>
38 #include <QImage>
39 #include <QMenu>
40 #include <QMenuBar>
41 #include <QMessageBox>
42 #include <QPainter>
43 #include <QPen>
44 #include <QSettings>
45 #include <QTreeWidgetItem>
46 #include <QTreeWidgetItemIterator>
47 
48 void te::qt::widgets::SetChildrenCheckState(QTreeWidgetItem* item, int column, Qt::CheckState state)
49 {
50  if(item && item->child(0))
51  {
52  QTreeWidgetItemIterator it(item->child(0));
53 
54  while(*it)
55  {
56  (*it)->setCheckState(column, state);
57 
58  ++it;
59  }
60  }
61 }
62 
63 QImage* te::qt::widgets::GetImage(te::color::RGBAColor** img, int width, int height)
64 {
65  QImage* qimg = new QImage(width, height, QImage::Format_ARGB32);
66 
67  for(int i = 0; i < height; ++i)
68  {
69  unsigned char* u = qimg->scanLine(i);
70 
71  for(int j = 0; j < width; ++j)
72  {
73  te::color::RGBAColor c = img[i][j];
74  QRgb val = qRgba(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
75  QRgb* v = (QRgb*)(u+j*4);
76  *v = val;
77  }
78  }
79 
80  return qimg;
81 }
82 
84 {
85  te::color::RGBAColor** rgba = new te::color::RGBAColor*[img->height()];
86  for(int i = 0; i < img->height(); ++i)
87  {
88  te::color::RGBAColor* s = new te::color::RGBAColor[img->width()];
89 
90  for(int j = 0; j < img->width(); ++j)
91  s[j] = img->pixel(j, i);
92 
93  rgba[i] = s;
94  }
95 
96  return rgba;
97 }
98 
99 QStyle::StandardPixmap toQStyle(const QMessageBox::Icon& icon)
100 {
101  switch(icon)
102  {
103  case QMessageBox::Question:
104  return QStyle::SP_MessageBoxQuestion;
105  break;
106 
107  case QMessageBox::Warning:
108  return QStyle::SP_MessageBoxWarning;
109  break;
110 
111  case QMessageBox::Critical:
112  return QStyle::SP_MessageBoxCritical;
113  break;
114 
115  default:
116  return QStyle::SP_MediaVolumeMuted;
117  break;
118  }
119 }
120 
121 QMenu* te::qt::widgets::FindMenu(const QString& mnuText, QMenu* mnu)
122 {
123  return mnu->findChild<QMenu*>(mnuText);
124 }
125 
126 QMenu* te::qt::widgets::FindMenu(const QString& mnuText, QMenuBar* bar)
127 {
128  return bar->findChild<QMenu*>(mnuText);
129 }
130 
131 QAction* te::qt::widgets::FindAction(const QString& actText, QMenu* mnu)
132 {
133  return mnu->findChild<QAction*>(actText);
134 }
135 
136 QAction* te::qt::widgets::FindAction(const QString& actText, QMenuBar* mnuBar)
137 {
138  return mnuBar->findChild<QAction*>(actText);
139 }
140 
141 QMenu* CreateMenu(const QString& mnuName, QMenu* p)
142 {
143  QMenu* mnu = p->findChild<QMenu*>(mnuName);
144 
145  if(mnu == 0)
146  {
147  QStringList ls = mnuName.split(".");
148  mnu = p->addMenu(ls[ls.size()-1]);
149 
150  mnu->setObjectName(mnuName);
151  }
152 
153  return mnu;
154 }
155 
156 QMenu* CreateMenu(const QString& mnuName, QMenuBar* p)
157 {
158  QMenu* mnu = te::qt::widgets::FindMenu(mnuName, p);
159 
160  if(mnu == 0)
161  {
162  QStringList ls = mnuName.split(".");
163  mnu = new QMenu(ls[ls.size()-1], p);
164  mnu->setObjectName(mnuName);
165 
166  QMenu* helpMnu = te::qt::widgets::FindMenu("Help", p);
167 
168  QAction* act = (helpMnu == 0) ? 0 : helpMnu->menuAction();
169 
170  p->insertMenu(act, mnu);
171  }
172 
173  return mnu;
174 }
175 
176 QMenu* te::qt::widgets::GetMenu(const QString& mnuText, QMenu* mnu)
177 {
178  QMenu* res = FindMenu(mnuText, mnu);
179 
180  QStringList mnus = mnuText.split('.');
181 
182  for(int i=1; i<mnus.size(); i++)
183  mnus[i] = mnus[i-1] + "." + mnus[i];
184 
185  for(int i=0; i<mnus.size(); i++)
186  res = (i == 0) ?
187  CreateMenu(mnus[i], mnu) :
188  CreateMenu(mnus[i], res);
189 
190  return res;
191 }
192 
193 QMenu* te::qt::widgets::GetMenu(const QString& mnuText, QMenuBar* bar)
194 {
195  QMenu* res = FindMenu(mnuText, bar);
196 
197  QStringList mnus = mnuText.split('.');
198 
199  for(int i=1; i<mnus.size(); i++)
200  mnus[i] = mnus[i-1] + "." + mnus[i];
201 
202  for(int i=0; i<mnus.size(); i++)
203  {
204  res = (i == 0) ?
205  CreateMenu(mnus[i], bar) :
206  CreateMenu(mnus[i], res);
207  }
208  return res;
209 }
210 
212 {
213  return te::color::RGBAColor(color.red(), color.green(), color.blue(), color.alpha());
214 }
215 
217 {
218  QColor qcolor(color.getRgba());
219  qcolor.setAlpha(qAlpha(color.getRgba()));
220  return qcolor;
221 }
222 
223 void te::qt::widgets::Config2DrawPolygons(te::map::Canvas* canvas, const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth)
224 {
225  canvas->setPolygonContourWidth(contourWidth);
226  canvas->setPolygonContourColor(Convert2TerraLib(contourColor));
227  canvas->setPolygonFillColor(Convert2TerraLib(fillColor));
228 }
229 
230 void te::qt::widgets::Config2DrawLines(te::map::Canvas* canvas, const QColor& color, const std::size_t& width)
231 {
232  canvas->setLineWidth(width);
233  canvas->setLineColor(Convert2TerraLib(color));
234 }
235 
236 void te::qt::widgets::Config2DrawPoints(te::map::Canvas* canvas, const QColor& color, const std::size_t& width)
237 {
238  canvas->setPointWidth(width);
239  canvas->setPointColor(Convert2TerraLib(color));
240 }
241 
242 void te::qt::widgets::Config2DrawPoints(te::map::Canvas* canvas, const QString& markName, const std::size_t& size,
243  const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth)
244 {
245  te::se::Stroke* stroke = te::se::CreateStroke(contourColor.name().toStdString(),
246  QString::number(contourWidth).toStdString(),
247  QString::number(contourColor.alphaF()).toStdString());
248 
249  te::se::Fill* fill = te::se::CreateFill(fillColor.name().toStdString(),
250  QString::number(fillColor.alphaF()).toStdString());
251 
252  te::se::Mark* mark = te::se::CreateMark(markName.toStdString(), stroke, fill);
253 
255 
257  canvas->setPointPattern(rgba, size, size);
258 
259  te::common::Free(rgba, size);
260 
261  delete mark;
262 }
263 
264 void te::qt::widgets::Config2DrawLayerSelection(te::map::Canvas* canvas, const QColor& selectionColor, const te::gm::GeomType& type)
265 {
266  switch(type)
267  {
268  case te::gm::PolygonType:
276  {
277  QColor fillColor = selectionColor;
278  fillColor.setAlpha(128);
279 
280  Config2DrawPolygons(canvas, fillColor, Qt::black, 2);
281  }
282  break;
283 
292  {
293  QColor fillColor = selectionColor;
294  fillColor.setAlpha(128);
295 
296  Config2DrawLines(canvas, fillColor, 6);
297  }
298  break;
299 
300  case te::gm::PointType:
301  case te::gm::PointZType:
302  case te::gm::PointMType:
303  case te::gm::PointZMType:
308  {
309  QColor fillColor = selectionColor;
310  fillColor.setAlpha(70);
311 
312  QColor contourColor = selectionColor;
313  contourColor.setAlpha(150);
314 
315  Config2DrawPoints(canvas, "square", 24, fillColor, contourColor,2);
316  }
317  break;
318 
319  default:
320  return;
321  }
322 }
323 
324 QPixmap te::qt::widgets::CreatePixmapIcon(const int& size, const QColor& penColor, const QColor& brushColor, const int& contourSize)
325 {
326  QPixmap pix(size, size);
327  pix.fill(Qt::transparent);
328 
329  int offset = 2;
330 
331  QPainterPath path;
332  path.addRect(offset, offset, pix.width() - 2 * offset, pix.height() - 2 * offset);
333 
334  QPen pen;
335  pen.setColor(penColor);
336  pen.setWidth(contourSize);
337 
338  QBrush brush;
339  brush.setColor(brushColor);
340 
341  QPainter p(&pix);
342  p.setPen(pen);
343  p.setBrush(brushColor);
344 
345  p.fillPath(path, brush);
346  p.drawPath(path);
347 
348  return pix;
349 }
350 
351 void te::qt::widgets::AddFilePathToSettings(const QString& path, const QString& typeFile)
352 {
353  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
354 
355  QString key = "Last used file path/" + typeFile;
356 
357  sett.setValue(key, path);
358 }
359 
360 QString te::qt::widgets::GetFilePathFromSettings(const QString& typeFile)
361 {
362  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
363 
364  QString key = "Last used file path/" + typeFile;
365 
366  return sett.value(key).toString();
367 }
368 
369 QString te::qt::widgets::Convert2Qt(const std::string& text, const te::common::CharEncoding& encoding)
370 {
371  try
372  {
373  switch(encoding)
374  {
376  return text.c_str();
377 
378  case te::common::UTF8:
379  return QString::fromUtf8(text.c_str());
380 
381  case te::common::LATIN1:
382  return QString::fromLatin1(text.c_str());
383 
384  // continues...
385 
386  default:
387  {
388 #ifdef TERRALIB_CHARENCODING_ENABLED
389  std::string latin1 = te::common::CharEncodingConv::convert(text, encoding, te::common::LATIN1);
390  return QString::fromLatin1(latin1.c_str());
391 #else
392  return text.c_str();
393 #endif
394  }
395  }
396  }
397  catch(...)
398  {
399  return text.c_str();
400  }
401 }
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
Definition: RGBAColor.h:315
TEQTWIDGETSEXPORT te::color::RGBAColor Convert2TerraLib(const QColor &color)
It converts a Qt Color to TerraLib Color.
Definition: Utils.cpp:211
TEQTWIDGETSEXPORT void Config2DrawLayerSelection(te::map::Canvas *canvas, const QColor &selectionColor, const te::gm::GeomType &type)
It configs (i.e. prepares) the given canvas to draw a layer selection.
Definition: Utils.cpp:264
virtual void setPointWidth(int w)=0
It sets the point width. If point has a patterns, this pattern is scaled to width.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
CharEncoding
Supported charsets (character encoding).
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
TEQTWIDGETSEXPORT QAction * FindAction(const QString &actText, QMenu *mnu)
Definition: Utils.cpp:131
QStyle::StandardPixmap toQStyle(const QMessageBox::Icon &icon)
Definition: Utils.cpp:99
virtual void setPolygonFillColor(const te::color::RGBAColor &color)=0
It sets the color used to fill the draw of polygon geometries.
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:351
virtual void setPointPattern(te::color::RGBAColor **pattern, int ncols, int nrows)=0
It sets the point pattern.
virtual void setLineWidth(int w)=0
It sets the line width.
TEQTWIDGETSEXPORT void Config2DrawLines(te::map::Canvas *canvas, const QColor &color, const std::size_t &width=1)
It configs (i.e. prepares) the given canvas to draw lines.
Definition: Utils.cpp:230
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
static std::string convert(const std::string &src, const CharEncoding &fromCode, const CharEncoding &toCode)
An static method that converts the source string to a target charset.
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
TEQTWIDGETSEXPORT void Config2DrawPolygons(te::map::Canvas *canvas, const QColor &fillColor, const QColor &contourColor, const std::size_t &contourWidth=1)
It configs (i.e. prepares) the given canvas to draw polygons.
Definition: Utils.cpp:223
QMenu * CreateMenu(const QString &mnuName, QMenu *p)
Definition: Utils.cpp:141
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
static MarkRendererManager & getInstance()
It returns a reference to the singleton instance.
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
Definition: Utils.cpp:54
TEQTWIDGETSEXPORT void SetChildrenCheckState(QTreeWidgetItem *item, int column, Qt::CheckState state)
It sets the check state for the children of a item.
Definition: Utils.cpp:48
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas *canvas, const QColor &color, const std::size_t &width=1)
It configs (i.e. prepares) the given canvas to draw points.
Definition: Utils.cpp:236
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Definition: Utils.cpp:130
virtual void setPolygonContourWidth(int w)=0
It sets the polygon contour width.
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
Definition: Utils.cpp:54
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
virtual void setPointColor(const te::color::RGBAColor &color)=0
It sets the point drawing color.
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
Definition: Utils.cpp:63
TEQTWIDGETSEXPORT QMenu * FindMenu(const QString &mnuText, QMenu *mnu)
Finds a menu item in the mnu object.
Definition: Utils.cpp:121
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
Definition: Config.h:46
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
virtual void setPolygonContourColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw the boundary of polygon geometries.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TEQTWIDGETSEXPORT QColor Convert2Qt(const te::color::RGBAColor &color)
It converts a TerraLib Color to Qt Color.
Definition: Utils.cpp:216
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:360
virtual void setLineColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw line geometries.
TEQTWIDGETSEXPORT QMenu * GetMenu(const QString &mnuText, QMenu *mnu)
Gets a menu or submenu contained in the mnu object.
Definition: Utils.cpp:176
TEQTWIDGETSEXPORT QPixmap CreatePixmapIcon(const int &size, const QColor &penColor, const QColor &brushColor, const int &contourSize)
It creates a pixmap to use as icon (to be used as legend icon).
Definition: Utils.cpp:324
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
Definition: Utils.cpp:109