From cb961007c534b260b779ed513d33843a9dce01f4 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 27 Nov 2012 14:18:41 +0100 Subject: Examples: move widgets specific "tools" examples to the correct place examples/tools -> examples/widgets/tools Change-Id: I8b9e23c45e07ce5cd9da8f24a9a9f7ae10b2b107 Reviewed-by: hjk --- examples/widgets/tools/plugandpaint/interfaces.h | 114 ++++++++ examples/widgets/tools/plugandpaint/main.cpp | 55 ++++ examples/widgets/tools/plugandpaint/mainwindow.cpp | 309 +++++++++++++++++++++ examples/widgets/tools/plugandpaint/mainwindow.h | 103 +++++++ examples/widgets/tools/plugandpaint/paintarea.cpp | 195 +++++++++++++ examples/widgets/tools/plugandpaint/paintarea.h | 91 ++++++ .../tools/plugandpaint/plugandpaint.desktop | 11 + .../widgets/tools/plugandpaint/plugandpaint.pro | 25 ++ .../widgets/tools/plugandpaint/plugindialog.cpp | 156 +++++++++++ examples/widgets/tools/plugandpaint/plugindialog.h | 76 +++++ 10 files changed, 1135 insertions(+) create mode 100644 examples/widgets/tools/plugandpaint/interfaces.h create mode 100644 examples/widgets/tools/plugandpaint/main.cpp create mode 100644 examples/widgets/tools/plugandpaint/mainwindow.cpp create mode 100644 examples/widgets/tools/plugandpaint/mainwindow.h create mode 100644 examples/widgets/tools/plugandpaint/paintarea.cpp create mode 100644 examples/widgets/tools/plugandpaint/paintarea.h create mode 100644 examples/widgets/tools/plugandpaint/plugandpaint.desktop create mode 100644 examples/widgets/tools/plugandpaint/plugandpaint.pro create mode 100644 examples/widgets/tools/plugandpaint/plugindialog.cpp create mode 100644 examples/widgets/tools/plugandpaint/plugindialog.h (limited to 'examples/widgets/tools/plugandpaint') diff --git a/examples/widgets/tools/plugandpaint/interfaces.h b/examples/widgets/tools/plugandpaint/interfaces.h new file mode 100644 index 0000000000..b2261a1eb2 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/interfaces.h @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INTERFACES_H +#define INTERFACES_H + +#include + +QT_BEGIN_NAMESPACE +class QImage; +class QPainter; +class QWidget; +class QPainterPath; +class QPoint; +class QRect; +class QString; +class QStringList; +QT_END_NAMESPACE + +//! [0] +class BrushInterface +{ +public: + virtual ~BrushInterface() {} + + virtual QStringList brushes() const = 0; + virtual QRect mousePress(const QString &brush, QPainter &painter, + const QPoint &pos) = 0; + virtual QRect mouseMove(const QString &brush, QPainter &painter, + const QPoint &oldPos, const QPoint &newPos) = 0; + virtual QRect mouseRelease(const QString &brush, QPainter &painter, + const QPoint &pos) = 0; +}; +//! [0] + +//! [1] +class ShapeInterface +{ +public: + virtual ~ShapeInterface() {} + + virtual QStringList shapes() const = 0; + virtual QPainterPath generateShape(const QString &shape, + QWidget *parent) = 0; +}; +//! [1] + +//! [2] +class FilterInterface +{ +public: + virtual ~FilterInterface() {} + + virtual QStringList filters() const = 0; + virtual QImage filterImage(const QString &filter, const QImage &image, + QWidget *parent) = 0; +}; +//! [2] + +QT_BEGIN_NAMESPACE +//! [3] //! [4] +#define BrushInterface_iid "org.qt-project.Qt.Examples.PlugAndPaint.BrushInterface" + +Q_DECLARE_INTERFACE(BrushInterface, BrushInterface_iid) +//! [3] + +#define ShapeInterface_iid "org.qt-project.Qt.Examples.PlugAndPaint.ShapeInterface" + +Q_DECLARE_INTERFACE(ShapeInterface, ShapeInterface_iid) +//! [5] +#define FilterInterface_iid "org.qt-project.Qt.Examples.PlugAndPaint.FilterInterface" + +Q_DECLARE_INTERFACE(FilterInterface, FilterInterface_iid) +//! [4] //! [5] +QT_END_NAMESPACE + +#endif diff --git a/examples/widgets/tools/plugandpaint/main.cpp b/examples/widgets/tools/plugandpaint/main.cpp new file mode 100644 index 0000000000..3b290da62b --- /dev/null +++ b/examples/widgets/tools/plugandpaint/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include "mainwindow.h" +#include +#include + +Q_IMPORT_PLUGIN(BasicToolsPlugin) + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/plugandpaint/mainwindow.cpp b/examples/widgets/tools/plugandpaint/mainwindow.cpp new file mode 100644 index 0000000000..5626cdb82c --- /dev/null +++ b/examples/widgets/tools/plugandpaint/mainwindow.cpp @@ -0,0 +1,309 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "mainwindow.h" +#include "paintarea.h" +#include "plugindialog.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +MainWindow::MainWindow() : + paintArea(new PaintArea), + scrollArea(new QScrollArea) +{ + scrollArea->setBackgroundRole(QPalette::Dark); + scrollArea->setWidget(paintArea); + setCentralWidget(scrollArea); + + createActions(); + createMenus(); + loadPlugins(); + + setWindowTitle(tr("Plug & Paint")); + + if (!brushActionGroup->actions().isEmpty()) + brushActionGroup->actions().first()->trigger(); + + QTimer::singleShot(500, this, SLOT(aboutPlugins())); +} + +void MainWindow::open() +{ + const QString fileName = QFileDialog::getOpenFileName(this, + tr("Open File"), + QDir::currentPath()); + if (!fileName.isEmpty()) { + if (!paintArea->openImage(fileName)) { + QMessageBox::information(this, tr("Plug & Paint"), + tr("Cannot load %1.").arg(fileName)); + return; + } + paintArea->adjustSize(); + } +} + +bool MainWindow::saveAs() +{ + const QString initialPath = QDir::currentPath() + "/untitled.png"; + + const QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), + initialPath); + if (fileName.isEmpty()) { + return false; + } else { + return paintArea->saveImage(fileName, "png"); + } +} + +void MainWindow::brushColor() +{ + const QColor newColor = QColorDialog::getColor(paintArea->brushColor()); + if (newColor.isValid()) + paintArea->setBrushColor(newColor); +} + +void MainWindow::brushWidth() +{ + bool ok; + const int newWidth = QInputDialog::getInt(this, tr("Plug & Paint"), + tr("Select brush width:"), + paintArea->brushWidth(), + 1, 50, 1, &ok); + if (ok) + paintArea->setBrushWidth(newWidth); +} + +//! [0] +void MainWindow::changeBrush() +{ + QAction *action = qobject_cast(sender()); + BrushInterface *iBrush = qobject_cast(action->parent()); + const QString brush = action->text(); + + paintArea->setBrush(iBrush, brush); +} +//! [0] + +//! [1] +void MainWindow::insertShape() +{ + QAction *action = qobject_cast(sender()); + ShapeInterface *iShape = qobject_cast(action->parent()); + + const QPainterPath path = iShape->generateShape(action->text(), this); + if (!path.isEmpty()) + paintArea->insertShape(path); +} +//! [1] + +//! [2] +void MainWindow::applyFilter() +{ + QAction *action = qobject_cast(sender()); + FilterInterface *iFilter = + qobject_cast(action->parent()); + + const QImage image = iFilter->filterImage(action->text(), paintArea->image(), + this); + paintArea->setImage(image); +} +//! [2] + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Plug & Paint"), + tr("The Plug & Paint example demonstrates how to write Qt " + "applications that can be extended through plugins.")); +} + +//! [3] +void MainWindow::aboutPlugins() +{ + PluginDialog dialog(pluginsDir.path(), pluginFileNames, this); + dialog.exec(); +} +//! [3] + +void MainWindow::createActions() +{ + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAsAct = new QAction(tr("&Save As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + brushColorAct = new QAction(tr("&Brush Color..."), this); + connect(brushColorAct, SIGNAL(triggered()), this, SLOT(brushColor())); + + brushWidthAct = new QAction(tr("&Brush Width..."), this); + connect(brushWidthAct, SIGNAL(triggered()), this, SLOT(brushWidth())); + + brushActionGroup = new QActionGroup(this); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + aboutPluginsAct = new QAction(tr("About &Plugins"), this); + connect(aboutPluginsAct, SIGNAL(triggered()), this, SLOT(aboutPlugins())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + brushMenu = menuBar()->addMenu(tr("&Brush")); + brushMenu->addAction(brushColorAct); + brushMenu->addAction(brushWidthAct); + brushMenu->addSeparator(); + + shapesMenu = menuBar()->addMenu(tr("&Shapes")); + + filterMenu = menuBar()->addMenu(tr("&Filter")); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); + helpMenu->addAction(aboutPluginsAct); +} + +//! [4] +void MainWindow::loadPlugins() +{ + foreach (QObject *plugin, QPluginLoader::staticInstances()) + populateMenus(plugin); +//! [4] //! [5] + + pluginsDir = QDir(qApp->applicationDirPath()); + +#if defined(Q_OS_WIN) + if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release") + pluginsDir.cdUp(); +#elif defined(Q_OS_MAC) + if (pluginsDir.dirName() == "MacOS") { + pluginsDir.cdUp(); + pluginsDir.cdUp(); + pluginsDir.cdUp(); + } +#endif + pluginsDir.cd("plugins"); +//! [5] + +//! [6] + foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { + QPluginLoader loader(pluginsDir.absoluteFilePath(fileName)); + QObject *plugin = loader.instance(); + if (plugin) { + populateMenus(plugin); + pluginFileNames += fileName; +//! [6] //! [7] + } +//! [7] //! [8] + } +//! [8] + +//! [9] + brushMenu->setEnabled(!brushActionGroup->actions().isEmpty()); + shapesMenu->setEnabled(!shapesMenu->actions().isEmpty()); + filterMenu->setEnabled(!filterMenu->actions().isEmpty()); +} +//! [9] + +//! [10] +void MainWindow::populateMenus(QObject *plugin) +{ + BrushInterface *iBrush = qobject_cast(plugin); + if (iBrush) + addToMenu(plugin, iBrush->brushes(), brushMenu, SLOT(changeBrush()), + brushActionGroup); + + ShapeInterface *iShape = qobject_cast(plugin); + if (iShape) + addToMenu(plugin, iShape->shapes(), shapesMenu, SLOT(insertShape())); + + FilterInterface *iFilter = qobject_cast(plugin); + if (iFilter) + addToMenu(plugin, iFilter->filters(), filterMenu, SLOT(applyFilter())); +} +//! [10] + +void MainWindow::addToMenu(QObject *plugin, const QStringList &texts, + QMenu *menu, const char *member, + QActionGroup *actionGroup) +{ + foreach (QString text, texts) { + QAction *action = new QAction(text, plugin); + connect(action, SIGNAL(triggered()), this, member); + menu->addAction(action); + + if (actionGroup) { + action->setCheckable(true); + actionGroup->addAction(action); + } + } +} diff --git a/examples/widgets/tools/plugandpaint/mainwindow.h b/examples/widgets/tools/plugandpaint/mainwindow.h new file mode 100644 index 0000000000..c4ea76dec4 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/mainwindow.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QAction; +class QActionGroup; +class QMenu; +class QScrollArea; +QT_END_NAMESPACE +class PaintArea; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void open(); + bool saveAs(); + void brushColor(); + void brushWidth(); + void changeBrush(); + void insertShape(); + void applyFilter(); + void about(); + void aboutPlugins(); + +private: + void createActions(); + void createMenus(); + void loadPlugins(); + void populateMenus(QObject *plugin); + void addToMenu(QObject *plugin, const QStringList &texts, QMenu *menu, + const char *member, QActionGroup *actionGroup = 0); + + PaintArea *paintArea; + QScrollArea *scrollArea; + 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; +}; + +#endif diff --git a/examples/widgets/tools/plugandpaint/paintarea.cpp b/examples/widgets/tools/plugandpaint/paintarea.cpp new file mode 100644 index 0000000000..5cae92cb31 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/paintarea.cpp @@ -0,0 +1,195 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "paintarea.h" + +#include +#include + +PaintArea::PaintArea(QWidget *parent) : + QWidget(parent), + theImage(500, 400, QImage::Format_RGB32), + color(Qt::blue), + thickness(3), + brushInterface(0), + lastPos(-1, -1) +{ + setAttribute(Qt::WA_StaticContents); + setAttribute(Qt::WA_NoBackground); + + theImage.fill(qRgb(255, 255, 255)); +} + +bool PaintArea::openImage(const QString &fileName) +{ + QImage image; + if (!image.load(fileName)) + return false; + + setImage(image); + return true; +} + +bool PaintArea::saveImage(const QString &fileName, const char *fileFormat) +{ + return theImage.save(fileName, fileFormat); +} + +void PaintArea::setImage(const QImage &image) +{ + theImage = image.convertToFormat(QImage::Format_RGB32); + update(); + updateGeometry(); +} + +void PaintArea::insertShape(const QPainterPath &path) +{ + pendingPath = path; +#ifndef QT_NO_CURSOR + setCursor(Qt::CrossCursor); +#endif +} + +void PaintArea::setBrushColor(const QColor &color) +{ + this->color = color; +} + +void PaintArea::setBrushWidth(int width) +{ + thickness = width; +} + +//! [0] +void PaintArea::setBrush(BrushInterface *brushInterface, const QString &brush) +{ + this->brushInterface = brushInterface; + this->brush = brush; +} +//! [0] + +QSize PaintArea::sizeHint() const +{ + return theImage.size(); +} + +void PaintArea::paintEvent(QPaintEvent * /* event */) +{ + QPainter painter(this); + painter.drawImage(QPoint(0, 0), theImage); +} + +void PaintArea::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + if (!pendingPath.isEmpty()) { + QPainter painter(&theImage); + setupPainter(painter); + + const QRectF boundingRect = pendingPath.boundingRect(); + QLinearGradient gradient(boundingRect.topRight(), + boundingRect.bottomLeft()); + gradient.setColorAt(0.0, QColor(color.red(), color.green(), + color.blue(), 63)); + gradient.setColorAt(1.0, QColor(color.red(), color.green(), + color.blue(), 191)); + painter.setBrush(gradient); + painter.translate(event->pos() - boundingRect.center()); + painter.drawPath(pendingPath); + + pendingPath = QPainterPath(); +#ifndef QT_NO_CURSOR + unsetCursor(); +#endif + update(); + } else { + if (brushInterface) { + QPainter painter(&theImage); + setupPainter(painter); + const QRect rect = brushInterface->mousePress(brush, painter, + event->pos()); + update(rect); + } + + lastPos = event->pos(); + } + } +} + +//! [1] +void PaintArea::mouseMoveEvent(QMouseEvent *event) +{ + if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) { + if (brushInterface) { + QPainter painter(&theImage); + setupPainter(painter); + const QRect rect = brushInterface->mouseMove(brush, painter, lastPos, + event->pos()); + update(rect); + } + + lastPos = event->pos(); + } +} +//! [1] + +void PaintArea::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) { + if (brushInterface) { + QPainter painter(&theImage); + setupPainter(painter); + QRect rect = brushInterface->mouseRelease(brush, painter, + event->pos()); + update(rect); + } + + lastPos = QPoint(-1, -1); + } +} + +void PaintArea::setupPainter(QPainter &painter) +{ + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap, + Qt::RoundJoin)); +} diff --git a/examples/widgets/tools/plugandpaint/paintarea.h b/examples/widgets/tools/plugandpaint/paintarea.h new file mode 100644 index 0000000000..1bc0f810e0 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/paintarea.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PAINTAREA_H +#define PAINTAREA_H + +#include +#include +#include +#include + +class BrushInterface; + +class PaintArea : public QWidget +{ + Q_OBJECT + +public: + PaintArea(QWidget *parent = 0); + + bool openImage(const QString &fileName); + bool saveImage(const QString &fileName, const char *fileFormat); + void setImage(const QImage &image); + void insertShape(const QPainterPath &path); + void setBrushColor(const QColor &color); + void setBrushWidth(int width); + void setBrush(BrushInterface *brushInterface, const QString &brush); + + QImage image() const { return theImage; } + QColor brushColor() const { return color; } + int brushWidth() const { return thickness; } + QSize sizeHint() const; + +protected: + void paintEvent(QPaintEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + +private: + void setupPainter(QPainter &painter); + + QImage theImage; + QColor color; + int thickness; + + BrushInterface *brushInterface; + QString brush; + QPoint lastPos; + + QPainterPath pendingPath; +}; + +#endif diff --git a/examples/widgets/tools/plugandpaint/plugandpaint.desktop b/examples/widgets/tools/plugandpaint/plugandpaint.desktop new file mode 100644 index 0000000000..e39d512225 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugandpaint.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Plug appnameplaceholder Paint +Exec=/opt/usr/bin/plugandpaint +Icon=plugandpaint +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/plugandpaint/plugandpaint.pro b/examples/widgets/tools/plugandpaint/plugandpaint.pro new file mode 100644 index 0000000000..2c8c5117ee --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugandpaint.pro @@ -0,0 +1,25 @@ +#! [0] +HEADERS = interfaces.h \ + mainwindow.h \ + paintarea.h \ + plugindialog.h +SOURCES = main.cpp \ + mainwindow.cpp \ + paintarea.cpp \ + plugindialog.cpp + +LIBS = -L$${QT_BUILD_TREE}/examples/widgets/tools/plugandpaint/plugins -lpnp_basictools + +if(!debug_and_release|build_pass):CONFIG(debug, debug|release) { + mac:LIBS = $$member(LIBS, 0) $$member(LIBS, 1)_debug + win32:LIBS = $$member(LIBS, 0) $$member(LIBS, 1)d +} +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS plugandpaint.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/plugandpaint/plugindialog.cpp b/examples/widgets/tools/plugandpaint/plugindialog.cpp new file mode 100644 index 0000000000..597f90c349 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugindialog.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "plugindialog.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames, + QWidget *parent) : + QDialog(parent), + label(new QLabel), + treeWidget(new QTreeWidget), + okButton(new QPushButton(tr("OK"))) +{ + treeWidget->setAlternatingRowColors(false); + treeWidget->setSelectionMode(QAbstractItemView::NoSelection); + treeWidget->setColumnCount(1); + treeWidget->header()->hide(); + + okButton->setDefault(true); + + connect(okButton, SIGNAL(clicked()), this, SLOT(close())); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->setColumnStretch(0, 1); + mainLayout->setColumnStretch(2, 1); + mainLayout->addWidget(label, 0, 0, 1, 3); + mainLayout->addWidget(treeWidget, 1, 0, 1, 3); + mainLayout->addWidget(okButton, 2, 1); + setLayout(mainLayout); + + interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon), + QIcon::Normal, QIcon::On); + interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon), + QIcon::Normal, QIcon::Off); + featureIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); + + setWindowTitle(tr("Plugin Information")); + findPlugins(path, fileNames); +} + +//! [0] +void PluginDialog::findPlugins(const QString &path, + const QStringList &fileNames) +{ + label->setText(tr("Plug & Paint found the following plugins\n" + "(looked in %1):") + .arg(QDir::toNativeSeparators(path))); + + const QDir dir(path); + + foreach (QObject *plugin, QPluginLoader::staticInstances()) + populateTreeWidget(plugin, tr("%1 (Static Plugin)") + .arg(plugin->metaObject()->className())); + + foreach (QString fileName, fileNames) { + QPluginLoader loader(dir.absoluteFilePath(fileName)); + QObject *plugin = loader.instance(); + if (plugin) + populateTreeWidget(plugin, fileName); + } +} +//! [0] + +//! [1] +void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text) +{ + QTreeWidgetItem *pluginItem = new QTreeWidgetItem(treeWidget); + pluginItem->setText(0, text); + treeWidget->setItemExpanded(pluginItem, true); + + QFont boldFont = pluginItem->font(0); + boldFont.setBold(true); + pluginItem->setFont(0, boldFont); + + if (plugin) { + BrushInterface *iBrush = qobject_cast(plugin); + if (iBrush) + addItems(pluginItem, "BrushInterface", iBrush->brushes()); + + ShapeInterface *iShape = qobject_cast(plugin); + if (iShape) + addItems(pluginItem, "ShapeInterface", iShape->shapes()); + + FilterInterface *iFilter = + qobject_cast(plugin); + if (iFilter) + addItems(pluginItem, "FilterInterface", iFilter->filters()); + } +} +//! [1] + +void PluginDialog::addItems(QTreeWidgetItem *pluginItem, + const char *interfaceName, + const QStringList &features) +{ + QTreeWidgetItem *interfaceItem = new QTreeWidgetItem(pluginItem); + interfaceItem->setText(0, interfaceName); + interfaceItem->setIcon(0, interfaceIcon); + + foreach (QString feature, features) { + if (feature.endsWith("...")) + feature.chop(3); + QTreeWidgetItem *featureItem = new QTreeWidgetItem(interfaceItem); + featureItem->setText(0, feature); + featureItem->setIcon(0, featureIcon); + } +} diff --git a/examples/widgets/tools/plugandpaint/plugindialog.h b/examples/widgets/tools/plugandpaint/plugindialog.h new file mode 100644 index 0000000000..ff4a5d9c09 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugindialog.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PLUGINDIALOG_H +#define PLUGINDIALOG_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QLabel; +class QPushButton; +class QStringList; +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +class PluginDialog : public QDialog +{ + Q_OBJECT + +public: + PluginDialog(const QString &path, const QStringList &fileNames, + QWidget *parent = 0); + +private: + void findPlugins(const QString &path, const QStringList &fileNames); + void populateTreeWidget(QObject *plugin, const QString &text); + void addItems(QTreeWidgetItem *pluginItem, const char *interfaceName, + const QStringList &features); + + QLabel *label; + QTreeWidget *treeWidget; + QPushButton *okButton; + QIcon interfaceIcon; + QIcon featureIcon; +}; + +#endif -- cgit v1.2.3