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