PaintArea Class Reference

#include <paintarea.h>

Inheritance diagram for PaintArea:

Inheritance graph
[legend]
Collaboration diagram for PaintArea:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 34 of file paintarea.h.

Public Member Functions

 PaintArea (QWidget *parent=0)
bool openImage (const QString &fileName)
bool saveImage (const QString &fileName, const char *fileFormat)
void setImage (const QImage &image)
void insertShape (const QPainterPath &path)
void setBrushColor (const QColor &color)
void setBrushWidth (int width)
void setBrush (BrushInterface *brushInterface, const QString &brush)
QImage image () const
QColor brushColor () const
int brushWidth () const
QSize sizeHint () const

Protected Member Functions

void paintEvent (QPaintEvent *event)
void mousePressEvent (QMouseEvent *event)
void mouseMoveEvent (QMouseEvent *event)
void mouseReleaseEvent (QMouseEvent *event)

Private Member Functions

void setupPainter (QPainter &painter)

Private Attributes

QImage theImage
QColor color
int thickness
BrushInterfacebrushInterface
QString brush
QPoint lastPos
QPainterPath pendingPath


Constructor & Destructor Documentation

PaintArea::PaintArea ( QWidget parent = 0  ) 

Definition at line 29 of file paintarea.cpp.

References Qt::blue, brushInterface, color, QImage::fill(), QImage::Format_RGB32, lastPos, qRgb(), QWidget::setAttribute(), theImage, thickness, Qt::WA_NoBackground, and Qt::WA_StaticContents.

00030     : QWidget(parent)
00031 {
00032     setAttribute(Qt::WA_StaticContents);
00033     setAttribute(Qt::WA_NoBackground);
00034 
00035     theImage = QImage(500, 400, QImage::Format_RGB32);
00036     theImage.fill(qRgb(255, 255, 255));
00037     color = Qt::blue;
00038     thickness = 3;
00039     brushInterface = 0;
00040     lastPos = QPoint(-1, -1);
00041 }

Here is the call graph for this function:


Member Function Documentation

bool PaintArea::openImage ( const QString fileName  ) 

Definition at line 43 of file paintarea.cpp.

References image(), QImage::load(), and setImage().

00044 {
00045     QImage image;
00046     if (!image.load(fileName))
00047         return false;
00048 
00049     setImage(image);
00050     return true;
00051 }

Here is the call graph for this function:

bool PaintArea::saveImage ( const QString fileName,
const char *  fileFormat 
)

Definition at line 53 of file paintarea.cpp.

References QImage::save(), and theImage.

00054 {
00055     return theImage.save(fileName, fileFormat);
00056 }

Here is the call graph for this function:

void PaintArea::setImage ( const QImage image  ) 

Definition at line 58 of file paintarea.cpp.

References QImage::convertToFormat(), QImage::Format_RGB32, image(), theImage, QWidget::update(), and QWidget::updateGeometry().

Referenced by MainWindow::applyFilter(), and openImage().

00059 {
00060     theImage = image.convertToFormat(QImage::Format_RGB32);
00061     update();
00062     updateGeometry();
00063 }

Here is the call graph for this function:

void PaintArea::insertShape ( const QPainterPath path  ) 

Definition at line 65 of file paintarea.cpp.

References Qt::CrossCursor, path, pendingPath, and QWidget::setCursor().

Referenced by MainWindow::insertShape().

00066 {
00067     pendingPath = path;
00068     setCursor(Qt::CrossCursor);
00069 }

Here is the call graph for this function:

void PaintArea::setBrushColor ( const QColor color  ) 

Definition at line 71 of file paintarea.cpp.

References color.

Referenced by MainWindow::brushColor().

00072 {
00073     this->color = color;
00074 }

void PaintArea::setBrushWidth ( int  width  ) 

Definition at line 76 of file paintarea.cpp.

References thickness.

Referenced by MainWindow::brushWidth().

00077 {
00078     thickness = width;
00079 }

void PaintArea::setBrush ( BrushInterface brushInterface,
const QString brush 
)

