Utils.h
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.h
22 
23  \brief This file contains utility functions for dealing with Qt controls.
24  */
25 
26 #ifndef __TERRALIB_QTWIDGETS_INTERNAL_UTILS_H
27 #define __TERRALIB_QTWIDGETS_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "../../color/RGBAColor.h"
31 #include "../../geometry/Enums.h"
32 #include "../../maptools/AbstractLayer.h"
33 #include "../../maptools/Enums.h"
34 #include "../../maptools/MapDisplay.h"
35 #include "Config.h"
36 #include "Globals.h"
37 
38 // Qt
39 #include <QColor>
40 #include <QModelIndex>
41 #include <QObject>
42 #include <QPixmap>
43 #include <QString>
44 #include <QHeaderView>
45 
46 // STL
47 #include <string>
48 #include <set>
49 
50 // Forward declarations
51 class QAbstractItemModel;
52 class QAction;
53 class QActionGroup;
54 class QClipboard;
55 class QImage;
56 class QMenu;
57 class QMenuBar;
58 class QTreeWidgetItem;
59 
60 namespace te
61 {
62  namespace map
63  {
64  class Canvas;
65  }
66 
67  namespace qt
68  {
69  namespace widgets
70  {
71  class LayerItemView;
72 
73  /*!
74  \brief It sets the check state for the children of a item.
75  */
76  TEQTWIDGETSEXPORT void SetChildrenCheckState(QTreeWidgetItem* item, int column, Qt::CheckState state);
77 
78  /*!
79  \brief It returns the file format as a NULL terminated string.
80 
81  \return The file format as a NULL terminated string.
82  */
83  inline const char* GetFormat(te::map::ImageType t)
84  {
85  switch(t)
86  {
87  case te::map::PNG:
88  return Globals::sm_pngFmt;
89 
90  case te::map::JPEG:
91  return Globals::sm_jpegFmt;
92 
93  case te::map::GIF:
94  return Globals::sm_gifFmt;
95 
96  case te::map::BMP:
97  return Globals::sm_bmpFmt;
98 
99  case te::map::XPM:
100  return Globals::sm_xpmFmt;
101 
102  case te::map::XBM:
103  return Globals::sm_xbmFmt;
104 
105  default:
106  return Globals::sm_unknownFmt;
107  };
108  }
109 
110  /*!
111  \brief It creates a QImage from an RGBA color array.
112 
113  \param img Image - a square matrix of int's where each position corresponds to a RGBA color.
114  \param width The image width.
115  \param height The image height.
116 
117  \return A pointer to a QImage.
118 
119  \note The caller of this method will take the ownership of the returned pointer.
120  */
121  TEQTWIDGETSEXPORT QImage* GetImage(te::color::RGBAColor** img, int width, int height);
122 
123  /*!
124  \brief It creates a RGBA color array from a QImage.
125 
126  \param img QImage that will be converted.
127 
128  \return Image - a square matrix of int's where each position corresponds to a RGBA color.
129 
130  \note The caller of this method will take the ownership of the returned pointer.
131  */
133 
134  /*!
135  \brief Finds a menu item in the \a mnu object.
136 
137  Use this function to find menus and submenus. There is a sintaxe that is for required sub-menus. Use the names separated by \".\" character,
138  e.g to return a menu named \"Layers\" inside another menu named \"Add\" calls this function using "Add.Layers" as the value of the
139  \a mnuText argument.
140 
141  \note Pay attention on the translations.
142 
143  \param mnuText Name of the QMenu searched.
144 
145  \param mnu The menu where the submenu must be searched.
146 
147  \return The pointer to the QMenu object or NULL if it do not exists.
148  */
149  TEQTWIDGETSEXPORT QMenu* FindMenu(const QString& mnuText, QMenu* mnu);
150 
151  /*!
152  \overload QMenu* findMenu(const QString& mnuText, QMenuBar* bar)
153 
154  The difference for the above function is that this will search in all QMenu objects contained in the \a bar.
155  */
156  TEQTWIDGETSEXPORT QMenu* FindMenu(const QString& mnuText, QMenuBar* bar);
157 
158  /*!
159  \brief Gets a menu or submenu contained in the \a mnu object.
160 
161  This function searches by the menu exactly as the FindMenu function. The difference is that if the menu do not exists,
162  this function will create it, in all levels that may be required. Use the syntax described above to the search / create the submenus desired.
163 
164  \param mnuText Name of the menu. (This will be used as texts of the menus.)
165 
166  \param mnu The menu that will used in search.
167 
168  \return A pointer to a QMenu object.
169  */
170  TEQTWIDGETSEXPORT QMenu* GetMenu(const QString& mnuText, QMenu* mnu);
171 
172  /*!
173  \overload QMenu* getMenu(const QString& mnuText, QMenuBar* bar)
174 
175  The difference for the above function is that it will search in all QMenu objects contained in the \a bar.
176  */
177  TEQTWIDGETSEXPORT QMenu* GetMenu(const QString& mnuText, QMenuBar* bar);
178 
179  /*!
180  \brief
181 
182  \param
183  \param
184 
185  \return
186  */
187  TEQTWIDGETSEXPORT QAction* FindAction(const QString& actText, QMenu* mnu);
188 
189  /*!
190  \brief
191 
192  \param
193  \param
194 
195  \return
196  */
197  TEQTWIDGETSEXPORT QAction* FindAction(const QString& actText, QMenuBar* mnuBar);
198 
199  /*!
200  \brief
201 
202  \param
203  \param
204 
205  \return
206  */
207  TEQTWIDGETSEXPORT QActionGroup* FindActionGroup(const QString& actGroupText, QMenu* mnu);
208 
209  /*!
210  \brief
211 
212  \param
213  \param
214 
215  \return
216  */
217  TEQTWIDGETSEXPORT QActionGroup* FindActionGroup(const QString& actText, QMenuBar* mnuBar);
218 
219  /*!
220  \brief It converts a Qt Color to TerraLib Color.
221 
222  \param color The Qt Color that will be converted.
223  */
225 
226  /*!
227  \brief It converts a TerraLib Color to Qt Color.
228 
229  \param color The TerraLib Color that will be converted.
230  */
232 
233  /*!
234  \brief It configs (i.e. prepares) the given canvas to draw polygons.
235 
236  \param canvas The canvas that will be configured. Do not pass NULL!
237  \param fillColor The color that will be used to fill the polygons.
238  \param contourColor The color that will be used to contour the polygons.
239  \param contourWidth The width that will be used to contour the polygons.
240  */
241  TEQTWIDGETSEXPORT void Config2DrawPolygons(te::map::Canvas* canvas, const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth = 1);
242 
243  /*!
244  \brief It configs (i.e. prepares) the given canvas to draw lines.
245 
246  \param canvas The canvas that will be configured. Do not pass NULL!
247  \param color The color that will be used to draw the lines.
248  \param width The width that will be used to draw the lines.
249  */
250  TEQTWIDGETSEXPORT void Config2DrawLines(te::map::Canvas* canvas, const QColor& color, const std::size_t& width = 1);
251 
252  /*!
253  \brief It configs (i.e. prepares) the given canvas to draw points.
254 
255  \param canvas The canvas that will be configured. Do not pass NULL!
256  \param color The color that will be used to draw the points.
257  \param width The width that will be used to draw the points.
258  */
259  TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas* canvas, const QColor& color, const std::size_t& width = 1);
260 
261  /*!
262  \brief It configs (i.e. prepares) the given canvas to draw points.
263 
264  \param canvas The canvas that will be configured. Do not pass NULL!
265  \param markName The mark name that will be used to draw the points.
266  \param size The size that will be used to draw the points.
267  \param fillColor The color that will be used to fill the mark.
268  \param contourColor The color that will be used to contour the mark.
269  \param contourWidth The width that will be used to contour the mark.
270  */
271  TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas* canvas, const QString& markName, const std::size_t& size,
272  const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth = 1);
273 
274  /*!
275  \brief It configs (i.e. prepares) the given canvas to draw a layer selection.
276 
277  \param canvas The canvas that will be configured. Do not pass NULL!
278  \param selectionColor The color that will be used to draw the selection.
279  \param type The geometry type that will be drawn.
280  */
281  TEQTWIDGETSEXPORT void Config2DrawLayerSelection(te::map::Canvas* canvas, const QColor& selectionColor, const te::gm::GeomType& type);
282 
283  /*!
284  \brief It creates a pixmap to use as icon (to be used as legend icon).
285 
286  \param size The height and width for the pixmap.
287  \param penColor The color that will be used to set the pen.
288  \param brushColor The color that will be used to set the brush.
289  \param contourSize The countour size.
290  */
291  TEQTWIDGETSEXPORT QPixmap CreatePixmapIcon(const int& size, const QColor& penColor, const QColor& brushColor, const int& contourSize);
292 
293  /*!
294  \brief Save last used path in QSettings.
295 
296  \param path The path to last used file.
297 
298  \param typeFile The type of file opened. Actually we assume three values: "vector", "raster", "database". Any other value may be not used in the system.
299 
300  \note The params are case sensitive. It means that a "Vector" value for the argument \a typeFile is unkowned by application.
301  */
302  TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString& path, const QString& typeFile);
303 
304  /*!
305  \brief Returns the value of the last saved file path for the \a typeFile required.
306 
307  \param typeFile The type of file opened. Actually we assume three values: "vector", "raster", "database". Any other value may be not used in the system.
308 
309  \return The path if encountered or a null QString.
310  */
311  TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString& typeFile);
312 
313  /*!
314  \brief It converts the given string to an appropriate character encoding.
315 
316  \param text The source string.
317  \param encoding The type of the character encoding of the string to be converted to an appropriate charset.
318 
319  \return The converted string.
320  */
321  TEQTWIDGETSEXPORT QString Convert2Qt(const std::string& text, te::core::EncodingType encoding);
322 
323  /*!
324  \brief Returns a disk raster file selection filter base on current supported formats.
325  \param creationSupport Return only those extensions with dataset creation support.
326  \return Returns a disk raster file selection filter base on current supported formats.
327  */
328  TEQTWIDGETSEXPORT QString GetDiskRasterFileSelFilter( const bool creationSupport );
329 
330  /*!
331  \brief Returns all GDAL supported raster extensions.
332  \param extensionsVec The requested extensions.
333  \param creationSupport If true, only those extensions with dataset creation support will be returned.
334  */
335  TEQTWIDGETSEXPORT void GetGDALSupportedRasterExtensions(std::vector<std::string>& extensionsVec,
336  const bool creationSupport );
337 
338  /*!
339  \brief Returns a disk vector file selection filter base on current supported formats.
340 
341  \param creationSupport Return only those extensions with dataset creation support.
342 
343  \return Returns a disk vector file selection filter base on current supported formats.
344  */
345  TEQTWIDGETSEXPORT QString GetDiskVectorFileSelFilter( const bool creationSupport, std::set<std::string> extensionFilter= std::set<std::string>());
346 
347  /*!
348  \brief Returns all GDAL supported vector extensions.
349  \param extensionsVec The requested extensions.
350  \param creationSupport If true, only those extensions with dataset creation support will be returned.
351  */
352  TEQTWIDGETSEXPORT void GetGDALSupportedVectorExtensions(std::vector<std::string>& extensionsVec,
353  const bool creationSupport );
354 
355  TEQTWIDGETSEXPORT std::list<te::map::AbstractLayerPtr> GetSelectedLayersOnly(te::qt::widgets::LayerItemView* view, const bool getFolder = false);
356 
357  TEQTWIDGETSEXPORT void GetValidLayers(QAbstractItemModel* model, const QModelIndex& parent, std::vector<te::map::AbstractLayerPtr>& layers);
358 
359  TEQTWIDGETSEXPORT void GetValidLayers(QAbstractItemModel* model, const QModelIndex& parent, std::list<te::map::AbstractLayerPtr>& layers);
360 
361  TEQTWIDGETSEXPORT void GetChangedAndVisibleLayers(const QModelIndexList& idxs, std::list<te::map::AbstractLayerPtr>& layers);
362 
363 
364  /*!
365  \brief Verify if the SRID from informed Layers are valids to execute the operation.
366 
367  \param firstSRID The SRID from the first layer.
368  \param secondSRID The SRID from the second layer.
369  \param msg Fiils this parameter in case of invalid SRIDs.
370 
371  return Resturns a bool value. If false, it fills the message parameter.
372  */
373  TEQTWIDGETSEXPORT bool isValidSRIDs(const int& firstSRID, const int& secondSRID, std::string& msg);
374 
375  /*!
376  * \brief Returns the size in pixels.
377  *
378  * \param device Dots per inch.
379  *
380  * \param mm Measure in millimeters.
381  *
382  * \param printer True if the drawing is on a printer.
383  *
384  * \return (\a mm * Physical dpi ) / 25,4mm (inch)
385  */
386  TEQTWIDGETSEXPORT int MillimetersToPixels(const QPaintDevice* device, const double& mm, const bool& printer);
387 
388  /*!
389  * \brief Returns a transparent pixmap of size \a s.
390  *
391  * \param s Size in pixels.
392  *
393  * \param c Background color.
394  *
395  * \return A transparent pixmap of size \a s.
396  */
397  TEQTWIDGETSEXPORT QPixmap* GetCleanImage(const QSize& s, const QColor& c);
398 
399  /*!
400  \brief Returns the user data location path.
401 
402  \return Returns the user data location path.
403  */
405 
406  /** @name DMS Functions
407  * Utilities functions section for DMS treatment.
408  */
409  //@{
410 
411  QString ConvertDecimalToDegree(const double& value, int precision);
412 
413  /*!
414  * \brief Returns the value as a formatted string.
415  *
416  * \param d Degrees.
417  *
418  * \param m Minutes.
419  *
420  * \param s Seconds.
421  *
422  * \return A string formatted as a degrees-minutes-seconds value.
423  */
424  TEQTWIDGETSEXPORT QString GetDMSAsString(const int& d, const int& m, const int& s);
425 
426  /*!
427  * \brief Transforms a degree-minute-second value into a decimal-degree value.
428  *
429  * \param d Degrees.
430  *
431  * \param m Minutes.
432  *
433  * \param s Seconds.
434  *
435  * \return The decimal-degree value.
436  */
437  TEQTWIDGETSEXPORT double DMS2DD(const int& d, const int& m, const int& s);
438 
439  /*!
440  * \brief Transforms a decimal-degree value in a degree-minute-second formatted string.
441  *
442  * \param value Value in decimal-degrees.
443  *
444  * \return The degree-minute-second representing the given \a value.
445  */
446  TEQTWIDGETSEXPORT QString DD2DMS(const double& value);
447 
448  /*!
449  * \brief Format coord precision value to show value.
450  *
451  * \param value Value in current srid.
452  *
453  * \return The string value with right precision.
454  */
455  TEQTWIDGETSEXPORT QString FormatCoord(const double& value, te::map::MapDisplay* display);
456 
457  /*!
458  * \brief Returns the QClipboard object
459  *
460  * \return Returns the QClipboard object
461  */
463 
464  /*!
465  * \brief Returns the QClipboard object
466  *
467  * \return Returns the QClipboard object
468  */
469  TEQTWIDGETSEXPORT void SetHeaderViewResizeMode(QHeaderView* header, QHeaderView::ResizeMode mode);
470 
471  //@}
472 
473  /*!
474  * \brief Converts a std::string to a QString
475  *
476  * \return The converted QString
477  */
478  TEQTWIDGETSEXPORT QString Convert2QString(const std::string& stdString);
479 
480  /*!
481  * \brief Converts a QString to a std::string
482  *
483  * \return The converted std::string
484  */
485  TEQTWIDGETSEXPORT std::string Convert2StdString(const QString& qString);
486 
487 
488 
489  } // end namespace widgets
490  } // end namespace qt
491 } // end namespace te
492 
493 #endif // __TERRALIB_QTWIDGETS_INTERNAL_UTILS_H
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:58
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:93
This class controls how a set of layers are displayed.
Definition: MapDisplay.h:46
static const char * sm_xpmFmt
Definition: Globals.h:56
static const char * sm_gifFmt
Definition: Globals.h:54
static const char * sm_pngFmt
Definition: Globals.h:52
static const char * sm_unknownFmt
Definition: Globals.h:58
static const char * sm_bmpFmt
Definition: Globals.h:55
static const char * sm_xbmFmt
Definition: Globals.h:57
static const char * sm_jpegFmt
Definition: Globals.h:53
A specialization of QTreeView for manipulate layers.
Definition: LayerItemView.h:79
EncodingType
Supported character encodings.
Definition: CharEncoding.h:51
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:42
ImageType
This enum specifies the possible input and output image formats supported by the canvas API.
Definition: Enums.h:39
@ BMP
Definition: Enums.h:44
@ GIF
Definition: Enums.h:43
@ XBM
Definition: Enums.h:46
@ XPM
Definition: Enums.h:45
@ JPEG
Definition: Enums.h:42
@ PNG
Definition: Enums.h:41
TEQTWIDGETSEXPORT std::list< te::map::AbstractLayerPtr > GetSelectedLayersOnly(te::qt::widgets::LayerItemView *view, const bool getFolder=false)
TEQTWIDGETSEXPORT std::string Convert2StdString(const QString &qString)
Converts a QString to a std::string.
TEQTWIDGETSEXPORT void SetChildrenCheckState(QTreeWidgetItem *item, int column, Qt::CheckState state)
It sets the check state for the children of a item.
TEQTWIDGETSEXPORT int MillimetersToPixels(const QPaintDevice *device, const double &mm, const bool &printer)
Returns the size in pixels.
TEQTWIDGETSEXPORT bool isValidSRIDs(const int &firstSRID, const int &secondSRID, std::string &msg)
Verify if the SRID from informed Layers are valids to execute the operation.
TEQTWIDGETSEXPORT void GetValidLayers(QAbstractItemModel *model, const QModelIndex &parent, std::vector< te::map::AbstractLayerPtr > &layers)
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
TEQTWIDGETSEXPORT double DMS2DD(const int &d, const int &m, const int &s)
Transforms a degree-minute-second value into a decimal-degree value.
TEQTWIDGETSEXPORT QString GetDiskRasterFileSelFilter(const bool creationSupport)
Returns a disk raster file selection filter base on current supported formats.
const char * GetFormat(te::map::ImageType t)
It returns the file format as a NULL terminated string.
Definition: Utils.h:83
TEQTWIDGETSEXPORT void SetHeaderViewResizeMode(QHeaderView *header, QHeaderView::ResizeMode mode)
Returns the QClipboard object.
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.
TEQTWIDGETSEXPORT QFont Convert2Qt(const te::se::Font *font)
It converts a TerraLib Font to Qt Font.
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.
TEQTWIDGETSEXPORT QString FormatCoord(const double &value, te::map::MapDisplay *display)
Format coord precision value to show value.
TEQTWIDGETSEXPORT te::se::Font * Convert2TerraLib(const QFont &font)
It converts a Qt Font to TerraLib Font.
TEQTWIDGETSEXPORT QPixmap * GetCleanImage(const QSize &s, const QColor &c)
Returns a transparent pixmap of size s.
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).
TEQTWIDGETSEXPORT QMenu * FindMenu(const QString &mnuText, QMenu *mnu)
Finds a menu item in the mnu object.
TEQTWIDGETSEXPORT QString Convert2QString(const std::string &stdString)
Converts a std::string to a QString.
TEQTWIDGETSEXPORT QClipboard * GetClipboard()
Returns the QClipboard object.
TEQTWIDGETSEXPORT QString GetDiskVectorFileSelFilter(const bool creationSupport, std::set< std::string > extensionFilter=std::set< std::string >())
Returns a disk vector file selection filter base on current supported formats.
TEQTWIDGETSEXPORT void GetGDALSupportedRasterExtensions(std::vector< std::string > &extensionsVec, const bool creationSupport)
Returns all GDAL supported raster extensions.
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.
TEQTWIDGETSEXPORT QActionGroup * FindActionGroup(const QString &actGroupText, QMenu *mnu)
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
TEQTWIDGETSEXPORT QAction * FindAction(const QString &actText, QMenu *mnu)
TEQTWIDGETSEXPORT QString DD2DMS(const double &value)
Transforms a decimal-degree value in a degree-minute-second formatted string.
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.
QString ConvertDecimalToDegree(const double &value, int precision)
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
TEQTWIDGETSEXPORT void GetChangedAndVisibleLayers(const QModelIndexList &idxs, std::list< te::map::AbstractLayerPtr > &layers)
TEQTWIDGETSEXPORT QString GetDMSAsString(const int &d, const int &m, const int &s)
Returns the value as a formatted string.
TEQTWIDGETSEXPORT QMenu * GetMenu(const QString &mnuText, QMenu *mnu)
Gets a menu or submenu contained in the mnu object.
TEQTWIDGETSEXPORT QString GetUserDataLocation()
Returns the user data location path.
TEQTWIDGETSEXPORT void GetGDALSupportedVectorExtensions(std::vector< std::string > &extensionsVec, const bool creationSupport)
Returns all GDAL supported vector extensions.
TerraLib.
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
Proxy configuration file for TerraView (see terraview_config.h).