Qt,Qt5: implemented DCGetPixel() for widgets. patch by Mark Malakanov, changed by me. issue #29276

git-svn-id: trunk@62840 -
This commit is contained in:
zeljko 2020-04-01 08:36:37 +00:00
parent 1a5015dca6
commit b9799e15cb
4 changed files with 75 additions and 4 deletions

View File

@ -114,6 +114,8 @@ type
procedure SetDefaultAppFontName;
protected
FPenForSetPixel: QPenH;
FInGetPixel: boolean;
FStockNullBrush: HBRUSH;
FStockBlackBrush: HBRUSH;
FStockLtGrayBrush: HBRUSH;

View File

@ -88,6 +88,8 @@ begin
FAppEvenFilterHook := nil;
FAppFocusChangedHook := nil;
FPenForSetPixel := nil;
FInGetPixel := False;
QtGDIObjects := TQtGDIObjects.Create;
InitStockItems;
QtWidgetSet := Self;
@ -1322,16 +1324,45 @@ end;
function TQtWidgetSet.DCGetPixel(CanvasHandle: HDC; X, Y: integer): TGraphicsColor;
var
Color: QColorH;
Pixmap: QPixmapH;
Image: QImageH;
Widget: QWidgetH;
Rgb: QRgb;
begin
Result := clNone;
if not IsValidDC(CanvasHandle) then Exit;
if (TQtDeviceContext(CanvasHandle).vImage <> nil) then
if (TQtDeviceContext(CanvasHandle).vImage <> nil) and
(TQtDeviceContext(CanvasHandle).vImage.Handle <> nil) then
begin
Color := QColor_create(QImage_pixel(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y));
Result := RGBToColor(QColor_red(Color), QColor_green(Color), QColor_blue(Color));
QColor_destroy(Color);
end else
if (TQtDeviceContext(CanvasHandle).Widget <> nil) and (TQtDeviceContext(CanvasHandle).Parent <> nil) then
begin
Widget := TQtDeviceContext(CanvasHandle).Parent;
if FInGetPixel then
raise Exception.CreateFmt('TQtWidgetSet.DCGetPixel called recursively, probably from Paint method of QWidget %u.',[PtrUInt(Widget)]);
FInGetPixel := true;
try
Pixmap:= QPixmap_create(1,1);
Image := QImage_create(1,1, QImageFormat_ARGB32);
QPixmap_grabWindow(Pixmap, QWidget_winId(Widget), X, Y, 1, 1);
QPixmap_toImage(PixMap, Image);
Rgb := QImage_Pixel(Image, 0,0);
Color := QColor_create(Rgb);
Result := RGBToColor(QColor_red(Color), QColor_green(Color), QColor_blue(Color));
finally
QPixmap_destroy(Pixmap);
QImage_destroy(Image);
if Color <> nil then
QColor_destroy(Color);
FInGetPixel := false;
end;
end;
end;
@ -1362,7 +1393,8 @@ begin
ColorRef := TColorRef(ColorToRGB(AColor));
QColor_fromRgb(@Color, Red(ColorRef), Green(ColorRef), Blue(ColorRef));
AQColor := QColor_create(PQColor(@Color));
QImage_setPixel(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y, QColor_rgb(AQColor));
if QImage_valid(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y) then
QImage_setPixel(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y, QColor_rgb(AQColor));
QColor_destroy(AQColor);
end else
begin

View File

@ -111,6 +111,8 @@ type
procedure SetDefaultAppFontName;
protected
FPenForSetPixel: QPenH;
FInGetPixel: boolean;
FStockNullBrush: HBRUSH;
FStockBlackBrush: HBRUSH;
FStockLtGrayBrush: HBRUSH;

View File

@ -40,6 +40,8 @@ begin
FAppEvenFilterHook := nil;
FAppFocusChangedHook := nil;
FPenForSetPixel := nil;
FInGetPixel := False;
QtGDIObjects := TQtGDIObjects.Create;
InitStockItems;
QtWidgetSet := Self;
@ -1253,16 +1255,48 @@ end;
function TQtWidgetSet.DCGetPixel(CanvasHandle: HDC; X, Y: integer): TGraphicsColor;
var
Color: QColorH;
Pixmap: QPixmapH;
Image: QImageH;
Widget: QWidgetH;
Rgb: QRgb;
AWindow: QWindowH;
begin
Result := clNone;
if not IsValidDC(CanvasHandle) then Exit;
if (TQtDeviceContext(CanvasHandle).vImage <> nil) then
if (TQtDeviceContext(CanvasHandle).vImage <> nil) and
(TQtDeviceContext(CanvasHandle).vImage.Handle <> nil) then
begin
Color := QColor_create(QImage_pixel(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y));
Result := RGBToColor(QColor_red(Color), QColor_green(Color), QColor_blue(Color));
QColor_destroy(Color);
end else
if (TQtDeviceContext(CanvasHandle).Widget <> nil) and (TQtDeviceContext(CanvasHandle).Parent <> nil) then
begin
Widget := TQtDeviceContext(CanvasHandle).Parent;
if FInGetPixel then
raise Exception.CreateFmt('TQtWidgetSet.DCGetPixel called recursively, probably from Paint method of QWidget %u.',[PtrUInt(Widget)]);
FInGetPixel := true;
try
Pixmap:= QPixmap_create(1,1);
Image := QImage_create(1,1, QImageFormat_ARGB32);
AWindow := QWidget_windowHandle(Widget);
if AWindow <> nil then
QScreen_grabWindow(QWindow_screen(AWindow), Pixmap, QWidget_winId(Widget), X, Y, 1, 1)
else
QScreen_grabWindow(QGuiApplication_primaryScreen(), Pixmap, QWidget_winId(Widget), X, Y, 1, 1);
QPixmap_toImage(PixMap, Image);
Rgb := QImage_Pixel(Image, 0,0);
Color := QColor_create(Rgb);
Result := RGBToColor(QColor_red(Color), QColor_green(Color), QColor_blue(Color));
finally
QPixmap_destroy(Pixmap);
QImage_destroy(Image);
if Color <> nil then
QColor_destroy(Color);
FInGetPixel := false;
end;
end;
end;
@ -1293,7 +1327,8 @@ begin
ColorRef := TColorRef(ColorToRGB(AColor));
QColor_fromRgb(@Color, Red(ColorRef), Green(ColorRef), Blue(ColorRef));
AQColor := QColor_create(PQColor(@Color));
QImage_setPixel(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y, QColor_rgb(AQColor));
if QImage_valid(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y) then
QImage_setPixel(TQtDeviceContext(CanvasHandle).vImage.Handle, X, Y, QColor_rgb(AQColor));
QColor_destroy(AQColor);
end else
begin