diff --git a/src/editing/basepreview.cpp b/src/editing/basepreview.cpp index f436011..43045dd 100644 --- a/src/editing/basepreview.cpp +++ b/src/editing/basepreview.cpp @@ -327,7 +327,7 @@ int PreviewDrawingManager::chunkCount() /*************** BasePreview ***************/ BasePreview::BasePreview(QWidget* parent) : -QWidget(parent) +DrawingWidget(parent) { this->_lock_drawing = new QMutex(); @@ -687,12 +687,11 @@ void BasePreview::resizeEvent(QResizeEvent* event) this->_lock_drawing->unlock(); } -void BasePreview::paintEvent(QPaintEvent*) +void BasePreview::doDrawing(QPainter* painter) { - QPainter painter(this); if (isEnabled()) { - painter.drawImage(0, 0, *this->_pixbuf); + painter->drawImage(0, 0, *this->_pixbuf); QImage osd(_pixbuf->size(), _pixbuf->format()); osd.fill(0x00000000); @@ -700,11 +699,11 @@ void BasePreview::paintEvent(QPaintEvent*) { _osd[i]->apply(&osd, xoffset, yoffset, scaling); } - painter.drawImage(0, 0, osd); + painter->drawImage(0, 0, osd); } else { - painter.fillRect(rect(), QColor(100, 100, 100)); + painter->fillRect(rect(), QColor(100, 100, 100)); } } diff --git a/src/editing/basepreview.h b/src/editing/basepreview.h index 1c11547..1993a74 100644 --- a/src/editing/basepreview.h +++ b/src/editing/basepreview.h @@ -3,14 +3,16 @@ #include #include -#include #include #include #include #include #include -#include "previewosd.h" -#include "common/previewrenderer.h" + +#include "editing/previewosd.h" +#include "editing/common/DrawingWidget.h" +#include "editing/common/previewrenderer.h" + #include "rendering/tools/pack.h" #include "rendering/tools/color.h" @@ -29,7 +31,9 @@ public: bool value; }; -class BasePreview : public QWidget +class QPainter; + +class BasePreview : public DrawingWidget { Q_OBJECT @@ -83,7 +87,8 @@ private: void timerEvent(QTimerEvent* event); void showEvent(QShowEvent* event); void resizeEvent(QResizeEvent* event); - void paintEvent(QPaintEvent* event); + + void doDrawing(QPainter* painter); void contextMenuEvent(QContextMenuEvent* event); void mousePressEvent(QMouseEvent* event); diff --git a/src/editing/common/DrawingWidget.cpp b/src/editing/common/DrawingWidget.cpp new file mode 100644 index 0000000..b31be41 --- /dev/null +++ b/src/editing/common/DrawingWidget.cpp @@ -0,0 +1,23 @@ +#include "DrawingWidget.h" + +#include +#include + +DrawingWidget::DrawingWidget(QWidget* parent) : +QWidget(parent) +{ +} + +void DrawingWidget::paintEvent(QPaintEvent*) +{ + QPainter painter(this); + + QPainterPath mask; + mask.addRoundedRect(rect(), 8.0, 8.0); + + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setClipPath(mask); + painter.setRenderHint(QPainter::Antialiasing, false); + + doDrawing(&painter); +} diff --git a/src/editing/common/DrawingWidget.h b/src/editing/common/DrawingWidget.h new file mode 100644 index 0000000..dd3db01 --- /dev/null +++ b/src/editing/common/DrawingWidget.h @@ -0,0 +1,21 @@ +#ifndef DRAWINGWIDGET_H +#define DRAWINGWIDGET_H + +#include + +class QPaintEvent; +class QPainter; + +class DrawingWidget : public QWidget +{ + Q_OBJECT +public: + explicit DrawingWidget(QWidget* parent = 0); +protected: + virtual void doDrawing(QPainter* painter) = 0; +private: + virtual void paintEvent(QPaintEvent* event); +}; + +#endif /* DRAWINGWIDGET_H */ + diff --git a/src/editing/terrain/widgetterrainbasenoisepreview.cpp b/src/editing/terrain/widgetterrainbasenoisepreview.cpp index aacdc99..ba97b32 100644 --- a/src/editing/terrain/widgetterrainbasenoisepreview.cpp +++ b/src/editing/terrain/widgetterrainbasenoisepreview.cpp @@ -5,7 +5,7 @@ #include "tools.h" WidgetTerrainBaseNoisePreview::WidgetTerrainBaseNoisePreview(QWidget* parent) : - QWidget(parent) + DrawingWidget(parent) { _noise = NULL; } @@ -16,18 +16,17 @@ void WidgetTerrainBaseNoisePreview::setNoise(NoiseGenerator* noise) update(); } -void WidgetTerrainBaseNoisePreview::paintEvent(QPaintEvent* event) +void WidgetTerrainBaseNoisePreview::doDrawing(QPainter* painter) { - QPainter painter(this); - - painter.setBrush(Qt::SolidPattern); - painter.drawRect(rect()); + painter->setBrush(Qt::SolidPattern); + painter->drawRect(rect()); int height = this->height(); if (_noise) { - QRect boundaries = event->region().boundingRect(); + //QRect boundaries = event->region().boundingRect(); + QRect boundaries = rect(); // TODO Limit to updated region double value, factor; double minvalue, maxvalue; @@ -38,8 +37,8 @@ void WidgetTerrainBaseNoisePreview::paintEvent(QPaintEvent* event) { value = noiseGet1DTotal(_noise, 100.0 * ((double)x) / factor); - painter.setPen(Qt::white); - painter.drawLine(x, height - 1 - (value - minvalue) * factor, x, height - 1); + painter->setPen(Qt::white); + painter->drawLine(x, height - 1 - (value - minvalue) * factor, x, height - 1); } } } diff --git a/src/editing/terrain/widgetterrainbasenoisepreview.h b/src/editing/terrain/widgetterrainbasenoisepreview.h index f3e12a5..a232da8 100644 --- a/src/editing/terrain/widgetterrainbasenoisepreview.h +++ b/src/editing/terrain/widgetterrainbasenoisepreview.h @@ -1,10 +1,13 @@ #ifndef _PAYSAGES_EDITING_TERRAIN_WIDGETTERRAINBASENOISEPREVIEW_H_ #define _PAYSAGES_EDITING_TERRAIN_WIDGETTERRAINBASENOISEPREVIEW_H_ -#include +#include "editing/common/DrawingWidget.h" + #include "rendering/noise.h" -class WidgetTerrainBaseNoisePreview : public QWidget +class QPainter; + +class WidgetTerrainBaseNoisePreview : public DrawingWidget { Q_OBJECT public: @@ -13,11 +16,7 @@ public: void setNoise(NoiseGenerator* noise); protected: - virtual void paintEvent(QPaintEvent* event); - -signals: - -public slots: + virtual void doDrawing(QPainter* painter); private: NoiseGenerator* _noise;