#include <Wt/WPainter>
Public Types | |
enum | RenderHint { Antialiasing = 0X1 } |
Enumeration for render hints. More... | |
Public Member Functions | |
WPainter () | |
Default constructor. | |
WPainter (WPaintDevice *device) | |
Create a paint and initialize it for a paint device. | |
~WPainter () | |
Destructor. | |
bool | begin (WPaintDevice *device) |
Begin painting on a paint device. | |
bool | isActive () const |
Return whether this painter is active on a paint device. | |
bool | end () |
End painting. | |
WPaintDevice * | device () const |
The device on which this painter is active (or 0 if not active). | |
void | setRenderHint (RenderHint hint, bool on=true) |
Set a render hint. | |
int | renderHints () const |
Get the current render hints. | |
void | drawArc (const WRectF &rectangle, int startAngle, int spanAngle) |
Draw an arc. | |
void | drawArc (double x, double y, double width, double height, int startAngle, int spanAngle) |
Draw an arc. | |
void | drawChord (const WRectF &rectangle, int startAngle, int spanAngle) |
Draw a chord. | |
void | drawChord (double x, double y, double width, double height, int startAngle, int spanAngle) |
Draw a chord. | |
void | drawEllipse (const WRectF &rectangle) |
Draw an ellipse. | |
void | drawEllipse (double x, double y, double width, double height) |
Draw an ellipse. | |
void | drawImage (const WPointF &point, const Image &image) |
Draw an image. | |
void | drawImage (const WPointF &point, const Image &image, const WRectF &sourceRect) |
Draw part of an image. | |
void | drawImage (const WRectF &rect, const Image &image) |
Draw an image inside a rectangle. | |
void | drawImage (const WRectF &rect, const Image &image, const WRectF &sourceRect) |
Draw part of an image inside a rectangle. | |
void | drawImage (double x, double y, const Image &image, double sx=0, double sy=0, double sw=-1, double sh=-1) |
Draw part of an image. | |
void | drawLine (const WLineF &line) |
Draw a line. | |
void | drawLine (const WPointF &p1, const WPointF &p2) |
Draw a line. | |
void | drawLine (double x1, double y1, double x2, double y2) |
Draw a line. | |
void | drawLines (const WT_ARRAY WLineF *lines, int lineCount) |
Draws an array of lines. | |
void | drawLines (const WT_ARRAY WPointF *pointPairs, int lineCount) |
Draws an array of lines. | |
void | drawLines (const std::vector< WLineF > &lines) |
Draws an array of lines. | |
void | drawLines (const std::vector< WPointF > &pointPairs) |
Draws an array of lines. | |
void | drawPath (const WPainterPath &path) |
Draw a (complex) path. | |
void | drawPie (const WRectF &rectangle, int startAngle, int spanAngle) |
Draw a pie. | |
void | drawPie (double x, double y, double width, double height, int startAngle, int spanAngle) |
Draw a pie. | |
void | drawPoint (const WPointF &position) |
Draw a point. | |
void | drawPoint (double x, double y) |
Draw a point. | |
void | drawPoints (const WT_ARRAY WPointF *points, int pointCount) |
Draw a number of points. | |
void | drawPolygon (const WT_ARRAY WPointF *points, int pointCount) |
Draw a polygon. | |
void | drawPolyline (const WT_ARRAY WPointF *points, int pointCount) |
Draw a polyline. | |
void | drawRect (const WRectF &rectangle) |
Draw a rectangle. | |
void | drawRect (double x, double y, double width, double height) |
Draw a rectangle. | |
void | drawRects (const WT_ARRAY WRectF *rectangles, int rectCount) |
Draw a number of rectangles. | |
void | drawRects (const std::vector< WRectF > &rectangles) |
Draw a number of rectangles. | |
void | drawText (const WRectF &rectangle, WFlags< AlignmentFlag > flags, const WString &text) |
Draw text. | |
void | drawText (double x, double y, double width, double height, WFlags< AlignmentFlag > flags, const WString &text) |
Draw text. | |
void | fillPath (const WPainterPath &path, const WBrush &brush) |
Fill a (complex) path. | |
void | fillRect (const WRectF &rectangle, const WBrush &brush) |
Fill a rectangle. | |
void | fillRect (double x, double y, double width, double height, const WBrush &brush) |
Fill a rectangle. | |
void | strokePath (const WPainterPath &path, const WPen &pen) |
Stroke a path. | |
void | setBrush (const WBrush &brush) |
Change the fill style. | |
void | setFont (const WFont &font) |
Change the font. | |
void | setPen (const WPen &pen) |
Change the pen. | |
const WBrush & | brush () const |
Returns the current brush. | |
const WFont & | font () const |
Returns the current font. | |
const WPen & | pen () const |
Returns the current pen. | |
void | setClipping (bool enable) |
Enable or disable clipping. | |
bool | hasClipping () const |
Return whether clipping is enabled. | |
void | setClipPath (const WPainterPath &clipPath) |
Set the clip path. | |
WPainterPath | clipPath () const |
Get the clip path. | |
void | resetTransform () |
Reset the current transformation. | |
void | rotate (double angle) |
Rotate the logical coordinate system. | |
void | scale (double sx, double sy) |
Scale the logical coordinate system. | |
void | translate (const WPointF &offset) |
Translate the origin of the logical coordinate system. | |
void | translate (double dx, double dy) |
Translate the origin of the logical coordinate system. | |
void | setWorldTransform (const WTransform &matrix, bool combine=false) |
Set a transformation for the logical coordinate system. | |
const WTransform & | worldTransform () const |
Get the current world transformation matrix. | |
void | save () |
Save the current state. | |
void | restore () |
Get the last save state. | |
void | setViewPort (const WRectF &viewPort) |
Set the viewport. | |
void | setViewPort (double x, double y, double width, double height) |
Set the viewport. | |
WRectF | viewPort () const |
Returns the viewport. | |
void | setWindow (const WRectF &window) |
Set the window. | |
void | setWindow (double x, double y, double width, double height) |
Set the window. | |
WRectF | window () const |
Returns the current window. | |
WTransform | combinedTransform () const |
Returns the combined transformation matrix. | |
Classes | |
class | Image |
An image that can be rendered on a WPainter. More... |
The painter class provides a rich interface for painting on a WPaintDevice. To start painting on a device, either pass the device through the constructor WPainter(WPaintDevice *), or use begin(WPaintDevice *). Typically, you will instantiate a WPainter from within the WPaintedWidget::paintEvent() method, but you can also use a painter to paint directly to a particular paint device, for example to create an SVG image.
The painter maintains state such as the current pen, brush, font, transformation and clipping settings (see setClipping() and setClipPath()). A particular state can be saved using save() and later restored using restore().
The painting system distinguishes between device coordinates, logical coordinates, and local coordinates. The device coordinate system ranges from (0, 0) in the top left corner of the device, to (device()->width(), device()->height()) for the bottom right corner. The logical coordinate system defines a coordinate system that may be chosen independent of the geometry of the device, which is convenient to make abstraction of the actual device size. Finally, the current local coordinate system may be different from the logical coordinate system because of a world transformation. Initially, the local coordinate system coincides with the logical coordinate system, which coincides with the device coordinate system.
By setting a viewPort() and a window(), a viewPort transformation is defined which maps logical coordinates onto device coordinates. By changing the world transformation (using setWorldTransform(), or translate(), rotate(), scale() operations), it is defined how current local coordinates map onto logical coordinates.
Although the painter has support for clipping using an arbitrary path, not all devices support clipping.
Usage example:
class MyPaintedWidget : public Wt::WPaintedWidget { public: MyPaintedWidget(Wt::WContainerWidget *parent = 0) : Wt::WPaintedWidget(parent), foo_(100) { resize(200, 200); // provide a default size } void setFoo(int foo) { foo_ = foo; update(); // trigger a repaint } protected: void paintEvent(Wt::WPaintedWidget *paintDevice) { Wt::WPainter painter(paintDevice); painter.drawLine(20, 20, foo_, foo_); ... } private: int foo_; };
Wt::WPainter::WPainter | ( | ) |
Default constructor.
Before painting, you must invoke begin(WPaintDevice *) on a paint device.
bool Wt::WPainter::begin | ( | WPaintDevice * | device | ) |
Begin painting on a paint device.
Starts painting on a paint device. The paint device is automatically cleared to become entirely transparent.
bool Wt::WPainter::isActive | ( | ) | const |
bool Wt::WPainter::end | ( | ) |
End painting.
This method is called automatically from the destructor.
WPaintDevice* Wt::WPainter::device | ( | ) | const [inline] |
The device on which this painter is active (or 0 if not active).
void Wt::WPainter::setRenderHint | ( | RenderHint | hint, | |
bool | on = true | |||
) |
Set a render hint.
Renderers may ignore particular hints for which they have no support.
int Wt::WPainter::renderHints | ( | ) | const [inline] |
Get the current render hints.
Returns the logical OR of render hints currently set.
void Wt::WPainter::drawArc | ( | const WRectF & | rectangle, | |
int | startAngle, | |||
int | spanAngle | |||
) |
Draw an arc.
Draws an arc using the current pen, and fills using the current brush.
The arc is defined as a segment from an ellipse, which fits in the rectangle. The segment starts at startAngle, and spans an angle given by spanAngle. These angles have as unit 1/16th of a degree, and are measured counter-clockwise starting from the 3 o'clock position.
void Wt::WPainter::drawArc | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height, | |||
int | startAngle, | |||
int | spanAngle | |||
) |
Draw an arc.
This is an overloaded method for convenience.
void Wt::WPainter::drawChord | ( | const WRectF & | rectangle, | |
int | startAngle, | |||
int | spanAngle | |||
) |
Draw a chord.
Draws an arc using the current pen, and connects start and end point with a line. The area is filled using the current brush.
The arc is defined as a segment from an ellipse, which fits in the rectangle. The segment starts at startAngle, and spans an angle given by spanAngle. These angles have as unit 1/16th of a degree, and are measured counter-clockwise starting at 3 o'clock.
void Wt::WPainter::drawChord | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height, | |||
int | startAngle, | |||
int | spanAngle | |||
) |
Draw a chord.
This is an overloaded method for convenience.
void Wt::WPainter::drawEllipse | ( | const WRectF & | rectangle | ) |
Draw an ellipse.
Draws an ellipse using the current pen and fills it using the current brush.
The ellipse is defined as being bounded by the rectangle.
void Wt::WPainter::drawEllipse | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height | |||
) |
Draw an image.
Draws the image so that the top left corner corresponds to point.
This is an overloaded method provided for convenience.
void Wt::WPainter::drawImage | ( | const WPointF & | point, | |
const Image & | image, | |||
const WRectF & | sourceRect | |||
) |
Draw part of an image.
Draws the sourceRect rectangle from an image to the location point.
This is an overloaded method provided for convenience.
Draw an image inside a rectangle.
Draw the image inside rect (If necessary, the image is scaled to fit into the rectangle).
This is an overloaded method provided for convenience.
void Wt::WPainter::drawImage | ( | const WRectF & | rect, | |
const Image & | image, | |||
const WRectF & | sourceRect | |||
) |
Draw part of an image inside a rectangle.
Draws the sourceRect rectangle from an image inside rect (If necessary, the image is scaled to fit into the rectangle).
void Wt::WPainter::drawImage | ( | double | x, | |
double | y, | |||
const Image & | image, | |||
double | sx = 0 , |
|||
double | sy = 0 , |
|||
double | sw = -1 , |
|||
double | sh = -1 | |||
) |
Draw part of an image.
Draws the sourceRect rectangle with top left corner (sx, sy) and size sw x sh from an image to the location (x, y).
void Wt::WPainter::drawLine | ( | const WLineF & | line | ) |
Draw a line.
Draws a line using the current pen.
Draw a line.
Draws a line defined by two points.
void Wt::WPainter::drawLine | ( | double | x1, | |
double | y1, | |||
double | x2, | |||
double | y2 | |||
) |
Draw a line.
Draws a line defined by two points.
void Wt::WPainter::drawLines | ( | const WT_ARRAY WLineF * | lines, | |
int | lineCount | |||
) |
Draws an array of lines.
Draws the lineCount first lines from the given array of lines.
void Wt::WPainter::drawLines | ( | const WT_ARRAY WPointF * | pointPairs, | |
int | lineCount | |||
) |
Draws an array of lines.
Draws lineCount lines, where each line is specified using a begin and end point that are read from an array. Thus, the pointPairs array must have at least 2*lineCount points.
void Wt::WPainter::drawLines | ( | const std::vector< WLineF > & | lines | ) |
Draws an array of lines.
Draws the lines given in the vector.
void Wt::WPainter::drawLines | ( | const std::vector< WPointF > & | pointPairs | ) |
Draws an array of lines.
Draws a number of lines that are specified by pairs of begin- and endpoints. The vector should hold a number of points that is a multiple of two.
void Wt::WPainter::drawPath | ( | const WPainterPath & | path | ) |
Draw a (complex) path.
Draws and fills the given path using the current pen and brush.
void Wt::WPainter::drawPie | ( | const WRectF & | rectangle, | |
int | startAngle, | |||
int | spanAngle | |||
) |
Draw a pie.
Draws an arc using the current pen, and connects start and end point with the center of the corresponding ellipse. The area is filled using the current brush.
The arc is defined as a segment from an ellipse, which fits in the rectangle. The segment starts at startAngle, and spans an angle given by spanAngle. These angles have as unit 1/16th of a degree, and are measured counter-clockwise starting at 3 o'clock.
void Wt::WPainter::drawPie | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height, | |||
int | startAngle, | |||
int | spanAngle | |||
) |
void Wt::WPainter::drawPoint | ( | const WPointF & | position | ) |
void Wt::WPainter::drawPoint | ( | double | x, | |
double | y | |||
) |
void Wt::WPainter::drawPoints | ( | const WT_ARRAY WPointF * | points, | |
int | pointCount | |||
) |
Draw a number of points.
Draws the pointCount first points from the given array of points.
void Wt::WPainter::drawPolygon | ( | const WT_ARRAY WPointF * | points, | |
int | pointCount | |||
) |
Draw a polygon.
Draws a polygon that is specified by a list of points, using the current pen. The polygon is closed by connecting the last point with the first point, and filled using the current brush.
void Wt::WPainter::drawPolyline | ( | const WT_ARRAY WPointF * | points, | |
int | pointCount | |||
) |
Draw a polyline.
Draws a polyline that is specified by a list of points, using the current pen.
void Wt::WPainter::drawRect | ( | const WRectF & | rectangle | ) |
Draw a rectangle.
Draws and fills a rectangle using the current pen and brush.
void Wt::WPainter::drawRect | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height | |||
) |
void Wt::WPainter::drawRects | ( | const WT_ARRAY WRectF * | rectangles, | |
int | rectCount | |||
) |
Draw a number of rectangles.
Draws and fills the rectCount first rectangles from the given array, using the current pen and brush.
void Wt::WPainter::drawRects | ( | const std::vector< WRectF > & | rectangles | ) |
Draw a number of rectangles.
Draws and fills a list of rectangles using the current pen and brush.
void Wt::WPainter::drawText | ( | const WRectF & | rectangle, | |
WFlags< AlignmentFlag > | flags, | |||
const WString & | text | |||
) |
Draw text.
Draw text using inside the rectangle, using the current font. The text is aligned inside the rectangle following alignment indications given in flags. The text is drawn using the current transformation, pen color (pen()) and font settings (font()).
Flags is the logical OR of a horizontal and vertical alignment. Horizontal alignment may be one of AlignLeft, AlignCenter, or AlignRight. Vertical alignment is one of AlignTop, AlignMiddle or AlignBottom.
Note: HtmlCanvas will always render the text horizontally (unaffected by rotation), and unaffected by the scaling component of the transformation matrix. Also, text is overlayed on top of painted shapes, and is not covered by shapes that are painted after the text. Use the SVG and VML renderers (WPaintedWidget::inlineSvgVml) for the most accurate font rendering.
void Wt::WPainter::drawText | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height, | |||
WFlags< AlignmentFlag > | flags, | |||
const WString & | text | |||
) |
Draw text.
This is an overloaded method for convenience.
void Wt::WPainter::fillPath | ( | const WPainterPath & | path, | |
const WBrush & | brush | |||
) |
Fill a (complex) path.
Like drawPath(const WPainterPath&), but does not stroke the path, and fills the path with the given brush.
Fill a rectangle.
Like drawRect(const WRectF&), but does not stroke the rect, and fills the rect with the given brush.
void Wt::WPainter::fillRect | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height, | |||
const WBrush & | brush | |||
) |
Fill a rectangle.
This is an overloaded method for convenience.
void Wt::WPainter::strokePath | ( | const WPainterPath & | path, | |
const WPen & | pen | |||
) |
Stroke a path.
Like drawPath(const WPainterPath&), but does not fill the path, and strokes the path with the given pen.
void Wt::WPainter::setBrush | ( | const WBrush & | brush | ) |
Change the fill style.
Changes the fills style for subsequent draw operations.
void Wt::WPainter::setFont | ( | const WFont & | font | ) |
Change the font.
Changes the font for subsequent text rendering. Note that only font sizes that are defined as an explicit size (see WFont::FixedSize) will render correctly in all devices (SVG, VML, and HtmlCanvas).
void Wt::WPainter::setPen | ( | const WPen & | pen | ) |
Change the pen.
Changes the pen used for stroking subsequent draw operations.
const WBrush& Wt::WPainter::brush | ( | ) | const [inline] |
Returns the current brush.
Returns the brush style that is currently used for filling.
const WFont& Wt::WPainter::font | ( | ) | const [inline] |
Returns the current font.
Returns the font that is currently used for rendering text. The default font is a 10pt sans serif font.
const WPen& Wt::WPainter::pen | ( | ) | const [inline] |
Returns the current pen.
Returns the pen that is currently used for stroking.
void Wt::WPainter::setClipping | ( | bool | enable | ) |
Enable or disable clipping.
Enables are disables clipping for subsequent operations using the current clip path set using setClipPath().
Note: Clipping is not supported for the VML renderer.
bool Wt::WPainter::hasClipping | ( | ) | const [inline] |
Return whether clipping is enabled.
Note: Clipping is not supported for the VML renderer.
void Wt::WPainter::setClipPath | ( | const WPainterPath & | clipPath | ) |
Set the clip path.
Sets the path that is used for clipping subsequent drawing operations. The clip path is only used when clipping is enabled using setClipping(bool). The path is specified in local coordinates.
Note: Only clipping with a rectangle is supported for the VML renderer (see WPainterPath::addRect()). The rectangle must, after applying the combined transformation system, be aligned with the window.
WPainterPath Wt::WPainter::clipPath | ( | ) | const [inline] |
Get the clip path.
The clip path is returned as it was defined: in the local coordinates at time of definition.
void Wt::WPainter::resetTransform | ( | ) |
Reset the current transformation.
Resets the current transformation to the identity transformation matrix, so that the logical coordinate system coincides with the device coordinate system.
void Wt::WPainter::rotate | ( | double | angle | ) |
Rotate the logical coordinate system.
Rotates the logical coordinate system around its origin. The angle is specified in degrees, and positive values are clock-wise.
void Wt::WPainter::scale | ( | double | sx, | |
double | sy | |||
) |
Scale the logical coordinate system.
Scales the logical coordinate system around its origin, by a factor in the X and Y directions.
void Wt::WPainter::translate | ( | const WPointF & | offset | ) |
Translate the origin of the logical coordinate system.
Translates the origin of the logical coordinate system to a new location relative to the current logical coordinate system.
void Wt::WPainter::translate | ( | double | dx, | |
double | dy | |||
) |
Translate the origin of the logical coordinate system.
Translates the origin of the logical coordinate system to a new location relative to the logical coordinate system.
void Wt::WPainter::setWorldTransform | ( | const WTransform & | matrix, | |
bool | combine = false | |||
) |
Set a transformation for the logical coordinate system.
Sets a new transformation which transforms logical coordinates to device coordinates. When combine is true, the transformation is combined with the current world transformation matrix.
const WTransform& Wt::WPainter::worldTransform | ( | ) | const [inline] |
void Wt::WPainter::save | ( | ) |
Save the current state.
A copy of the current state is saved on a stack. This state will may later be restored by popping this state from the stack using restore().
The state that is saved is the current pen, brush, font, transformation and clipping settings (see setClipping() and setClipPath()).
void Wt::WPainter::restore | ( | ) |
void Wt::WPainter::setViewPort | ( | const WRectF & | viewPort | ) |
Set the viewport.
Selects the part of the device that will correspond to the logical coordinate system.
By default, the viewport spans the entire device: it is the rectangle (0, 0) to (device->width(), device->height()). The window defines how the viewport is mapped to logical coordinates.
void Wt::WPainter::setViewPort | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height | |||
) |
WRectF Wt::WPainter::viewPort | ( | ) | const [inline] |
void Wt::WPainter::setWindow | ( | const WRectF & | window | ) |
Set the window.
Defines the viewport rectangle in logical coordinates, and thus how logical coordinates map onto the viewPort.
By default, is (0, 0) to (device->width(), device->height()). Thus, the default window and viewport leave logical coordinates identical to device coordinates.
void Wt::WPainter::setWindow | ( | double | x, | |
double | y, | |||
double | width, | |||
double | height | |||
) |
WRectF Wt::WPainter::window | ( | ) | const [inline] |
WTransform Wt::WPainter::combinedTransform | ( | ) | const |
Returns the combined transformation matrix.
Returns the transformation matrix that maps coordinates to device coordinates. It is the combination of the current world transformation (which defines the transformation within the logical coordinate system) and the window/viewport transformation (which transforms logical coordinates to device coordinates).