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