From 3fede6cb547b783377e833c9b269d4cecfe47e61 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 6 Sep 2019 20:27:33 +0200 Subject: Cleanup QtWidgets (tools) examples Cleanup QtWidgets tools examples: - use member-init (clang-tidy) - fix includes/don't include QtWidgets globally - include own header first - use nullptr (clang-tidy) - avoid c-style casts - use QVector instead QList - use QItemDelegate instead QStyledItemDelegate Change-Id: Ibe9440cdf711e5cc2138c054864edebe1fc95731 Reviewed-by: Paul Wicking --- examples/widgets/tools/undo/commands.cpp | 36 +++++++------------- examples/widgets/tools/undo/commands.h | 14 ++++---- examples/widgets/tools/undo/document.cpp | 30 +++++++---------- examples/widgets/tools/undo/document.h | 15 ++++----- examples/widgets/tools/undo/mainwindow.cpp | 53 +++++++++++++++--------------- examples/widgets/tools/undo/mainwindow.h | 2 +- 6 files changed, 67 insertions(+), 83 deletions(-) (limited to 'examples/widgets/tools/undo') diff --git a/examples/widgets/tools/undo/commands.cpp b/examples/widgets/tools/undo/commands.cpp index 81561d2421..97204f9d2f 100644 --- a/examples/widgets/tools/undo/commands.cpp +++ b/examples/widgets/tools/undo/commands.cpp @@ -50,18 +50,16 @@ #include "commands.h" -static const int setShapeRectCommandId = 1; -static const int setShapeColorCommandId = 2; +static constexpr int setShapeRectCommandId = 1; +static constexpr int setShapeColorCommandId = 2; /****************************************************************************** ** AddShapeCommand */ AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent) - : QUndoCommand(parent) + : QUndoCommand(parent), m_doc(doc), m_shape(shape) { - m_doc = doc; - m_shape = shape; } void AddShapeCommand::undo() @@ -81,13 +79,11 @@ void AddShapeCommand::redo() */ RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName, - QUndoCommand *parent) - : QUndoCommand(parent) + QUndoCommand *parent) + : QUndoCommand(parent), m_doc(doc), m_shape(doc->shape(shapeName)) + , m_shapeName(shapeName) { setText(QObject::tr("Remove %1").arg(shapeName)); - m_doc = doc; - m_shape = doc->shape(shapeName); - m_shapeName = shapeName; } void RemoveShapeCommand::undo() @@ -105,15 +101,11 @@ void RemoveShapeCommand::redo() */ SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName, - const QColor &color, QUndoCommand *parent) - : QUndoCommand(parent) + const QColor &color, QUndoCommand *parent) + : QUndoCommand(parent), m_doc(doc), m_shapeName(shapeName) + , m_oldColor(doc->shape(shapeName).color()), m_newColor(color) { setText(QObject::tr("Set %1's color").arg(shapeName)); - - m_doc = doc; - m_shapeName = shapeName; - m_oldColor = doc->shape(shapeName).color(); - m_newColor = color; } void SetShapeColorCommand::undo() @@ -149,15 +141,11 @@ int SetShapeColorCommand::id() const */ SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName, - const QRect &rect, QUndoCommand *parent) - : QUndoCommand(parent) + const QRect &rect, QUndoCommand *parent) + : QUndoCommand(parent), m_doc(doc), m_shapeName(shapeName) + , m_oldRect(doc->shape(shapeName).rect()), m_newRect(rect) { setText(QObject::tr("Change %1's geometry").arg(shapeName)); - - m_doc = doc; - m_shapeName = shapeName; - m_oldRect = doc->shape(shapeName).rect(); - m_newRect = rect; } void SetShapeRectCommand::undo() diff --git a/examples/widgets/tools/undo/commands.h b/examples/widgets/tools/undo/commands.h index de3bebd740..ccb550fdd4 100644 --- a/examples/widgets/tools/undo/commands.h +++ b/examples/widgets/tools/undo/commands.h @@ -57,7 +57,8 @@ class AddShapeCommand : public QUndoCommand { public: - AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent = 0); + AddShapeCommand(Document *doc, const Shape &shape, + QUndoCommand *parent = nullptr); void undo() override; void redo() override; @@ -70,7 +71,8 @@ private: class RemoveShapeCommand : public QUndoCommand { public: - RemoveShapeCommand(Document *doc, const QString &shapeName, QUndoCommand *parent = 0); + RemoveShapeCommand(Document *doc, const QString &shapeName, + QUndoCommand *parent = nullptr); void undo() override; void redo() override; @@ -83,8 +85,8 @@ private: class SetShapeColorCommand : public QUndoCommand { public: - SetShapeColorCommand(Document *doc, const QString &shapeName, const QColor &color, - QUndoCommand *parent = 0); + SetShapeColorCommand(Document *doc, const QString &shapeName, + const QColor &color, QUndoCommand *parent = nullptr); void undo() override; void redo() override; @@ -102,8 +104,8 @@ private: class SetShapeRectCommand : public QUndoCommand { public: - SetShapeRectCommand(Document *doc, const QString &shapeName, const QRect &rect, - QUndoCommand *parent = 0); + SetShapeRectCommand(Document *doc, const QString &shapeName, + const QRect &rect, QUndoCommand *parent = nullptr); void undo() override; void redo() override; diff --git a/examples/widgets/tools/undo/document.cpp b/examples/widgets/tools/undo/document.cpp index 8935f98a7a..2deed83a99 100644 --- a/examples/widgets/tools/undo/document.cpp +++ b/examples/widgets/tools/undo/document.cpp @@ -48,14 +48,15 @@ ** ****************************************************************************/ -#include +#include "document.h" +#include "commands.h" + #include +#include #include #include -#include "document.h" -#include "commands.h" -static const int resizeHandleWidth = 6; +static constexpr int resizeHandleWidth = 6; /****************************************************************************** ** Shape @@ -96,26 +97,21 @@ QRect Shape::resizeHandle() const QString Shape::typeToString(Type type) { - QString result; - switch (type) { case Rectangle: - result = QLatin1String("Rectangle"); - break; + return QLatin1String("Rectangle"); case Circle: - result = QLatin1String("Circle"); - break; + return QLatin1String("Circle"); case Triangle: - result = QLatin1String("Triangle"); - break; + return QLatin1String("Triangle"); } - return result; + return QString(); } Shape::Type Shape::stringToType(const QString &s, bool *ok) { - if (ok != 0) + if (ok != nullptr) *ok = true; if (s == QLatin1String("Rectangle")) @@ -125,7 +121,7 @@ Shape::Type Shape::stringToType(const QString &s, bool *ok) if (s == QLatin1String("Triangle")) return Triangle; - if (ok != 0) + if (ok != nullptr) *ok = false; return Rectangle; } @@ -135,10 +131,8 @@ Shape::Type Shape::stringToType(const QString &s, bool *ok) */ Document::Document(QWidget *parent) - : QWidget(parent), m_currentIndex(-1), m_mousePressIndex(-1), m_resizeHandlePressed(false) + : QWidget(parent), m_undoStack(new QUndoStack(this)) { - m_undoStack = new QUndoStack(this); - setAutoFillBackground(true); setBackgroundRole(QPalette::Base); diff --git a/examples/widgets/tools/undo/document.h b/examples/widgets/tools/undo/document.h index 8076d7185c..01447ef541 100644 --- a/examples/widgets/tools/undo/document.h +++ b/examples/widgets/tools/undo/document.h @@ -70,7 +70,7 @@ public: QColor color() const; static QString typeToString(Type type); - static Type stringToType(const QString &s, bool *ok = 0); + static Type stringToType(const QString &s, bool *ok = nullptr); static const QSize minSize; @@ -88,7 +88,7 @@ class Document : public QWidget Q_OBJECT public: - Document(QWidget *parent = 0); + Document(QWidget *parent = nullptr); QString addShape(const Shape &shape); void deleteShape(const QString &shapeName); @@ -121,14 +121,13 @@ private: int indexAt(const QPoint &pos) const; QString uniqueName(const QString &name) const; - QList m_shapeList; - int m_currentIndex; - int m_mousePressIndex; + QVector m_shapeList; QPoint m_mousePressOffset; - bool m_resizeHandlePressed; QString m_fileName; - - QUndoStack *m_undoStack; + QUndoStack *m_undoStack = nullptr; + int m_currentIndex = -1; + int m_mousePressIndex = -1; + bool m_resizeHandlePressed = false; }; #endif // DOCUMENT_H diff --git a/examples/widgets/tools/undo/mainwindow.cpp b/examples/widgets/tools/undo/mainwindow.cpp index 118d604742..9d83e3067a 100644 --- a/examples/widgets/tools/undo/mainwindow.cpp +++ b/examples/widgets/tools/undo/mainwindow.cpp @@ -48,6 +48,10 @@ ** ****************************************************************************/ +#include "mainwindow.h" +#include "document.h" +#include "commands.h" + #include #include #include @@ -55,9 +59,6 @@ #include #include #include -#include "document.h" -#include "mainwindow.h" -#include "commands.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -122,17 +123,17 @@ MainWindow::MainWindow(QWidget *parent) void MainWindow::updateActions() { Document *doc = currentDocument(); - m_undoGroup->setActiveStack(doc == 0 ? 0 : doc->undoStack()); - QString shapeName = doc == 0 ? QString() : doc->currentShapeName(); - - actionAddRobot->setEnabled(doc != 0); - actionAddSnowman->setEnabled(doc != 0); - actionAddCircle->setEnabled(doc != 0); - actionAddRectangle->setEnabled(doc != 0); - actionAddTriangle->setEnabled(doc != 0); - actionClose->setEnabled(doc != 0); - actionSave->setEnabled(doc != 0 && !doc->undoStack()->isClean()); - undoLimit->setEnabled(doc != 0 && doc->undoStack()->count() == 0); + m_undoGroup->setActiveStack(doc == nullptr ? nullptr : doc->undoStack()); + QString shapeName = doc == nullptr ? QString() : doc->currentShapeName(); + + actionAddRobot->setEnabled(doc != nullptr); + actionAddSnowman->setEnabled(doc != nullptr); + actionAddCircle->setEnabled(doc != nullptr); + actionAddRectangle->setEnabled(doc != nullptr); + actionAddTriangle->setEnabled(doc != nullptr); + actionClose->setEnabled(doc != nullptr); + actionSave->setEnabled(doc != nullptr && !doc->undoStack()->isClean()); + undoLimit->setEnabled(doc != nullptr && doc->undoStack()->count() == 0); if (shapeName.isEmpty()) { actionRed->setEnabled(false); @@ -147,7 +148,7 @@ void MainWindow::updateActions() actionRemoveShape->setEnabled(true); } - if (doc != 0) { + if (doc != nullptr) { int index = documentTabs->indexOf(doc); Q_ASSERT(index != -1); static const QIcon unsavedIcon(":/icons/filesave.png"); @@ -264,7 +265,7 @@ void MainWindow::removeDocument(Document *doc) void MainWindow::saveDocument() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; for (;;) { @@ -298,7 +299,7 @@ void MainWindow::saveDocument() void MainWindow::closeDocument() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; if (!doc->undoStack()->isClean()) { @@ -338,10 +339,10 @@ static QRect randomRect(const QSize &s) { QSize min = Shape::minSize; - int left = (int) ((0.0 + s.width() - min.width())*(QRandomGenerator::global()->bounded(1.0))); - int top = (int) ((0.0 + s.height() - min.height())*(QRandomGenerator::global()->bounded(1.0))); - int width = (int) ((0.0 + s.width() - left - min.width())*(QRandomGenerator::global()->bounded(1.0))) + min.width(); - int height = (int) ((0.0 + s.height() - top - min.height())*(QRandomGenerator::global()->bounded(1.0))) + min.height(); + int left = qRound((s.width() - min.width()) * (QRandomGenerator::global()->bounded(1.0))); + int top = qRound((s.height() - min.height()) * (QRandomGenerator::global()->bounded(1.0))); + int width = qRound((s.width() - left - min.width()) * (QRandomGenerator::global()->bounded(1.0))) + min.width(); + int height = qRound((s.height() - top - min.height()) * (QRandomGenerator::global()->bounded(1.0))) + min.height(); return QRect(left, top, width, height); } @@ -349,7 +350,7 @@ static QRect randomRect(const QSize &s) void MainWindow::addShape() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; Shape::Type type; @@ -369,7 +370,7 @@ void MainWindow::addShape() void MainWindow::removeShape() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; QString shapeName = doc->currentShapeName(); @@ -382,7 +383,7 @@ void MainWindow::removeShape() void MainWindow::setShapeColor() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; QString shapeName = doc->currentShapeName(); @@ -409,7 +410,7 @@ void MainWindow::setShapeColor() void MainWindow::addSnowman() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; // Create a macro command using beginMacro() and endMacro() @@ -427,7 +428,7 @@ void MainWindow::addSnowman() void MainWindow::addRobot() { Document *doc = currentDocument(); - if (doc == 0) + if (doc == nullptr) return; // Compose a macro command by explicitly adding children to a parent command diff --git a/examples/widgets/tools/undo/mainwindow.h b/examples/widgets/tools/undo/mainwindow.h index 57c1f87ab2..87ee3084fb 100644 --- a/examples/widgets/tools/undo/mainwindow.h +++ b/examples/widgets/tools/undo/mainwindow.h @@ -61,7 +61,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindow Q_OBJECT public: - MainWindow(QWidget *parent = 0); + MainWindow(QWidget *parent = nullptr); void addDocument(Document *doc); void removeDocument(Document *doc); -- cgit v1.2.3