QWin32PrintEnginePrivate: Brush up the code

- Use nullptr
- Use member initialization
- Remove C-style casts

Task-number: QTBUG-114604
Change-Id: I6f9519010bfbd7c5afa07d9a8752b40c3b29673e
Reviewed-by: Wladimir Leuschner <wladimir.leuschner@qt.io>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit 9f7d2fc7f96fff0c8adc8c815a8cd9850073bd7b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Friedemann Kleint 2023-11-07 15:57:44 +01:00 committed by Qt Cherry-pick Bot
parent 14c4da28f1
commit 6b8c573422
3 changed files with 56 additions and 68 deletions

View File

@ -20,7 +20,7 @@ QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent)
} }
QPageSetupDialog::QPageSetupDialog(QWidget *parent) QPageSetupDialog::QPageSetupDialog(QWidget *parent)
: QDialog(*(new QPageSetupDialogPrivate(0)), parent) : QDialog(*(new QPageSetupDialogPrivate(nullptr)), parent)
{ {
setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup")); setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
setAttribute(Qt::WA_DontShowOnScreen); setAttribute(Qt::WA_DontShowOnScreen);
@ -41,7 +41,7 @@ int QPageSetupDialog::exec()
psd.lStructSize = sizeof(PAGESETUPDLG); psd.lStructSize = sizeof(PAGESETUPDLG);
// we need a temp DEVMODE struct if we don't have a global DEVMODE // we need a temp DEVMODE struct if we don't have a global DEVMODE
HGLOBAL hDevMode = 0; HGLOBAL hDevMode = nullptr;
int devModeSize = 0; int devModeSize = 0;
if (!engine->globalDevMode()) { if (!engine->globalDevMode()) {
devModeSize = sizeof(DEVMODE) + ep->devMode->dmDriverExtra; devModeSize = sizeof(DEVMODE) + ep->devMode->dmDriverExtra;
@ -63,9 +63,10 @@ int QPageSetupDialog::exec()
parent = parent ? parent->window() : QApplication::activeWindow(); parent = parent ? parent->window() : QApplication::activeWindow();
Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created)); Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
QWindow *parentWindow = parent ? parent->windowHandle() : 0; QWindow *parentWindow = parent ? parent->windowHandle() : nullptr;
psd.hwndOwner = parentWindow ? (HWND)QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow) : 0; psd.hwndOwner = parentWindow
? HWND(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow))
: nullptr;
psd.Flags = PSD_MARGINS; psd.Flags = PSD_MARGINS;
QPageLayout layout = d->printer->pageLayout(); QPageLayout layout = d->printer->pageLayout();
switch (layout.units()) { switch (layout.units()) {
@ -133,7 +134,7 @@ int QPageSetupDialog::exec()
// Make sure memory is allocated // Make sure memory is allocated
if (ep->ownsDevMode && ep->devMode) if (ep->ownsDevMode && ep->devMode)
free(ep->devMode); free(ep->devMode);
ep->devMode = (DEVMODE *) malloc(devModeSize); ep->devMode = reinterpret_cast<DEVMODE *>(malloc(devModeSize));
ep->ownsDevMode = true; ep->ownsDevMode = true;
// Copy // Copy

View File

@ -259,7 +259,7 @@ void QWin32PrintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem
if (!fallBack) { if (!fallBack) {
bool deleteFont = false; bool deleteFont = false;
HFONT hfont = NULL; HFONT hfont = nullptr;
if (ti.fontEngine->type() == QFontEngine::Win) { if (ti.fontEngine->type() == QFontEngine::Win) {
hfont = static_cast<HFONT>(ti.fontEngine->handle()); hfont = static_cast<HFONT>(ti.fontEngine->handle());
} }
@ -433,7 +433,7 @@ void QWin32PrintEngine::updateClipPath(const QPainterPath &clipPath, Qt::ClipOpe
bool doclip = true; bool doclip = true;
if (op == Qt::NoClip) { if (op == Qt::NoClip) {
SelectClipRgn(d->hdc, 0); SelectClipRgn(d->hdc, nullptr);
doclip = false; doclip = false;
} }
@ -722,7 +722,7 @@ void QWin32PrintEnginePrivate::strokePath_dev(const QPainterPath &path, const QC
joinStyle = PS_JOIN_ROUND; joinStyle = PS_JOIN_ROUND;
HPEN pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | capStyle | joinStyle, 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); HGDIOBJ old_pen = SelectObject(hdc, pen);
StrokePath(hdc); StrokePath(hdc);
@ -842,7 +842,8 @@ void QWin32PrintEnginePrivate::initialize()
txop = QTransform::TxNone; txop = QTransform::TxNone;
QString printerName = m_printDevice.id(); QString printerName = m_printDevice.id();
bool ok = OpenPrinter((LPWSTR)printerName.utf16(), (LPHANDLE)&hPrinter, 0); bool ok = OpenPrinter(reinterpret_cast<LPWSTR>(const_cast<ushort *>(printerName.utf16())),
reinterpret_cast<LPHANDLE>(&hPrinter), nullptr);
if (!ok) { if (!ok) {
qErrnoWarning("QWin32PrintEngine::initialize: OpenPrinter failed"); qErrnoWarning("QWin32PrintEngine::initialize: OpenPrinter failed");
return; return;
@ -851,10 +852,10 @@ void QWin32PrintEnginePrivate::initialize()
// Fetch the PRINTER_INFO_2 with DEVMODE data containing the // Fetch the PRINTER_INFO_2 with DEVMODE data containing the
// printer settings. // printer settings.
DWORD infoSize, numBytes; DWORD infoSize, numBytes;
GetPrinter(hPrinter, 2, NULL, 0, &infoSize); GetPrinter(hPrinter, 2, nullptr, 0, &infoSize);
hMem = GlobalAlloc(GHND, infoSize); hMem = GlobalAlloc(GHND, infoSize);
pInfo = (PRINTER_INFO_2*) GlobalLock(hMem); pInfo = reinterpret_cast<PRINTER_INFO_2*>(GlobalLock(hMem));
ok = GetPrinter(hPrinter, 2, (LPBYTE)pInfo, infoSize, &numBytes); ok = GetPrinter(hPrinter, 2, reinterpret_cast<LPBYTE>(pInfo), infoSize, &numBytes);
if (!ok) { if (!ok) {
qErrnoWarning("QWin32PrintEngine::initialize: GetPrinter failed"); qErrnoWarning("QWin32PrintEngine::initialize: GetPrinter failed");
@ -872,23 +873,25 @@ void QWin32PrintEnginePrivate::initialize()
// Attempt to get the DEVMODE a different way. // Attempt to get the DEVMODE a different way.
// Allocate the required buffer // Allocate the required buffer
LONG result = DocumentProperties(NULL, hPrinter, (LPWSTR)printerName.utf16(), auto *lpwPrinterName = reinterpret_cast<LPWSTR>(const_cast<ushort *>(printerName.utf16()));
NULL, NULL, 0); LONG result = DocumentProperties(nullptr, hPrinter, lpwPrinterName,
devMode = (DEVMODE *) malloc(result); nullptr, nullptr, 0);
devMode = reinterpret_cast<DEVMODE *>(malloc(result));
ownsDevMode = true; ownsDevMode = true;
// Get the default DevMode // Get the default DevMode
result = DocumentProperties(NULL, hPrinter, (LPWSTR)printerName.utf16(), result = DocumentProperties(nullptr, hPrinter, lpwPrinterName,
devMode, NULL, DM_OUT_BUFFER); devMode, nullptr, DM_OUT_BUFFER);
if (result != IDOK) { if (result != IDOK) {
qErrnoWarning("QWin32PrintEngine::initialize: Failed to obtain devMode"); qErrnoWarning("QWin32PrintEngine::initialize: Failed to obtain devMode");
free(devMode); free(devMode);
devMode = NULL; devMode = nullptr;
ownsDevMode = false; ownsDevMode = false;
} }
} }
hdc = CreateDC(NULL, (LPCWSTR)printerName.utf16(), 0, devMode); hdc = CreateDC(nullptr, reinterpret_cast<LPCWSTR>(printerName.utf16()),
nullptr, devMode);
if (!hdc) { if (!hdc) {
qErrnoWarning("QWin32PrintEngine::initialize: CreateDC failed"); qErrnoWarning("QWin32PrintEngine::initialize: CreateDC failed");
@ -917,11 +920,11 @@ void QWin32PrintEnginePrivate::initHDC()
{ {
Q_ASSERT(hdc); Q_ASSERT(hdc);
HDC display_dc = GetDC(0); HDC display_dc = GetDC(nullptr);
dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
dpi_display = GetDeviceCaps(display_dc, LOGPIXELSY); dpi_display = GetDeviceCaps(display_dc, LOGPIXELSY);
ReleaseDC(0, display_dc); ReleaseDC(nullptr, display_dc);
if (dpi_display == 0) { if (dpi_display == 0) {
qWarning("QWin32PrintEngine::metric: GetDeviceCaps() failed, " qWarning("QWin32PrintEngine::metric: GetDeviceCaps() failed, "
"might be a driver problem"); "might be a driver problem");
@ -964,11 +967,11 @@ void QWin32PrintEnginePrivate::release()
if (ownsDevMode) if (ownsDevMode)
free(devMode); free(devMode);
hdc = 0; hdc = nullptr;
hPrinter = 0; hPrinter = nullptr;
pInfo = 0; pInfo = nullptr;
hMem = 0; hMem = nullptr;
devMode = 0; devMode = nullptr;
ownsDevMode = false; ownsDevMode = false;
} }
@ -1572,7 +1575,7 @@ void QWin32PrintEngine::setGlobalDevMode(HGLOBAL globalDevNames, HGLOBAL globalD
d->ownsDevMode = false; d->ownsDevMode = false;
} }
d->devMode = dm; d->devMode = dm;
d->hdc = CreateDC(NULL, reinterpret_cast<const wchar_t *>(d->m_printDevice.id().utf16()), 0, dm); d->hdc = CreateDC(nullptr, reinterpret_cast<LPCWSTR>(d->m_printDevice.id().utf16()), nullptr, dm);
d->num_copies = d->devMode->dmCopies; d->num_copies = d->devMode->dmCopies;
d->updatePageLayout(); 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(); const bool has_kerning = ti.f && ti.f->kerning();
HFONT hfont = 0; HFONT hfont = nullptr;
bool deleteFont = false; bool deleteFont = false;
if (ti.fontEngine->type() == QFontEngine::Win) { if (ti.fontEngine->type() == QFontEngine::Win) {

View File

@ -83,25 +83,9 @@ class QWin32PrintEnginePrivate : public QAlphaPaintEnginePrivate
Q_DECLARE_PUBLIC(QWin32PrintEngine) Q_DECLARE_PUBLIC(QWin32PrintEngine)
public: public:
QWin32PrintEnginePrivate() : QWin32PrintEnginePrivate() :
hPrinter(0), printToFile(false), reinit(false),
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),
complex_xform(false), has_pen(false), has_brush(false), has_custom_paper_size(false), complex_xform(false), has_pen(false), has_brush(false), has_custom_paper_size(false),
embed_fonts(true), embed_fonts(true)
txop(0 /* QTransform::TxNone */)
{ {
} }
@ -142,19 +126,19 @@ public:
void debugMetrics() const; void debugMetrics() const;
// Windows GDI printer references. // Windows GDI printer references.
HANDLE hPrinter; HANDLE hPrinter = nullptr;
HGLOBAL globalDevMode; HGLOBAL globalDevMode = nullptr;
DEVMODE *devMode; DEVMODE *devMode = nullptr;
PRINTER_INFO_2 *pInfo; PRINTER_INFO_2 *pInfo = nullptr;
HGLOBAL hMem; HGLOBAL hMem = nullptr;
HDC hdc; HDC hdc = nullptr;
// True if devMode was allocated separately from pInfo. // True if devMode was allocated separately from pInfo.
bool ownsDevMode; bool ownsDevMode = false;
QPrinter::PrinterMode mode; QPrinter::PrinterMode mode = QPrinter::ScreenResolution;
// Print Device // Print Device
QPrintDevice m_printDevice; QPrintDevice m_printDevice;
@ -164,26 +148,26 @@ public:
QString m_creator; QString m_creator;
QString fileName; QString fileName;
QPrinter::PrinterState state; QPrinter::PrinterState state = QPrinter::Idle;
int resolution; int resolution = 0;
// Page Layout // Page Layout
QPageLayout m_pageLayout; QPageLayout m_pageLayout{QPageSize(QPageSize::A4),
QPageLayout::Portrait, QMarginsF{0, 0, 0, 0}};
// Page metrics cache // Page metrics cache
QRect m_paintRectPixels; QRect m_paintRectPixels;
QSize m_paintSizeMM; QSize m_paintSizeMM;
// Windows painting // Windows painting
qreal stretch_x; qreal stretch_x = 1;
qreal stretch_y; qreal stretch_y = 1;
int origin_x; int origin_x = 0;
int origin_y; int origin_y = 0;
int dpi_x; int dpi_x = 96;
int dpi_y; int dpi_y = 96;
int dpi_display; int dpi_display = 96;
int num_copies; int num_copies = 1;
uint printToFile : 1; uint printToFile : 1;
uint reinit : 1; uint reinit : 1;
@ -194,7 +178,7 @@ public:
uint has_custom_paper_size : 1; uint has_custom_paper_size : 1;
uint embed_fonts : 1; uint embed_fonts : 1;
uint txop; uint txop = 0; // QTransform::TxNone
QColor brush_color; QColor brush_color;
QPen pen; QPen pen;