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