diff options
author | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
---|---|---|
committer | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
commit | 8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76 (patch) | |
tree | a17e1a767a89542ab59907462206d7dcf2e504b2 /doc/src/snippets/separations | |
download | Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.zip Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.gz Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.bz2 |
Long live Qt for S60!
Diffstat (limited to 'doc/src/snippets/separations')
-rw-r--r-- | doc/src/snippets/separations/finalwidget.cpp | 127 | ||||
-rw-r--r-- | doc/src/snippets/separations/finalwidget.h | 78 | ||||
-rw-r--r-- | doc/src/snippets/separations/main.cpp | 51 | ||||
-rw-r--r-- | doc/src/snippets/separations/screenwidget.cpp | 218 | ||||
-rw-r--r-- | doc/src/snippets/separations/screenwidget.h | 87 | ||||
-rw-r--r-- | doc/src/snippets/separations/separations.pro | 7 | ||||
-rw-r--r-- | doc/src/snippets/separations/separations.qdoc | 55 | ||||
-rw-r--r-- | doc/src/snippets/separations/viewer.cpp | 329 | ||||
-rw-r--r-- | doc/src/snippets/separations/viewer.h | 90 |
9 files changed, 1042 insertions, 0 deletions
diff --git a/doc/src/snippets/separations/finalwidget.cpp b/doc/src/snippets/separations/finalwidget.cpp new file mode 100644 index 0000000..c6b103a --- /dev/null +++ b/doc/src/snippets/separations/finalwidget.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +/* +finalwidget.cpp + +A widget to display an image and a label containing a description. +*/ + +#include <QtGui> +#include "finalwidget.h" + +FinalWidget::FinalWidget(QWidget *parent, const QString &name, + const QSize &labelSize) + : QFrame(parent) +{ + hasImage = false; + imageLabel = new QLabel; + imageLabel->setFrameShadow(QFrame::Sunken); + imageLabel->setFrameShape(QFrame::StyledPanel); + imageLabel->setMinimumSize(labelSize); + nameLabel = new QLabel(name); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(imageLabel, 1); + layout->addWidget(nameLabel, 0); + setLayout(layout); +} + +/*! + If the mouse moves far enough when the left mouse button is held down, + start a drag and drop operation. +*/ + +void FinalWidget::mouseMoveEvent(QMouseEvent *event) +{ + if (!(event->buttons() & Qt::LeftButton)) + return; + if ((event->pos() - dragStartPosition).manhattanLength() + < QApplication::startDragDistance()) + return; + if (!hasImage) + return; + + QDrag *drag = new QDrag(this); + QMimeData *mimeData = new QMimeData; + +//! [0] + QByteArray output; + QBuffer outputBuffer(&output); + outputBuffer.open(QIODevice::WriteOnly); + imageLabel->pixmap()->toImage().save(&outputBuffer, "PNG"); + mimeData->setData("image/png", output); +//! [0] +/* +//! [1] + mimeData->setImageData(QVariant(*imageLabel->pixmap())); +//! [1] +*/ + drag->setMimeData(mimeData); + drag->setPixmap(imageLabel->pixmap()->scaled(64, 64, Qt::KeepAspectRatio)); +//! [2] + drag->setHotSpot(QPoint(drag->pixmap().width()/2, + drag->pixmap().height())); +//! [2] + + drag->start(); +} + +/*! + Check for left mouse button presses in order to enable drag and drop. +*/ + +void FinalWidget::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) + dragStartPosition = event->pos(); +} + +const QPixmap* FinalWidget::pixmap() const +{ + return imageLabel->pixmap(); +} + +void FinalWidget::setPixmap(const QPixmap &pixmap) +{ + imageLabel->setPixmap(pixmap); + hasImage = true; +} diff --git a/doc/src/snippets/separations/finalwidget.h b/doc/src/snippets/separations/finalwidget.h new file mode 100644 index 0000000..df45fcc --- /dev/null +++ b/doc/src/snippets/separations/finalwidget.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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 FINALWIDGET_H +#define FINALWIDGET_H + +#include <QFrame> +#include <QImage> +#include <QPoint> +#include <QSize> + +class QGridLayout; +class QLabel; +class QMouseEvent; +class QWidget; + +class FinalWidget : public QFrame +{ + Q_OBJECT + +public: + FinalWidget(QWidget *parent, const QString &name, const QSize &labelSize); + void setPixmap(const QPixmap &pixmap); + const QPixmap *pixmap() const; + +protected: + void mouseMoveEvent(QMouseEvent *event); + void mousePressEvent(QMouseEvent *event); + +private: + void createImage(); + + bool hasImage; + QImage originalImage; + QLabel *imageLabel; + QLabel *nameLabel; + QPoint dragStartPosition; +}; + +#endif diff --git a/doc/src/snippets/separations/main.cpp b/doc/src/snippets/separations/main.cpp new file mode 100644 index 0000000..0a859c2 --- /dev/null +++ b/doc/src/snippets/separations/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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 <QApplication> +#include "viewer.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Viewer viewer; + viewer.show(); + return app.exec(); +} diff --git a/doc/src/snippets/separations/screenwidget.cpp b/doc/src/snippets/separations/screenwidget.cpp new file mode 100644 index 0000000..daa276b --- /dev/null +++ b/doc/src/snippets/separations/screenwidget.cpp @@ -0,0 +1,218 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +/* +screenwidget.cpp + +A widget to display colour components from an image using independently +selected colors. Controls are provided to allow the image to be inverted, and +the color to be selection via a standard dialog. The image is displayed in a +label widget. +*/ + +#include <QApplication> +#include <QColorDialog> +#include <QGridLayout> +#include <QImage> +#include <QLabel> +#include <QMenu> +#include <QMimeData> +#include <QMouseEvent> +#include <QPixmap> +#include <QPushButton> +#include <QWidget> + +#include "screenwidget.h" + +/*! +Initializes the paint color, the mask color (cyan, magenta, +or yellow), connects the color selector and invert checkbox to functions, +and creates a two-by-two grid layout. +*/ + +ScreenWidget::ScreenWidget(QWidget *parent, QColor initialColor, + const QString &name, Separation mask, + const QSize &labelSize) + : QFrame(parent) +{ + paintColor = initialColor; + maskColor = mask; + inverted = false; + + imageLabel = new QLabel; + imageLabel->setFrameShadow(QFrame::Sunken); + imageLabel->setFrameShape(QFrame::StyledPanel); + imageLabel->setMinimumSize(labelSize); + + nameLabel = new QLabel(name); + colorButton = new QPushButton(tr("Modify...")); + colorButton->setBackgroundRole(QPalette::Button); + colorButton->setMinimumSize(32, 32); + + QPalette palette(colorButton->palette()); + palette.setColor(QPalette::Button, initialColor); + colorButton->setPalette(palette); + + invertButton = new QPushButton(tr("Invert")); + //invertButton->setToggleButton(true); + //invertButton->setOn(inverted); + invertButton->setEnabled(false); + + connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor())); + connect(invertButton, SIGNAL(clicked()), this, SLOT(invertImage())); + + QGridLayout *gridLayout = new QGridLayout; + gridLayout->addWidget(imageLabel, 0, 0, 1, 2); + gridLayout->addWidget(nameLabel, 1, 0); + gridLayout->addWidget(colorButton, 1, 1); + gridLayout->addWidget(invertButton, 2, 1, 1, 1); + setLayout(gridLayout); +} + +/*! + Creates a new image by separating out the cyan, magenta, or yellow + component, depending on the mask color specified in the constructor. + + The amount of the component found in each pixel of the image is used + to determine how much of a user-selected ink is used for each pixel + in the new image for the label widget. +*/ + +void ScreenWidget::createImage() +{ + newImage = originalImage.copy(); + + // Create CMY components for the ink being used. + float cyanInk = (255 - paintColor.red())/255.0; + float magentaInk = (255 - paintColor.green())/255.0; + float yellowInk = (255 - paintColor.blue())/255.0; + + int (*convert)(QRgb); + + switch (maskColor) { + case Cyan: + convert = qRed; + break; + case Magenta: + convert = qGreen; + break; + case Yellow: + convert = qBlue; + break; + } + + for (int y = 0; y < newImage.height(); ++y) { + for (int x = 0; x < newImage.width(); ++x) { + QRgb p(originalImage.pixel(x, y)); + + // Separate the source pixel into its cyan component. + int amount; + + if (inverted) + amount = convert(p); + else + amount = 255 - convert(p); + + QColor newColor( + 255 - qMin(int(amount * cyanInk), 255), + 255 - qMin(int(amount * magentaInk), 255), + 255 - qMin(int(amount * yellowInk), 255)); + + newImage.setPixel(x, y, newColor.rgb()); + } + } + + imageLabel->setPixmap(QPixmap::fromImage(newImage)); +} + +/*! + Returns a pointer to the modified image. +*/ + +QImage* ScreenWidget::image() +{ + return &newImage; +} + +/*! + Sets whether the amount of ink applied to the canvas is to be inverted + (subtracted from the maximum value) before the ink is applied. +*/ + +void ScreenWidget::invertImage() +{ + //inverted = invertButton->isOn(); + inverted = !inverted; + createImage(); + emit imageChanged(); +} + +/*! + Separate the current image into cyan, magenta, and yellow components. + Create a representation of how each component might appear when applied + to a blank white piece of paper. +*/ + +void ScreenWidget::setColor() +{ + QColor newColor = QColorDialog::getColor(paintColor); + + if (newColor.isValid()) { + paintColor = newColor; + QPalette palette(colorButton->palette()); + palette.setColor(QPalette::Button, paintColor); + colorButton->setPalette(palette); + createImage(); + emit imageChanged(); + } +} + +/*! + Records the original image selected by the user, creates a color + separation, and enables the invert image checkbox. +*/ + +void ScreenWidget::setImage(QImage &image) +{ + originalImage = image; + createImage(); + invertButton->setEnabled(true); +} diff --git a/doc/src/snippets/separations/screenwidget.h b/doc/src/snippets/separations/screenwidget.h new file mode 100644 index 0000000..3371028 --- /dev/null +++ b/doc/src/snippets/separations/screenwidget.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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 SCREENWIDGET_H +#define SCREENWIDGET_H + +#include <QColor> +#include <QFrame> +#include <QImage> +#include <QSize> + +class QGridLayout; +class QLabel; +class QPushButton; +class QWidget; + +class ScreenWidget : public QFrame +{ + Q_OBJECT +public: + enum Separation { Cyan, Magenta, Yellow }; + + ScreenWidget(QWidget *parent, QColor initialColor, const QString &name, + Separation mask, const QSize &labelSize); + void setImage(QImage &image); + QImage* image(); + +signals: + void imageChanged(); + +public slots: + void setColor(); + void invertImage(); + +private: + void createImage(); + + bool inverted; + QColor paintColor; + QImage newImage; + QImage originalImage; + QLabel *imageLabel; + QLabel *nameLabel; + QPushButton *colorButton; + QPushButton *invertButton; + Separation maskColor; +}; + +#endif diff --git a/doc/src/snippets/separations/separations.pro b/doc/src/snippets/separations/separations.pro new file mode 100644 index 0000000..48af2c8 --- /dev/null +++ b/doc/src/snippets/separations/separations.pro @@ -0,0 +1,7 @@ +HEADERS = finalwidget.h \ + screenwidget.h \ + viewer.h +SOURCES = finalwidget.cpp \ + main.cpp \ + screenwidget.cpp \ + viewer.cpp diff --git a/doc/src/snippets/separations/separations.qdoc b/doc/src/snippets/separations/separations.qdoc new file mode 100644 index 0000000..5091352 --- /dev/null +++ b/doc/src/snippets/separations/separations.qdoc @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +/* + \example painting/separations + \title Color Separations Example + + This example enables simple color manipulation of images and demonstrates a + number of image-related features of Qt, from per-pixel image manipulation to + drag and drop handling of images. + + \image separations-example.png + + The application allows the user to load an image, shown in the top-left + part of the main window, and to adjust its color balance by replacing its + initial cyan, magenta, and yellow components with different colors. +*/ diff --git a/doc/src/snippets/separations/viewer.cpp b/doc/src/snippets/separations/viewer.cpp new file mode 100644 index 0000000..f33be69 --- /dev/null +++ b/doc/src/snippets/separations/viewer.cpp @@ -0,0 +1,329 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +/* +viewer.cpp + +Provides a main window for displaying a user-specified original image +with three color separations in a grid layout. + +A main menu provides entries for selecting files, and adjusting the +brightness of the separations. +*/ + +#include <QtGui> + +#include "finalwidget.h" +#include "screenwidget.h" +#include "viewer.h" + +/* + Constructor: initializes a default value for the brightness, creates + the main menu entries, and constructs a central widget that contains + enough space for images to be displayed. +*/ + +Viewer::Viewer() +{ + setWindowTitle(tr("QImage Color Separations")); + + brightness = 255; + + createMenus(); + setCentralWidget(createCentralWidget()); +} + +/* + Creates a main menu with two entries: a File menu, to allow the image + to be selected, and a Brightness menu to allow the brightness of the + separations to be changed. + + Initially, the Brightness menu items are disabled, but the first entry in + the menu is checked to reflect the default brightness. +*/ + +void Viewer::createMenus() +{ + fileMenu = new QMenu(tr("&File"), this); + brightnessMenu = new QMenu(tr("&Brightness"), this); + + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence("Ctrl+O")); + saveAction = fileMenu->addAction(tr("&Save...")); + saveAction->setShortcut(QKeySequence("Ctrl+S")); + saveAction->setEnabled(false); + QAction *quitAction = fileMenu->addAction(tr("E&xit")); + quitAction->setShortcut(QKeySequence("Ctrl+Q")); + + QAction *noBrightness = brightnessMenu->addAction(tr("&0%")); + noBrightness->setCheckable(true); + QAction *quarterBrightness = brightnessMenu->addAction(tr("&25%")); + quarterBrightness->setCheckable(true); + QAction *halfBrightness = brightnessMenu->addAction(tr("&50%")); + halfBrightness->setCheckable(true); + QAction *threeQuartersBrightness = brightnessMenu->addAction(tr("&75%")); + threeQuartersBrightness->setCheckable(true); + QAction *fullBrightness = brightnessMenu->addAction(tr("&100%")); + fullBrightness->setCheckable(true); + + menuMap[noBrightness] = None; + menuMap[quarterBrightness] = Quarter; + menuMap[halfBrightness] = Half; + menuMap[threeQuartersBrightness] = ThreeQuarters; + menuMap[fullBrightness] = Full; + + currentBrightness = fullBrightness; + currentBrightness->setChecked(true); + brightnessMenu->setEnabled(false); + + menuBar()->addMenu(fileMenu); + menuBar()->addMenu(brightnessMenu); + + connect(openAction, SIGNAL(triggered()), this, SLOT(chooseFile())); + connect(saveAction, SIGNAL(triggered()), this, SLOT(saveImage())); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(brightnessMenu, SIGNAL(triggered(QAction *)), this, + SLOT(setBrightness(QAction *))); +} + +/* + Constructs a central widget for the window consisting of a two-by-two + grid of labels, each of which will contain an image. We restrict the + size of the labels to 256 pixels, and ensure that the window cannot + be resized. +*/ + +QFrame* Viewer::createCentralWidget() +{ + QFrame* frame = new QFrame(this); + grid = new QGridLayout(frame); + grid->setSpacing(8); + grid->setMargin(4); + + layout()->setSizeConstraint(QLayout::SetFixedSize); + + QSize labelSize(256, 256); + + finalWidget = new FinalWidget(frame, tr("Final image"), labelSize); + + cyanWidget = new ScreenWidget(frame, Qt::cyan, tr("Cyan"), + ScreenWidget::Cyan, labelSize); + magentaWidget = new ScreenWidget(frame, Qt::magenta, tr("Magenta"), + ScreenWidget::Magenta, labelSize); + yellowWidget = new ScreenWidget(frame, Qt::yellow, tr("Yellow"), + ScreenWidget::Yellow, labelSize); + + connect(cyanWidget, SIGNAL(imageChanged()), this, SLOT(createImage())); + connect(magentaWidget, SIGNAL(imageChanged()), this, SLOT(createImage())); + connect(yellowWidget, SIGNAL(imageChanged()), this, SLOT(createImage())); + + grid->addWidget(finalWidget, 0, 0, Qt::AlignTop | Qt::AlignHCenter); + grid->addWidget(cyanWidget, 0, 1, Qt::AlignTop | Qt::AlignHCenter); + grid->addWidget(magentaWidget, 1, 0, Qt::AlignTop | Qt::AlignHCenter); + grid->addWidget(yellowWidget, 1, 1, Qt::AlignTop | Qt::AlignHCenter); + + return frame; +} + +/* + Provides a dialog window to allow the user to specify an image file. + If a file is selected, the appropriate function is called to process + and display it. +*/ + +void Viewer::chooseFile() +{ + QString imageFile = QFileDialog::getOpenFileName(this, + tr("Choose an image file to open"), path, tr("Images (*.*)")); + + if (!imageFile.isEmpty()) { + openImageFile(imageFile); + path = imageFile; + } +} + +/* + Changes the value of the brightness according to the entry selected in the + Brightness menu. The selected entry is checked, and the previously selected + entry is unchecked. + + The color separations are updated to use the new value for the brightness. +*/ + +void Viewer::setBrightness(QAction *action) +{ + if (!menuMap.contains(action) || scaledImage.isNull()) + return; + + Brightness amount = menuMap[action]; + + switch (amount) { + case None: + brightness = 0; break; + case Quarter: + brightness = 64; break; + case Half: + brightness = 128; break; + case ThreeQuarters: + brightness = 191; break; + case Full: + brightness = 255; break; + default: return; + } + + currentBrightness->setChecked(false); + currentBrightness = action; + currentBrightness->setChecked(true); + + createImage(); +} + +/* + Load the image from the file given, and create four pixmaps based + on the original image. + + The window caption is set, and the Brightness menu enabled if the image file + can be loaded. +*/ + +void Viewer::openImageFile(QString &imageFile) +{ + QImage originalImage; + + if (originalImage.load(imageFile)) { + setWindowTitle(imageFile); + //menuBar()->setItemEnabled(brightnessMenuId, true); + saveAction->setEnabled(true); + brightnessMenu->setEnabled(true); + + /* Note: the ScaleMin value may be different for Qt 4. */ + scaledImage = originalImage.scaled(256, 256, Qt::KeepAspectRatio); + + cyanWidget->setImage(scaledImage); + magentaWidget->setImage(scaledImage); + yellowWidget->setImage(scaledImage); + createImage(); + } + else + (void) QMessageBox::warning(this, tr("Cannot open file"), + tr("The selected file could not be opened."), + QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton); +} + +/* + Creates an image by combining the contents of the three screens + to present a page preview. + + The image associated with each screen is separated into cyan, + magenta, and yellow components. We add up the values for each + component from the three screen images, and subtract the totals + from the maximum value for each corresponding primary color. +*/ + +void Viewer::createImage() +{ + QImage newImage = scaledImage.copy(); + + QImage *image1 = cyanWidget->image(); + QImage *image2 = magentaWidget->image(); + QImage *image3 = yellowWidget->image(); + int darkness = 255 - brightness; + + for (int y = 0; y < newImage.height(); ++y) { + for (int x = 0; x < newImage.width(); ++x) { + + // Create three screens, using the quantities of the source + // CMY components to determine how much of each of the + // inks are to be put on each screen. + QRgb p1(image1->pixel(x, y)); + float cyan1 = 255 - qRed(p1); + float magenta1 = 255 - qGreen(p1); + float yellow1 = 255 - qBlue(p1); + + QRgb p2(image2->pixel(x, y)); + float cyan2 = 255 - qRed(p2); + float magenta2 = 255 - qGreen(p2); + float yellow2 = 255 - qBlue(p2); + + QRgb p3(image3->pixel(x, y)); + float cyan3 = 255 - qRed(p3); + float magenta3 = 255 - qGreen(p3); + float yellow3 = 255 - qBlue(p3); + + QColor newColor( + qMax(255 - int(cyan1+cyan2+cyan3) - darkness, 0), + qMax(255 - int(magenta1+magenta2+magenta3) - darkness, 0), + qMax(255 - int(yellow1+yellow2+yellow3) - darkness, 0)); + + newImage.setPixel(x, y, newColor.rgb()); + } + } + + finalWidget->setPixmap(QPixmap::fromImage(newImage)); +} + +/* + Provides a dialog window to allow the user to save the image file. +*/ + +void Viewer::saveImage() +{ + QString imageFile = QFileDialog::getSaveFileName(this, + tr("Choose a filename to save the image"), "", tr("Images (*.png)")); + + QFileInfo info(imageFile); + + if (!info.baseName().isEmpty()) { + QString newImageFile = QFileInfo(info.absoluteDir(), + info.baseName() + ".png").absoluteFilePath(); + + if (!finalWidget->pixmap()->save(newImageFile, "PNG")) + (void) QMessageBox::warning(this, tr("Cannot save file"), + tr("The file could not be saved."), + QMessageBox::Cancel, QMessageBox::NoButton, + QMessageBox::NoButton); + } + else + (void) QMessageBox::warning(this, tr("Cannot save file"), + tr("Please enter a valid filename."), + QMessageBox::Cancel, QMessageBox::NoButton, + QMessageBox::NoButton); +} diff --git a/doc/src/snippets/separations/viewer.h b/doc/src/snippets/separations/viewer.h new file mode 100644 index 0000000..148f081 --- /dev/null +++ b/doc/src/snippets/separations/viewer.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation 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 VIEWER_H +#define VIEWER_H + +#include <QImage> +#include <QMainWindow> +#include <QMap> + +class QAction; +class QFrame; +class QGridLayout; +class QLabel; +class QMenu; +class FinalWidget; +class ScreenWidget; + +class Viewer : public QMainWindow +{ + Q_OBJECT +public: + enum Brightness { None, Quarter, Half, ThreeQuarters, Full }; + Viewer(); + +public slots: + void chooseFile(); + void setBrightness(QAction *action); + void createImage(); + void saveImage(); + +private: + void createMenus(); + QFrame *createCentralWidget(); + void openImageFile(QString &filePath); + + FinalWidget *finalWidget; + int brightness; + QAction *currentBrightness; + QAction *saveAction; + QGridLayout *grid; + QImage scaledImage; + QMap <QAction*,Brightness> menuMap; + QMenu *brightnessMenu; + QMenu *fileMenu; + QString path; + ScreenWidget *cyanWidget; + ScreenWidget *magentaWidget; + ScreenWidget *yellowWidget; +}; + +#endif |