diff options
Diffstat (limited to 'examples/tools/plugandpaint')
-rw-r--r-- | examples/tools/plugandpaint/interfaces.h | 111 | ||||
-rw-r--r-- | examples/tools/plugandpaint/main.cpp | 58 | ||||
-rw-r--r-- | examples/tools/plugandpaint/mainwindow.cpp | 310 | ||||
-rw-r--r-- | examples/tools/plugandpaint/mainwindow.h | 104 | ||||
-rw-r--r-- | examples/tools/plugandpaint/paintarea.cpp | 196 | ||||
-rw-r--r-- | examples/tools/plugandpaint/paintarea.h | 92 | ||||
-rw-r--r-- | examples/tools/plugandpaint/plugandpaint.pro | 22 | ||||
-rw-r--r-- | examples/tools/plugandpaint/plugindialog.cpp | 157 | ||||
-rw-r--r-- | examples/tools/plugandpaint/plugindialog.h | 77 |
9 files changed, 1127 insertions, 0 deletions
diff --git a/examples/tools/plugandpaint/interfaces.h b/examples/tools/plugandpaint/interfaces.h new file mode 100644 index 0000000..87eb9d0 --- /dev/null +++ b/examples/tools/plugandpaint/interfaces.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INTERFACES_H +#define INTERFACES_H + +#include <QtPlugin> + +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] +Q_DECLARE_INTERFACE(BrushInterface, + "com.trolltech.PlugAndPaint.BrushInterface/1.0") +//! [3] +Q_DECLARE_INTERFACE(ShapeInterface, + "com.trolltech.PlugAndPaint.ShapeInterface/1.0") +//! [5] +Q_DECLARE_INTERFACE(FilterInterface, + "com.trolltech.PlugAndPaint.FilterInterface/1.0") +//! [4] //! [5] +QT_END_NAMESPACE + +#endif diff --git a/examples/tools/plugandpaint/main.cpp b/examples/tools/plugandpaint/main.cpp new file mode 100644 index 0000000..7778cac --- /dev/null +++ b/examples/tools/plugandpaint/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include "mainwindow.h" +#include <QtPlugin> +#include <QApplication> + +QT_BEGIN_NAMESPACE +Q_IMPORT_PLUGIN(pnp_basictools) +QT_END_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/tools/plugandpaint/mainwindow.cpp b/examples/tools/plugandpaint/mainwindow.cpp new file mode 100644 index 0000000..224bc71 --- /dev/null +++ b/examples/tools/plugandpaint/mainwindow.cpp @@ -0,0 +1,310 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "mainwindow.h" +#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 <QColorDialog> +#include <QInputDialog> +#include <QApplication> + +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::getInteger(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<QAction *>(sender()); + BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent()); + const QString brush = action->text(); + + paintArea->setBrush(iBrush, brush); +} +//! [0] + +//! [1] +void MainWindow::insertShape() +{ + QAction *action = qobject_cast<QAction *>(sender()); + ShapeInterface *iShape = qobject_cast<ShapeInterface *>(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<QAction *>(sender()); + FilterInterface *iFilter = + qobject_cast<FilterInterface *>(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 <b>Plug & Paint</b> 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->setShortcut(tr("Ctrl+O")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAsAct = new QAction(tr("&Save As..."), this); + saveAsAct->setShortcut(tr("Ctrl+S")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcut(tr("Ctrl+Q")); + 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<BrushInterface *>(plugin); + if (iBrush) + addToMenu(plugin, iBrush->brushes(), brushMenu, SLOT(changeBrush()), + brushActionGroup); + + ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin); + if (iShape) + addToMenu(plugin, iShape->shapes(), shapesMenu, SLOT(insertShape())); + + FilterInterface *iFilter = qobject_cast<FilterInterface *>(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/tools/plugandpaint/mainwindow.h b/examples/tools/plugandpaint/mainwindow.h new file mode 100644 index 0000000..47afb2d --- /dev/null +++ b/examples/tools/plugandpaint/mainwindow.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QDir> +#include <QMainWindow> +#include <QStringList> + +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/tools/plugandpaint/paintarea.cpp b/examples/tools/plugandpaint/paintarea.cpp new file mode 100644 index 0000000..9d5b3c5 --- /dev/null +++ b/examples/tools/plugandpaint/paintarea.cpp @@ -0,0 +1,196 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "paintarea.h" + +#include <QPainter> +#include <QMouseEvent> + +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/tools/plugandpaint/paintarea.h b/examples/tools/plugandpaint/paintarea.h new file mode 100644 index 0000000..e4215d5 --- /dev/null +++ b/examples/tools/plugandpaint/paintarea.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PAINTAREA_H +#define PAINTAREA_H + +#include <QColor> +#include <QImage> +#include <QPainterPath> +#include <QWidget> + +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/tools/plugandpaint/plugandpaint.pro b/examples/tools/plugandpaint/plugandpaint.pro new file mode 100644 index 0000000..15bc21e --- /dev/null +++ b/examples/tools/plugandpaint/plugandpaint.pro @@ -0,0 +1,22 @@ +#! [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/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 diff --git a/examples/tools/plugandpaint/plugindialog.cpp b/examples/tools/plugandpaint/plugindialog.cpp new file mode 100644 index 0000000..5e37a8b --- /dev/null +++ b/examples/tools/plugandpaint/plugindialog.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "plugindialog.h" + +#include <QPluginLoader> +#include <QStringList> +#include <QDir> + +#include <QLabel> +#include <QGridLayout> +#include <QPushButton> +#include <QTreeWidget> +#include <QTreeWidgetItem> +#include <QHeaderView> + +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<BrushInterface *>(plugin); + if (iBrush) + addItems(pluginItem, "BrushInterface", iBrush->brushes()); + + ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin); + if (iShape) + addItems(pluginItem, "ShapeInterface", iShape->shapes()); + + FilterInterface *iFilter = + qobject_cast<FilterInterface *>(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/tools/plugandpaint/plugindialog.h b/examples/tools/plugandpaint/plugindialog.h new file mode 100644 index 0000000..73cb6e4 --- /dev/null +++ b/examples/tools/plugandpaint/plugindialog.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PLUGINDIALOG_H +#define PLUGINDIALOG_H + +#include <QDialog> +#include <QIcon> + +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 |