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,
175  double anchorX = 0.5, double anchorY = 0.5,
176  int displacementX = 0, int displacementY = 0);
177 
178  void drawText(const te::gm::Point* p,
179  const std::string& txt,
180  float angle = 0.0,
181  double anchorX = 0.5, double anchorY = 0.5,
182  int displacementX = 0, int displacementY = 0);
183 
184  void drawText(const double& x, const double& y,
185  const std::string& txt,
186  float angle = 0.0,
187  double anchorX = 0.5, double anchorY = 0.5,
188  int displacementX = 0, int displacementY = 0);
189 
190  te::gm::Polygon* getTextBoundary(int x, int y,
191  const std::string& txt,
192  float angle = 0.0,
193  double anchorX = 0.5, double anchorY = 0.5,
194  int displacementX = 0, int displacementY = 0);
195 
196  te::gm::Polygon* getTextBoundary(const te::gm::Point* p,
197  const std::string& txt,
198  float angle = 0.0,
199  double anchorX = 0.5, double anchorY = 0.5,
200  int displacementX = 0, int displacementY = 0);
201 
202  te::gm::Polygon* getTextBoundary(const double& x, const double& y,
203  const std::string& txt,
204  float angle = 0.0,
205  double anchorX = 0.5, double anchorY = 0.5,
206  int displacementX = 0, int displacementY = 0);
207 
208  void setTextColor(const te::color::RGBAColor& color);
209 
210  void setTextOpacity(int opacity);
211 
212  void setFontFamily(const std::string& family);
213 
214  void setTextPointSize(double psize);
215 
216  void setTextStyle(te::se::Font::FontStyleType style);
217 
218  void setTextWeight(te::se::Font::FontWeightType weight);
219 
220  void setTextStretch(std::size_t stretch);
221 
222  void setTextUnderline(bool b);
223 
224  void setTextOverline(bool b);
225 
226  void setTextStrikeOut(bool b);
227 
228  void setTextDecorationColor(const te::color::RGBAColor& color);
229 
230  void setTextDecorationWidth(int width);
231 
232  void setTextContourColor(const te::color::RGBAColor& color);
233 
234  void setTextContourEnabled(bool b);
235 
236  void setTextContourOpacity(int opacity);
237 
238  void setTextContourWidth(int width);
239 
240  void setTextJustification(int justType);
241 
242  void setTextMultiLineSpacing(int spacing);
243 
244  void setPointColor(const te::color::RGBAColor& color);
245 
246  void setPointWidth(int w);
247 
248  void setPointPattern(te::color::RGBAColor** pattern, int ncols, int nrows);
249 
250  void setPointPattern(char* pattern, std::size_t size, te::map::ImageType t);
251 
252  void setPointPatternRotation(const double& angle);
253 
254  void setPointPatternOpacity(int opacity);
255 
256  void setLineColor(const te::color::RGBAColor& color);
257 
258  void setLinePattern(te::color::RGBAColor** pattern, int ncols, int nrows);
259 
260  void setLinePattern(char* pattern, std::size_t size, te::map::ImageType t);
261 
262  void setLinePatternRotation(const double& angle);
263 
264  void setLinePatternOpacity(int opacity);
265 
266  void setLineDashStyle(te::map::LineDashStyle style);
267 
268  void setLineDashStyle(const std::vector<double>& style);
269 
270  void setLineCapStyle(te::map::LineCapStyle style);
271 
272  void setLineJoinStyle(te::map::LineJoinStyle style);
273 
274  void setLineWidth(int w);
275 
276  void setPolygonFillColor(const te::color::RGBAColor& color);
277 
278  void setPolygonFillColor(const QBrush& color);
279 
280  void setPolygonContourColor(const te::color::RGBAColor& color);
281 
282  void setPolygonFillPattern(te::color::RGBAColor** pattern, int ncols, int nrows);
283 
284  void setPolygonFillPattern(char* pattern, std::size_t size, te::map::ImageType t);
285 
286  void setPolygonPatternWidth(int w);
287 
288  void setPolygonPatternRotation(const double& angle);
289 
290  void setPolygonPatternOpacity(int opacity);
291 
292  void setPolygonContourPattern(te::color::RGBAColor** pattern, int ncols, int nrows);
293 
294  void setPolygonContourPattern(char* pattern, std::size_t size, te::map::ImageType t);
295 
296  void setPolygonContourWidth(int w);
297 
298  void setPolygonContourPatternRotation(const double& angle);
299 
300  void setPolygonContourPatternOpacity(int opacity);
301 
302  void setPolygonContourDashStyle(te::map::LineDashStyle style);
303 
304  void setPolygonContourDashStyle(const std::vector<double>& style);
305 
306  void setPolygonContourCapStyle(te::map::LineCapStyle style);
307 
308  void setPolygonContourJoinStyle(te::map::LineJoinStyle style);
309 
310  void setEraseMode();
311 
312  void setNormalMode();
313 
314  //@}
315 
316  /** @name Qt Canvas Specific Methos
317  * Methods that belongs only to Qt Canvas.
318  */
319  //@{
320 
321  /*!
322  \brief It draws a text.
323 
324  Color and font family should be defined in advance.
325 
326  \param p The text entry point i device coordinate.
327  \param txt The text to be drawed.
328  \param angle The text rotation.
329  \param anchorX The horizontal text anchor.
330  \param anchorY The vertical text anchor.
331  \param displacementX The horizontal text displacement.
332  \param displacementY The vertical text displacement.
333 
334  \note Qt Canvas extended method.
335  */
336  void drawText(const QPoint& p,
337  const std::string& txt,
338  float angle = 0.0,
339  double anchorX = 0.5, double anchorY = 0.5,
340  int displacementX = 0, int displacementY = 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 anchorX The horizontal text anchor.
349  \param anchorY The vertical text anchor.
350  \param displacementX The horizontal text displacement.
351  \param displacementY The vertical text displacement.
352 
353  \return The text boundary in world coordinate.
354 
355  \note The caller of this method will take the ownership of the returned Polygon.
356 
357  \warning Qt Canvas extended method.
358  */
359  te::gm::Polygon* getTextBoundary(const QPoint& p, const std::string& txt, float angle = 0.0,
360  double anchorX = 0.5, double anchorY = 0.5,
361  int displacementX = 0, int displacementY = 0);
362 
363  /*!
364  \brief It returns the internal pixmap used to draw geographical objects.
365 
366  \return The internal pixmap used to draw geographical objects. Don't delete it! Null is returned if device is not a pixmap.
367 
368  \warning Qt Canvas extended method.
369 
370  \note You can not delete this pointer.
371  */
372  QPixmap* getPixmap() const;
373 
374  /*!
375  \brief It returns the internal image used to draw geographical objects.
376 
377  \return The internal image used to draw geographical objects. Don't delete it! Null is returned if device is not a image.
378 
379  \warning Qt Canvas extended method.
380 
381  \note You can not delete this pointer.
382  */
383  QImage* getImage() const;
384 
385  /*!
386  \brief It returns the internal device used to draw geographical objects.
387 
388  \return The internal device used to draw geographical objects. Don't delete it!
389 
390  \warning Qt Canvas extended method.
391 
392  \note You can not delete this pointer.
393  */
394  QPaintDevice* getDevice() const;
395 
396  /*!
397  \brief It sets new device as QPrinter.
398 
399  \param device The new paint device.
400  \param takeOwnerShip If true the canvas will take the ownership of the given device otherwise it is the caller responsability to release the device.
401  */
402  void setDevice(QPaintDevice* device, bool takeOwnerShip);
403 
404  /*!
405  \brief It returns the device resolution.
406 
407  \return The device resolution.
408  */
409  int getResolution();
410 
411  /*!
412  \brief It returns the matrix.
413 
414  \return The marix.
415  */
416  QMatrix getMatrix();
417 
418  void setMatrix(const QMatrix& matrix);
419 
420  /*! \brief Sets the given render hint on the canvas painter if on is true; otherwise clears the render hint. */
421  void setRenderHint(QPainter::RenderHint hint, bool on = true);
422 
423  //@}
424 
425  QPainter* getPainter();
426 
427  private:
428 
429  /** @name Copy Constructor and Assignment Operator
430  * Copy constructor and assignment operator not allowed.
431  */
432  //@{
433 
434  /*!
435  \brief Copy constructor not allowed.
436 
437  \param rhs The right-hand-side copy that would be used to copy from.
438  */
439  Canvas(const Canvas& rhs);
440 
441  /*!
442  \brief Assignment operator not allowed.
443 
444  \param rhs The right-hand-side copy that would be used to copy from.
445 
446  \return A reference to this object.
447  */
448  Canvas& operator=(const Canvas& rhs);
449 
450  //@}
451 
452  /** @name Auxiliary Methods
453  * Auxiliary methods.
454  */
455  //@{
456 
457  /*!
458  \brief It adjusts the given pen to use the given pattern.
459 
460  \param pen The pen that will be adjusted.
461  \param style The line custom dash style.
462  */
463  void setLineDashStyle(QPen& pen, const std::vector<double>& style);
464 
465  /*!
466  \brief It updates the alpha channel of the given image using the given opacity value.
467 
468  \param img The image that will be updated.
469  \param opacity The opacity value that will be used.
470  */
471  void updateAlpha(QImage& img, const int& opacity);
472 
473  /*!
474  \brief It creates two patterns. A pattern used to draw selected objects and
475  the other used to erase this pattern.
476  */
477  void createPointPatterns();
478 
479  /*!
480  \brief It draw the polygon contour.
481  */
482  void drawContour(const te::gm::LineString* line);
483 
484 
485  //@}
486 
487  private:
488 
489  QMatrix m_matrix; //!< Matrix that transforms the world coordinate to device coordinate.
490 
491  QPainter* m_painter; //!< The painter used to draw geometric objects.
492  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
493  bool m_isPainterOwner; //!< Tells if canvas is the owner of the painter.
494 
495  bool m_isDeviceOwner; //!< Tells if canvas is the owner of the paint device.
496 
497  QColor m_bgColor; //!< Canvas background color. Defaults: white fully transparent.
498 
499  bool m_erase; //!< used for erase operation.
500  QPen m_ptPen; //!< The pen used to draw points.
501  QPointF m_pt; //!< Point buffer to avoid creating another point instance.
502  int m_ptWidth; //!< The width for point markers and point pattern.
503  QColor m_ptColor; //!< The color used to draw point (pixel) or marker.
504  QColor m_ptColorFrom; //!< Indicates the color that originated the pattern that shows the status of selection. Used for optmization
505  QImage* m_ptImg; //!< The marker or pattern used to point.
506  QImage* m_ptImgRotated; //!< The marker or pattern already with rotation used to draw points.
507  QImage* m_ptSelectionPatternImg; //!< The marker or pattern used to show selection status. The color is mixed with point pattern.
508  QImage* m_ptClearPatternImg; //!< The marker or pattern used to clear (erase point).
509  double m_ptRotation; //!< The point pattern rotation.
510  int m_ptVOffset; //!< Vertical offset in pixels (in device coordinate) applied to point pattern or marker.
511  int m_ptHOffset; //!< Horizontal offset in pixels (in device coordinate) applied to point pattern or marker.
512 
513  QColor m_lnColor; //!< The color used to draw lines.
514  QPen m_lnPen; //!< The pen used to draw lines.
515 
516  QColor m_polyContourColor; //!< The color used to draw polygon contour.
517  QPen m_polyContourPen; //!< The pen used to draw contour polygon.
518 
519  QColor m_polyColor; //!< The color used to fill polygon (solid, marker or pattern).
520  QBrush m_polyBrush; //!< The brush used to fill polygon.
521  QBrush m_polyDefaultBrush; //!< The brush used to fill polygon.
522  QImage* m_polyImage; //!< The pattern image used to fill polygon.
523  QImage* m_polyRotatedImage; //!< The pattern rotated image used to fill polygon.
524  int m_polyPatternWidth; //!< The width used to draw marker or pattern.
525 
526  QPen m_txtContourPen; //!< The pen used to draw the text contour.
527  bool m_txtContourEnabled; //!< The flag indicates whether the outline of the text should be drawn.
528  QBrush m_txtBrush; //!< The brush used to draw texts.
529  QFont m_font; //!< The text font.
530  int m_txtLetterSpacing; //!< Text letter spacing.
531  int m_txtWordSpacing; //!< Text word spacing.
532  //int m_txtLineJustification; //!< Text multi line justification.
533  int m_txtLineSpacing; //!< Text multi line spacing.
534 
535  std::map<std::string, QPixmap*> m_patterns; //!< The pixmap styles used to draw patterns.
536  };
537  } // end namespace widgets
538  } // end namespace qt
539 } // end namespace te
540 
541 #endif // __TERRALIB_QT_WIDGETS_INTERNAL_CANVAS_H
542 
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
QMatrix m_matrix
Matrix that transforms the world coordinate to device coordinate.
Definition: Canvas.h:489
int m_txtLetterSpacing
Text letter spacing.
Definition: Canvas.h:530
QColor m_bgColor
Canvas background color. Defaults: white fully transparent.
Definition: Canvas.h:497
QImage * m_ptSelectionPatternImg
The marker or pattern used to show selection status. The color is mixed with point pattern...
Definition: Canvas.h:507
int m_txtLineSpacing
Text multi line spacing.
Definition: Canvas.h:533
QPointF m_pt
Point buffer to avoid creating another point instance.
Definition: Canvas.h:501
QColor m_lnColor
The color used to draw lines.
Definition: Canvas.h:513
QImage * m_ptClearPatternImg
The marker or pattern used to clear (erase point).
Definition: Canvas.h:508
QColor m_polyColor
The color used to fill polygon (solid, marker or pattern).
Definition: Canvas.h:519
QBrush m_polyDefaultBrush
The brush used to fill polygon.
Definition: Canvas.h:521
bool m_isDeviceOwner
Tells if canvas is the owner of the paint device.
Definition: Canvas.h:495
double m_ptRotation
The point pattern rotation.
Definition: Canvas.h:509
std::map< std::string, QPixmap * > m_patterns
The pixmap styles used to draw patterns.
Definition: Canvas.h:535
QImage * m_polyImage
The pattern image used to fill polygon.
Definition: Canvas.h:522
QImage * m_polyRotatedImage
The pattern rotated image used to fill polygon.
Definition: Canvas.h:523
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:516
bool m_isPainterOwner
Tells if canvas is the owner of the painter.
Definition: Canvas.h:493
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:528
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
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:527
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:524
QColor m_ptColorFrom
Indicates the color that originated the pattern that shows the status of selection. Used for optmization.
Definition: Canvas.h:504
QPen m_lnPen
The pen used to draw lines.
Definition: Canvas.h:514
QPen m_polyContourPen
The pen used to draw contour polygon.
Definition: Canvas.h:517
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:74
int m_ptVOffset
Vertical offset in pixels (in device coordinate) applied to point pattern or marker.
Definition: Canvas.h:510
int m_txtWordSpacing
Text word spacing.
Definition: Canvas.h:531
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:492
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:500
int m_ptHOffset
Horizontal offset in pixels (in device coordinate) applied to point pattern or marker.
Definition: Canvas.h:511
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:520
QImage * m_ptImg
The marker or pattern used to point.
Definition: Canvas.h:505
int m_ptWidth
The width for point markers and point pattern.
Definition: Canvas.h:502
QImage * m_ptImgRotated
The marker or pattern already with rotation used to draw points.
Definition: Canvas.h:506
QPainter * m_painter
The painter used to draw geometric objects.
Definition: Canvas.h:491
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:529
It is a collection of other geometric objects.
QPen m_txtContourPen
The pen used to draw the text contour.
Definition: Canvas.h:526
bool m_erase
used for erase operation.
Definition: Canvas.h:499
QColor m_ptColor
The color used to draw point (pixel) or marker.
Definition: Canvas.h:503