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