Definition at line 81 of file paintarea.cpp.

References brush, and brushInterface.

Referenced by MainWindow::changeBrush().

00082 {
00083     this->brushInterface = brushInterface;
00084     this->brush = brush;
00085 }

QImage PaintArea::image (  )  const [inline]

Definition at line 49 of file paintarea.h.

References theImage.

Referenced by MainWindow::applyFilter(), openImage(), and setImage().

00049 { return theImage; }

QColor PaintArea::brushColor (  )  const [inline]

Definition at line 50 of file paintarea.h.

References color.

Referenced by MainWindow::brushColor().

00050 { return color; }

int PaintArea::brushWidth (  )  const [inline]

Definition at line 51 of file paintarea.h.

References thickness.

Referenced by MainWindow::brushWidth().

00051 { return thickness; }

QSize PaintArea::sizeHint (  )  const [virtual]

Reimplemented from QWidget.

Definition at line 87 of file paintarea.cpp.

References QImage::size(), and theImage.

00088 {
00089     return theImage.size();
00090 }

Here is the call graph for this function:

void PaintArea::paintEvent ( QPaintEvent event  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive paint events which are passed in the event parameter.

A paint event is a request to repaint all or part of the widget. It can happen as a result of repaint() or update(), or because the widget was obscured and has now been uncovered, or for many other reasons.

Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region(). This speed optimization does not change the result, as painting is clipped to that region during event processing. QListView and QTableView do this, for example.

Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). repaint() does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so that you're painting on the widget's background.

The background can be set using setBackgroundRole() and setPalette().

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker.

Note: Under X11 it is possible to toggle the global double buffering by calling qt_x11_set_global_double_buffer(). Example usage:

See also:
event(), repaint(), update(), QPainter, QPixmap, QPaintEvent, {Analog Clock Example}

Reimplemented from QWidget.

Definition at line 92 of file paintarea.cpp.

References QPainter::drawImage(), and theImage.

00093 {
00094     QPainter painter(this);
00095     painter.drawImage(QPoint(0, 0), theImage);
00096 }

Here is the call graph for this function:

void PaintArea::mousePressEvent ( QMouseEvent event  )  [protected, virtual]

This event handler, for event event, can be reimplemented in a subclass to receive mouse press events for the widget.

If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

See also:
mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}

Reimplemented from QWidget.

Definition at line 98 of file paintarea.cpp.

References QColor::blue, QRectF::bottomLeft(), QPainterPath::boundingRect(), brush, brushInterface, QRectF::center(), color, QWidget::event(), QColor::green, QPainterPath::isEmpty(), lastPos, Qt::LeftButton, BrushInterface::mousePress(), pendingPath, QWidget::rect(), QColor::red, setupPainter(), theImage, QRectF::topRight(), QRectF::translate(), QWidget::unsetCursor(), and QWidget::update().

00099 {
00100     if (event->button() == Qt::LeftButton) {
00101         if (!pendingPath.isEmpty()) {
00102             QPainter painter(&theImage);
00103             setupPainter(painter);
00104 
00105             QRectF boundingRect = pendingPath.boundingRect();
00106             QLinearGradient gradient(boundingRect.topRight(),
00107                                      boundingRect.bottomLeft());
00108             gradient.setColorAt(0.0, QColor(color.red(), color.green(),
00109                                             color.blue(), 63));
00110             gradient.setColorAt(1.0, QColor(color.red(), color.green(),
00111                                             color.blue(), 191));
00112             painter.setBrush(gradient);
00113             painter.translate(event->pos() - boundingRect.center());
00114             painter.drawPath(pendingPath);
00115 
00116             pendingPath = QPainterPath();
00117             unsetCursor();
00118             update();
00119         } else {
00120             if (brushInterface) {
00121                 QPainter painter(&theImage);
00122                 setupPainter(painter);
00123                 QRect rect = brushInterface->mousePress(brush, painter,
00124                                                         event->pos());
00125                 update(rect);
00126             }
00127 
00128             lastPos = event->pos();
00129         }
00130     }
00131 }

