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) 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 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 "../../dataaccess/datasource/DataSourceCapabilities.h"
35 #include "../../dataaccess/datasource/DataSourceFactory.h"
36 #include "../../dataaccess/datasource/DataSource.h"
37 #include "../../common/StringUtils.h"
38 #include "Utils.h"
39 
40 // Qt
41 #include <QApplication>
42 #include <QActionGroup>
43 #include <QImage>
44 #include <QMenu>
45 #include <QMenuBar>
46 #include <QMessageBox>
47 #include <QPainter>
48 #include <QPen>
49 #include <QSettings>
50 #include <QTreeWidgetItem>
51 #include <QTreeWidgetItemIterator>
52 #include <QString>
53 
54 void te::qt::widgets::SetChildrenCheckState(QTreeWidgetItem* item, int column, Qt::CheckState state)
55 {
56  if(item && item->child(0))
57  {
58  QTreeWidgetItemIterator it(item->child(0));
59 
60  while(*it)
61  {
62  (*it)->setCheckState(column, state);
63 
64  ++it;
65  }
66  }
67 }
68 
69 QImage* te::qt::widgets::GetImage(te::color::RGBAColor** img, int width, int height)
70 {
71  QImage* qimg = new QImage(width, height, QImage::Format_ARGB32);
72 
73  for(int i = 0; i < height; ++i)
74  {
75  unsigned char* u = qimg->scanLine(i);
76 
77  for(int j = 0; j < width; ++j)
78  {
79  te::color::RGBAColor c = img[i][j];
80  QRgb val = qRgba(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
81  QRgb* v = (QRgb*)(u+j*4);
82  *v = val;
83  }
84  }
85 
86  return qimg;
87 }
88 
90 {
91  te::color::RGBAColor** rgba = new te::color::RGBAColor*[img->height()];
92  for(int i = 0; i < img->height(); ++i)
93  {
94  te::color::RGBAColor* s = new te::color::RGBAColor[img->width()];
95 
96  for(int j = 0; j < img->width(); ++j)
97  s[j] = img->pixel(j, i);
98 
99  rgba[i] = s;
100  }
101 
102  return rgba;
103 }
104 
105 QStyle::StandardPixmap toQStyle(const QMessageBox::Icon& icon)
106 {
107  switch(icon)
108  {
109  case QMessageBox::Question:
110  return QStyle::SP_MessageBoxQuestion;
111  break;
112 
113  case QMessageBox::Warning:
114  return QStyle::SP_MessageBoxWarning;
115  break;
116 
117  case QMessageBox::Critical:
118  return QStyle::SP_MessageBoxCritical;
119  break;
120 
121  default:
122  return QStyle::SP_MediaVolumeMuted;
123  break;
124  }
125 }
126 
127 QMenu* te::qt::widgets::FindMenu(const QString& mnuText, QMenu* mnu)
128 {
129  return mnu->findChild<QMenu*>(mnuText);
130 }
131 
132 QMenu* te::qt::widgets::FindMenu(const QString& mnuText, QMenuBar* bar)
133 {
134  return bar->findChild<QMenu*>(mnuText);
135 }
136 
137 QAction* te::qt::widgets::FindAction(const QString& actText, QMenu* mnu)
138 {
139  return mnu->findChild<QAction*>(actText);
140 }
141 
142 QAction* te::qt::widgets::FindAction(const QString& actText, QMenuBar* mnuBar)
143 {
144  return mnuBar->findChild<QAction*>(actText);
145 }
146 
147 QActionGroup* te::qt::widgets::FindActionGroup(const QString& actGroupText, QMenu* mnu)
148 {
149  return mnu->findChild<QActionGroup*>(actGroupText);
150 }
151 
152 QActionGroup* te::qt::widgets::FindActionGroup(const QString& actGroupText, QMenuBar* mnuBar)
153 {
154  return mnuBar->findChild<QActionGroup*>(actGroupText);
155 }
156 
157 QMenu* CreateMenu(const QString& mnuName, QMenu* p)
158 {
159  QMenu* mnu = p->findChild<QMenu*>(mnuName);
160 
161  if(mnu == 0)
162  {
163  QStringList ls = mnuName.split(".");
164  mnu = p->addMenu(ls[ls.size()-1]);
165 
166  mnu->setObjectName(mnuName);
167  }
168 
169  return mnu;
170 }
171 
172 QMenu* CreateMenu(const QString& mnuName, QMenuBar* p)
173 {
174  QMenu* mnu = te::qt::widgets::FindMenu(mnuName, p);
175 
176  if(mnu == 0)
177  {
178  QStringList ls = mnuName.split(".");
179  mnu = new QMenu(ls[ls.size()-1], p);
180  mnu->setObjectName(mnuName);
181 
182  QMenu* helpMnu = te::qt::widgets::FindMenu("Help", p);
183 
184  QAction* act = (helpMnu == 0) ? 0 : helpMnu->menuAction();
185 
186  p->insertMenu(act, mnu);
187  }
188 
189  return mnu;
190 }
191 
192 QMenu* te::qt::widgets::GetMenu(const QString& mnuText, QMenu* mnu)
193 {
194  QMenu* res = FindMenu(mnuText, mnu);
195 
196  QStringList mnus = mnuText.split('.');
197 
198  for(int i=1; i<mnus.size(); i++)
199  mnus[i] = mnus[i-1] + "." + mnus[i];
200 
201  for(int i=0; i<mnus.size(); i++)
202  res = (i == 0) ?
203  CreateMenu(mnus[i], mnu) :
204  CreateMenu(mnus[i], res);
205 
206  return res;
207 }
208 
209 QMenu* te::qt::widgets::GetMenu(const QString& mnuText, QMenuBar* bar)
210 {
211  QMenu* res = FindMenu(mnuText, bar);
212 
213  QStringList mnus = mnuText.split('.');
214 
215  for(int i=1; i<mnus.size(); i++)
216  mnus[i] = mnus[i-1] + "." + mnus[i];
217 
218  for(int i=0; i<mnus.size(); i++)
219  {
220  res = (i == 0) ?
221  CreateMenu(mnus[i], bar) :
222  CreateMenu(mnus[i], res);
223  }
224  return res;
225 }
226 
228 {
229  return te::color::RGBAColor(color.red(), color.green(), color.blue(), color.alpha());
230 }
231 
233 {
234  QColor qcolor(color.getRgba());
235  qcolor.setAlpha(qAlpha(color.getRgba()));
236  return qcolor;
237 }
238 
239 void te::qt::widgets::Config2DrawPolygons(te::map::Canvas* canvas, const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth)
240 {
241  canvas->setPolygonContourWidth(contourWidth);
242  canvas->setPolygonContourColor(Convert2TerraLib(contourColor));
243  canvas->setPolygonFillColor(Convert2TerraLib(fillColor));
244 }
245 
246 void te::qt::widgets::Config2DrawLines(te::map::Canvas* canvas, const QColor& color, const std::size_t& width)
247 {
248  canvas->setLineWidth(width);
249  canvas->setLineColor(Convert2TerraLib(color));
250 }
251 
252 void te::qt::widgets::Config2DrawPoints(te::map::Canvas* canvas, const QColor& color, const std::size_t& width)
253 {
254  canvas->setPointWidth(width);
255  canvas->setPointColor(Convert2TerraLib(color));
256 }
257 
258 void te::qt::widgets::Config2DrawPoints(te::map::Canvas* canvas, const QString& markName, const std::size_t& size,
259  const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth)
260 {
261  te::se::Stroke* stroke = te::se::CreateStroke(contourColor.name().toStdString(),
262  QString::number(contourWidth).toStdString(),
263  QString::number(contourColor.alphaF()).toStdString());
264 
265  te::se::Fill* fill = te::se::CreateFill(fillColor.name().toStdString(),
266  QString::number(fillColor.alphaF()).toStdString());
267 
268  te::se::Mark* mark = te::se::CreateMark(markName.toStdString(), stroke, fill);
269 
271 
273  canvas->setPointPattern(rgba, size, size);
274 
275  te::common::Free(rgba, size);
276 
277  delete mark;
278 }
279 
280 void te::qt::widgets::Config2DrawLayerSelection(te::map::Canvas* canvas, const QColor& selectionColor, const te::gm::GeomType& type)
281 {
282  switch(type)
283  {
284  case te::gm::PolygonType:
292  {
293  QColor fillColor = selectionColor;
294  fillColor.setAlpha(128);
295 
296  Config2DrawPolygons(canvas, fillColor, Qt::black, 2);
297  }
298  break;
299 
308  {
309  QColor fillColor = selectionColor;
310  fillColor.setAlpha(128);
311 
312  Config2DrawLines(canvas, fillColor, 6);
313  }
314  break;
315 
316  case te::gm::PointType:
317  case te::gm::PointZType:
318  case te::gm::PointMType:
319  case te::gm::PointZMType:
324  {
325  QColor fillColor = selectionColor;
326  fillColor.setAlpha(70);
327 
328  QColor contourColor = selectionColor;
329  contourColor.setAlpha(150);
330 
331  Config2DrawPoints(canvas, "square", 24, fillColor, contourColor,2);
332  }
333  break;
334 
335  default:
336  return;
337  }
338 }
339 
340 QPixmap te::qt::widgets::CreatePixmapIcon(const int& size, const QColor& penColor, const QColor& brushColor, const int& contourSize)
341 {
342  QPixmap pix(size, size);
343  pix.fill(Qt::transparent);
344 
345  int offset = 2;
346 
347  QPainterPath path;
348  path.addRect(offset, offset, pix.width() - 2 * offset, pix.height() - 2 * offset);
349 
350  QPen pen;
351  pen.setColor(penColor);
352  pen.setWidth(contourSize);
353 
354  QBrush brush;
355  brush.setColor(brushColor);
356 
357  QPainter p(&pix);
358  p.setPen(pen);
359  p.setBrush(brushColor);
360 
361  p.fillPath(path, brush);
362  p.drawPath(path);
363 
364  return pix;
365 }
366 
367 void te::qt::widgets::AddFilePathToSettings(const QString& path, const QString& typeFile)
368 {
369  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
370 
371  QString key = "Last used file path/" + typeFile;
372 
373  sett.setValue(key, path);
374 }
375 
376 QString te::qt::widgets::GetFilePathFromSettings(const QString& typeFile)
377 {
378  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
379 
380  QString key = "Last used file path/" + typeFile;
381 
382  return sett.value(key).toString();
383 }
384 
385 QString te::qt::widgets::Convert2Qt(const std::string& text, const te::common::CharEncoding& encoding)
386 {
387  try
388  {
389  switch(encoding)
390  {
392  return text.c_str();
393 
394  case te::common::UTF8:
395  return QString::fromUtf8(text.c_str());
396 
397  case te::common::LATIN1:
398  return QString::fromLatin1(text.c_str());
399 
400  // continues...
401 
402  default:
403  {
404 #ifdef TERRALIB_CHARENCODING_ENABLED
405  std::string latin1 = te::common::CharEncodingConv::convert(text, encoding, te::common::LATIN1);
406  return QString::fromLatin1(latin1.c_str());
407 #else
408  return text.c_str();
409 #endif
410  }
411  }
412  }
413  catch(...)
414  {
415  return text.c_str();
416  }
417 }
418 
420 {
421  QString filter;
422 
423  if( te::da::DataSourceFactory::find( "GDAL" ) )
424  {
425  std::auto_ptr< te::da::DataSource > dsPtr = te::da::DataSourceFactory::make( "GDAL" );
426 
427  if( dsPtr.get() )
428  {
429  std::map< std::string, std::string > specCap = dsPtr->getCapabilities().getSpecificCapabilities();
430 
431  if( specCap.find( "SUPPORTED_EXTENSIONS" ) != specCap.end() )
432  {
433  std::string fileExtensions = specCap[ "SUPPORTED_EXTENSIONS" ];
434  std::string extFilter;
435  std::string uCaseToken;
436  std::string lCaseToken;
437  std::vector< std::string > tokens;
438  te::common::Tokenize( fileExtensions, tokens, ";" );
439 
440  for( unsigned int tokensIdx = 0 ; tokensIdx < tokens.size() ; ++tokensIdx )
441  {
442  uCaseToken = te::common::Convert2UCase( tokens[ tokensIdx ] );
443  lCaseToken = te::common::Convert2LCase( tokens[ tokensIdx ] );
444  extFilter = uCaseToken + " Raster File (" + "*." + lCaseToken + " *." +
445  uCaseToken + ")";
446 
447  if( uCaseToken == "TIF" )
448  {
449  filter = QString( extFilter.c_str() ) + ( filter.isEmpty() ? "" : ";;" ) + filter;
450  }
451  else
452  {
453  filter += ( filter.isEmpty() ? "" : ";;" ) + QString( extFilter.c_str() );
454  }
455  }
456  }
457  }
458  }
459 
460  filter += QString( ( filter.isEmpty() ? "" : ";;" ) ) +
461  "Web Map Service - WMS (*.xml *.wms);;Web Coverage Service - WCS (*.xml *.wcs);;All Files (*.*)";
462 
463  return filter;
464 }
465 
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:227
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:280
virtual void setPointWidth(int w)=0
It sets the point width. If point has a patterns, this pattern is scaled to width.
static bool find(const std::string &dsType)
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:137
QStyle::StandardPixmap toQStyle(const QMessageBox::Icon &icon)
Definition: Utils.cpp:105
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:197
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:367
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:246
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
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
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:239
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
QMenu * CreateMenu(const QString &mnuName, QMenu *p)
Definition: Utils.cpp:157
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
TEQTWIDGETSEXPORT QActionGroup * FindActionGroup(const QString &actGroupText, QMenu *mnu)
Definition: Utils.cpp:147
static MarkRendererManager & getInstance()
It returns a reference to the singleton instance.
static std::auto_ptr< DataSource > make(const std::string &dsType)
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
Definition: Utils.cpp:55
TEQTWIDGETSEXPORT void SetChildrenCheckState(QTreeWidgetItem *item, int column, Qt::CheckState state)
It sets the check state for the children of a item.
Definition: Utils.cpp:54
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 QString GetDiskRasterFileSelFilter()
Returns a disk raster file selection filter base on current supported formats.
Definition: Utils.cpp:419
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:252
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:69
TEQTWIDGETSEXPORT QMenu * FindMenu(const QString &mnuText, QMenu *mnu)
Finds a menu item in the mnu object.
Definition: Utils.cpp:127
#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:232
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:376
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:192
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:340
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
Definition: Utils.cpp:109