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>
This commit is contained in:
parent
62abbe34b8
commit
a14a943f9a
@ -50,8 +50,9 @@
|
||||
|
||||
//! [0]
|
||||
#include "mainwindow.h"
|
||||
#include <QtPlugin>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QtPlugin>
|
||||
|
||||
Q_IMPORT_PLUGIN(BasicToolsPlugin)
|
||||
|
||||
|
@ -54,19 +54,18 @@
|
||||
#include "paintarea.h"
|
||||
#include "plugindialog.h"
|
||||
|
||||
#include <QPluginLoader>
|
||||
#include <QTimer>
|
||||
|
||||
#include <QScrollArea>
|
||||
#include <QMessageBox>
|
||||
#include <QActionGroup>
|
||||
#include <QAction>
|
||||
#include <QActionGroup>
|
||||
#include <QApplication>
|
||||
#include <QColorDialog>
|
||||
#include <QFileDialog>
|
||||
#include <QInputDialog>
|
||||
#include <QMenu>
|
||||
#include <QMenuBar>
|
||||
#include <QFileDialog>
|
||||
#include <QColorDialog>
|
||||
#include <QInputDialog>
|
||||
#include <QApplication>
|
||||
#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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -48,18 +48,17 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "basictoolsplugin.h"
|
||||
|
||||
#include <QtMath>
|
||||
#include <QtWidgets>
|
||||
|
||||
#include <qmath.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "basictoolsplugin.h"
|
||||
|
||||
//! [0]
|
||||
QStringList BasicToolsPlugin::brushes() const
|
||||
{
|
||||
return QStringList() << tr("Pencil") << tr("Air Brush")
|
||||
<< tr("Random Letters");
|
||||
return {tr("Pencil"), tr("Air Brush"), tr("Random Letters")};
|
||||
}
|
||||
//! [0]
|
||||
|
||||
@ -132,7 +131,7 @@ QRect BasicToolsPlugin::mouseRelease(const QString & /* brush */,
|
||||
//! [5]
|
||||
QStringList BasicToolsPlugin::shapes() const
|
||||
{
|
||||
return QStringList() << tr("Circle") << tr("Star") << tr("Text...");
|
||||
return {tr("Circle"), tr("Star"), tr("Text...")};
|
||||
}
|
||||
//! [5]
|
||||
|
||||
@ -169,8 +168,7 @@ QPainterPath BasicToolsPlugin::generateShape(const QString &shape,
|
||||
//! [7]
|
||||
QStringList BasicToolsPlugin::filters() const
|
||||
{
|
||||
return QStringList() << tr("Invert Pixels") << tr("Swap RGB")
|
||||
<< tr("Grayscale");
|
||||
return {tr("Invert Pixels"), tr("Swap RGB"), tr("Grayscale")};
|
||||
}
|
||||
//! [7]
|
||||
|
||||
@ -187,7 +185,7 @@ QImage BasicToolsPlugin::filterImage(const QString &filter, const QImage &image,
|
||||
} else if (filter == tr("Grayscale")) {
|
||||
for (int y = 0; y < result.height(); ++y) {
|
||||
for (int x = 0; x < result.width(); ++x) {
|
||||
int pixel = result.pixel(x, y);
|
||||
QRgb pixel = result.pixel(x, y);
|
||||
int gray = qGray(pixel);
|
||||
int alpha = qAlpha(pixel);
|
||||
result.setPixel(x, y, qRgba(gray, gray, gray, alpha));
|
||||
|
@ -48,17 +48,17 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "extrafiltersplugin.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "extrafiltersplugin.h"
|
||||
|
||||
QStringList ExtraFiltersPlugin::filters() const
|
||||
{
|
||||
return QStringList() << tr("Flip Horizontally") << tr("Flip Vertically")
|
||||
<< tr("Smudge...") << tr("Threshold...");
|
||||
return {tr("Flip Horizontally"), tr("Flip Vertically"),
|
||||
tr("Smudge..."), tr("Threshold...")};
|
||||
}
|
||||
|
||||
QImage ExtraFiltersPlugin::filterImage(const QString &filter,
|
||||
@ -70,14 +70,14 @@ QImage ExtraFiltersPlugin::filterImage(const QString &filter,
|
||||
if (filter == tr("Flip Horizontally")) {
|
||||
for (int y = 0; y < original.height(); ++y) {
|
||||
for (int x = 0; x < original.width(); ++x) {
|
||||
int pixel = original.pixel(original.width() - x - 1, y);
|
||||
QRgb pixel = original.pixel(original.width() - x - 1, y);
|
||||
result.setPixel(x, y, pixel);
|
||||
}
|
||||
}
|
||||
} else if (filter == tr("Flip Vertically")) {
|
||||
for (int y = 0; y < original.height(); ++y) {
|
||||
for (int x = 0; x < original.width(); ++x) {
|
||||
int pixel = original.pixel(x, original.height() - y - 1);
|
||||
QRgb pixel = original.pixel(x, original.height() - y - 1);
|
||||
result.setPixel(x, y, pixel);
|
||||
}
|
||||
}
|
||||
@ -90,11 +90,11 @@ QImage ExtraFiltersPlugin::filterImage(const QString &filter,
|
||||
for (int i = 0; i < numIters; ++i) {
|
||||
for (int y = 1; y < original.height() - 1; ++y) {
|
||||
for (int x = 1; x < original.width() - 1; ++x) {
|
||||
int p1 = original.pixel(x, y);
|
||||
int p2 = original.pixel(x, y + 1);
|
||||
int p3 = original.pixel(x, y - 1);
|
||||
int p4 = original.pixel(x + 1, y);
|
||||
int p5 = original.pixel(x - 1, y);
|
||||
QRgb p1 = original.pixel(x, y);
|
||||
QRgb p2 = original.pixel(x, y + 1);
|
||||
QRgb p3 = original.pixel(x, y - 1);
|
||||
QRgb p4 = original.pixel(x + 1, y);
|
||||
QRgb p5 = original.pixel(x - 1, y);
|
||||
|
||||
int red = (qRed(p1) + qRed(p2) + qRed(p3) + qRed(p4)
|
||||
+ qRed(p5)) / 5;
|
||||
@ -119,7 +119,7 @@ QImage ExtraFiltersPlugin::filterImage(const QString &filter,
|
||||
int factor = 256 / threshold;
|
||||
for (int y = 0; y < original.height(); ++y) {
|
||||
for (int x = 0; x < original.width(); ++x) {
|
||||
int pixel = original.pixel(x, y);
|
||||
QRgb pixel = original.pixel(x, y);
|
||||
result.setPixel(x, y, qRgba(qRed(pixel) / factor * factor,
|
||||
qGreen(pixel) / factor * factor,
|
||||
qBlue(pixel) / factor * factor,
|
||||
|
Loading…
x
Reference in New Issue
Block a user