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