From a213011a53f12f101d08a04afc8fdacd2d54a232 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 4 May 2016 11:45:27 +0200 Subject: [PATCH] QtGui: Introduce delegating constructors. Reduce code duplication by chaining constructors. Change-Id: Ida25105e33cc3ef870f416931212e2216e9c6dfb Reviewed-by: Shawn Rutledge Reviewed-by: Lars Knoll --- src/gui/image/qimage.cpp | 3 +- src/gui/image/qimagereader.cpp | 5 +-- src/gui/image/qimagewriter.cpp | 5 +-- src/gui/image/qpixmap.cpp | 12 +----- src/gui/itemmodels/qstandarditemmodel.cpp | 17 ++------ src/gui/kernel/qevent.cpp | 36 +++++----------- src/gui/opengl/qopenglframebufferobject.cpp | 9 +--- src/gui/opengl/qopenglpaintdevice.cpp | 2 +- src/gui/opengl/qopengltexture.cpp | 2 +- src/gui/painting/qbrush.cpp | 48 +++------------------ src/gui/text/qtextdocumentwriter.cpp | 5 +-- src/gui/util/qvalidator.cpp | 15 +++---- 12 files changed, 36 insertions(+), 123 deletions(-) diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 68e2847f02..162a1a20b8 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -767,9 +767,8 @@ QImage::QImage() Q_DECL_NOEXCEPT drawing onto it with QPainter. */ QImage::QImage(int width, int height, Format format) - : QPaintDevice() + : QImage(QSize(width, height), format) { - d = QImageData::create(QSize(width, height), format); } /*! diff --git a/src/gui/image/qimagereader.cpp b/src/gui/image/qimagereader.cpp index dd1f0c8943..0102ff5981 100644 --- a/src/gui/image/qimagereader.cpp +++ b/src/gui/image/qimagereader.cpp @@ -633,12 +633,9 @@ QImageReader::QImageReader(QIODevice *device, const QByteArray &format) \sa setFileName() */ QImageReader::QImageReader(const QString &fileName, const QByteArray &format) - : d(new QImageReaderPrivate(this)) + : QImageReader(new QFile(fileName), format) { - QFile *file = new QFile(fileName); - d->device = file; d->deleteDevice = true; - d->format = format; } /*! diff --git a/src/gui/image/qimagewriter.cpp b/src/gui/image/qimagewriter.cpp index ddf9f2dda9..1a0bb4d6ff 100644 --- a/src/gui/image/qimagewriter.cpp +++ b/src/gui/image/qimagewriter.cpp @@ -326,12 +326,9 @@ QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format) by inspecting the extension of \a fileName. */ QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format) - : d(new QImageWriterPrivate(this)) + : QImageWriter(new QFile(fileName), format) { - QFile *file = new QFile(fileName); - d->device = file; d->deleteDevice = true; - d->format = format; } /*! diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index 09d23c0084..18df8c35a6 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -125,12 +125,8 @@ QPixmap::QPixmap() */ QPixmap::QPixmap(int w, int h) - : QPaintDevice() + : QPixmap(QSize(w, h)) { - if (!qt_pixmap_thread_test()) - doInit(0, 0, QPlatformPixmap::PixmapType); - else - doInit(w, h, QPlatformPixmap::PixmapType); } /*! @@ -144,12 +140,8 @@ QPixmap::QPixmap(int w, int h) */ QPixmap::QPixmap(const QSize &size) - : QPaintDevice() + : QPixmap(size, QPlatformPixmap::PixmapType) { - if (!qt_pixmap_thread_test()) - doInit(0, 0, QPlatformPixmap::PixmapType); - else - doInit(size.width(), size.height(), QPlatformPixmap::PixmapType); } /*! diff --git a/src/gui/itemmodels/qstandarditemmodel.cpp b/src/gui/itemmodels/qstandarditemmodel.cpp index 6f30f0ea25..ff54602a5d 100644 --- a/src/gui/itemmodels/qstandarditemmodel.cpp +++ b/src/gui/itemmodels/qstandarditemmodel.cpp @@ -705,20 +705,16 @@ void QStandardItemModelPrivate::columnsRemoved(QStandardItem *parent, Constructs an item. */ QStandardItem::QStandardItem() - : d_ptr(new QStandardItemPrivate) + : QStandardItem(*new QStandardItemPrivate) { - Q_D(QStandardItem); - d->q_ptr = this; } /*! Constructs an item with the given \a text. */ QStandardItem::QStandardItem(const QString &text) - : d_ptr(new QStandardItemPrivate) + : QStandardItem(*new QStandardItemPrivate) { - Q_D(QStandardItem); - d->q_ptr = this; setText(text); } @@ -726,22 +722,17 @@ QStandardItem::QStandardItem(const QString &text) Constructs an item with the given \a icon and \a text. */ QStandardItem::QStandardItem(const QIcon &icon, const QString &text) - : d_ptr(new QStandardItemPrivate) + : QStandardItem(text) { - Q_D(QStandardItem); - d->q_ptr = this; setIcon(icon); - setText(text); } /*! Constructs an item with \a rows rows and \a columns columns of child items. */ QStandardItem::QStandardItem(int rows, int columns) - : d_ptr(new QStandardItemPrivate) + : QStandardItem(*new QStandardItemPrivate) { - Q_D(QStandardItem); - d->q_ptr = this; setRowCount(rows); setColumnCount(columns); } diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 2d3707c049..305ce5c82e 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -289,7 +289,7 @@ QMouseEvent::QMouseEvent(Type type, const QPointF &localPos, Qt::MouseButton but QMouseEvent::QMouseEvent(Type type, const QPointF &localPos, const QPointF &screenPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers) - : QInputEvent(type, modifiers), l(localPos), w(localPos), s(screenPos), b(button), mouseState(buttons), caps(0) + : QMouseEvent(type, localPos, localPos, screenPos, button, buttons, modifiers) {} /*! @@ -342,7 +342,7 @@ QMouseEvent::QMouseEvent(Type type, const QPointF &localPos, const QPointF &wind QMouseEvent::QMouseEvent(QEvent::Type type, const QPointF &localPos, const QPointF &windowPos, const QPointF &screenPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, Qt::MouseEventSource source) - : QInputEvent(type, modifiers), l(localPos), w(windowPos), s(screenPos), b(button), mouseState(buttons), caps(0) + : QMouseEvent(type, localPos, windowPos, screenPos, button, buttons, modifiers) { QGuiApplicationPrivate::setMouseEventSource(this, source); } @@ -829,9 +829,8 @@ QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, int delta QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, QPoint pixelDelta, QPoint angleDelta, int qt4Delta, Qt::Orientation qt4Orientation, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers) - : QInputEvent(Wheel, modifiers), p(pos), g(globalPos), pixelD(pixelDelta), - angleD(angleDelta), qt4D(qt4Delta), qt4O(qt4Orientation), mouseState(buttons), ph(Qt::NoScrollPhase), - src(Qt::MouseEventNotSynthesized), invertedScrolling(false) + : QWheelEvent(pos, globalPos, pixelDelta, angleDelta, qt4Delta, qt4Orientation, + buttons, modifiers, Qt::NoScrollPhase) {} /*! @@ -860,9 +859,8 @@ QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, QPoint pixelDelta, QPoint angleDelta, int qt4Delta, Qt::Orientation qt4Orientation, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, Qt::ScrollPhase phase) - : QInputEvent(Wheel, modifiers), p(pos), g(globalPos), pixelD(pixelDelta), - angleD(angleDelta), qt4D(qt4Delta), qt4O(qt4Orientation), mouseState(buttons), ph(phase), - src(Qt::MouseEventNotSynthesized), invertedScrolling(false) + : QWheelEvent(pos, globalPos, pixelDelta, angleDelta, qt4Delta, qt4Orientation, + buttons, modifiers, phase, Qt::MouseEventNotSynthesized) {} /*! @@ -896,9 +894,8 @@ QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, QPoint pixelDelta, QPoint angleDelta, int qt4Delta, Qt::Orientation qt4Orientation, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, Qt::ScrollPhase phase, Qt::MouseEventSource source) - : QInputEvent(Wheel, modifiers), p(pos), g(globalPos), pixelD(pixelDelta), - angleD(angleDelta), qt4D(qt4Delta), qt4O(qt4Orientation), mouseState(buttons), ph(phase), src(source), - invertedScrolling(false) + : QWheelEvent(pos, globalPos, pixelDelta, angleDelta, qt4Delta, qt4Orientation, + buttons, modifiers, phase, source, false) {} /*! @@ -1767,7 +1764,7 @@ QIconDragEvent::~QIconDragEvent() coordinates. */ QContextMenuEvent::QContextMenuEvent(Reason reason, const QPoint &pos, const QPoint &globalPos) - : QInputEvent(ContextMenu), p(pos), gp(globalPos), reas(reason) + : QContextMenuEvent(reason, pos, globalPos, Qt::NoModifier) {} /*! @@ -2450,19 +2447,8 @@ QTabletEvent::QTabletEvent(Type type, const QPointF &pos, const QPointF &globalP int device, int pointerType, qreal pressure, int xTilt, int yTilt, qreal tangentialPressure, qreal rotation, int z, Qt::KeyboardModifiers keyState, qint64 uniqueID) - : QInputEvent(type, keyState), - mPos(pos), - mGPos(globalPos), - mDev(device), - mPointerType(pointerType), - mXT(xTilt), - mYT(yTilt), - mZ(z), - mPress(pressure), - mTangential(tangentialPressure), - mRot(rotation), - mUnique(uniqueID), - mExtra(new QTabletEventPrivate(Qt::NoButton, Qt::NoButton)) + : QTabletEvent(type, pos, globalPos, device, pointerType, pressure, xTilt, yTilt, + tangentialPressure, rotation, z, keyState, uniqueID, Qt::NoButton, Qt::NoButton) { } diff --git a/src/gui/opengl/qopenglframebufferobject.cpp b/src/gui/opengl/qopenglframebufferobject.cpp index 597d347915..b932ee60c9 100644 --- a/src/gui/opengl/qopenglframebufferobject.cpp +++ b/src/gui/opengl/qopenglframebufferobject.cpp @@ -856,10 +856,8 @@ QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, GLenum tar \sa size(), texture() */ QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, GLenum target) - : d_ptr(new QOpenGLFramebufferObjectPrivate) + : QOpenGLFramebufferObject(QSize(width, height), target) { - Q_D(QOpenGLFramebufferObject); - d->init(this, QSize(width, height), NoAttachment, target, effectiveInternalFormat(0)); } /*! \overload @@ -883,11 +881,8 @@ QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, const QOpe */ QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, const QOpenGLFramebufferObjectFormat &format) - : d_ptr(new QOpenGLFramebufferObjectPrivate) + : QOpenGLFramebufferObject(QSize(width, height), format) { - Q_D(QOpenGLFramebufferObject); - d->init(this, QSize(width, height), format.attachment(), format.textureTarget(), - format.internalTextureFormat(), format.samples(), format.mipmap()); } /*! \overload diff --git a/src/gui/opengl/qopenglpaintdevice.cpp b/src/gui/opengl/qopenglpaintdevice.cpp index 75f09d6031..e539ee0e31 100644 --- a/src/gui/opengl/qopenglpaintdevice.cpp +++ b/src/gui/opengl/qopenglpaintdevice.cpp @@ -137,7 +137,7 @@ QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size) \sa QOpenGLContext::currentContext() */ QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height) - : d_ptr(new QOpenGLPaintDevicePrivate(QSize(width, height))) + : QOpenGLPaintDevice(QSize(width, height)) { } diff --git a/src/gui/opengl/qopengltexture.cpp b/src/gui/opengl/qopengltexture.cpp index 1db61b7ca1..0a46eea85d 100644 --- a/src/gui/opengl/qopengltexture.cpp +++ b/src/gui/opengl/qopengltexture.cpp @@ -2216,7 +2216,7 @@ QOpenGLTexture::QOpenGLTexture(Target target) OpenGL context. */ QOpenGLTexture::QOpenGLTexture(const QImage& image, MipMapGeneration genMipMaps) - : d_ptr(new QOpenGLTexturePrivate(QOpenGLTexture::Target2D, this)) + : QOpenGLTexture(QOpenGLTexture::Target2D) { setData(image, genMipMaps); } diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp index 3a296ac40c..ee1f01e850 100644 --- a/src/gui/painting/qbrush.cpp +++ b/src/gui/painting/qbrush.cpp @@ -458,13 +458,8 @@ QBrush::QBrush(const QImage &image) */ QBrush::QBrush(Qt::BrushStyle style) + : QBrush(QColor(Qt::black), style) { - if (qbrush_check_type(style)) - init(Qt::black, style); - else { - d.reset(nullBrushInstance()); - d->ref.ref(); - } } /*! @@ -491,13 +486,8 @@ QBrush::QBrush(const QColor &color, Qt::BrushStyle style) \sa setColor(), setStyle() */ QBrush::QBrush(Qt::GlobalColor color, Qt::BrushStyle style) + : QBrush(QColor(color), style) { - if (qbrush_check_type(style)) - init(color, style); - else { - d.reset(nullBrushInstance()); - d->ref.ref(); - } } /*! @@ -1673,13 +1663,8 @@ QLinearGradient::QLinearGradient(const QPointF &start, const QPointF &finalStop) \sa QGradient::setColorAt(), QGradient::setStops() */ QLinearGradient::QLinearGradient(qreal xStart, qreal yStart, qreal xFinalStop, qreal yFinalStop) + : QLinearGradient(QPointF(xStart, yStart), QPointF(xFinalStop, yFinalStop)) { - m_type = LinearGradient; - m_spread = PadSpread; - m_data.linear.x1 = xStart; - m_data.linear.y1 = yStart; - m_data.linear.x2 = xFinalStop; - m_data.linear.y2 = yFinalStop; } @@ -1882,19 +1867,8 @@ QRadialGradient::QRadialGradient(const QPointF ¢er, qreal radius) */ QRadialGradient::QRadialGradient(qreal cx, qreal cy, qreal radius, qreal fx, qreal fy) + : QRadialGradient(QPointF(cx, cy), radius, QPointF(fx, fy)) { - m_type = RadialGradient; - m_spread = PadSpread; - m_data.radial.cx = cx; - m_data.radial.cy = cy; - m_data.radial.cradius = radius; - - QPointF adapted_focal = qt_radial_gradient_adapt_focal_point(QPointF(cx, cy), - radius, - QPointF(fx, fy)); - - m_data.radial.fx = adapted_focal.x(); - m_data.radial.fy = adapted_focal.y(); } /*! @@ -1904,14 +1878,8 @@ QRadialGradient::QRadialGradient(qreal cx, qreal cy, qreal radius, qreal fx, qre \sa QGradient::setColorAt(), QGradient::setStops() */ QRadialGradient::QRadialGradient(qreal cx, qreal cy, qreal radius) + : QRadialGradient(QPointF(cx, cy), radius) { - m_type = RadialGradient; - m_spread = PadSpread; - m_data.radial.cx = cx; - m_data.radial.cy = cy; - m_data.radial.cradius = radius; - m_data.radial.fx = cx; - m_data.radial.fy = cy; } @@ -2211,12 +2179,8 @@ QConicalGradient::QConicalGradient(const QPointF ¢er, qreal angle) */ QConicalGradient::QConicalGradient(qreal cx, qreal cy, qreal angle) + : QConicalGradient(QPointF(cx, cy), angle) { - m_type = ConicalGradient; - m_spread = PadSpread; - m_data.conical.cx = cx; - m_data.conical.cy = cy; - m_data.conical.angle = angle; } diff --git a/src/gui/text/qtextdocumentwriter.cpp b/src/gui/text/qtextdocumentwriter.cpp index a7c58ac2a6..731aaf1fcf 100644 --- a/src/gui/text/qtextdocumentwriter.cpp +++ b/src/gui/text/qtextdocumentwriter.cpp @@ -139,12 +139,9 @@ QTextDocumentWriter::QTextDocumentWriter(QIODevice *device, const QByteArray &fo format by inspecting the extension of \a fileName. */ QTextDocumentWriter::QTextDocumentWriter(const QString &fileName, const QByteArray &format) - : d(new QTextDocumentWriterPrivate(this)) + : QTextDocumentWriter(new QFile(fileName), format) { - QFile *file = new QFile(fileName); - d->device = file; d->deleteDevice = true; - d->format = format; } /*! diff --git a/src/gui/util/qvalidator.cpp b/src/gui/util/qvalidator.cpp index 90c6e7a7b8..fb9dfc9f69 100644 --- a/src/gui/util/qvalidator.cpp +++ b/src/gui/util/qvalidator.cpp @@ -216,7 +216,7 @@ public: */ QValidator::QValidator(QObject * parent) - : QObject(*new QValidatorPrivate, parent) + : QValidator(*new QValidatorPrivate, parent) { } @@ -337,10 +337,8 @@ void QValidator::fixup(QString &) const */ QIntValidator::QIntValidator(QObject * parent) - : QValidator(parent) + : QIntValidator(INT_MIN, INT_MAX, parent) { - b = INT_MIN; - t = INT_MAX; } @@ -589,11 +587,8 @@ public: */ QDoubleValidator::QDoubleValidator(QObject * parent) - : QValidator(*new QDoubleValidatorPrivate , parent) + : QDoubleValidator(-HUGE_VAL, HUGE_VAL, 1000, parent) { - b = -HUGE_VAL; - t = HUGE_VAL; - dec = 1000; } @@ -845,7 +840,7 @@ QDoubleValidator::Notation QDoubleValidator::notation() const */ QRegExpValidator::QRegExpValidator(QObject *parent) - : QValidator(parent), r(QString::fromLatin1(".*")) + : QRegExpValidator(QRegExp(QString::fromLatin1(".*")), parent) { } @@ -982,7 +977,7 @@ QRegularExpressionValidator::QRegularExpressionValidator(QObject *parent) */ QRegularExpressionValidator::QRegularExpressionValidator(const QRegularExpression &re, QObject *parent) - : QValidator(*new QRegularExpressionValidatorPrivate, parent) + : QRegularExpressionValidator(parent) { Q_D(QRegularExpressionValidator); d->setRegularExpression(re);