All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../common/CharEncodingConv.h"
31 #include "../../color/RGBAColor.h"
32 #include "../../geometry/Enums.h"
33 #include "../../maptools/Enums.h"
34 #include "Config.h"
35 #include "Globals.h"
36 
37 // Qt
38 #include <QColor>
39 #include <QObject>
40 #include <QPixmap>
41 #include <QString>
42 
43 // STL
44 #include <string>
45 
46 // Forward declarations
47 class QAction;
48 class QActionGroup;
49 class QImage;
50 class QMenu;
51 class QMenuBar;
52 class QTreeWidgetItem;
53 
54 namespace te
55 {
56  namespace map
57  {
58  class Canvas;
59  }
60 
61  namespace qt
62  {
63  namespace widgets
64  {
65  /*!
66  \brief It sets the check state for the children of a item.
67  */
68  TEQTWIDGETSEXPORT void SetChildrenCheckState(QTreeWidgetItem* item, int column, Qt::CheckState state);
69 
70  /*!
71  \brief It returns the file format as a NULL terminated string.
72 
73  \return The file format as a NULL terminated string.
74  */
75  inline const char* GetFormat(te::map::ImageType t)
76  {
77  switch(t)
78  {
79  case te::map::PNG:
80  return Globals::sm_pngFmt;
81 
82  case te::map::JPEG:
83  return Globals::sm_jpegFmt;
84 
85  case te::map::GIF:
86  return Globals::sm_gifFmt;
87 
88  case te::map::BMP:
89  return Globals::sm_bmpFmt;
90 
91  case te::map::XPM:
92  return Globals::sm_xpmFmt;
93 
94  case te::map::XBM:
95  return Globals::sm_xbmFmt;
96 
97  default:
99  };
100  }
101 
102  /*!
103  \brief It creates a QImage from an RGBA color array.
104 
105  \param img Image - a square matrix of int's where each position corresponds to a RGBA color.
106  \param width The image width.
107  \param height The image height.
108 
109  \return A pointer to a QImage.
110 
111  \note The caller of this method will take the ownership of the returned pointer.
112  */
113  TEQTWIDGETSEXPORT QImage* GetImage(te::color::RGBAColor** img, int width, int height);
114 
115  /*!
116  \brief It creates a RGBA color array from a QImage.
117 
118  \param img QImage that will be converted.
119 
120  \return Image - a square matrix of int's where each position corresponds to a RGBA color.
121 
122  \note The caller of this method will take the ownership of the returned pointer.
123  */
125 
126  /*!
127  \brief Finds a menu item in the \a mnu object.
128 
129  Use this function to find menus and submenus. There is a sintaxe that is for required sub-menus. Use the names separated by \".\" character,
130  e.g to return a menu named \"Layers\" inside another menu named \"Add\" calls this function using "Add.Layers" as the value of the
131  \a mnuText argument.
132 
133  \note Pay attention on the translations.
134 
135  \param mnuText Name of the QMenu searched.
136 
137  \param mnu The menu where the submenu must be searched.
138 
139  \return The pointer to the QMenu object or NULL if it do not exists.
140  */
141  TEQTWIDGETSEXPORT QMenu* FindMenu(const QString& mnuText, QMenu* mnu);
142 
143  /*!
144  \overload QMenu* findMenu(const QString& mnuText, QMenuBar* bar)
145 
146  The difference for the above function is that this will search in all QMenu objects contained in the \a bar.
147  */
148  TEQTWIDGETSEXPORT QMenu* FindMenu(const QString& mnuText, QMenuBar* bar);
149 
150  /*!
151  \brief Gets a menu or submenu contained in the \a mnu object.
152 
153  This function searches by the menu exactly as the FindMenu function. The difference is that if the menu do not exists,
154  this function will create it, in all levels that may be required. Use the syntax described above to the search / create the submenus desired.
155 
156  \param mnuText Name of the menu. (This will be used as texts of the menus.)
157 
158  \param mnu The menu that will used in search.
159 
160  \return A pointer to a QMenu object.
161  */
162  TEQTWIDGETSEXPORT QMenu* GetMenu(const QString& mnuText, QMenu* mnu);
163 
164  /*!
165  \overload QMenu* getMenu(const QString& mnuText, QMenuBar* bar)
166 
167  The difference for the above function is that it will search in all QMenu objects contained in the \a bar.
168  */
169  TEQTWIDGETSEXPORT QMenu* GetMenu(const QString& mnuText, QMenuBar* bar);
170 
171  /*!
172  \brief
173 
174  \param
175  \param
176 
177  \return
178  */
179  TEQTWIDGETSEXPORT QAction* FindAction(const QString& actText, QMenu* mnu);
180 
181  /*!
182  \brief
183 
184  \param
185  \param
186 
187  \return
188  */
189  TEQTWIDGETSEXPORT QAction* FindAction(const QString& actText, QMenuBar* mnuBar);
190 
191  /*!
192  \brief
193 
194  \param
195  \param
196 
197  \return
198  */
199  TEQTWIDGETSEXPORT QActionGroup* FindActionGroup(const QString& actGroupText, QMenu* mnu);
200 
201  /*!
202  \brief
203 
204  \param
205  \param
206 
207  \return
208  */
209  TEQTWIDGETSEXPORT QActionGroup* FindActionGroup(const QString& actText, QMenuBar* mnuBar);
210 
211  /*!
212  \brief It converts a Qt Color to TerraLib Color.
213 
214  \param color The Qt Color that will be converted.
215  */
217 
218  /*!
219  \brief It converts a TerraLib Color to Qt Color.
220 
221  \param color The TerraLib Color that will be converted.
222  */
224 
225  /*!
226  \brief It configs (i.e. prepares) the given canvas to draw polygons.
227 
228  \param canvas The canvas that will be configured. Do not pass NULL!
229  \param fillColor The color that will be used to fill the polygons.
230  \param contourColor The color that will be used to contour the polygons.
231  \param contourWidth The width that will be used to contour the polygons.
232  */
233  TEQTWIDGETSEXPORT void Config2DrawPolygons(te::map::Canvas* canvas, const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth = 1);
234 
235  /*!
236  \brief It configs (i.e. prepares) the given canvas to draw lines.
237 
238  \param canvas The canvas that will be configured. Do not pass NULL!
239  \param color The color that will be used to draw the lines.
240  \param width The width that will be used to draw the lines.
241  */
242  TEQTWIDGETSEXPORT void Config2DrawLines(te::map::Canvas* canvas, const QColor& color, const std::size_t& width = 1);
243 
244  /*!
245  \brief It configs (i.e. prepares) the given canvas to draw points.
246 
247  \param canvas The canvas that will be configured. Do not pass NULL!
248  \param color The color that will be used to draw the points.
249  \param width The width that will be used to draw the points.
250  */
251  TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas* canvas, const QColor& color, const std::size_t& width = 1);
252 
253  /*!
254  \brief It configs (i.e. prepares) the given canvas to draw points.
255 
256  \param canvas The canvas that will be configured. Do not pass NULL!
257  \param markName The mark name that will be used to draw the points.
258  \param size The size that will be used to draw the points.
259  \param fillColor The color that will be used to fill the mark.
260  \param contourColor The color that will be used to contour the mark.
261  \param contourWidth The width that will be used to contour the mark.
262  */
263  TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas* canvas, const QString& markName, const std::size_t& size,
264  const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth = 1);
265 
266  /*!
267  \brief It configs (i.e. prepares) the given canvas to draw a layer selection.
268 
269  \param canvas The canvas that will be configured. Do not pass NULL!
270  \param selectionColor The color that will be used to draw the selection.
271  \param type The geometry type that will be drawn.
272  */
273  TEQTWIDGETSEXPORT void Config2DrawLayerSelection(te::map::Canvas* canvas, const QColor& selectionColor, const te::gm::GeomType& type);
274 
275  /*!
276  \brief It creates a pixmap to use as icon (to be used as legend icon).
277 
278  \param size The height and width for the pixmap.
279  \param penColor The color that will be used to set the pen.
280  \param brushColor The color that will be used to set the brush.
281  \param contourSize The countour size.
282  */
283  TEQTWIDGETSEXPORT QPixmap CreatePixmapIcon(const int& size, const QColor& penColor, const QColor& brushColor, const int& contourSize);
284 
285  /*!
286  \brief Save last used path in QSettings.
287 
288  \param path The path to last used file.
289 
290  \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.
291 
292  \note The params are case sensitive. It means that a "Vector" value for the argument \a typeFile is unkowned by application.
293  */
294  TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString& path, const QString& typeFile);
295 
296  /*!
297  \brief Returns the value of the last saved file path for the \a typeFile required.
298 
299  \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.
300 
301  \return The path if encountered or a null QString.
302  */
303  TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString& typeFile);
304 
305  /*!
306  \brief It converts the given string to an appropriate character encoding.
307 
308  \param text The source string.
309  \param encoding The type of the character encoding of the string to be converted to an appropriate charset.
310 
311  \return The converted string.
312  */
313  TEQTWIDGETSEXPORT QString Convert2Qt(const std::string& text, const te::common::CharEncoding& encoding);
314 
315  /*!
316  \brief Returns a disk raster file selection filter base on current supported formats.
317 
318  \return Returns a disk raster file selection filter base on current supported formats.
319  */
321 
322  } // end namespace widgets
323  } // end namespace qt
324 } // end namespace te
325 
326 #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 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
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
CharEncoding
Supported charsets (character encoding).
TEQTWIDGETSEXPORT QAction * FindAction(const QString &actText, QMenu *mnu)
Definition: Utils.cpp:137
static const char * sm_xpmFmt
Definition: Globals.h:56
static const char * sm_jpegFmt
Definition: Globals.h:53
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:367
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
Configuration flags for the TerraLib Qt Widgets.
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
const char * GetFormat(te::map::ImageType t)
It returns the file format as a NULL terminated string.
Definition: Utils.h:75
static const char * sm_xbmFmt
Definition: Globals.h:57
TEQTWIDGETSEXPORT QActionGroup * FindActionGroup(const QString &actGroupText, QMenu *mnu)
Definition: Utils.cpp:147
static const char * sm_pngFmt
Definition: Globals.h:52
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
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
static const char * sm_unknownFmt
Definition: Globals.h:58
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
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
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:66
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
static const char * sm_bmpFmt
Definition: Globals.h:55
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
static const char * sm_gifFmt
Definition: Globals.h:54