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