summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools/plugandpaint/app
diff options
context:
space:
mode:
authorAndre Hartmann <aha_1980@gmx.de>2018-05-11 16:31:51 +0200
committerAndré Hartmann <aha_1980@gmx.de>2018-06-06 11:55:44 +0000
commita14a943f9ac3d1e85514d7fb6688c84e624ac850 (patch)
tree8da635836270c52a9289975bbf4eaa545f509398 /examples/widgets/tools/plugandpaint/app
parent62abbe34b84a1c224d27f3eb2f735749f5f24b4f (diff)
Examples: Cleanup and modernize Plug&Paint
* Use C++11 member init * Use auto where appropriate * Replace foreach with range-for * Replace last Qt4-style connects * Sort includes to common style * Fix some Clang warnings Task-number: QTBUG-60635 Change-Id: I61c98b34b954e416dd175b56ca589125217083de Reviewed-by: Venugopal Shivashankar <Venugopal.Shivashankar@qt.io> Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'examples/widgets/tools/plugandpaint/app')
-rw-r--r--examples/widgets/tools/plugandpaint/app/main.cpp3
-rw-r--r--examples/widgets/tools/plugandpaint/app/mainwindow.cpp55
-rw-r--r--examples/widgets/tools/plugandpaint/app/mainwindow.h34
-rw-r--r--examples/widgets/tools/plugandpaint/app/paintarea.cpp9
-rw-r--r--examples/widgets/tools/plugandpaint/app/paintarea.h12
-rw-r--r--examples/widgets/tools/plugandpaint/app/plugindialog.cpp29
-rw-r--r--examples/widgets/tools/plugandpaint/app/plugindialog.h8
7 files changed, 72 insertions, 78 deletions
diff --git a/examples/widgets/tools/plugandpaint/app/main.cpp b/examples/widgets/tools/plugandpaint/app/main.cpp
index f157957da5..f50fd86b97 100644
--- a/examples/widgets/tools/plugandpaint/app/main.cpp
+++ b/examples/widgets/tools/plugandpaint/app/main.cpp
@@ -50,8 +50,9 @@
//! [0]
#include "mainwindow.h"
-#include <QtPlugin>
+
#include <QApplication>
+#include <QtPlugin>
Q_IMPORT_PLUGIN(BasicToolsPlugin)
diff --git a/examples/widgets/tools/plugandpaint/app/mainwindow.cpp b/examples/widgets/tools/plugandpaint/app/mainwindow.cpp
index b2287ee072..ebe1150eea 100644
--- a/examples/widgets/tools/plugandpaint/app/mainwindow.cpp
+++ b/examples/widgets/tools/plugandpaint/app/mainwindow.cpp
@@ -54,19 +54,18 @@
#include "paintarea.h"
#include "plugindialog.h"
-#include <QPluginLoader>
-#include <QTimer>
-
-#include <QScrollArea>
-#include <QMessageBox>
-#include <QActionGroup>
#include <QAction>
-#include <QMenu>
-#include <QMenuBar>
-#include <QFileDialog>
+#include <QActionGroup>
+#include <QApplication>
#include <QColorDialog>
+#include <QFileDialog>
#include <QInputDialog>
-#include <QApplication>
+#include <QMenu>
+#include <QMenuBar>
+#include <QMessageBox>
+#include <QPluginLoader>
+#include <QScrollArea>
+#include <QTimer>
MainWindow::MainWindow() :
paintArea(new PaintArea),
@@ -109,11 +108,10 @@ bool MainWindow::saveAs()
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
initialPath);
- if (fileName.isEmpty()) {
+ if (fileName.isEmpty())
return false;
- } else {
- return paintArea->saveImage(fileName, "png");
- }
+
+ return paintArea->saveImage(fileName, "png");
}
void MainWindow::brushColor()
@@ -137,8 +135,8 @@ void MainWindow::brushWidth()
//! [0]
void MainWindow::changeBrush()
{
- QAction *action = qobject_cast<QAction *>(sender());
- BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent());
+ auto action = qobject_cast<QAction *>(sender());
+ auto iBrush = qobject_cast<BrushInterface *>(action->parent());
const QString brush = action->text();
paintArea->setBrush(iBrush, brush);
@@ -148,8 +146,8 @@ void MainWindow::changeBrush()
//! [1]
void MainWindow::insertShape()
{
- QAction *action = qobject_cast<QAction *>(sender());
- ShapeInterface *iShape = qobject_cast<ShapeInterface *>(action->parent());
+ auto action = qobject_cast<QAction *>(sender());
+ auto iShape = qobject_cast<ShapeInterface *>(action->parent());
const QPainterPath path = iShape->generateShape(action->text(), this);
if (!path.isEmpty())
@@ -160,9 +158,8 @@ void MainWindow::insertShape()
//! [2]
void MainWindow::applyFilter()
{
- QAction *action = qobject_cast<QAction *>(sender());
- FilterInterface *iFilter =
- qobject_cast<FilterInterface *>(action->parent());
+ auto action = qobject_cast<QAction *>(sender());
+ auto iFilter = qobject_cast<FilterInterface *>(action->parent());
const QImage image = iFilter->filterImage(action->text(), paintArea->image(),
this);
@@ -245,7 +242,8 @@ void MainWindow::createMenus()
//! [4]
void MainWindow::loadPlugins()
{
- foreach (QObject *plugin, QPluginLoader::staticInstances())
+ const auto staticInstances = QPluginLoader::staticInstances();
+ for (QObject *plugin : staticInstances)
populateMenus(plugin);
//! [4] //! [5]
@@ -265,7 +263,8 @@ void MainWindow::loadPlugins()
//! [5]
//! [6]
- foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
+ const auto entryList = pluginsDir.entryList(QDir::Files);
+ for (const QString &fileName : entryList) {
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin) {
@@ -287,16 +286,16 @@ void MainWindow::loadPlugins()
//! [10]
void MainWindow::populateMenus(QObject *plugin)
{
- BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin);
+ auto iBrush = qobject_cast<BrushInterface *>(plugin);
if (iBrush)
addToMenu(plugin, iBrush->brushes(), brushMenu, &MainWindow::changeBrush,
brushActionGroup);
- ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin);
+ auto iShape = qobject_cast<ShapeInterface *>(plugin);
if (iShape)
addToMenu(plugin, iShape->shapes(), shapesMenu, &MainWindow::insertShape);
- FilterInterface *iFilter = qobject_cast<FilterInterface *>(plugin);
+ auto iFilter = qobject_cast<FilterInterface *>(plugin);
if (iFilter)
addToMenu(plugin, iFilter->filters(), filterMenu, &MainWindow::applyFilter);
}
@@ -306,8 +305,8 @@ void MainWindow::addToMenu(QObject *plugin, const QStringList &texts,
QMenu *menu, Member member,
QActionGroup *actionGroup)
{
- foreach (QString text, texts) {
- QAction *action = new QAction(text, plugin);
+ for (const QString &text : texts) {
+ auto action = new QAction(text, plugin);
connect(action, &QAction::triggered, this, member);
menu->addAction(action);
diff --git a/examples/widgets/tools/plugandpaint/app/mainwindow.h b/examples/widgets/tools/plugandpaint/app/mainwindow.h
index 7d2c085c23..2b54264893 100644
--- a/examples/widgets/tools/plugandpaint/app/mainwindow.h
+++ b/examples/widgets/tools/plugandpaint/app/mainwindow.h
@@ -89,27 +89,27 @@ private:
void loadPlugins();
void populateMenus(QObject *plugin);
void addToMenu(QObject *plugin, const QStringList &texts, QMenu *menu,
- Member member, QActionGroup *actionGroup = 0);
+ Member member, QActionGroup *actionGroup = nullptr);
- PaintArea *paintArea;
- QScrollArea *scrollArea;
+ PaintArea *paintArea = nullptr;
+ QScrollArea *scrollArea = nullptr;
QDir pluginsDir;
QStringList pluginFileNames;
- QMenu *fileMenu;
- QMenu *brushMenu;
- QMenu *shapesMenu;
- QMenu *filterMenu;
- QMenu *helpMenu;
- QActionGroup *brushActionGroup;
- QAction *openAct;
- QAction *saveAsAct;
- QAction *exitAct;
- QAction *brushWidthAct;
- QAction *brushColorAct;
- QAction *aboutAct;
- QAction *aboutQtAct;
- QAction *aboutPluginsAct;
+ QMenu *fileMenu = nullptr;
+ QMenu *brushMenu = nullptr;
+ QMenu *shapesMenu = nullptr;
+ QMenu *filterMenu = nullptr;
+ QMenu *helpMenu = nullptr;
+ QActionGroup *brushActionGroup = nullptr;
+ QAction *openAct = nullptr;
+ QAction *saveAsAct = nullptr;
+ QAction *exitAct = nullptr;
+ QAction *brushWidthAct = nullptr;
+ QAction *brushColorAct = nullptr;
+ QAction *aboutAct = nullptr;
+ QAction *aboutQtAct = nullptr;
+ QAction *aboutPluginsAct = nullptr;
};
#endif
diff --git a/examples/widgets/tools/plugandpaint/app/paintarea.cpp b/examples/widgets/tools/plugandpaint/app/paintarea.cpp
index 5d2170bfd4..4295e04cc0 100644
--- a/examples/widgets/tools/plugandpaint/app/paintarea.cpp
+++ b/examples/widgets/tools/plugandpaint/app/paintarea.cpp
@@ -52,16 +52,11 @@
#include "interfaces.h"
#include "paintarea.h"
-#include <QPainter>
#include <QMouseEvent>
+#include <QPainter>
PaintArea::PaintArea(QWidget *parent) :
- QWidget(parent),
- theImage(500, 400, QImage::Format_RGB32),
- color(Qt::blue),
- thickness(3),
- brushInterface(0),
- lastPos(-1, -1)
+ QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_NoBackground);
diff --git a/examples/widgets/tools/plugandpaint/app/paintarea.h b/examples/widgets/tools/plugandpaint/app/paintarea.h
index 1c8ee1ee1b..1d1e6006c8 100644
--- a/examples/widgets/tools/plugandpaint/app/paintarea.h
+++ b/examples/widgets/tools/plugandpaint/app/paintarea.h
@@ -63,7 +63,7 @@ class PaintArea : public QWidget
Q_OBJECT
public:
- PaintArea(QWidget *parent = 0);
+ PaintArea(QWidget *parent = nullptr);
bool openImage(const QString &fileName);
bool saveImage(const QString &fileName, const char *fileFormat);
@@ -87,13 +87,13 @@ protected:
private:
void setupPainter(QPainter &painter);
- QImage theImage;
- QColor color;
- int thickness;
+ QImage theImage = {500, 400, QImage::Format_RGB32};
+ QColor color = Qt::blue;
+ int thickness = 3;
- BrushInterface *brushInterface;
+ BrushInterface *brushInterface = nullptr;
QString brush;
- QPoint lastPos;
+ QPoint lastPos = {-1, -1};
QPainterPath pendingPath;
};
diff --git a/examples/widgets/tools/plugandpaint/app/plugindialog.cpp b/examples/widgets/tools/plugandpaint/app/plugindialog.cpp
index 43eb34f423..af5828f67e 100644
--- a/examples/widgets/tools/plugandpaint/app/plugindialog.cpp
+++ b/examples/widgets/tools/plugandpaint/app/plugindialog.cpp
@@ -52,16 +52,15 @@
#include "interfaces.h"
#include "plugindialog.h"
-#include <QPluginLoader>
-#include <QStringList>
#include <QDir>
-
-#include <QLabel>
#include <QGridLayout>
+#include <QHeaderView>
+#include <QLabel>
+#include <QPluginLoader>
#include <QPushButton>
+#include <QStringList>
#include <QTreeWidget>
#include <QTreeWidgetItem>
-#include <QHeaderView>
PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames,
QWidget *parent) :
@@ -107,11 +106,12 @@ void PluginDialog::findPlugins(const QString &path,
const QDir dir(path);
- foreach (QObject *plugin, QPluginLoader::staticInstances())
+ const auto staticInstances = QPluginLoader::staticInstances();
+ for (QObject *plugin : staticInstances)
populateTreeWidget(plugin, tr("%1 (Static Plugin)")
.arg(plugin->metaObject()->className()));
- foreach (QString fileName, fileNames) {
+ for (const QString &fileName : fileNames) {
QPluginLoader loader(dir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin)
@@ -123,7 +123,7 @@ void PluginDialog::findPlugins(const QString &path,
//! [1]
void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text)
{
- QTreeWidgetItem *pluginItem = new QTreeWidgetItem(treeWidget);
+ auto pluginItem = new QTreeWidgetItem(treeWidget);
pluginItem->setText(0, text);
treeWidget->setItemExpanded(pluginItem, true);
@@ -132,16 +132,15 @@ void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text)
pluginItem->setFont(0, boldFont);
if (plugin) {
- BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin);
+ auto iBrush = qobject_cast<BrushInterface *>(plugin);
if (iBrush)
addItems(pluginItem, "BrushInterface", iBrush->brushes());
- ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin);
+ auto iShape = qobject_cast<ShapeInterface *>(plugin);
if (iShape)
addItems(pluginItem, "ShapeInterface", iShape->shapes());
- FilterInterface *iFilter =
- qobject_cast<FilterInterface *>(plugin);
+ auto iFilter = qobject_cast<FilterInterface *>(plugin);
if (iFilter)
addItems(pluginItem, "FilterInterface", iFilter->filters());
}
@@ -152,14 +151,14 @@ void PluginDialog::addItems(QTreeWidgetItem *pluginItem,
const char *interfaceName,
const QStringList &features)
{
- QTreeWidgetItem *interfaceItem = new QTreeWidgetItem(pluginItem);
+ auto interfaceItem = new QTreeWidgetItem(pluginItem);
interfaceItem->setText(0, interfaceName);
interfaceItem->setIcon(0, interfaceIcon);
- foreach (QString feature, features) {
+ for (QString feature : features) {
if (feature.endsWith("..."))
feature.chop(3);
- QTreeWidgetItem *featureItem = new QTreeWidgetItem(interfaceItem);
+ auto featureItem = new QTreeWidgetItem(interfaceItem);
featureItem->setText(0, feature);
featureItem->setIcon(0, featureIcon);
}
diff --git a/examples/widgets/tools/plugandpaint/app/plugindialog.h b/examples/widgets/tools/plugandpaint/app/plugindialog.h
index f8e5bcda24..b18b30e62c 100644
--- a/examples/widgets/tools/plugandpaint/app/plugindialog.h
+++ b/examples/widgets/tools/plugandpaint/app/plugindialog.h
@@ -68,7 +68,7 @@ class PluginDialog : public QDialog
public:
PluginDialog(const QString &path, const QStringList &fileNames,
- QWidget *parent = 0);
+ QWidget *parent = nullptr);
private:
void findPlugins(const QString &path, const QStringList &fileNames);
@@ -76,9 +76,9 @@ private:
void addItems(QTreeWidgetItem *pluginItem, const char *interfaceName,
const QStringList &features);
- QLabel *label;
- QTreeWidget *treeWidget;
- QPushButton *okButton;
+ QLabel *label = nullptr;
+ QTreeWidget *treeWidget = nullptr;
+ QPushButton *okButton = nullptr;
QIcon interfaceIcon;
QIcon featureIcon;
};