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  void calcAspectRatio(double& llx, double& lly, double& urx, double& ury,
105 
106  void calcAspectRatio(te::gm::Envelope* envelope,
107  const te::map::AlignType hAlign = te::map::Center,
108  const te::map::AlignType vAlign = te::map::Center);
109 
110  void setBackgroundColor(const te::color::RGBAColor& color);
111 
112  te::color::RGBAColor getBackgroundColor() const;
113 
114  void clear();
115 
116  void resize(int w, int h);
117 
118  int getWidth() const;
119 
120  int getHeight() const;
121 
122  void draw(const te::gm::Geometry* geom);
123 
124  void draw(const te::gm::Point* point);
125 
126  void draw(const te::gm::MultiPoint* mpoint);
127 
128  void draw(const te::gm::LineString* line);
129 
130  void draw(const te::gm::MultiLineString* mline);
131 
132  void draw(const te::gm::Polygon* poly);
133 
134  void draw(const te::gm::MultiPolygon* mpoly);
135 
136  void draw(const te::gm::GeometryCollection* g);
137 
138  void draw(const te::gm::MultiSurface* g);
139 
140  void save(const char* fileName, te::map::ImageType t, int quality = 75, int fg = 0) const;
141 
142  char* getImage(te::map::ImageType t, std::size_t& size, int quality = 75, int fg = 0) const;
143 
144  te::color::RGBAColor** getImage(const int x = 0, const int y = 0, const int w = 0, const int h = 0) const;
145 
146  void freeImage(char* img) const;
147 
148  void drawImage(char* src, std::size_t size, te::map::ImageType t);
149 
150  void drawImage(te::color::RGBAColor** src, int w, int h);
151 
152  void drawImage(int x, int y, char* src, std::size_t size, te::map::ImageType t);
153 
154  void drawImage(int x, int y, te::color::RGBAColor** src, int w, int h);
155 
156  void drawImage(int x, int y, int w, int h, char* src, std::size_t size, te::map::ImageType t);
157 
158  void drawImage(int x, int y, int w, int h, te::color::RGBAColor** src, int srcw, int srch);
159 
160  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);
161 
162  void drawImage(int x, int y, int w, int h, te::color::RGBAColor** src, int sx, int sy, int sw, int sh);
163 
164  void drawImage(int x, int y, te::rst::Raster* src, int opacity = TE_OPAQUE);
165 
166  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);
167 
168  void drawPixel(int x, int y);
169 
170  void drawPixel(int x, int y, const te::color::RGBAColor& color);
171 
172  void drawText(int x, int y,
173  const std::string& txt,
174  float angle = 0.0,
177 
178  void drawText(const te::gm::Point* p,
179  const std::string& txt,
180  float angle = 0.0,
183 
184  void drawText(const double& x, const double& y,
185  const std::string& txt,
186  float angle = 0.0,
189 
190  void draw(const te::at::Text* tx);
191 
192  te::gm::Polygon* getTextBoundary(int x, int y,
193  const std::string& txt,
194  float angle = 0.0,
197 
198  te::gm::Polygon* getTextBoundary(const te::gm::Point* p,
199  const std::string& txt,
200  float angle = 0.0,
203 
204  te::gm::Polygon* getTextBoundary(const double& x, const double& y,
205  const std::string& txt,
206  float angle = 0.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::at::FontStyle style);
219 
220  void setTextWeight(te::at::FontWeight 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(te::at::LineJustification just);
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 hAlign The horizontal text alignment.
332  \param vAlign The vertical text alignment.
333 
334  \note Qt Canvas extended method.
335  */
336  void drawText(const QPoint& p,
337  const std::string& txt,
338  float angle = 0.0,
341 
342  /*!
343  \brief It returns text boundary. Color and font family should be defined in advance.
344 
345  \param p The text entry point in device coordinate.
346  \param tx The text to be drawed.
347  \param angle The text rotation.
348  \param hAlign The horizontal text alignment.
349  \param vAlign The vertical text alignment.
350 
351  \return The text boundary in world coordinate.
352 
353  \note The caller of this method will take the ownership of the returned Polygon.
354 
355  \warning Qt Canvas extended method.
356  */
357  te::gm::Polygon* getTextBoundary(const QPoint& p, const std::string& txt, float angle = 0.0,
360 
361  /*!
362  \brief It returns the internal pixmap used to draw geographical objects.
363 
364  \return The internal pixmap used to draw geographical objects. Don't delete it! Null is returned if device is not a pixmap.
365 
366  \warning Qt Canvas extended method.
367 
368  \note You can not delete this pointer.
369  */
370  QPixmap* getPixmap() const;
371 
372  /*!
373  \brief It returns the internal image used to draw geographical objects.
374 
375  \return The internal image used to draw geographical objects. Don't delete it! Null is returned if device is not a image.
376 
377  \warning Qt Canvas extended method.
378 
379  \note You can not delete this pointer.
380  */
381  QImage* getImage() const;
382 
383  /*!
384  \brief It returns the internal device used to draw geographical objects.
385 
386  \return The internal device used to draw geographical objects. Don't delete it!
387 
388  \warning Qt Canvas extended method.
389 
390  \note You can not delete this pointer.
391  */
392  QPaintDevice* getDevice() const;
393 
394  /*!
395  \brief It sets new device as QPrinter.
396 
397  \param device The new paint device.
398  \param takeOwnerShip If true the canvas will take the ownership of the given device otherwise it is the caller responsability to release the device.
399  */
400  void setDevice(QPaintDevice* device, bool takeOwnerShip);
401 
402  /*!
403  \brief It returns the device resolution.
404 
405  \return The device resolution.
406  */
407  int getResolution();
408 
409  /*!
410  \brief It returns the matrix.
411 
412  \return The marix.
413  */
414  QMatrix getMatrix();
415 
416  void setMatrix(const QMatrix& matrix);
417 
418  /*! \brief Sets the given render hint on the canvas painter if on is true; otherwise clears the render hint. */
419  void setRenderHint(QPainter::RenderHint hint, bool on = true);
420 
421  //@}
422 
423  QPainter* getPainter();
424 
425  private:
426 
427  /** @name Copy Constructor and Assignment Operator
428  * Copy constructor and assignment operator not allowed.
429  */
430  //@{
431 
432  /*!
433  \brief Copy constructor not allowed.
434 
435  \param rhs The right-hand-side copy that would be used to copy from.
436  */
437  Canvas(const Canvas& rhs);
438 
439  /*!
440  \brief Assignment operator not allowed.
441 
442  \param rhs The right-hand-side copy that would be used to copy from.
443 
444  \return A reference to this object.
445  */
446  Canvas& operator=(const Canvas& rhs);
447 
448  //@}
449 
450  /** @name Auxiliary Methods
451  * Auxiliary methods.
452  */
453  //@{
454 
455  /*!
456  \brief It adjusts the given pen to use the given pattern.
457 
458  \param pen The pen that will be adjusted.
459  \param style The line custom dash style.
460  */
461  void setLineDashStyle(QPen& pen, const std::vector<double>& style);
462 
463  /*!
464  \brief It updates the alpha channel of the given image using the given opacity value.
465 
466  \param img The image that will be updated.
467  \param opacity The opacity value that will be used.
468  */
469  void updateAlpha(QImage& img, const int& opacity);
470 
471  /*!
472  \brief It creates two patterns. A pattern used to draw selected objects and
473  the other used to erase this pattern.
474  */
475  void createPointPatterns();
476 
477  /*!
478  \brief It draw the polygon contour.
479  */
480  void drawContour(const te::gm::LineString* line);
481 
482 
483  //@}
484 
485  private:
486 
487  QMatrix m_matrix; //!< Matrix that transforms the world coordinate to device coordinate.
488 
489  QPainter* m_painter; //!< The painter used to draw geometric objects.
490  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
491  bool m_isPainterOwner; //!< Tells if canvas is the owner of the painter.
492 
493  bool m_isDeviceOwner; //!< Tells if canvas is the owner of the paint device.
494 
495  QColor m_bgColor; //!< Canvas background color. Defaults: white fully transparent.
496 
497  bool m_erase; //!< used for erase operation.
498  QPen m_ptPen; //!< The pen used to draw points.
499  QPointF m_pt; //!< Point buffer to avoid creating another point instance.
500  int m_ptWidth; //!< The width for point markers and point pattern.
501  QColor m_ptColor; //!< The color used to draw point (pixel) or marker.
502  QColor m_ptColorFrom; //!< Indicates the color that originated the pattern that shows the status of selection. Used for optmization
503  QImage* m_ptImg; //!< The marker or pattern used to point.
504  QImage* m_ptImgRotated; //!< The marker or pattern already with rotation used to draw points.
505  QImage* m_ptSelectionPatternImg; //!< The marker or pattern used to show selection status. The color is mixed with point pattern.
506  QImage* m_ptClearPatternImg; //!< The marker or pattern used to clear (erase point).
507  double m_ptRotation; //!< The point pattern rotation.
508  int m_ptVOffset; //!< Vertical offset in pixels (in device coordinate) applied to point pattern or marker.
509  int m_ptHOffset; //!< Horizontal offset in pixels (in device coordinate) applied to point pattern or marker.
510 
511  QColor m_lnColor; //!< The color used to draw lines.
512  QPen m_lnPen; //!< The pen used to draw lines.
513 
514  QColor m_polyContourColor; //!< The color used to draw polygon contour.
515  QPen m_polyContourPen; //!< The pen used to draw contour polygon.
516 
517  QColor m_polyColor; //!< The color used to fill polygon (solid, marker or pattern).
518  QBrush m_polyBrush; //!< The brush used to fill polygon.
519  QBrush m_polyDefaultBrush; //!< The brush used to fill polygon.
520  QImage* m_polyImage; //!< The pattern image used to fill polygon.
521  QImage* m_polyRotatedImage; //!< The pattern rotated image used to fill polygon.
522  int m_polyPatternWidth; //!< The width used to draw marker or pattern.
523 
524  QPen m_txtContourPen; //!< The pen used to draw the text contour.
525  bool m_txtContourEnabled; //!< The flag indicates whether the outline of the text should be drawn.
526  QBrush m_txtBrush; //!< The brush used to draw texts.
527  QFont m_font; //!< The text font.
528  int m_txtLetterSpacing; //!< Text letter spacing.
529  int m_txtWordSpacing; //!< Text word spacing.
530  //int m_txtLineJustification; //!< Text multi line justification.
531  int m_txtLineSpacing; //!< Text multi line spacing.
532 
533  std::map<std::string, QPixmap*> m_patterns; //!< The pixmap styles used to draw patterns.
534  };
535  } // end namespace widgets
536  } // end namespace qt
537 } // end namespace te
538 
539 #endif // __TERRALIB_QT_WIDGETS_INTERNAL_CANVAS_H
540 
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
FontWeight
Font weight for drawing text.
Definition: Enums.h:50
QMatrix m_matrix
Matrix that transforms the world coordinate to device coordinate.
Definition: Canvas.h:487
int m_txtLetterSpacing
Text letter spacing.
Definition: Canvas.h:528
QColor m_bgColor
Canvas background color. Defaults: white fully transparent.
Definition: Canvas.h:495
LineJustification
Line justification for drawing multi line text.
Definition: Enums.h:106
FontStyle
Font style for drawing text.
Definition: Enums.h:38
QImage * m_ptSelectionPatternImg
The marker or pattern used to show selection status. The color is mixed with point pattern...
Definition: Canvas.h:505
int m_txtLineSpacing
Text multi line spacing.
Definition: Canvas.h:531
QPointF m_pt
Point buffer to avoid creating another point instance.
Definition: Canvas.h:499
QColor m_lnColor
The color used to draw lines.
Definition: Canvas.h:511
A Text may contain 1 or more Text Elements.
Definition: Text.h:51
QImage * m_ptClearPatternImg
The marker or pattern used to clear (erase point).
Definition: Canvas.h:506
QColor m_polyColor
The color used to fill polygon (solid, marker or pattern).
Definition: Canvas.h:517
QBrush m_polyDefaultBrush
The brush used to fill polygon.
Definition: Canvas.h:519
bool m_isDeviceOwner
Tells if canvas is the owner of the paint device.
Definition: Canvas.h:493
double m_ptRotation
The point pattern rotation.
Definition: Canvas.h:507
std::map< std::string, QPixmap * > m_patterns
The pixmap styles used to draw patterns.
Definition: Canvas.h:533
QImage * m_polyImage
The pattern image used to fill polygon.
Definition: Canvas.h:520
QImage * m_polyRotatedImage
The pattern rotated image used to fill polygon.
Definition: Canvas.h:521
AlignType
This enum contains values to control the alignment of components (like Canvas and MapDisplay)...
Definition: Enums.h:124
QColor m_polyContourColor
The color used to draw polygon contour.
Definition: Canvas.h:514
bool m_isPainterOwner
Tells if canvas is the owner of the painter.
Definition: Canvas.h:491
LineCapStyle
This enum encodes enumerated values telling how line strings should be capped (at the two ends of the...
Definition: Enums.h:71
QBrush m_txtBrush
The brush used to draw texts.
Definition: Canvas.h:526
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
HorizontalAlignment
Horizontal alignment for drawing text.
Definition: Enums.h:81
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:525
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
URI C++ Library.
int m_polyPatternWidth
The width used to draw marker or pattern.
Definition: Canvas.h:522
QColor m_ptColorFrom
Indicates the color that originated the pattern that shows the status of selection. Used for optmization.
Definition: Canvas.h:502
QPen m_lnPen
The pen used to draw lines.
Definition: Canvas.h:512
QPen m_polyContourPen
The pen used to draw contour polygon.
Definition: Canvas.h:515
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
int m_ptVOffset
Vertical offset in pixels (in device coordinate) applied to point pattern or marker.
Definition: Canvas.h:508
int m_txtWordSpacing
Text word spacing.
Definition: Canvas.h:529
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:490
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:55
LineJoinStyle
This enum encodes enumerated values telling how line strings should be joined (between line segments)...
Definition: Enums.h:83
VerticalAlignment
Vertical alignment for drawing text.
Definition: Enums.h:93
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:498
int m_ptHOffset
Horizontal offset in pixels (in device coordinate) applied to point pattern or marker.
Definition: Canvas.h:509
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:518
QImage * m_ptImg
The marker or pattern used to point.
Definition: Canvas.h:503
int m_ptWidth
The width for point markers and point pattern.
Definition: Canvas.h:500
QImage * m_ptImgRotated
The marker or pattern already with rotation used to draw points.
Definition: Canvas.h:504
QPainter * m_painter
The painter used to draw geometric objects.
Definition: Canvas.h:489
QFont m_font
The text font.
Definition: Canvas.h:527
It is a collection of other geometric objects.
QPen m_txtContourPen
The pen used to draw the text contour.
Definition: Canvas.h:524
bool m_erase
used for erase operation.
Definition: Canvas.h:497
QColor m_ptColor
The color used to draw point (pixel) or marker.
Definition: Canvas.h:501