Commit 85abf746 authored by Christoffer Ackelman's avatar Christoffer Ackelman

Backported unique_ptr from C++11.

parent 17c471c9
...@@ -465,7 +465,9 @@ int XttTbl::command(char* input_str) ...@@ -465,7 +465,9 @@ int XttTbl::command(char* input_str)
/* Read command file */ /* Read command file */
sts = readcmdfile(&command[1]); sts = readcmdfile(&command[1]);
if (sts == DCLI__NOFILE) { if (sts == DCLI__NOFILE) {
message('E', "Unable to open file"); char tmp[200];
snprintf(tmp, 200, "Unable to open file \"%s\"", &command[1]);
message('E', tmp);
return DCLI__SUCCESS; return DCLI__SUCCESS;
} else if (EVEN(sts)) } else if (EVEN(sts))
return sts; return sts;
...@@ -486,7 +488,9 @@ int XttTbl::command(char* input_str) ...@@ -486,7 +488,9 @@ int XttTbl::command(char* input_str)
/* Read command file */ /* Read command file */
sts = readcmdfile(&symbol_value[1]); sts = readcmdfile(&symbol_value[1]);
if (sts == DCLI__NOFILE) { if (sts == DCLI__NOFILE) {
message('E', "Unable to open file"); char tmp[200];
snprintf(tmp, 200, "Unable to open file \"%s\"", &symbol_value[1]);
message('E', tmp);
return DCLI__SUCCESS; return DCLI__SUCCESS;
} else if (EVEN(sts)) } else if (EVEN(sts))
return sts; return sts;
......
#ifndef CO_UNIQUE_PTR
#define CO_UNIQUE_PTR
#include "co_debug.h"
// std::remove_reference
template<typename T> struct remove_reference { typedef T type; };
template<typename T> struct remove_reference<T&> { typedef T type; };
template<typename T> struct remove_reference<T&&> { typedef T type; };
// std::forward
template<typename T>
T&& forward(typename remove_reference<T>::type& x) {
return static_cast<T&&>(x);
}
template<typename T>
T&& forward(typename remove_reference<T>::type&& x) {
return static_cast<T&&>(x);
}
// std::move
template<typename T>
typename remove_reference<T>::type&& move(T&& x) {
return ((typename remove_reference<T>::type&&)x);
}
template<typename T>
class default_delete {
public:
void operator()(T* ptr) const {
delete ptr;
}
};
template<typename T>
class default_delete<T[]> {
public:
void operator()(T* ptr) const {
delete[] ptr;
}
};
// std::unique_ptr for single objects -> free memory with 'delete'
template<typename T, typename Deleter = default_delete<T>>
class unique_ptr {
public:
typedef unique_ptr<T, Deleter> this_type;
unique_ptr(T* p) : value(p) {
}
unique_ptr(T* p, Deleter d) : value(p), del(d) {
}
unique_ptr(this_type&& x) : value(x.release()), del(forward<Deleter>(x.get_deleter())) {
}
this_type& operator=(this_type&& x) {
reset(x.release());
del = move(forward<Deleter>(x.get_deleter()));
return *this;
}
~unique_ptr() {
reset();
}
void reset(T* p = NULL) {
if (p != value) {
get_deleter()(value);
value = p;
}
}
T* release() {
T* const tmp = value;
value = NULL;
return tmp;
}
T& operator*() const {
return *value;
}
T* operator->() const {
return value;
}
T* get() const {
return value;
}
Deleter& get_deleter() {
return del;
}
const Deleter& get_deleter() const {
return del;
}
protected:
T* value;
Deleter del;
unique_ptr(const this_type&);
unique_ptr& operator=(const this_type&);
unique_ptr& operator=(T* p);
};
// std::unique_ptr for arrays -> free memory with 'delete[]'
template<typename T, typename Deleter>
class unique_ptr<T[], Deleter> {
public:
typedef unique_ptr<T[], Deleter> this_type;
unique_ptr(T* p, Deleter d = default_delete<T[]>()) : value(p), del(d) {
}
unique_ptr(this_type&& x) : value(x.release()), del(forward<Deleter>(x.get_deleter())) {
}
this_type& operator=(this_type&& x) {
reset(x.release());
del = move(forward<Deleter>(x.get_deleter()));
return *this;
}
~unique_ptr() {
reset();
}
void reset(T* p = NULL) {
if (p != value) {
get_deleter()(value);
value = p;
}
}
T* release() {
T* const tmp = value;
value = NULL;
return tmp;
}
T& operator[](int i) const {
return value[i];
}
T* get() const {
return value;
}
Deleter& get_deleter() {
return del;
}
const Deleter& get_deleter() const {
return del;
}
protected:
T* value;
Deleter del;
unique_ptr(const this_type&);
unique_ptr& operator=(const this_type&);
unique_ptr& operator=(T* p);
};
#endif
\ No newline at end of file
...@@ -108,10 +108,10 @@ static QFont get_font(int painter_type, double size, const char* fontstr = "Luci ...@@ -108,10 +108,10 @@ static QFont get_font(int painter_type, double size, const char* fontstr = "Luci
return res; return res;
} }
static QPainter* get_painter( static unique_ptr<QPainter> get_painter(
QPaintDevice* window, FlowDrawQt* draw_ctx, int painter_type, int size) QPaintDevice* window, FlowDrawQt* draw_ctx, int painter_type, int size)
{ {
QPainter* painter = new QPainter(window); unique_ptr<QPainter> painter(new QPainter(window));
switch (painter_type) { switch (painter_type) {
case flow_eDrawType_LineDashed: case flow_eDrawType_LineDashed:
case flow_eDrawType_LineDashedRed: { case flow_eDrawType_LineDashedRed: {
...@@ -836,7 +836,7 @@ void FlowDrawQt::enable_event(FlowCtx* ctx, flow_eEvent event, ...@@ -836,7 +836,7 @@ void FlowDrawQt::enable_event(FlowCtx* ctx, flow_eEvent event,
ctx->enable_event(event, event_type, event_cb); ctx->enable_event(event, event_type, event_cb);
} }
QPainter* FlowDrawQt::get_painter(int painter_type, int size, bool nav) unique_ptr<QPainter> FlowDrawQt::get_painter(int painter_type, int size, bool nav)
{ {
if (nav) { if (nav) {
return ::get_painter(nav_window_canvas, this, painter_type, size); return ::get_painter(nav_window_canvas, this, painter_type, size);
...@@ -856,7 +856,7 @@ int FlowDrawQt::rect_helper(FlowCtx* ctx, int painter_type, int size, int x, ...@@ -856,7 +856,7 @@ int FlowDrawQt::rect_helper(FlowCtx* ctx, int painter_type, int size, int x,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, size, nav); unique_ptr<QPainter> painter = get_painter(painter_type, size, nav);
if (fill) { if (fill) {
// debug_print("fillRect [%d, %d, %d, %d] painter_type=%d, with color %s\n", x, y, width, height, painter_type, qPrintable(painter->brush().color().name())); // debug_print("fillRect [%d, %d, %d, %d] painter_type=%d, with color %s\n", x, y, width, height, painter_type, qPrintable(painter->brush().color().name()));
...@@ -867,8 +867,6 @@ int FlowDrawQt::rect_helper(FlowCtx* ctx, int painter_type, int size, int x, ...@@ -867,8 +867,6 @@ int FlowDrawQt::rect_helper(FlowCtx* ctx, int painter_type, int size, int x,
painter->drawRect(x, y, width, height); painter->drawRect(x, y, width, height);
} }
delete painter;
return 1; return 1;
} }
...@@ -921,7 +919,7 @@ int FlowDrawQt::triangle_helper(FlowCtx* ctx, int painter_type, int size, int x, ...@@ -921,7 +919,7 @@ int FlowDrawQt::triangle_helper(FlowCtx* ctx, int painter_type, int size, int x,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, size, nav); unique_ptr<QPainter> painter = get_painter(painter_type, size, nav);
QPolygon poly(4); QPolygon poly(4);
poly[0] = QPoint(x, y + height); poly[0] = QPoint(x, y + height);
...@@ -942,8 +940,6 @@ int FlowDrawQt::triangle_helper(FlowCtx* ctx, int painter_type, int size, int x, ...@@ -942,8 +940,6 @@ int FlowDrawQt::triangle_helper(FlowCtx* ctx, int painter_type, int size, int x,
painter->drawPolygon(poly); painter->drawPolygon(poly);
} }
delete painter;
return 1; return 1;
} }
...@@ -1004,7 +1000,7 @@ int FlowDrawQt::arrow_helper(FlowCtx* ctx, int painter_type, int size, int x1, ...@@ -1004,7 +1000,7 @@ int FlowDrawQt::arrow_helper(FlowCtx* ctx, int painter_type, int size, int x1,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, size, nav); unique_ptr<QPainter> painter = get_painter(painter_type, size, nav);
QPolygon poly(4); QPolygon poly(4);
poly[0] = QPoint(x1, y1); poly[0] = QPoint(x1, y1);
...@@ -1017,8 +1013,6 @@ int FlowDrawQt::arrow_helper(FlowCtx* ctx, int painter_type, int size, int x1, ...@@ -1017,8 +1013,6 @@ int FlowDrawQt::arrow_helper(FlowCtx* ctx, int painter_type, int size, int x1,
// debug_print("arrow (%d, %d) (%d, %d) (%d, %d)\n", x1, y1, x2, y2, x3, y3); // debug_print("arrow (%d, %d) (%d, %d) (%d, %d)\n", x1, y1, x2, y2, x3, y3);
painter->fillPath(path, painter->brush()); painter->fillPath(path, painter->brush());
delete painter;
return 1; return 1;
} }
...@@ -1069,11 +1063,10 @@ int FlowDrawQt::arc_helper(FlowCtx* ctx, int painter_type, int size, int x, ...@@ -1069,11 +1063,10 @@ int FlowDrawQt::arc_helper(FlowCtx* ctx, int painter_type, int size, int x,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, size, nav); unique_ptr<QPainter> painter = get_painter(painter_type, size, nav);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
// debug_print("arc [%d, %d, %d, %d], %d, %d\n", x, y, width, height, angle1, angle2); // debug_print("arc [%d, %d, %d, %d], %d, %d\n", x, y, width, height, angle1, angle2);
painter->drawArc(x, y, width, height, angle1 * 16, angle2 * 16); painter->drawArc(x, y, width, height, angle1 * 16, angle2 * 16);
delete painter;
return 1; return 1;
} }
...@@ -1130,10 +1123,9 @@ int FlowDrawQt::line_helper(FlowCtx* ctx, int painter_type, int size, int x1, ...@@ -1130,10 +1123,9 @@ int FlowDrawQt::line_helper(FlowCtx* ctx, int painter_type, int size, int x1,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, size, nav); unique_ptr<QPainter> painter = get_painter(painter_type, size, nav);
// debug_print("line (%d, %d) (%d, %d)\n", x1, y1, x2, y2); // debug_print("line (%d, %d) (%d, %d)\n", x1, y1, x2, y2);
painter->drawLine(x1, y1, x2, y2); painter->drawLine(x1, y1, x2, y2);
delete painter;
return 1; return 1;
} }
...@@ -1182,7 +1174,7 @@ int FlowDrawQt::text_pango_helper(FlowCtx* ctx, int x, int y, char* text, ...@@ -1182,7 +1174,7 @@ int FlowDrawQt::text_pango_helper(FlowCtx* ctx, int x, int y, char* text,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, idx); unique_ptr<QPainter> painter = get_painter(painter_type, idx);
if (dimmed) { if (dimmed) {
painter->setBrush(QBrush(flow_allocate_color(this, "gray"))); painter->setBrush(QBrush(flow_allocate_color(this, "gray")));
painter->setPen(QPen(painter->brush(), size + 1)); painter->setPen(QPen(painter->brush(), size + 1));
...@@ -1210,8 +1202,6 @@ int FlowDrawQt::text_pango_helper(FlowCtx* ctx, int x, int y, char* text, ...@@ -1210,8 +1202,6 @@ int FlowDrawQt::text_pango_helper(FlowCtx* ctx, int x, int y, char* text,
painter->drawText(x, ROUND(y - 0.8 * height), rect.width(), height, Qt::TextDontClip, str, &rect); painter->drawText(x, ROUND(y - 0.8 * height), rect.width(), height, Qt::TextDontClip, str, &rect);
} }
delete painter;
return 1; return 1;
} }
...@@ -1275,12 +1265,10 @@ int FlowDrawQt::nav_text_helper(FlowCtx* ctx, flow_eDrawType painter_type, ...@@ -1275,12 +1265,10 @@ int FlowDrawQt::nav_text_helper(FlowCtx* ctx, flow_eDrawType painter_type,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, size, true); unique_ptr<QPainter> painter = get_painter(painter_type, size, true);
// debug_print("drawText %s at (%d, %d)\n", text, x, y); // debug_print("drawText %s at (%d, %d)\n", text, x, y);
painter->drawText(x, y, QString::fromLocal8Bit(text, len)); painter->drawText(x, y, QString::fromLocal8Bit(text, len));
delete painter;
return 1; return 1;
} }
...@@ -1343,12 +1331,12 @@ int FlowDrawQt::pixmap(FlowCtx* ctx, int x, int y, ...@@ -1343,12 +1331,12 @@ int FlowDrawQt::pixmap(FlowCtx* ctx, int x, int y,
pms = (draw_sPixmap*)pixmaps; pms = (draw_sPixmap*)pixmaps;
QPainter* painter = get_painter(painter_type, idx); {
unique_ptr<QPainter> painter = get_painter(painter_type, idx);
painter->fillRect(x, y, pdata->width, pdata->height, painter->brush()); painter->fillRect(x, y, pdata->width, pdata->height, painter->brush());
delete painter; }
painter = get_painter(flow_eDrawType_LineErase, idx); unique_ptr<QPainter> painter = get_painter(flow_eDrawType_LineErase, idx);
QRegion clipRegion = QRegion(pms->pixmap[idx].createMaskFromColor(Qt::black)); QRegion clipRegion = QRegion(pms->pixmap[idx].createMaskFromColor(Qt::black));
painter->setClipRegion(clipRegion.translated(x, y)); painter->setClipRegion(clipRegion.translated(x, y));
...@@ -1356,8 +1344,6 @@ int FlowDrawQt::pixmap(FlowCtx* ctx, int x, int y, ...@@ -1356,8 +1344,6 @@ int FlowDrawQt::pixmap(FlowCtx* ctx, int x, int y,
painter->fillRect(x, y, pdata->width, pdata->height, painter->brush()); painter->fillRect(x, y, pdata->width, pdata->height, painter->brush());
delete painter;
return 1; return 1;
} }
...@@ -1374,14 +1360,15 @@ int FlowDrawQt::pixmap_inverse(FlowCtx* ctx, int x, int y, ...@@ -1374,14 +1360,15 @@ int FlowDrawQt::pixmap_inverse(FlowCtx* ctx, int x, int y,
pms = (draw_sPixmap*)pixmaps; pms = (draw_sPixmap*)pixmaps;
QPainter* painter = get_painter(flow_eDrawType_LineErase, idx); {
// debug_print("pixmap_inverse fillRect [%d, %d, %d, %d] with color %s\n", x, unique_ptr<QPainter> painter = get_painter(flow_eDrawType_LineErase, idx);
// y, pdata->width, pdata->height, // debug_print("pixmap_inverse fillRect [%d, %d, %d, %d] with color %s\n", x,
// painter->brush().color().name().toAscii().data()); // y, pdata->width, pdata->height,
painter->fillRect(x, y, pdata->width, pdata->height, painter->brush()); // painter->brush().color().name().toAscii().data());
delete painter; painter->fillRect(x, y, pdata->width, pdata->height, painter->brush());
}
painter = get_painter(painter_type, idx); unique_ptr<QPainter> painter = get_painter(painter_type, idx);
QRegion clipRegion = QRegion(pms->pixmap[idx].createMaskFromColor(Qt::black)); QRegion clipRegion = QRegion(pms->pixmap[idx].createMaskFromColor(Qt::black));
painter->setClipRegion(clipRegion.translated(x, y)); painter->setClipRegion(clipRegion.translated(x, y));
...@@ -1392,8 +1379,6 @@ int FlowDrawQt::pixmap_inverse(FlowCtx* ctx, int x, int y, ...@@ -1392,8 +1379,6 @@ int FlowDrawQt::pixmap_inverse(FlowCtx* ctx, int x, int y,
// painter->brush().color().name().toAscii().data()); // painter->brush().color().name().toAscii().data());
painter->fillRect(x, y, pdata->width, pdata->height, painter->brush()); painter->fillRect(x, y, pdata->width, pdata->height, painter->brush());
delete painter;
return 1; return 1;
} }
...@@ -1406,11 +1391,10 @@ int FlowDrawQt::pixmap_erase(FlowCtx* ctx, int x, int y, ...@@ -1406,11 +1391,10 @@ int FlowDrawQt::pixmap_erase(FlowCtx* ctx, int x, int y,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, idx); unique_ptr<QPainter> painter = get_painter(painter_type, idx);
// debug_print("pixmap_erase [%d, %d, %d, %d]\n", x, y, pdata->width, // debug_print("pixmap_erase [%d, %d, %d, %d]\n", x, y, pdata->width,
// pdata->height); // pdata->height);
painter->eraseRect(x, y, pdata->width, pdata->height); painter->eraseRect(x, y, pdata->width, pdata->height);
delete painter;
return 1; return 1;
} }
...@@ -1428,11 +1412,10 @@ int FlowDrawQt::nav_pixmap(FlowCtx* ctx, int x, int y, ...@@ -1428,11 +1412,10 @@ int FlowDrawQt::nav_pixmap(FlowCtx* ctx, int x, int y,
pms = (draw_sPixmap*)pixmaps; pms = (draw_sPixmap*)pixmaps;
QPainter* painter = get_painter(painter_type, idx); unique_ptr<QPainter> painter = get_painter(painter_type, idx);
// debug_print("nav_pixmap [%d, %d, %d, %d]\n", x, y, pdata->width, // debug_print("nav_pixmap [%d, %d, %d, %d]\n", x, y, pdata->width,
// pdata->height); // pdata->height);
painter->drawPixmap(x, y, pdata->width, pdata->height, pms->pixmap[idx]); painter->drawPixmap(x, y, pdata->width, pdata->height, pms->pixmap[idx]);
delete painter;
return 1; return 1;
} }
...@@ -1446,11 +1429,10 @@ int FlowDrawQt::nav_pixmap_erase(FlowCtx* ctx, int x, int y, ...@@ -1446,11 +1429,10 @@ int FlowDrawQt::nav_pixmap_erase(FlowCtx* ctx, int x, int y,
return 1; return 1;
} }
QPainter* painter = get_painter(painter_type, idx); unique_ptr<QPainter> painter = get_painter(painter_type, idx);
// debug_print("nav_pixmap_erase [%d, %d, %d, %d]\n", x, y, pdata->width, // debug_print("nav_pixmap_erase [%d, %d, %d, %d]\n", x, y, pdata->width,
// pdata->height); // pdata->height);
painter->eraseRect(x, y, pdata->width, pdata->height); painter->eraseRect(x, y, pdata->width, pdata->height);
delete painter;
return 1; return 1;
} }
...@@ -1478,17 +1460,18 @@ int FlowDrawQt::image(FlowCtx* ctx, int x, int y, int width, int height, ...@@ -1478,17 +1460,18 @@ int FlowDrawQt::image(FlowCtx* ctx, int x, int y, int width, int height,
return 1; return 1;
} }
QPainter* painter = get_painter(flow_eDrawType_Line, 0); unique_ptr<QPainter> painter = get_painter(flow_eDrawType_Line, 0);
if (clip_mask) { if (clip_mask) {
set_image_clip_mask(painter, clip_mask, x, y); QRegion clipRegion
= QRegion(((QPixmap*)clip_mask)->createMaskFromColor(Qt::black));
painter->setClipRegion(clipRegion.translated(x, y));
painter->setClipping(true);
} }
// debug_print("image [%d, %d, %d, %d]\n", x, y, width, height); // debug_print("image [%d, %d, %d, %d]\n", x, y, width, height);
painter->drawImage(QRect(x, y, width, height), *((QImage*)image)); painter->drawImage(QRect(x, y, width, height), *((QImage*)image));
delete painter;
return 1; return 1;
} }
...@@ -1498,13 +1481,11 @@ void FlowDrawQt::clear(FlowCtx* ctx) ...@@ -1498,13 +1481,11 @@ void FlowDrawQt::clear(FlowCtx* ctx)
return; return;
} }
QPainter* painter = new QPainter(window_canvas); unique_ptr<QPainter> painter = new QPainter(window_canvas);
painter->setBackground(QBrush(background)); painter->setBackground(QBrush(background));
// debug_print("clear color=%s\n", painter->background().color().name().toAscii().data()); // debug_print("clear color=%s\n", painter->background().color().name().toAscii().data());
painter->eraseRect(window_canvas->rect()); painter->eraseRect(window_canvas->rect());
delete painter;
} }
void FlowDrawQt::nav_clear(FlowCtx* ctx) void FlowDrawQt::nav_clear(FlowCtx* ctx)
...@@ -1513,13 +1494,11 @@ void FlowDrawQt::nav_clear(FlowCtx* ctx) ...@@ -1513,13 +1494,11 @@ void FlowDrawQt::nav_clear(FlowCtx* ctx)
return; return;
} }
QPainter* painter = new QPainter(nav_window_canvas); unique_ptr<QPainter> painter = new QPainter(nav_window_canvas);
painter->setBackground(QBrush(background)); painter->setBackground(QBrush(background));
// debug_print("nav_clear color=%s\n\n", painter->background().color().name().toAscii().data()); // debug_print("nav_clear color=%s\n\n", painter->background().color().name().toAscii().data());
painter->eraseRect(nav_window_canvas->rect()); painter->eraseRect(nav_window_canvas->rect());
delete painter;
} }
void FlowDrawQt::get_window_size(FlowCtx* ctx, int* width, int* height) void FlowDrawQt::get_window_size(FlowCtx* ctx, int* width, int* height)
...@@ -1652,7 +1631,7 @@ int FlowDrawQt::get_text_extent(FlowCtx* ctx, const char* text, int len, ...@@ -1652,7 +1631,7 @@ int FlowDrawQt::get_text_extent(FlowCtx* ctx, const char* text, int len,
int FlowDrawQt::get_text_extent_pango(FlowCtx* ctx, const char* text, int len, int FlowDrawQt::get_text_extent_pango(FlowCtx* ctx, const char* text, int len,
flow_eDrawType painter_type, int idx, double size, int* width, int* height) flow_eDrawType painter_type, int idx, double size, int* width, int* height)
{ {
QPainter* painter = get_painter(painter_type, idx); unique_ptr<QPainter> painter = get_painter(painter_type, idx);
QString str; QString str;
if (ctx->text_coding != flow_eTextCoding_UTF_8) { if (ctx->text_coding != flow_eTextCoding_UTF_8) {
...@@ -1668,8 +1647,6 @@ int FlowDrawQt::get_text_extent_pango(FlowCtx* ctx, const char* text, int len, ...@@ -1668,8 +1647,6 @@ int FlowDrawQt::get_text_extent_pango(FlowCtx* ctx, const char* text, int len,
*width = boundingRect.width(); *width = boundingRect.width();
*height = boundingRect.height(); *height = boundingRect.height();
delete painter;
return 1; return 1;
} }
...@@ -1682,7 +1659,7 @@ void FlowDrawQt::copy_area(FlowCtx* ctx, int x, int y) ...@@ -1682,7 +1659,7 @@ void FlowDrawQt::copy_area(FlowCtx* ctx, int x, int y)
// debug_print("copy_area: x=%d, y=%d\n", x, y); // debug_print("copy_area: x=%d, y=%d\n", x, y);
QPixmap screenShot = QPixmap::grabWidget(window); QPixmap screenShot = QPixmap::grabWidget(window);
QPainter* painter = get_painter(flow_eDrawType_Line, 3); unique_ptr<QPainter> painter = get_painter(flow_eDrawType_Line, 3);
if (x >= 0 && y >= 0) { if (x >= 0 && y >= 0) {
painter->drawPixmap( painter->drawPixmap(
x, y, screenShot, 0, 0, ctx->window_width - x, ctx->window_height - y); x, y, screenShot, 0, 0, ctx->window_width - x, ctx->window_height - y);
...@@ -1724,7 +1701,6 @@ void FlowDrawQt::copy_area(FlowCtx* ctx, int x, int y) ...@@ -1724,7 +1701,6 @@ void FlowDrawQt::copy_area(FlowCtx* ctx, int x, int y)
x, ctx->window_height + y, ctx->window_width, ctx->window_height); x, ctx->window_height + y, ctx->window_width, ctx->window_height);
} }
} }
delete painter;
} }
void FlowDrawQt::clear_area( void FlowDrawQt::clear_area(
...@@ -1734,13 +1710,11 @@ void FlowDrawQt::clear_area( ...@@ -1734,13 +1710,11 @@ void FlowDrawQt::clear_area(
return; return;
} }
QPainter* painter = new QPainter(window_canvas); unique_ptr<QPainter> painter = new QPainter(window_canvas);
painter->setBackground(QBrush(background)); painter->setBackground(QBrush(background));
// debug_print("clear_area [%d, %d, %d, %d], color=%s\n", ll_x, ll_y, ur_x - ll_x, ur_y - ll_y, painter->background().color().name().toAscii().data()); // debug_print("clear_area [%d, %d, %d, %d], color=%s\n", ll_x, ll_y, ur_x - ll_x, ur_y - ll_y, painter->background().color().name().toAscii().data());
painter->eraseRect(ll_x, ll_y, ur_x - ll_x, ur_y - ll_y); painter->eraseRect(ll_x, ll_y, ur_x - ll_x, ur_y - ll_y);
delete painter;
} }
void FlowDrawQt::set_inputfocus(FlowCtx* ctx) void FlowDrawQt::set_inputfocus(FlowCtx* ctx)
...@@ -1754,15 +1728,6 @@ void FlowDrawQt::set_click_sensitivity(FlowCtx* ctx, int value) ...@@ -1754,15 +1728,6 @@ void FlowDrawQt::set_click_sensitivity(FlowCtx* ctx, int value)
click_sensitivity = value; click_sensitivity = value;
} }
void FlowDrawQt::set_image_clip_mask(
QPainter* painter, flow_tPixmap pixmap, int x, int y)
{
QRegion clipRegion
= QRegion(((QPixmap*)pixmap)->createMaskFromColor(Qt::black));
painter->setClipRegion(clipRegion.translated(x, y));
painter->setClipping(true);
}
void FlowDrawQt::set_white_background(FlowCtx* ctx) void FlowDrawQt::set_white_background(FlowCtx* ctx)
{ {
background = flow_allocate_color(this, "white"); background = flow_allocate_color(this, "white");
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#ifndef flow_draw_qt_h #ifndef flow_draw_qt_h
#define flow_draw_qt_h #define flow_draw_qt_h
#include "co_smart_ptr.h"
#include "flow_draw.h" #include "flow_draw.h"
#include <QColor> #include <QColor>
...@@ -205,9 +207,6 @@ public: ...@@ -205,9 +207,6 @@ public:
void set_click_sensitivity(FlowCtx* ctx, int value); void set_click_sensitivity(FlowCtx* ctx, int value);
void set_image_clip_mask(
QPainter* painter, flow_tPixmap pixmap, int x, int y);
void set_white_background(FlowCtx* ctx); void set_white_background(FlowCtx* ctx);
int image_get_width(flow_tImImage image); int image_get_width(flow_tImImage image);
...@@ -233,7 +232,7 @@ public: ...@@ -233,7 +232,7 @@ public:
void* flow_ctx, int page_border, int* sts); void* flow_ctx, int page_border, int* sts);
private: private:
QPainter* get_painter(int painter_type, int size, bool nav = false); unique_ptr<QPainter> get_painter(int painter_type, int size, bool nav = false);
int rect_helper(FlowCtx* ctx, int painter_type, int size, int x, int y, int rect_helper(FlowCtx* ctx, int painter_type, int size, int x, int y,
int width, int height, bool nav = false, bool fill = false); int width, int height, bool nav = false, bool fill = false);
......
...@@ -77,8 +77,8 @@ static QColor glow_allocate_color( ...@@ -77,8 +77,8 @@ static QColor glow_allocate_color(
static QColor glow_allocate_custom_color(GlowDrawQt* draw_ctx, static QColor glow_allocate_custom_color(GlowDrawQt* draw_ctx,
glow_eDrawType drawtype, int rgb_red, int rgb_green, int rgb_blue); glow_eDrawType drawtype, int rgb_red, int rgb_green, int rgb_blue);
static int glow_read_color_file( static unique_ptr<QColor[]> glow_read_color_file(
const char* filename, QColor** color_array, int* size); const char* filename, int* size, int* sts);
static QColor draw_type_to_color( static QColor draw_type_to_color(
GlowDrawQt* draw_ctx, int painter_type, int size) GlowDrawQt* draw_ctx, int painter_type, int size)
...@@ -101,14 +101,13 @@ static QColor draw_type_to_color( ...@@ -101,14 +101,13 @@ static QColor draw_type_to_color(
default: { default: {
if (painter_type >= glow_eDrawType_Color4 if (painter_type >= glow_eDrawType_Color4
&& painter_type <= glow_eDrawType_Color300) { && painter_type <= glow_eDrawType_Color300) {
QColor* color_array; int sts = 0;
int sts = glow_read_color_file( unique_ptr<QColor[]> color_array = glow_read_color_file(
"/home/claes/test/ge_colors.dat", &color_array, &size); "/home/claes/test/ge_colors.dat", &size, &sts);
if (ODD(sts)) { if (ODD(sts)) {
QColor color_p = color_array[painter_type - glow_eDrawType_Color4]; QColor color_p = color_array[painter_type - glow_eDrawType_Color4];
QColor color = glow_allocate_color( QColor color = glow_allocate_color(
draw_ctx, color_p.red(), color_p.green(), color_p.blue()); draw_ctx, color_p.red(), color_p.green(), color_p.blue());
delete color_array;
return color; return color;
} }
} }
...@@ -160,10 +159,10 @@ static QFont get_font(int font_idx, int painter_type, double size) ...@@ -160,10 +159,10 @@ static QFont get_font(int font_idx, int painter_type, double size)
return res; return res;
} }
static QPainter* get_painter( static unique_ptr<QPainter> get_painter(
QPaintDevice* window, GlowDrawQt* draw_ctx, int painter_type, int size) QPaintDevice* window, GlowDrawQt* draw_ctx, int painter_type, int size)
{ {
QPainter* painter = new QPainter(window); unique_ptr<QPainter> painter = new QPainter(window);
switch (painter_type) { switch (painter_type) {
case glow_eDrawType_LineDashed: case glow_eDrawType_LineDashed:
case glow_eDrawType_LineDashedRed: { case glow_eDrawType_LineDashedRed: {
...@@ -898,7 +897,7 @@ int GlowDrawQt::rect_helper(GlowWind* wind, int painter_type, int size, int x, ...@@ -898,7 +897,7 @@ int GlowDrawQt::rect_helper(GlowWind* wind, int painter_type, int size, int x,
} }
DrawWindQt* w = (DrawWindQt*)wind->window; DrawWindQt* w = (DrawWindQt*)wind->window;
QPainter* painter = get_painter(w->buffer, this, painter_type, size); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, size);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -913,8 +912,6 @@ int GlowDrawQt::rect_helper(GlowWind* wind, int painter_type, int size, int x, ...@@ -913,8 +912,6 @@ int GlowDrawQt::rect_helper(GlowWind* wind, int painter_type, int size, int x,
painter->drawRect(x, y, width, height); painter->drawRect(x, y, width, height);
} }
delete painter;
return 1; return 1;
} }
...@@ -948,7 +945,7 @@ int GlowDrawQt::arrow_helper(GlowWind* wind, int painter_type, int size, int x1, ...@@ -948,7 +945,7 @@ int GlowDrawQt::arrow_helper(GlowWind* wind, int painter_type, int size, int x1,
poly[2] = QPoint(x3, y3); poly[2] = QPoint(x3, y3);
poly[3] = QPoint(x1, y1); poly[3] = QPoint(x1, y1);
QPainter* painter = get_painter(w->buffer, this, painter_type, size); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, size);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -958,7 +955,6 @@ int GlowDrawQt::arrow_helper(GlowWind* wind, int painter_type, int size, int x1, ...@@ -958,7 +955,6 @@ int GlowDrawQt::arrow_helper(GlowWind* wind, int painter_type, int size, int x1,
path.addPolygon(poly); path.addPolygon(poly);
// debug_print("arrow (%d, %d) (%d, %d) (%d, %d)\n", x1, y1, x2, y2, x3, y3); // debug_print("arrow (%d, %d) (%d, %d) (%d, %d)\n", x1, y1, x2, y2, x3, y3);
painter->fillPath(path, painter->brush()); painter->fillPath(path, painter->brush());
delete painter;
return 1; return 1;
} }
...@@ -1003,7 +999,7 @@ int GlowDrawQt::arc_helper(GlowWind* wind, int painter_type, int size, int x, ...@@ -1003,7 +999,7 @@ int GlowDrawQt::arc_helper(GlowWind* wind, int painter_type, int size, int x,
} }
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, size); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, size);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1018,8 +1014,6 @@ int GlowDrawQt::arc_helper(GlowWind* wind, int painter_type, int size, int x, ...@@ -1018,8 +1014,6 @@ int GlowDrawQt::arc_helper(GlowWind* wind, int painter_type, int size, int x,
painter->drawArc(x, y, width, height, angle1 * 16, angle2 * 16); painter->drawArc(x, y, width, height, angle1 * 16, angle2 * 16);
} }
delete painter;
return 1; return 1;
} }
...@@ -1095,7 +1089,7 @@ int GlowDrawQt::line_helper(GlowWind* wind, int painter_type, int size, int x1, ...@@ -1095,7 +1089,7 @@ int GlowDrawQt::line_helper(GlowWind* wind, int painter_type, int size, int x1,
return 1; return 1;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, size); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, size);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1149,8 +1143,6 @@ int GlowDrawQt::line_helper(GlowWind* wind, int painter_type, int size, int x1, ...@@ -1149,8 +1143,6 @@ int GlowDrawQt::line_helper(GlowWind* wind, int painter_type, int size, int x1,
// debug_print("line (%d, %d) (%d, %d)\n", x1, y1, x2, y2); // debug_print("line (%d, %d) (%d, %d)\n", x1, y1, x2, y2);
painter->drawLine(x1, y1, x2, y2); painter->drawLine(x1, y1, x2, y2);
delete painter;
return 1; return 1;
} }
...@@ -1200,7 +1192,7 @@ int GlowDrawQt::polyline_helper(GlowWind* wind, int painter_type, int size, ...@@ -1200,7 +1192,7 @@ int GlowDrawQt::polyline_helper(GlowWind* wind, int painter_type, int size,
} }
DrawWindQt* w = (DrawWindQt*)wind->window; DrawWindQt* w = (DrawWindQt*)wind->window;
QPainter* painter = get_painter(w->buffer, this, painter_type, size); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, size);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1208,19 +1200,15 @@ int GlowDrawQt::polyline_helper(GlowWind* wind, int painter_type, int size, ...@@ -1208,19 +1200,15 @@ int GlowDrawQt::polyline_helper(GlowWind* wind, int painter_type, int size,
if (fill) { if (fill) {
int cnt; int cnt;
QPoint* qpoints = points_to_qt_points_curve(wind, points, point_cnt, &cnt); unique_ptr<QPoint[]> qpoints = points_to_qt_points_curve(wind, points, point_cnt, &cnt);
painter->drawPolygon(qpoints, cnt); painter->drawPolygon(qpoints.get(), cnt);
delete[] qpoints;
} else { } else {
QPoint* qpoints = points_to_qt_points(points, point_cnt); unique_ptr<QPoint[]> qpoints = points_to_qt_points(points, point_cnt);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
// debug_print("polyline painter_type=%d, fill=%d, point_cnt=%d\n", painter_type, fill, point_cnt); // debug_print("polyline painter_type=%d, fill=%d, point_cnt=%d\n", painter_type, fill, point_cnt);
painter->drawPolyline(qpoints, point_cnt); painter->drawPolyline(qpoints.get(), point_cnt);
delete[] qpoints;
} }
delete painter;
return 1; return 1;
} }
...@@ -1271,7 +1259,7 @@ int GlowDrawQt::text(GlowWind* wind, int x, int y, char* text, int len, ...@@ -1271,7 +1259,7 @@ int GlowDrawQt::text(GlowWind* wind, int x, int y, char* text, int len,
font_idx = glow_eFont_Helvetica; font_idx = glow_eFont_Helvetica;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, idx); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, idx);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1292,8 +1280,6 @@ int GlowDrawQt::text(GlowWind* wind, int x, int y, char* text, int len, ...@@ -1292,8 +1280,6 @@ int GlowDrawQt::text(GlowWind* wind, int x, int y, char* text, int len,
painter->drawText(x, y, rect.width(), rect.height(), Qt::TextDontClip, painter->drawText(x, y, rect.width(), rect.height(), Qt::TextDontClip,
QString::fromLocal8Bit(text, len)); QString::fromLocal8Bit(text, len));
delete painter;
return 1; return 1;
} }
...@@ -1316,7 +1302,7 @@ int GlowDrawQt::text_cursor(GlowWind* wind, int x, int y, char* text, int len, ...@@ -1316,7 +1302,7 @@ int GlowDrawQt::text_cursor(GlowWind* wind, int x, int y, char* text, int len,
width = 0; width = 0;
} }
QPainter* painter = get_painter(w->buffer, this, color, 1); unique_ptr<QPainter> painter = get_painter(w->buffer, this, color, 1);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1325,8 +1311,6 @@ int GlowDrawQt::text_cursor(GlowWind* wind, int x, int y, char* text, int len, ...@@ -1325,8 +1311,6 @@ int GlowDrawQt::text_cursor(GlowWind* wind, int x, int y, char* text, int len,
// debug_print("cursor %d,%d to %d,%d\n", x + width, y + descent, x + width, y - height + descent); // debug_print("cursor %d,%d to %d,%d\n", x + width, y + descent, x + width, y - height + descent);
painter->drawLine(x + width, y + descent, x + width, y - height + descent); painter->drawLine(x + width, y + descent, x + width, y - height + descent);
delete painter;
return 1; return 1;
} }
...@@ -1354,7 +1338,7 @@ int GlowDrawQt::text_erase(GlowWind* wind, int x, int y, char* text, int len, ...@@ -1354,7 +1338,7 @@ int GlowDrawQt::text_erase(GlowWind* wind, int x, int y, char* text, int len,
painter_type = glow_eDrawType_TextHelveticaEraseBold; painter_type = glow_eDrawType_TextHelveticaEraseBold;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, idx); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, idx);
if (font_idx != glow_eFont_Helvetica) { if (font_idx != glow_eFont_Helvetica) {
painter->setFont(get_font(font_idx, painter_type, idx)); painter->setFont(get_font(font_idx, painter_type, idx));
...@@ -1370,8 +1354,6 @@ int GlowDrawQt::text_erase(GlowWind* wind, int x, int y, char* text, int len, ...@@ -1370,8 +1354,6 @@ int GlowDrawQt::text_erase(GlowWind* wind, int x, int y, char* text, int len,
painter->drawText(x, y, rect.width(), rect.height(), Qt::TextDontClip, painter->drawText(x, y, rect.width(), rect.height(), Qt::TextDontClip,
QString::fromLocal8Bit(text, len)); QString::fromLocal8Bit(text, len));
delete painter;
return 1; return 1;
} }
...@@ -1418,7 +1400,7 @@ int GlowDrawQt::image_helper(GlowWind* wind, int x, int y, int width, ...@@ -1418,7 +1400,7 @@ int GlowDrawQt::image_helper(GlowWind* wind, int x, int y, int width,
return 1; return 1;
} }
QPainter* painter = get_painter(w->buffer, this, glow_eDrawType_Line, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, glow_eDrawType_Line, 0);
if (clip_mask) { if (clip_mask) {
set_image_clip_mask(painter, clip_mask, x, y); set_image_clip_mask(painter, clip_mask, x, y);
...@@ -1433,8 +1415,6 @@ int GlowDrawQt::image_helper(GlowWind* wind, int x, int y, int width, ...@@ -1433,8 +1415,6 @@ int GlowDrawQt::image_helper(GlowWind* wind, int x, int y, int width,
painter->drawImage(QRect(x, y, width, height), *((QImage*)image)); painter->drawImage(QRect(x, y, width, height), *((QImage*)image));
} }
delete painter;
return 1; return 1;
} }
...@@ -1489,11 +1469,9 @@ void GlowDrawQt::copy_buffer( ...@@ -1489,11 +1469,9 @@ void GlowDrawQt::copy_buffer(
+ w->clip_rectangle[w->clip_cnt - 1].height()); + w->clip_rectangle[w->clip_cnt - 1].height());
} }
QPainter* painter = get_painter(w->buffer, this, glow_eDrawType_Line, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, glow_eDrawType_Line, 0);
painter->drawImage(x0, y0, *(w->buffer), x0, y0, x1 - x0, y1 - y0); painter->drawImage(x0, y0, *(w->buffer), x0, y0, x1 - x0, y1 - y0);
delete painter;
} }
void GlowDrawQt::get_window_size(GlowWind* wind, int* width, int* height) void GlowDrawQt::get_window_size(GlowWind* wind, int* width, int* height)
...@@ -1659,7 +1637,7 @@ void GlowDrawQt::copy_area(GlowWind* wind, int x, int y) ...@@ -1659,7 +1637,7 @@ void GlowDrawQt::copy_area(GlowWind* wind, int x, int y)
// debug_print("copy_area [%d, %d, %d, %d]\n", x, y, window_width, window_height); // debug_print("copy_area [%d, %d, %d, %d]\n", x, y, window_width, window_height);
QPainter* painterBuffer unique_ptr<QPainter> painterBuffer
= get_painter(w->buffer, this, glow_eDrawType_Line, 3); = get_painter(w->buffer, this, glow_eDrawType_Line, 3);
if (x >= 0 && y >= 0) { if (x >= 0 && y >= 0) {
painterBuffer->drawImage( painterBuffer->drawImage(
...@@ -1698,8 +1676,6 @@ void GlowDrawQt::copy_area(GlowWind* wind, int x, int y) ...@@ -1698,8 +1676,6 @@ void GlowDrawQt::copy_area(GlowWind* wind, int x, int y)
painterBuffer->eraseRect(x, window_height + y, window_width - x, -y); painterBuffer->eraseRect(x, window_height + y, window_width - x, -y);
} }
} }
delete painterBuffer;
} }
void GlowDrawQt::clear_area( void GlowDrawQt::clear_area(
...@@ -1710,14 +1686,12 @@ void GlowDrawQt::clear_area( ...@@ -1710,14 +1686,12 @@ void GlowDrawQt::clear_area(
} }
DrawWindQt* w = (DrawWindQt*)wind->window; DrawWindQt* w = (DrawWindQt*)wind->window;
QPainter* painter = new QPainter(w->buffer); unique_ptr<QPainter> painter = new QPainter(w->buffer);
painter->setBackground(QBrush(background)); painter->setBackground(QBrush(background));
// debug_print("clear_area [%d, %d, %d, %d], color=%s\n", ll_x, ll_y, ur_x - ll_x, ur_y - ll_y, qPrintable(painter->background().color().name())); // debug_print("clear_area [%d, %d, %d, %d], color=%s\n", ll_x, ll_y, ur_x - ll_x, ur_y - ll_y, qPrintable(painter->background().color().name()));
painter->eraseRect(ll_x, ll_y, ur_x - ll_x, ur_y - ll_y); painter->eraseRect(ll_x, ll_y, ur_x - ll_x, ur_y - ll_y);
delete painter;
} }
void GlowDrawQt::set_inputfocus(GlowWind* wind) void GlowDrawQt::set_inputfocus(GlowWind* wind)
...@@ -1726,30 +1700,31 @@ void GlowDrawQt::set_inputfocus(GlowWind* wind) ...@@ -1726,30 +1700,31 @@ void GlowDrawQt::set_inputfocus(GlowWind* wind)
w->window->setFocus(Qt::OtherFocusReason); w->window->setFocus(Qt::OtherFocusReason);
} }
static int glow_read_color_file( static unique_ptr<QColor[]> glow_read_color_file(
const char* filename, QColor** color_array, int* size) const char* filename, int* size, int* sts)
{ {
char line[80]; char line[80];
QColor* color_p;
std::ifstream fp; std::ifstream fp;
int nr; int nr;
int line_cnt; int line_cnt;
float f_red, f_green, f_blue; float f_red, f_green, f_blue;
unique_ptr<QColor[]> color_array(new QColor[300]);
if (!check_file(filename)) { if (!check_file(filename)) {
return 0; *sts = 0;
return color_array;
} else { } else {
printf("** Opening color file %s\n", filename); printf("** Opening color file %s\n", filename);
fp.open(filename); fp.open(filename);
if (!fp) { if (!fp) {
return GLOW__FILEOPEN; *sts = GLOW__FILEOPEN;
return color_array;
} }
*color_array = new QColor[300];
*size = 0; *size = 0;
line_cnt = 0; line_cnt = 0;
color_p = *color_array;
while (*size < 300) { while (*size < 300) {
fp.getline(line, sizeof(line)); fp.getline(line, sizeof(line));
if (line[0] == 0) { if (line[0] == 0) {
...@@ -1763,16 +1738,16 @@ static int glow_read_color_file( ...@@ -1763,16 +1738,16 @@ static int glow_read_color_file(
if (nr != 3) { if (nr != 3) {
printf("** Syntax error in file %s, line %d", filename, line_cnt); printf("** Syntax error in file %s, line %d", filename, line_cnt);
} else { } else {
color_p->setRgb( color_array[*size].setRgb(
int(f_red * 255), int(f_green * 255), int(f_blue * 255)); int(f_red * 255), int(f_green * 255), int(f_blue * 255));
color_p++;
(*size)++; (*size)++;
} }
} }
fp.close(); fp.close();
} }
return 1; *sts = 1;
return color_array;
} }
void GlowDrawQt::set_background(GlowWind* wind, glow_eDrawType drawtype, void GlowDrawQt::set_background(GlowWind* wind, glow_eDrawType drawtype,
...@@ -1827,7 +1802,7 @@ void GlowDrawQt::reset_background(GlowWind* wind) ...@@ -1827,7 +1802,7 @@ void GlowDrawQt::reset_background(GlowWind* wind)
m_wind->window->setPalette(pal); m_wind->window->setPalette(pal);
} }
void GlowDrawQt::set_clip(DrawWind* wind, QPainter* painter) void GlowDrawQt::set_clip(DrawWind* wind, unique_ptr<QPainter>& painter)
{ {
DrawWindQt* w = (DrawWindQt*)wind; DrawWindQt* w = (DrawWindQt*)wind;
painter->setClipRect(w->clip_rectangle[w->clip_cnt - 1]); painter->setClipRect(w->clip_rectangle[w->clip_cnt - 1]);
...@@ -1835,7 +1810,7 @@ void GlowDrawQt::set_clip(DrawWind* wind, QPainter* painter) ...@@ -1835,7 +1810,7 @@ void GlowDrawQt::set_clip(DrawWind* wind, QPainter* painter)
} }
void GlowDrawQt::set_image_clip_mask( void GlowDrawQt::set_image_clip_mask(
QPainter* painter, glow_tPixmap pixmap, int x, int y) unique_ptr<QPainter>& painter, glow_tPixmap pixmap, int x, int y)
{ {
QRegion clipRegion QRegion clipRegion
= QRegion(((QPixmap*)pixmap)->createMaskFromColor(Qt::black)); = QRegion(((QPixmap*)pixmap)->createMaskFromColor(Qt::black));
...@@ -1912,7 +1887,7 @@ int GlowDrawQt::draw_point( ...@@ -1912,7 +1887,7 @@ int GlowDrawQt::draw_point(
return 1; return 1;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, 0);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1920,8 +1895,6 @@ int GlowDrawQt::draw_point( ...@@ -1920,8 +1895,6 @@ int GlowDrawQt::draw_point(
painter->drawPoint(x1, y1); painter->drawPoint(x1, y1);
delete painter;
return 1; return 1;
} }
...@@ -1933,8 +1906,8 @@ int GlowDrawQt::draw_points(GlowWind* wind, glow_sPointX* points, int point_num, ...@@ -1933,8 +1906,8 @@ int GlowDrawQt::draw_points(GlowWind* wind, glow_sPointX* points, int point_num,
} }
DrawWindQt* w = (DrawWindQt*)wind->window; DrawWindQt* w = (DrawWindQt*)wind->window;
QPoint* qpoints = points_to_qt_points(points, point_num); unique_ptr<QPoint[]> qpoints = points_to_qt_points(points, point_num);
QPainter* painter = get_painter(w->buffer, this, painter_type, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, 0);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -1946,12 +1919,9 @@ int GlowDrawQt::draw_points(GlowWind* wind, glow_sPointX* points, int point_num, ...@@ -1946,12 +1919,9 @@ int GlowDrawQt::draw_points(GlowWind* wind, glow_sPointX* points, int point_num,
painter->brush()); painter->brush());
} }
} else { } else {
painter->drawPoints(qpoints, point_num); painter->drawPoints(qpoints.get(), point_num);
} }
delete painter;
delete[] qpoints;
return 1; return 1;
} }
...@@ -2009,7 +1979,7 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx) ...@@ -2009,7 +1979,7 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx)
} }
if (w->background_pixmap) { if (w->background_pixmap) {
QPainter* painterBg unique_ptr<QPainter> painterBg
= get_painter(w->background_pixmap, this, glow_eDrawType_Line, 0); = get_painter(w->background_pixmap, this, glow_eDrawType_Line, 0);
if (w->clip_on) { if (w->clip_on) {
...@@ -2029,8 +1999,6 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx) ...@@ -2029,8 +1999,6 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx)
} }
} }
} }
delete painterBg;
} else { } else {
glow_eDrawType bg; glow_eDrawType bg;
if (cctx) { if (cctx) {
...@@ -2039,7 +2007,7 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx) ...@@ -2039,7 +2007,7 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx)
bg = ((GrowCtx*)ctx)->background_color; bg = ((GrowCtx*)ctx)->background_color;
} }
QPainter* painter = get_painter(w->buffer, this, bg, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, bg, 0);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -2047,8 +2015,6 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx) ...@@ -2047,8 +2015,6 @@ void GlowDrawQt::buffer_background(DrawWind* wind, GlowCtx* cctx)
painter->fillRect(subwindow_x, subwindow_y, window_width, window_height, painter->fillRect(subwindow_x, subwindow_y, window_width, window_height,
painter->brush()); painter->brush());
delete painter;
} }
} }
...@@ -2218,9 +2184,9 @@ int GlowDrawQt::print(char* filename, double x0, double x1, int end) ...@@ -2218,9 +2184,9 @@ int GlowDrawQt::print(char* filename, double x0, double x1, int end)
return 1; return 1;
} }
QPoint* GlowDrawQt::points_to_qt_points(glow_sPointX* points, int point_cnt) unique_ptr<QPoint[]> GlowDrawQt::points_to_qt_points(glow_sPointX* points, int point_cnt)
{ {
QPoint* qpoints = new QPoint[point_cnt]; unique_ptr<QPoint[]> qpoints(new QPoint[point_cnt]);
for (int i = 0; i < point_cnt; i++) { for (int i = 0; i < point_cnt; i++) {
qpoints[i].setX(points[i].x); qpoints[i].setX(points[i].x);
qpoints[i].setY(points[i].y); qpoints[i].setY(points[i].y);
...@@ -2231,10 +2197,10 @@ QPoint* GlowDrawQt::points_to_qt_points(glow_sPointX* points, int point_cnt) ...@@ -2231,10 +2197,10 @@ QPoint* GlowDrawQt::points_to_qt_points(glow_sPointX* points, int point_cnt)
// //
// Points outside window in x direction excluded // Points outside window in x direction excluded
// //
QPoint* GlowDrawQt::points_to_qt_points_curve( unique_ptr<QPoint[]> GlowDrawQt::points_to_qt_points_curve(
GlowWind* w, glow_sPointX* points, int point_cnt, int* cnt) GlowWind* w, glow_sPointX* points, int point_cnt, int* cnt)
{ {
QPoint* qpoints = new QPoint[point_cnt]; unique_ptr<QPoint[]> qpoints(new QPoint[point_cnt]);
int idx = 0; int idx = 0;
int last_idx = 0; int last_idx = 0;
for (int i = 0; i < point_cnt; i++) { for (int i = 0; i < point_cnt; i++) {
...@@ -2855,7 +2821,7 @@ int GlowDrawQt::gradient_fill_rect(GlowWind* wind, int x, int y, int w, int h, ...@@ -2855,7 +2821,7 @@ int GlowDrawQt::gradient_fill_rect(GlowWind* wind, int x, int y, int w, int h,
return 1; return 1;
} }
QPainter* painter = new QPainter(ww->buffer); unique_ptr<QPainter> painter = new QPainter(ww->buffer);
if (ww->clip_on) { if (ww->clip_on) {
set_clip(ww, painter); set_clip(ww, painter);
...@@ -2869,7 +2835,6 @@ int GlowDrawQt::gradient_fill_rect(GlowWind* wind, int x, int y, int w, int h, ...@@ -2869,7 +2835,6 @@ int GlowDrawQt::gradient_fill_rect(GlowWind* wind, int x, int y, int w, int h,
painter->fillRect(QRect(x, y, w, h), *pat); painter->fillRect(QRect(x, y, w, h), *pat);
delete pat; delete pat;
delete painter;
return 1; return 1;
} }
...@@ -2883,7 +2848,7 @@ int GlowDrawQt::gradient_fill_rectrounded(GlowWind* wind, int x, int y, int w, ...@@ -2883,7 +2848,7 @@ int GlowDrawQt::gradient_fill_rectrounded(GlowWind* wind, int x, int y, int w,
return 1; return 1;
} }
QPainter* painter = new QPainter(ww->buffer); unique_ptr<QPainter> painter = new QPainter(ww->buffer);
if (ww->clip_on) { if (ww->clip_on) {
set_clip(ww, painter); set_clip(ww, painter);
...@@ -2905,8 +2870,6 @@ int GlowDrawQt::gradient_fill_rectrounded(GlowWind* wind, int x, int y, int w, ...@@ -2905,8 +2870,6 @@ int GlowDrawQt::gradient_fill_rectrounded(GlowWind* wind, int x, int y, int w,
delete pat; delete pat;
delete painter;
return 1; return 1;
} }
...@@ -2925,7 +2888,7 @@ int GlowDrawQt::gradient_fill_arc(GlowWind* wind, int x, int y, int w, int h, ...@@ -2925,7 +2888,7 @@ int GlowDrawQt::gradient_fill_arc(GlowWind* wind, int x, int y, int w, int h,
angle1 = angle1 + (-angle1 / 360 + 1) * 360; angle1 = angle1 + (-angle1 / 360 + 1) * 360;
} }
QPainter* painter = new QPainter(ww->buffer); unique_ptr<QPainter> painter = new QPainter(ww->buffer);
if (ww->clip_on) { if (ww->clip_on) {
set_clip(ww, painter); set_clip(ww, painter);
...@@ -2954,8 +2917,6 @@ int GlowDrawQt::gradient_fill_arc(GlowWind* wind, int x, int y, int w, int h, ...@@ -2954,8 +2917,6 @@ int GlowDrawQt::gradient_fill_arc(GlowWind* wind, int x, int y, int w, int h,
delete pat; delete pat;
delete painter;
return 1; return 1;
} }
...@@ -2968,7 +2929,7 @@ int GlowDrawQt::gradient_fill_polyline(GlowWind* wind, glow_sPointX* points, ...@@ -2968,7 +2929,7 @@ int GlowDrawQt::gradient_fill_polyline(GlowWind* wind, glow_sPointX* points,
return 1; return 1;
} }
QPainter* painter = new QPainter(ww->buffer); unique_ptr<QPainter> painter = new QPainter(ww->buffer);
QPainterPath path; QPainterPath path;
double x0, y0, x1, y1; double x0, y0, x1, y1;
...@@ -3008,8 +2969,6 @@ int GlowDrawQt::gradient_fill_polyline(GlowWind* wind, glow_sPointX* points, ...@@ -3008,8 +2969,6 @@ int GlowDrawQt::gradient_fill_polyline(GlowWind* wind, glow_sPointX* points,
delete pat; delete pat;
delete painter;
return 1; return 1;
} }
...@@ -3026,7 +2985,7 @@ int GlowDrawQt::text_qt(GlowWind* wind, int x, int y, char* text, int len, ...@@ -3026,7 +2985,7 @@ int GlowDrawQt::text_qt(GlowWind* wind, int x, int y, char* text, int len,
font_idx = glow_eFont_Helvetica; font_idx = glow_eFont_Helvetica;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, idx); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, idx);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -3073,8 +3032,6 @@ int GlowDrawQt::text_qt(GlowWind* wind, int x, int y, char* text, int len, ...@@ -3073,8 +3032,6 @@ int GlowDrawQt::text_qt(GlowWind* wind, int x, int y, char* text, int len,
// debug_print("textQt %s on %d,%d with color %s, painter_type=%d, color=%d\n", text, px, py, qPrintable(painter->pen().color().name()), painter_type, color); // debug_print("textQt %s on %d,%d with color %s, painter_type=%d, color=%d\n", text, px, py, qPrintable(painter->pen().color().name()), painter_type, color);
painter->drawText(px, py, rect.width(), rect.height(), Qt::TextDontClip, str); painter->drawText(px, py, rect.width(), rect.height(), Qt::TextDontClip, str);
delete painter;
return 1; return 1;
} }
...@@ -3101,7 +3058,7 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len, ...@@ -3101,7 +3058,7 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len,
int width, height; int width, height;
{ {
QPainter* painter = get_painter(w->buffer, this, painter_type, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, 0);
QString str; QString str;
if (((GrowCtx*)ctx)->text_coding != glow_eTextCoding_UTF_8) { if (((GrowCtx*)ctx)->text_coding != glow_eTextCoding_UTF_8) {
...@@ -3116,8 +3073,6 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len, ...@@ -3116,8 +3073,6 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len,
width = rect.width(); width = rect.width();
height = rect.height(); height = rect.height();
height = (int)(height * 0.9); height = (int)(height * 0.9);
delete painter;
} }
if (rot == 90) { if (rot == 90) {
...@@ -3137,7 +3092,7 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len, ...@@ -3137,7 +3092,7 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len,
ph = height; ph = height;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, idx); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, idx);
if (w->clip_on) { if (w->clip_on) {
set_clip(w, painter); set_clip(w, painter);
...@@ -3146,8 +3101,6 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len, ...@@ -3146,8 +3101,6 @@ int GlowDrawQt::text_erase_qt(GlowWind* wind, int x, int y, char* text, int len,
// debug_print("textEraseQt %s on %d,%d\n", text, px, py); // debug_print("textEraseQt %s on %d,%d\n", text, px, py);
painter->fillRect(px, py, pw, ph, painter->brush()); painter->fillRect(px, py, pw, ph, painter->brush());
delete painter;
return 1; return 1;
} }
...@@ -3167,7 +3120,7 @@ int GlowDrawQt::get_text_extent_qt(const char* text, int len, ...@@ -3167,7 +3120,7 @@ int GlowDrawQt::get_text_extent_qt(const char* text, int len,
painter_type = glow_eDrawType_TextHelveticaEraseBold; painter_type = glow_eDrawType_TextHelveticaEraseBold;
} }
QPainter* painter = get_painter(w->buffer, this, painter_type, 0); unique_ptr<QPainter> painter = get_painter(w->buffer, this, painter_type, 0);
painter->setFont(get_font(font_idx, painter_type, size)); painter->setFont(get_font(font_idx, painter_type, size));
QString str; QString str;
...@@ -3191,8 +3144,6 @@ int GlowDrawQt::get_text_extent_qt(const char* text, int len, ...@@ -3191,8 +3144,6 @@ int GlowDrawQt::get_text_extent_qt(const char* text, int len,
} }
*descent = (int)(FONT_DESCENT * lheight); *descent = (int)(FONT_DESCENT * lheight);
delete painter;
return 1; return 1;
} }
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#ifndef glow_draw_qt_h #ifndef glow_draw_qt_h
#define glow_draw_qt_h #define glow_draw_qt_h
#include <stack> #include "co_smart_ptr.h"
#include "glow_customcolors.h" #include "glow_customcolors.h"
#include "glow_draw.h" #include "glow_draw.h"
...@@ -175,7 +175,7 @@ public: ...@@ -175,7 +175,7 @@ public:
int pixmap_height); int pixmap_height);
virtual void reset_background(GlowWind* w); virtual void reset_background(GlowWind* w);
virtual void set_image_clip_mask( virtual void set_image_clip_mask(
QPainter* painter, glow_tPixmap pixmap, int x, int y); unique_ptr<QPainter>& painter, glow_tPixmap pixmap, int x, int y);
virtual int set_clip_rectangle( virtual int set_clip_rectangle(
GlowWind* w, int ll_x, int ll_y, int ur_x, int ur_y); GlowWind* w, int ll_x, int ll_y, int ur_x, int ur_y);
...@@ -192,13 +192,13 @@ public: ...@@ -192,13 +192,13 @@ public:
virtual void buffer_background(DrawWind* w, GlowCtx* cctx); virtual void buffer_background(DrawWind* w, GlowCtx* cctx);
virtual int print(char* filename, double x0, double x1, int end); virtual int print(char* filename, double x0, double x1, int end);
virtual int export_image(char* filename); virtual int export_image(char* filename);
void set_clip(DrawWind* w, QPainter* painter); void set_clip(DrawWind* w, unique_ptr<QPainter>& painter);
virtual void set_timer(GlowCtx* ctx, int time_ms, virtual void set_timer(GlowCtx* ctx, int time_ms,
void (*callback_func)(GlowCtx* ctx), void** id); void (*callback_func)(GlowCtx* ctx), void** id);
virtual void remove_timer(void* id); virtual void remove_timer(void* id);
int init_nav(QWidget* nav_widget); int init_nav(QWidget* nav_widget);
QPoint* points_to_qt_points(glow_sPointX* points, int point_cnt); unique_ptr<QPoint[]> points_to_qt_points(glow_sPointX* points, int point_cnt);
QPoint* points_to_qt_points_curve( unique_ptr<QPoint[]> points_to_qt_points_curve(
GlowWind* w, glow_sPointX* points, int point_cnt, int* cnt); GlowWind* w, glow_sPointX* points, int point_cnt, int* cnt);
int image_get_width(glow_tImImage image); int image_get_width(glow_tImImage image);
int image_get_height(glow_tImImage image); int image_get_height(glow_tImImage image);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment