#ifndef RENDERAREA_H
#define RENDERAREA_H
#include <QColor>
#include <QImage>
#include <QPainterPath>
#include <QWidget>
#include <QFile>
#include <QPainter>
#include <QMouseEvent>
#include <QByteArray>
#include <QString>
#include "interfaces.h"
{
Q_OBJECT
public:
bool openImage
(const QString &fileName
);
bool saveImage
(const QString &fileName,
const char *fileFormat
);
void setImage
(const QImage &image
);
void setBrushColor
(const QColor &color
);
void setBrushWidth(int width);
void setBrush
(BrushInterface
*brushInterface,
const QString &brush
);
QImage image
() const { return theImage;
} QColor brushColor
() const { return color;
} int brushWidth() const { return thickness; }
protected:
private:
int thickness;
BrushInterface *brushInterface;
};
#endif // RENDERAREA_H
#ifndef RENDERAREA_H
#define RENDERAREA_H
#include <QColor>
#include <QImage>
#include <QPainterPath>
#include <QWidget>
#include <QFile>
#include <QPainter>
#include <QMouseEvent>
#include <QByteArray>
#include <QString>
#include "interfaces.h"
class RenderArea : public QWidget
{
Q_OBJECT
public:
RenderArea(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 { return theImage; }
QColor brushColor() const { return color; }
int brushWidth() const { return thickness; }
QSize sizeHint() const;
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
void setupPainter(QPainter &painter);
QImage theImage;
QColor color;
int thickness;
BrushInterface *brushInterface;
QString brush;
QPoint lastPos;
QPainterPath pendingPath;
};
#endif // RENDERAREA_H
To copy to clipboard, switch view to plain text mode
#include "RenderArea.h"
RenderArea
::RenderArea(QWidget *parent
) : color(Qt::blue),
thickness(3),
brushInterface(0),
lastPos(-1, -1)
{
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_NoBackground);
theImage.load(":/resources/Screenshot.png");
}
bool RenderArea
::openImage(const QString &fileName
) {
if (!image.load(fileName))
return false;
setImage(image);
return true;
}
bool RenderArea
::saveImage(const QString &fileName,
const char *fileFormat
) {
return theImage.save(fileName, fileFormat);
}
void RenderArea
::setImage(const QImage &image
) {
theImage
= image.
convertToFormat(QImage::Format_RGB32);
update();
updateGeometry();
}
{
pendingPath = path;
#ifndef QT_NO_CURSOR
setCursor(Qt::CrossCursor);
#endif
}
void RenderArea
::setBrushColor(const QColor &color
) {
this->color = color;
}
void RenderArea::setBrushWidth(int width)
{
thickness = width;
}
void RenderArea
::setBrush(BrushInterface
*brushInterface,
const QString &brush
) {
this->brushInterface = brushInterface;
this->brush = brush;
}
QSize RenderArea
::sizeHint() const {
return theImage.size();
}
{
painter.
drawImage(QPoint(0,
0), theImage
);
}
{
if (event->button() == Qt::LeftButton) {
if (!pendingPath.isEmpty()) {
setupPainter(painter);
const QRectF boundingRect
= pendingPath.
boundingRect();
boundingRect.bottomLeft());
gradient.
setColorAt(0.0,
QColor(color.
red(), color.
green(),
color.blue(), 63));
gradient.
setColorAt(1.0,
QColor(color.
red(), color.
green(),
color.blue(), 191));
painter.setBrush(gradient);
painter.translate(event->pos() - boundingRect.center());
painter.drawPath(pendingPath);
#ifndef QT_NO_CURSOR
unsetCursor();
#endif
update();
} else {
if (brushInterface) {
setupPainter(painter);
const QRect rect
= brushInterface
->mousePress
(brush, painter,
event->pos());
update(rect);
}
lastPos = event->pos();
}
}
}
{
if ((event
->buttons
() & Qt
::LeftButton) && lastPos
!= QPoint(-1,
-1)) { if (brushInterface) {
setupPainter(painter);
const QRect rect
= brushInterface
->mouseMove
(brush, painter, lastPos, event
->pos
());
update(rect);
}
lastPos = event->pos();
}
}
{
if (event
->button
() == Qt
::LeftButton && lastPos
!= QPoint(-1,
-1)) { if (brushInterface) {
setupPainter(painter);
QRect rect
= brushInterface
->mouseRelease
(brush, painter,
event->pos());
update(rect);
}
}
}
void RenderArea
::setupPainter(QPainter &painter
) {
painter.
setRenderHint(QPainter::Antialiasing,
true);
painter.
setPen(QPen(color, thickness, Qt
::SolidLine, Qt
::RoundCap,
Qt::RoundJoin));
}
#include "RenderArea.h"
RenderArea::RenderArea(QWidget *parent) :
QWidget(parent),
color(Qt::blue),
thickness(3),
brushInterface(0),
lastPos(-1, -1)
{
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_NoBackground);
theImage.load(":/resources/Screenshot.png");
}
bool RenderArea::openImage(const QString &fileName)
{
QImage image;
if (!image.load(fileName))
return false;
setImage(image);
return true;
}
bool RenderArea::saveImage(const QString &fileName, const char *fileFormat)
{
return theImage.save(fileName, fileFormat);
}
void RenderArea::setImage(const QImage &image)
{
theImage = image.convertToFormat(QImage::Format_RGB32);
update();
updateGeometry();
}
void RenderArea::insertShape(const QPainterPath &path)
{
pendingPath = path;
#ifndef QT_NO_CURSOR
setCursor(Qt::CrossCursor);
#endif
}
void RenderArea::setBrushColor(const QColor &color)
{
this->color = color;
}
void RenderArea::setBrushWidth(int width)
{
thickness = width;
}
void RenderArea::setBrush(BrushInterface *brushInterface, const QString &brush)
{
this->brushInterface = brushInterface;
this->brush = brush;
}
QSize RenderArea::sizeHint() const
{
return theImage.size();
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), theImage);
}
void RenderArea::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (!pendingPath.isEmpty()) {
QPainter painter(&theImage);
setupPainter(painter);
const QRectF boundingRect = pendingPath.boundingRect();
QLinearGradient gradient(boundingRect.topRight(),
boundingRect.bottomLeft());
gradient.setColorAt(0.0, QColor(color.red(), color.green(),
color.blue(), 63));
gradient.setColorAt(1.0, QColor(color.red(), color.green(),
color.blue(), 191));
painter.setBrush(gradient);
painter.translate(event->pos() - boundingRect.center());
painter.drawPath(pendingPath);
pendingPath = QPainterPath();
#ifndef QT_NO_CURSOR
unsetCursor();
#endif
update();
} else {
if (brushInterface) {
QPainter painter(&theImage);
setupPainter(painter);
const QRect rect = brushInterface->mousePress(brush, painter,
event->pos());
update(rect);
}
lastPos = event->pos();
}
}
}
void RenderArea::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) {
if (brushInterface) {
QPainter painter(&theImage);
setupPainter(painter);
const QRect rect = brushInterface->mouseMove(brush, painter, lastPos, event->pos());
update(rect);
}
lastPos = event->pos();
}
}
void RenderArea::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) {
if (brushInterface) {
QPainter painter(&theImage);
setupPainter(painter);
QRect rect = brushInterface->mouseRelease(brush, painter,
event->pos());
update(rect);
}
lastPos = QPoint(-1, -1);
}
}
void RenderArea::setupPainter(QPainter &painter)
{
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
}
To copy to clipboard, switch view to plain text mode
Bookmarks