qwt_graphic.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_GRAPHIC_H
  10. #define QWT_GRAPHIC_H
  11. #include "qwt_global.h"
  12. #include "qwt_null_paintdevice.h"
  13. #include <qmetatype.h>
  14. #include <qimage.h>
  15. #include <qpixmap.h>
  16. class QwtPainterCommand;
  17. /*!
  18. \brief A paint device for scalable graphics
  19. QwtGraphic is the representation of a graphic that is tailored for
  20. scalability. Like QPicture it will be initialized by QPainter
  21. operations and can be replayed later to any target paint device.
  22. While the usual image representations QImage and QPixmap are not
  23. scalable Qt offers two paint devices, that might be candidates
  24. for representing a vector graphic:
  25. - QPicture\n
  26. Unfortunately QPicture had been forgotten, when Qt4
  27. introduced floating point based render engines. Its API
  28. is still on integers, what make it unusable for proper scaling.
  29. - QSvgRenderer/QSvgGenerator\n
  30. Unfortunately QSvgRenderer hides to much information about
  31. its nodes in internal APIs, that are necessary for proper
  32. layout calculations. Also it is derived from QObject and
  33. can't be copied like QImage/QPixmap.
  34. QwtGraphic maps all scalable drawing primitives to a QPainterPath
  35. and stores them together with the painter state changes
  36. ( pen, brush, transformation ... ) in a list of QwtPaintCommands.
  37. For being a complete QPaintDevice it also stores pixmaps or images,
  38. what is somehow against the idea of the class, because these objects
  39. can't be scaled without a loss in quality.
  40. The main issue about scaling a QwtGraphic object are the pens used for
  41. drawing the outlines of the painter paths. While non cosmetic pens
  42. ( QPen::isCosmetic() ) are scaled with the same ratio as the path,
  43. cosmetic pens have a fixed width. A graphic might have paths with
  44. different pens - cosmetic and non-cosmetic.
  45. QwtGraphic caches 2 different rectangles:
  46. - control point rectangle\n
  47. The control point rectangle is the bounding rectangle of all
  48. control point rectangles of the painter paths, or the target
  49. rectangle of the pixmaps/images.
  50. - bounding rectangle\n
  51. The bounding rectangle extends the control point rectangle by
  52. what is needed for rendering the outline with an unscaled pen.
  53. Because the offset for drawing the outline depends on the shape
  54. of the painter path ( the peak of a triangle is different than the flat side )
  55. scaling with a fixed aspect ratio always needs to be calculated from the
  56. control point rectangle.
  57. \sa QwtPainterCommand
  58. */
  59. class QWT_EXPORT QwtGraphic: public QwtNullPaintDevice
  60. {
  61. public:
  62. /*!
  63. Hint how to render a graphic
  64. \sa setRenderHint(), testRenderHint()
  65. */
  66. enum RenderHint
  67. {
  68. /*!
  69. When rendering a QwtGraphic a specific scaling between
  70. the controlPointRect() and the coordinates of the target rectangle
  71. is set up internally in render().
  72. When RenderPensUnscaled is set this specific scaling is applied
  73. for the control points only, but not for the pens.
  74. All other painter transformations ( set up by application code )
  75. are supposed to work like usual.
  76. \sa render();
  77. */
  78. RenderPensUnscaled = 0x1
  79. };
  80. /*!
  81. \brief Render hints
  82. The default setting is to disable all hints
  83. */
  84. typedef QFlags<RenderHint> RenderHints;
  85. QwtGraphic();
  86. QwtGraphic( const QwtGraphic & );
  87. virtual ~QwtGraphic();
  88. QwtGraphic& operator=( const QwtGraphic & );
  89. void reset();
  90. bool isNull() const;
  91. bool isEmpty() const;
  92. void render( QPainter * ) const;
  93. void render( QPainter *, const QSizeF &,
  94. Qt::AspectRatioMode = Qt::IgnoreAspectRatio ) const;
  95. void render( QPainter *, const QRectF &,
  96. Qt::AspectRatioMode = Qt::IgnoreAspectRatio ) const;
  97. void render( QPainter *, const QPointF &,
  98. Qt::Alignment = Qt::AlignTop | Qt::AlignLeft ) const;
  99. QPixmap toPixmap() const;
  100. QPixmap toPixmap( const QSize &,
  101. Qt::AspectRatioMode = Qt::IgnoreAspectRatio ) const;
  102. QImage toImage() const;
  103. QImage toImage( const QSize &,
  104. Qt::AspectRatioMode = Qt::IgnoreAspectRatio ) const;
  105. QRectF scaledBoundingRect( double sx, double sy ) const;
  106. QRectF boundingRect() const;
  107. QRectF controlPointRect() const;
  108. const QVector< QwtPainterCommand > &commands() const;
  109. void setCommands( QVector< QwtPainterCommand > & );
  110. void setDefaultSize( const QSizeF & );
  111. QSizeF defaultSize() const;
  112. void setRenderHint( RenderHint, bool on = true );
  113. bool testRenderHint( RenderHint ) const;
  114. protected:
  115. virtual QSize sizeMetrics() const;
  116. virtual void drawPath( const QPainterPath & );
  117. virtual void drawPixmap( const QRectF &,
  118. const QPixmap &, const QRectF & );
  119. virtual void drawImage( const QRectF &,
  120. const QImage &, const QRectF &, Qt::ImageConversionFlags );
  121. virtual void updateState( const QPaintEngineState &state );
  122. private:
  123. void updateBoundingRect( const QRectF & );
  124. void updateControlPointRect( const QRectF & );
  125. class PathInfo;
  126. class PrivateData;
  127. PrivateData *d_data;
  128. };
  129. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtGraphic::RenderHints )
  130. Q_DECLARE_METATYPE( QwtGraphic )
  131. #endif