Here is the call graph for this function:

void PaintArea::mouseMoveEvent ( QMouseEvent event  )  [protected, virtual]

This event handler, for event event, can be reimplemented in a subclass to receive mouse move events for the widget.

If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.

QMouseEvent::pos() reports the position of the mouse cursor, relative to this widget. For press and release events, the position is usually the same as the position of the last mouse move event, but it might be different if the user's hand shakes. This is a feature of the underlying window system, not Qt.

See also:
setMouseTracking(), mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), event(), QMouseEvent, {Scribble Example}

Reimplemented from QWidget.

Definition at line 133 of file paintarea.cpp.

References brush, brushInterface, QWidget::event(), lastPos, Qt::LeftButton, QWidget::rect(), setupPainter(), theImage, and QWidget::update().

00134 {
00135     if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) {
00136         if (brushInterface) {
00137             QPainter painter(&theImage);
00138             setupPainter(painter);
00139             QRect rect = brushInterface->mouseMove(brush, painter, lastPos,
00140                                                    event->pos());
00141             update(rect);
00142         }
00143 
00144         lastPos = event->pos();
00145     }
00146 }

Here is the call graph for this function:

void PaintArea::mouseReleaseEvent ( QMouseEvent event  )  [protected, virtual]

This event handler, for event event, can be reimplemented in a subclass to receive mouse release events for the widget.

See also:
mousePressEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}

Reimplemented from QWidget.

Definition at line 148 of file paintarea.cpp.

References brush, brushInterface, QWidget::event(), lastPos, Qt::LeftButton, QWidget::rect(), setupPainter(), theImage, and QWidget::update().

00149 {
00150     if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) {
00151         if (brushInterface) {
00152             QPainter painter(&theImage);
00153             setupPainter(painter);
00154             QRect rect = brushInterface->mouseRelease(brush, painter,
00155                                                       event->pos());
00156             update(rect);
00157         }
00158 
00159         lastPos = QPoint(-1, -1);
00160     }
00161 }

Here is the call graph for this function:

void PaintArea::setupPainter ( QPainter painter  )  [private]

Definition at line 163 of file paintarea.cpp.

References QPainter::Antialiasing, color, Qt::RoundCap, Qt::RoundJoin, QPainter::setPen(), QPainter::setRenderHint(), Qt::SolidLine, and thickness.

Referenced by mouseMoveEvent(), mousePressEvent(), and mouseReleaseEvent().

00164 {
00165     painter.setRenderHint(QPainter::Antialiasing, true);
00166     painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap,
00167                    Qt::RoundJoin));
00168 }

Here is the call graph for this function:


Member Data Documentation

QImage PaintArea::theImage [private]

Definition at line 63 of file paintarea.h.

Referenced by image(), mouseMoveEvent(), mousePressEvent(), mouseReleaseEvent(), PaintArea(), paintEvent(), saveImage(), setImage(), and sizeHint().

QColor PaintArea::color [private]

Definition at line 64 of file paintarea.h.

Referenced by brushColor(), mousePressEvent(), PaintArea(), setBrushColor(), and setupPainter().

int PaintArea::thickness [private]

Definition at line 65 of file paintarea.h.

Referenced by brushWidth(), PaintArea(), setBrushWidth(), and setupPainter().

BrushInterface* PaintArea::brushInterface [private]

Definition at line 67 of file paintarea.h.

Referenced by mouseMoveEvent(), mousePressEvent(), mouseReleaseEvent(), PaintArea(), and setBrush().

QString PaintArea::brush [private]

Definition at line 68 of file paintarea.h.

Referenced by mouseMoveEvent(), mousePressEvent(), mouseReleaseEvent(), and setBrush().

QPoint PaintArea::lastPos [private]

Definition at line 69 of file paintarea.h.

Referenced by mouseMoveEvent(), mousePressEvent(), mouseReleaseEvent(), and PaintArea().

QPainterPath PaintArea::pendingPath [private]

Definition at line 71 of file paintarea.h.

Referenced by insertShape(), and mousePressEvent().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 15:16:28 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1