Canvas.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 terralib/qt/widgets/canvas/Canvas.h
22 
23  \brief A canvas built on top of Qt.
24  */
25 
26 #ifndef __TERRALIB_QT_WIDGETS_INTERNAL_CANVAS_H
27 #define __TERRALIB_QT_WIDGETS_INTERNAL_CANVAS_H
28 
29 // TerraLib
30 #include "../../../maptools/Canvas.h"
31 #include "../Config.h"
32 
33 // Qt
34 #include <QColor>
35 #include <QPainter>
36 #include <QPolygonF>
37 #include <QWidget>
38 
39 namespace te
40 {
41  namespace qt
42  {
43  namespace widgets
44  {
45  /*!
46  \class Canvas
47 
48  \brief A canvas built on top of Qt.
49 
50  \ingroup widgets
51 
52  \sa te::map::Canvas
53  */
55  {
56  public:
57 
58  /** @name Initializer Methods
59  * Methods related to instantiation and destruction.
60  */
61  //@{
62 
63  /*!
64  \brief It initializes a new Canvas and associates a pixmap as the default device.
65 
66  \param w The internal pixmap width in pixels.
67  \param h The internal pixmap height in pixels.
68  \param devType The internal device. It can be Pixmap or Image.
69  */
70  Canvas(int w, int h, int devType = QInternal::Pixmap);
71 
72  /*!
73  \brief It initializes a new Canvas associated with the given paint device.
74 
75  \param device The paint device that will be associated to this canvas.
76 
77  \note The canvas will NOT take the ownership of the given device.
78  */
79  Canvas(QPaintDevice* device);
80 
81  /*!
82  \brief It initializes a new Canvas associated with the given painter.
83 
84  \param device The painter that will be associated to this canvas.
85 
86  \note The canvas will NOT take the ownership of the given painter.
87  */
88  Canvas(QPainter* painter);
89 
90  /*! \brief Destructor. */
91  ~Canvas();
92 
93  //@}
94 
95  /** @name Methods Inherited From Abstract Canvas
96  * These methods are re-implementations from abstract canvas.
97  */
98  //@{
99 
100  void setWindow(const double& llx, const double& lly,
101  const double& urx, const double& ury);
102 
103  const te::gm::Envelope& getWindow() const;
104 
105  void calcAspectRatio(double& llx, double& lly, double& urx, double& ury,
107 
108  void calcAspectRatio(te::gm::Envelope* envelope,
109  const te::map::AlignType hAlign = te::map::Center,
110  const te::map::AlignType vAlign = te::map::Center);
111 
112  void setBackgroundColor(const te::color::RGBAColor& color);
113 
114  te::color::RGBAColor getBackgroundColor() const;
115 
116  void clear();
117 
118  void resize(int w, int h);
119 
120  int getWidth() const;
121 
122  int getHeight() const;
123 
124  void draw(const te::gm::Geometry* geom);
125 
126  void draw(const te::gm::Point* point);
127 
128  void draw(const te::gm::MultiPoint* mpoint);
129 
130  void draw(const te::gm::LineString* line);
131 
132  void draw(const te::gm::MultiLineString* mline);
133 
134  void draw(const te::gm::Polygon* poly);
135 
136  void draw(const te::gm::MultiPolygon* mpoly);
137 
138  void draw(const te::gm::GeometryCollection* g);
139 
140  void draw(const te::gm::MultiSurface* g);
141 
142  void save(const char* fileName, te::map::ImageType t, int quality = 75, int fg = 0) const;
143 
144  char* getImage(te::map::ImageType t, std::size_t& size, int quality = 75, int fg = 0) const;
145 
146  te::color::RGBAColor** getImage(const int x = 0, const int y = 0, const int w = 0, const int h = 0) const;
147 
148  void freeImage(char* img) const;
149 
150  void drawImage(char* src, std::size_t size, te::map::ImageType t);
151 
152  void drawImage(te::color::RGBAColor** src, int w, int h);
153 
154  void drawImage(int x, int y, char* src, std::size_t size, te::map::ImageType t);
155 
156  void drawImage(int x, int y, te::color::RGBAColor** src, int w, int h);
157 
158  void drawImage(int x, int y, int w, int h, char* src, std::size_t size, te::map::ImageType t);
159 
160  void drawImage(int x, int y, int w, int h, te::color::RGBAColor** src, int srcw, int srch);
161 
162  void drawImage(int x, int y, int w, int h, char* src, std::size_t size, te::map::ImageType t, int sx, int sy, int sw, int sh);
163 
164  void drawImage(int x, int y, int w, int h, te::color::RGBAColor** src, int sx, int sy, int sw, int sh);
165 
166  void drawImage(int x, int y, te::rst::Raster* src, int opacity = TE_OPAQUE);
167 
168  void drawImage(int x, int y, int w, int h, te::rst::Raster* src, int sx, int sy, int sw, int sh, int opacity = TE_OPAQUE);
169 
170  void drawPixel(int x, int y);
171 
172  void drawPixel(int x, int y, const te::color::RGBAColor& color);
173 
174  void drawText(int x, int y,
175  const std::string& txt,
176  float angle = 0.0,
177  double anchorX = 0.5, double anchorY = 0.5,
178  int displacementX = 0, int displacementY = 0);
179 
180  void drawText(const te::gm::Point* p,
181  const std::string& txt,
182  float angle = 0.0,
183  double anchorX = 0.5, double anchorY = 0.5,
184  int displacementX = 0, int displacementY = 0);
185 
186  void drawText(const double& x, const double& y,
187  const std::string& txt,
188  float angle = 0.0,
189  double anchorX = 0.5, double anchorY = 0.5,
190  int displacementX = 0, int displacementY = 0);
191 
192  te::gm::Polygon* getTextBoundary(int x, int y,
193  const std::string& txt,
194  float angle = 0.0,
195  double anchorX = 0.5, double anchorY = 0.5,
196  int displacementX = 0, int displacementY = 0);
197 
198  te::gm::Polygon* getTextBoundary(const te::gm::Point* p,
199  const std::string& txt,
200  float angle = 0.0,
201  double anchorX = 0.5, double anchorY = 0.5,
202  int displacementX = 0, int displacementY = 0);
203 
204  te::gm::Polygon* getTextBoundary(const double& x, const double& y,
205  const std::string& txt,
206  float angle = 0.0,
207  double anchorX = 0.5, double anchorY = 0.5,
208  int displacementX = 0, int displacementY = 0);
209 
210  void setTextColor(const te::color::RGBAColor& color);
211 
212  void setTextOpacity(int opacity);
213 
214  void setFontFamily(const std::string& family);
215 
216  void setTextPointSize(double psize);
217 
218  void setTextStyle(te::se::Font::FontStyleType style);
219 
220  void setTextWeight(te::se::Font::FontWeightType weight);
221 
222  void setTextStretch(std::size_t stretch);
223 
224  void setTextUnderline(bool b);
225 
226  void setTextOverline(bool b);
227 
228  void setTextStrikeOut(bool b);
229 
230  void setTextDecorationColor(const te::color::RGBAColor& color);
231 
232  void setTextDecorationWidth(int width);
233 
234  void setTextContourColor(const te::color::RGBAColor& color);
235 
236  void setTextContourEnabled(bool b);
237 
238  void setTextContourOpacity(int opacity);
239 
240  void setTextContourWidth(int width);
241 
242  void setTextJustification(int justType);
243 
244  void setTextMultiLineSpacing(int spacing);
245 
246  void setPointColor(const te::color::RGBAColor& color);
247 
248  void setPointWidth(int w);
249 
250  void setPointPattern(te::color::RGBAColor** pattern, int ncols, int nrows);
251 
252  void setPointPattern(char* pattern, std::size_t size, te::map::ImageType t);
253 
254  void setPointPatternRotation(const double& angle);
255 
256  void setPointPatternOpacity(int opacity);
257 
258  void setLineColor(const te::color::RGBAColor& color);
259 
260  void setLinePattern(te::color::RGBAColor** pattern, int ncols, int nrows);
261 
262  void setLinePattern(char* pattern, std::size_t size, te::map::ImageType t);
263 
264  void setLinePatternRotation(const double& angle);
265 
266  void setLinePatternOpacity(int opacity);
267 
268  void setLineDashStyle(te::map::LineDashStyle style);
269 
270  void setLineDashStyle(const std::vector<double>& style);
271 
272  void setLineCapStyle(te::map::LineCapStyle style);
273 
274  void setLineJoinStyle(te::map::LineJoinStyle style);
275 
276  void setLineWidth(int w);
277 
278  void setPolygonFillColor(const te::color::RGBAColor& color);
279 
280  void setPolygonFillColor(const QBrush& color);
281 
282  void setPolygonContourColor(const te::color::RGBAColor& color);
283 
284  void setPolygonFillPattern(te::color::RGBAColor** pattern, int ncols, int nrows);
285 
286  void setPolygonFillPattern(char* pattern, std::size_t size, te::map::ImageType t);
287 
288  void setPolygonPatternWidth(int w);
289 
290  void setPolygonPatternRotation(const double& angle);
291 
292  void setPolygonPatternOpacity(int opacity);
293 
294  void setPolygonContourPattern(te::color::RGBAColor** pattern, int ncols, int nrows);
295 
296  void setPolygonContourPattern(char* pattern, std::size_t size, te::map::ImageType t);
297 
298  void setPolygonContourWidth(int w);
299 
300  void setPolygonContourPatternRotation(const double& angle);
301 
302  void setPolygonContourPatternOpacity(int opacity);
303 
304  void setPolygonContourDashStyle(te::map::LineDashStyle style);
305 
306  void setPolygonContourDashStyle(const std::vector<double>& style);
307 
308  void setPolygonContourCapStyle(te::map::LineCapStyle style);
309 
310  void setPolygonContourJoinStyle(te::map::LineJoinStyle style);
311 
312  void setEraseMode();
313 
314  void setNormalMode();
315 
316  //@}
317 
318  /** @name Qt Canvas Specific Methos
319  * Methods that belongs only to Qt Canvas.
320  */
321  //@{
322 
323  /*!
324  \brief It draws a text.
325 
326  Color and font family should be defined in advance.
327 
328  \param p The text entry point i device coordinate.
329  \param txt The text to be drawed.
330  \param angle The text rotation.
331  \param anchorX The horizontal text anchor.
332  \param anchorY The vertical text anchor.
333  \param displacementX The horizontal text displacement.
334  \param displacementY The vertical text displacement.
335 
336  \note Qt Canvas extended method.
337  */
338  void drawText(const QPoint& p,
339  const std::string& txt,
340  float angle = 0.0,
341  double anchorX = 0.5, double anchorY = 0.5,
342  int displacementX = 0, int displacementY = 0);
343 
344  /*!
345  \brief It returns text boundary. Color and font family should be defined in advance.
346 
347  \param p The text entry point in device coordinate.
348  \param tx The text to be drawed.
349  \param angle The text rotation.
350  \param anchorX The horizontal text anchor.
351  \param anchorY The vertical text anchor.
352  \param displacementX The horizontal text displacement.
353  \param displacementY The vertical text displacement.
354 
355  \return The text boundary in world coordinate.
356 
357  \note The caller of this method will take the ownership of the returned Polygon.
358 
359  \warning Qt Canvas extended method.
360  */
361  te::gm::Polygon* getTextBoundary(const QPoint& p, const std::string& txt, float angle = 0.0,
362  double anchorX = 0.5, double anchorY = 0.5,
363  int displacementX = 0, int displacementY = 0);
364 
365  /*!
366  \brief It returns the internal pixmap used to draw geographical objects.
367 
368  \return The internal pixmap used to draw geographical objects. Don't delete it! Null is returned if device is not a pixmap.
369 
370  \warning Qt Canvas extended method.
371 
372  \note You can not delete this pointer.
373  */
374  QPixmap* getPixmap() const;
375 
376  /*!
377  \brief It returns the internal image used to draw geographical objects.
378 
379  \return The internal image used to draw geographical objects. Don't delete it! Null is returned if device is not a image.
380 
381  \warning Qt Canvas extended method.
382 
383  \note You can not delete this pointer.
384  */
385  QImage* getImage() const;
386 
387  /*!
388  \brief It returns the internal device used to draw geographical objects.
389 
390  \return The internal device used to draw geographical objects. Don't delete it!
391 
392  \warning Qt Canvas extended method.
393 
394  \note You can not delete this pointer.
395  */
396  QPaintDevice* getDevice() const;
397 
398  /*!
399  \brief It sets new device as QPrinter.
400 
401  \param device The new paint device.
402  \param takeOwnerShip If true the canvas will take the ownership of the given device otherwise it is the caller responsability to release the device.
403  */
404  void setDevice(QPaintDevice* device, bool takeOwnerShip);
405 
406  /*!
407  \brief It returns the device resolution.
408 
409  \return The device resolution.
410  */
411  int getResolution();
412 
413  /*!
414  \brief It returns the matrix.
415 
416  \return The marix.
417  */
418  QMatrix getMatrix();
419 
420  void setMatrix(const QMatrix& matrix);
421 
422  /*! \brief Sets the given render hint on the canvas painter if on is true; otherwise clears the render hint. */
423  void setRenderHint(QPainter::RenderHint hint, bool on = true);
424 
425  //@}
426 
427  QPainter* getPainter();
428 
429  private:
430 
431  /** @name Copy Constructor and Assignment Operator
432  * Copy constructor and assignment operator not allowed.
433  */
434  //@{
435 
436  /*!
437  \brief Copy constructor not allowed.
438 
439  \param rhs The right-hand-side copy that would be used to copy from.
440  */
441  Canvas(const Canvas& rhs);
442 
443  /*!
444  \brief Assignment operator not allowed.
445 
446  \param rhs The right-hand-side copy that would be used to copy from.
447 
448  \return A reference to this object.
449  */
450  Canvas& operator=(const Canvas& rhs);
451 
452  //@}
453 
454  /** @name Auxiliary Methods
455  * Auxiliary methods.
456  */
457  //@{
458 
459  /*!
460  \brief It adjusts the given pen to use the given pattern.
461 
462  \param pen The pen that will be adjusted.
463  \param style The line custom dash style.
464  */
465  void setLineDashStyle(QPen& pen, const std::vector<double>& style);
466 
467  /*!
468  \brief It updates the alpha channel of the given image using the given opacity value.
469 
470  \param img The image that will be updated.
471  \param opacity The opacity value that will be used.
472  */
473  void updateAlpha(QImage& img, const int& opacity);
474 
475  /*!
476  \brief It creates two patterns. A pattern used to draw selected objects and
477  the other used to erase this pattern.
478  */
479  void createPointPatterns();
480 
481  /*!
482  \brief It draw the polygon contour.
483  */
484  void drawContour(const te::gm::LineString* line);
485 
486 
487  //@}
488 
489  private:
490 
491  QMatrix m_matrix; //!< Matrix that transforms the world coordinate to device coordinate.
492  te::gm::Envelope* m_envelope; //!< Stores the current world (or window) coordinates area
493 
494  QPainter* m_painter; //!< The painter used to draw geometric objects.
495  QTransform m_painterTransform; //!< This transform holds the original transform of the painter. It is stored to be combined to the canvas matrix every time setWindow is called
496  bool m_isPainterOwner; //!< Tells if canvas is the owner of the painter.
497 
498  bool m_isDeviceOwner; //!< Tells if canvas is the owner of the paint device.
499 
500  QColor m_bgColor; //!< Canvas background color. Defaults: white fully transparent.
501 
502  bool m_erase; //!< used for erase operation.
503  QPen m_ptPen; //!< The pen used to draw points.
504  QPointF m_pt; //!< Point buffer to avoid creating another point instance.
505  int m_ptWidth; //!< The width for point markers and point pattern.
506  QColor m_ptColor; //!< The color used to draw point (pixel) or marker.
507  QColor m_ptColorFrom; //!< Indicates the color that originated the pattern that shows the status of selection. Used for optmization
508  QImage* m_ptImg; //!< The marker or pattern used to point.
509  QImage* m_ptImgRotated; //!< The marker or pattern already with rotation used to draw points.
510  QImage* m_ptSelectionPatternImg; //!< The marker or pattern used to show selection status. The color is mixed with point pattern.
511  QImage* m_ptClearPatternImg; //!< The marker or pattern used to clear (erase point).
512  double m_ptRotation; //!< The point pattern rotation.
513  int m_ptVOffset; //!< Vertical offset in pixels (in device coordinate) applied to point pattern or marker.
514  int m_ptHOffset; //!< Horizontal offset in pixels (in device coordinate) applied to point pattern or marker.
515 
516  QColor m_lnColor; //!< The color used to draw lines.
517  QPen m_lnPen; //!< The pen used to draw lines.
518 
519  QColor m_polyContourColor; //!< The color used to draw polygon contour.
520  QPen m_polyContourPen; //!< The pen used to draw contour polygon.
521 
522  QColor m_polyColor; //!< The color used to fill polygon (solid, marker or pattern).
523  QBrush m_polyBrush; //!< The brush used to fill polygon.
524  QBrush m_polyDefaultBrush; //!< The brush used to fill polygon.
525  QImage* m_polyImage; //!< The pattern image used to fill polygon.
526  int m_polyPatternWidth; //!< The width used to draw marker or pattern.
527 
528  QPen m_txtContourPen; //!< The pen used to draw the text contour.
529  bool m_txtContourEnabled; //!< The flag indicates whether the outline of the text should be drawn.
530  QBrush m_txtBrush; //!< The brush used to draw texts.
531  QFont m_font; //!< The text font.
532  int m_txtLetterSpacing; //!< Text letter spacing.
533  int m_txtWordSpacing; //!< Text word spacing.
534  //int m_txtLineJustification; //!< Text multi line justification.
535  int m_txtLineSpacing; //!< Text multi line spacing.
536 
537  std::map<std::string, QPixmap*> m_patterns; //!< The pixmap styles used to draw patterns.
538  };
539  } // end namespace widgets
540  } // end namespace qt
541 } // end namespace te
542 
543 #endif // __TERRALIB_QT_WIDGETS_INTERNAL_CANVAS_H
544 
ImageType
This enum specifies the possible input and output image formats supported by the canvas API...
Definition: Enums.h:38
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
te::gm::Envelope * m_envelope
Stores the current world (or window) coordinates area.
Definition: Canvas.h:492
QMatrix m_matrix
Matrix that transforms the world coordinate to device coordinate.
Definition: Canvas.h:491
int m_txtLetterSpacing
Text letter spacing.
Definition: Canvas.h:532
QColor m_bgColor
Canvas background color. Defaults: white fully transparent.
Definition: Canvas.h:500
QImage * m_ptSelectionPatternImg
The marker or pattern used to show selection status. The color is mixed with point pattern...
Definition: Canvas.h:510
int m_txtLineSpacing
Text multi line spacing.
Definition: Canvas.h:535
QPointF m_pt
Point buffer to avoid creating another point instance.
Definition: Canvas.h:504
QColor m_lnColor
The color used to draw lines.
Definition: Canvas.h:516
QImage * m_ptClearPatternImg
The marker or pattern used to clear (erase point).
Definition: Canvas.h:511
QColor m_polyColor
The color used to fill polygon (solid, marker or pattern).
Definition: Canvas.h:522
QBrush m_polyDefaultBrush
The brush used to fill polygon.
Definition: Canvas.h:524
bool m_isDeviceOwner
Tells if canvas is the owner of the paint device.
Definition: Canvas.h:498
double m_ptRotation
The point pattern rotation.
Definition: Canvas.h:512
std::map< std::string, QPixmap * > m_patterns
The pixmap styles used to draw patterns.
Definition: Canvas.h:537
QImage * m_polyImage
The pattern image used to fill polygon.
Definition: Canvas.h:525
AlignType
This enum contains values to control the alignment of components (like Canvas and MapDisplay)...
Definition: Enums.h:125
QColor m_polyContourColor
The color used to draw polygon contour.
Definition: Canvas.h:519
bool m_isPainterOwner
Tells if canvas is the owner of the painter.
Definition: Canvas.h:496
LineCapStyle
This enum encodes enumerated values telling how line strings should be capped (at the two ends of the...
Definition: Enums.h:72
QBrush m_txtBrush
The brush used to draw texts.
Definition: Canvas.h:530
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:53
LineString is a curve with linear interpolation between points.
Definition: LineString.h:64
A point with x and y coordinate values.
Definition: Point.h:50
bool m_txtContourEnabled
The flag indicates whether the outline of the text should be drawn.
Definition: Canvas.h:529
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
An abstract class for raster data strucutures.
Definition: Raster.h:71
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:36
TerraLib.
int m_polyPatternWidth
The width used to draw marker or pattern.
Definition: Canvas.h:526
QColor m_ptColorFrom
Indicates the color that originated the pattern that shows the status of selection. Used for optmization.
Definition: Canvas.h:507
QPen m_lnPen
The pen used to draw lines.
Definition: Canvas.h:517
QPen m_polyContourPen
The pen used to draw contour polygon.
Definition: Canvas.h:520
FontStyleType
It defines the style to use for a font.
Definition: Font.h:72
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:77
int m_ptVOffset
Vertical offset in pixels (in device coordinate) applied to point pattern or marker.
Definition: Canvas.h:513
int m_txtWordSpacing
Text word spacing.
Definition: Canvas.h:533
A canvas built on top of Qt.
Definition: Canvas.h:54
QTransform m_painterTransform
This transform holds the original transform of the painter. It is stored to be combined to the canvas...
Definition: Canvas.h:495
MultiLineString is a MultiCurve whose elements are LineStrings.
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:92
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
LineDashStyle
This enum encodes enumerated values telling how lines should be drawn. e.g. as a plain line or dash l...
Definition: Enums.h:56
LineJoinStyle
This enum encodes enumerated values telling how line strings should be joined (between line segments)...
Definition: Enums.h:84
MultiSurface is a class that represents a 2-dimensional GeometryCollection whose elements are surface...
Definition: MultiSurface.h:54
QPen m_ptPen
The pen used to draw points.
Definition: Canvas.h:503
int m_ptHOffset
Horizontal offset in pixels (in device coordinate) applied to point pattern or marker.
Definition: Canvas.h:514
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
QBrush m_polyBrush
The brush used to fill polygon.
Definition: Canvas.h:523
QImage * m_ptImg
The marker or pattern used to point.
Definition: Canvas.h:508
int m_ptWidth
The width for point markers and point pattern.
Definition: Canvas.h:505
QImage * m_ptImgRotated
The marker or pattern already with rotation used to draw points.
Definition: Canvas.h:509
QPainter * m_painter
The painter used to draw geometric objects.
Definition: Canvas.h:494
FontWeightType
It gives the amount of weight or boldness to use for a font.
Definition: Font.h:84
QFont m_font
The text font.
Definition: Canvas.h:531
It is a collection of other geometric objects.
QPen m_txtContourPen
The pen used to draw the text contour.
Definition: Canvas.h:528
bool m_erase
used for erase operation.
Definition: Canvas.h:502
QColor m_ptColor
The color used to draw point (pixel) or marker.
Definition: Canvas.h:506