From 9f7d2fc7f96fff0c8adc8c815a8cd9850073bd7b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 7 Nov 2023 15:57:44 +0100 Subject: [PATCH] QWin32PrintEnginePrivate: Brush up the code - Use nullptr - Use member initialization - Remove C-style casts Pick-to: 6.6 Task-number: QTBUG-114604 Change-Id: I6f9519010bfbd7c5afa07d9a8752b40c3b29673e Reviewed-by: Wladimir Leuschner Reviewed-by: Oliver Wolff --- .../dialogs/qpagesetupdialog_win.cpp | 13 ++-- .../platform/windows/qprintengine_win.cpp | 49 ++++++++------- .../platform/windows/qprintengine_win_p.h | 62 +++++++------------ 3 files changed, 56 insertions(+), 68 deletions(-) diff --git a/src/printsupport/dialogs/qpagesetupdialog_win.cpp b/src/printsupport/dialogs/qpagesetupdialog_win.cpp index d7004ec3f1..8e735369b2 100644 --- a/src/printsupport/dialogs/qpagesetupdialog_win.cpp +++ b/src/printsupport/dialogs/qpagesetupdialog_win.cpp @@ -20,7 +20,7 @@ QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent) } QPageSetupDialog::QPageSetupDialog(QWidget *parent) - : QDialog(*(new QPageSetupDialogPrivate(0)), parent) + : QDialog(*(new QPageSetupDialogPrivate(nullptr)), parent) { setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup")); setAttribute(Qt::WA_DontShowOnScreen); @@ -41,7 +41,7 @@ int QPageSetupDialog::exec() psd.lStructSize = sizeof(PAGESETUPDLG); // we need a temp DEVMODE struct if we don't have a global DEVMODE - HGLOBAL hDevMode = 0; + HGLOBAL hDevMode = nullptr; int devModeSize = 0; if (!engine->globalDevMode()) { devModeSize = sizeof(DEVMODE) + ep->devMode->dmDriverExtra; @@ -63,9 +63,10 @@ int QPageSetupDialog::exec() parent = parent ? parent->window() : QApplication::activeWindow(); Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created)); - QWindow *parentWindow = parent ? parent->windowHandle() : 0; - psd.hwndOwner = parentWindow ? (HWND)QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow) : 0; - + QWindow *parentWindow = parent ? parent->windowHandle() : nullptr; + psd.hwndOwner = parentWindow + ? HWND(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow)) + : nullptr; psd.Flags = PSD_MARGINS; QPageLayout layout = d->printer->pageLayout(); switch (layout.units()) { @@ -133,7 +134,7 @@ int QPageSetupDialog::exec() // Make sure memory is allocated if (ep->ownsDevMode && ep->devMode) free(ep->devMode); - ep->devMode = (DEVMODE *) malloc(devModeSize); + ep->devMode = reinterpret_cast(malloc(devModeSize)); ep->ownsDevMode = true; // Copy diff --git a/src/printsupport/platform/windows/qprintengine_win.cpp b/src/printsupport/platform/windows/qprintengine_win.cpp index 681f58903e..753b60153e 100644 --- a/src/printsupport/platform/windows/qprintengine_win.cpp +++ b/src/printsupport/platform/windows/qprintengine_win.cpp @@ -259,7 +259,7 @@ void QWin32PrintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem if (!fallBack) { bool deleteFont = false; - HFONT hfont = NULL; + HFONT hfont = nullptr; if (ti.fontEngine->type() == QFontEngine::Win) { hfont = static_cast(ti.fontEngine->handle()); } @@ -433,7 +433,7 @@ void QWin32PrintEngine::updateClipPath(const QPainterPath &clipPath, Qt::ClipOpe bool doclip = true; if (op == Qt::NoClip) { - SelectClipRgn(d->hdc, 0); + SelectClipRgn(d->hdc, nullptr); doclip = false; } @@ -722,7 +722,7 @@ void QWin32PrintEnginePrivate::strokePath_dev(const QPainterPath &path, const QC joinStyle = PS_JOIN_ROUND; HPEN pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | capStyle | joinStyle, - (penWidth == 0) ? 1 : penWidth, &brush, 0, 0); + (penWidth == 0) ? 1 : penWidth, &brush, 0, nullptr); HGDIOBJ old_pen = SelectObject(hdc, pen); StrokePath(hdc); @@ -842,7 +842,8 @@ void QWin32PrintEnginePrivate::initialize() txop = QTransform::TxNone; QString printerName = m_printDevice.id(); - bool ok = OpenPrinter((LPWSTR)printerName.utf16(), (LPHANDLE)&hPrinter, 0); + bool ok = OpenPrinter(reinterpret_cast(const_cast(printerName.utf16())), + reinterpret_cast(&hPrinter), nullptr); if (!ok) { qErrnoWarning("QWin32PrintEngine::initialize: OpenPrinter failed"); return; @@ -851,10 +852,10 @@ void QWin32PrintEnginePrivate::initialize() // Fetch the PRINTER_INFO_2 with DEVMODE data containing the // printer settings. DWORD infoSize, numBytes; - GetPrinter(hPrinter, 2, NULL, 0, &infoSize); + GetPrinter(hPrinter, 2, nullptr, 0, &infoSize); hMem = GlobalAlloc(GHND, infoSize); - pInfo = (PRINTER_INFO_2*) GlobalLock(hMem); - ok = GetPrinter(hPrinter, 2, (LPBYTE)pInfo, infoSize, &numBytes); + pInfo = reinterpret_cast(GlobalLock(hMem)); + ok = GetPrinter(hPrinter, 2, reinterpret_cast(pInfo), infoSize, &numBytes); if (!ok) { qErrnoWarning("QWin32PrintEngine::initialize: GetPrinter failed"); @@ -872,23 +873,25 @@ void QWin32PrintEnginePrivate::initialize() // Attempt to get the DEVMODE a different way. // Allocate the required buffer - LONG result = DocumentProperties(NULL, hPrinter, (LPWSTR)printerName.utf16(), - NULL, NULL, 0); - devMode = (DEVMODE *) malloc(result); + auto *lpwPrinterName = reinterpret_cast(const_cast(printerName.utf16())); + LONG result = DocumentProperties(nullptr, hPrinter, lpwPrinterName, + nullptr, nullptr, 0); + devMode = reinterpret_cast(malloc(result)); ownsDevMode = true; // Get the default DevMode - result = DocumentProperties(NULL, hPrinter, (LPWSTR)printerName.utf16(), - devMode, NULL, DM_OUT_BUFFER); + result = DocumentProperties(nullptr, hPrinter, lpwPrinterName, + devMode, nullptr, DM_OUT_BUFFER); if (result != IDOK) { qErrnoWarning("QWin32PrintEngine::initialize: Failed to obtain devMode"); free(devMode); - devMode = NULL; + devMode = nullptr; ownsDevMode = false; } } - hdc = CreateDC(NULL, (LPCWSTR)printerName.utf16(), 0, devMode); + hdc = CreateDC(nullptr, reinterpret_cast(printerName.utf16()), + nullptr, devMode); if (!hdc) { qErrnoWarning("QWin32PrintEngine::initialize: CreateDC failed"); @@ -917,11 +920,11 @@ void QWin32PrintEnginePrivate::initHDC() { Q_ASSERT(hdc); - HDC display_dc = GetDC(0); + HDC display_dc = GetDC(nullptr); dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); dpi_display = GetDeviceCaps(display_dc, LOGPIXELSY); - ReleaseDC(0, display_dc); + ReleaseDC(nullptr, display_dc); if (dpi_display == 0) { qWarning("QWin32PrintEngine::metric: GetDeviceCaps() failed, " "might be a driver problem"); @@ -964,11 +967,11 @@ void QWin32PrintEnginePrivate::release() if (ownsDevMode) free(devMode); - hdc = 0; - hPrinter = 0; - pInfo = 0; - hMem = 0; - devMode = 0; + hdc = nullptr; + hPrinter = nullptr; + pInfo = nullptr; + hMem = nullptr; + devMode = nullptr; ownsDevMode = false; } @@ -1572,7 +1575,7 @@ void QWin32PrintEngine::setGlobalDevMode(HGLOBAL globalDevNames, HGLOBAL globalD d->ownsDevMode = false; } d->devMode = dm; - d->hdc = CreateDC(NULL, reinterpret_cast(d->m_printDevice.id().utf16()), 0, dm); + d->hdc = CreateDC(nullptr, reinterpret_cast(d->m_printDevice.id().utf16()), nullptr, dm); d->num_copies = d->devMode->dmCopies; d->updatePageLayout(); @@ -1715,7 +1718,7 @@ static void draw_text_item_win(const QPointF &pos, const QTextItemInt &ti, HDC h const bool has_kerning = ti.f && ti.f->kerning(); - HFONT hfont = 0; + HFONT hfont = nullptr; bool deleteFont = false; if (ti.fontEngine->type() == QFontEngine::Win) { diff --git a/src/printsupport/platform/windows/qprintengine_win_p.h b/src/printsupport/platform/windows/qprintengine_win_p.h index 54e3cf2814..f9b5e77c28 100644 --- a/src/printsupport/platform/windows/qprintengine_win_p.h +++ b/src/printsupport/platform/windows/qprintengine_win_p.h @@ -83,25 +83,9 @@ class QWin32PrintEnginePrivate : public QAlphaPaintEnginePrivate Q_DECLARE_PUBLIC(QWin32PrintEngine) public: QWin32PrintEnginePrivate() : - hPrinter(0), - globalDevMode(0), - devMode(0), - pInfo(0), - hMem(0), - hdc(0), - ownsDevMode(false), - mode(QPrinter::ScreenResolution), - state(QPrinter::Idle), - resolution(0), - m_pageLayout(QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF(0, 0, 0, 0))), - stretch_x(1), stretch_y(1), origin_x(0), origin_y(0), - dpi_x(96), dpi_y(96), dpi_display(96), - num_copies(1), - printToFile(false), - reinit(false), + printToFile(false), reinit(false), complex_xform(false), has_pen(false), has_brush(false), has_custom_paper_size(false), - embed_fonts(true), - txop(0 /* QTransform::TxNone */) + embed_fonts(true) { } @@ -142,19 +126,19 @@ public: void debugMetrics() const; // Windows GDI printer references. - HANDLE hPrinter; + HANDLE hPrinter = nullptr; - HGLOBAL globalDevMode; - DEVMODE *devMode; - PRINTER_INFO_2 *pInfo; - HGLOBAL hMem; + HGLOBAL globalDevMode = nullptr; + DEVMODE *devMode = nullptr; + PRINTER_INFO_2 *pInfo = nullptr; + HGLOBAL hMem = nullptr; - HDC hdc; + HDC hdc = nullptr; // True if devMode was allocated separately from pInfo. - bool ownsDevMode; + bool ownsDevMode = false; - QPrinter::PrinterMode mode; + QPrinter::PrinterMode mode = QPrinter::ScreenResolution; // Print Device QPrintDevice m_printDevice; @@ -164,26 +148,26 @@ public: QString m_creator; QString fileName; - QPrinter::PrinterState state; - int resolution; + QPrinter::PrinterState state = QPrinter::Idle; + int resolution = 0; // Page Layout - QPageLayout m_pageLayout; - + QPageLayout m_pageLayout{QPageSize(QPageSize::A4), + QPageLayout::Portrait, QMarginsF{0, 0, 0, 0}}; // Page metrics cache QRect m_paintRectPixels; QSize m_paintSizeMM; // Windows painting - qreal stretch_x; - qreal stretch_y; - int origin_x; - int origin_y; + qreal stretch_x = 1; + qreal stretch_y = 1; + int origin_x = 0; + int origin_y = 0; - int dpi_x; - int dpi_y; - int dpi_display; - int num_copies; + int dpi_x = 96; + int dpi_y = 96; + int dpi_display = 96; + int num_copies = 1; uint printToFile : 1; uint reinit : 1; @@ -194,7 +178,7 @@ public: uint has_custom_paper_size : 1; uint embed_fonts : 1; - uint txop; + uint txop = 0; // QTransform::TxNone QColor brush_color; QPen pen;