[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | /*
|
---|
| 42 | screenwidget.cpp
|
---|
| 43 |
|
---|
| 44 | A widget to display colour components from an image using independently
|
---|
| 45 | selected colors. Controls are provided to allow the image to be inverted, and
|
---|
| 46 | the color to be selection via a standard dialog. The image is displayed in a
|
---|
| 47 | label widget.
|
---|
| 48 | */
|
---|
| 49 |
|
---|
| 50 | #include <QApplication>
|
---|
| 51 | #include <QColorDialog>
|
---|
| 52 | #include <QGridLayout>
|
---|
| 53 | #include <QImage>
|
---|
| 54 | #include <QLabel>
|
---|
| 55 | #include <QMenu>
|
---|
| 56 | #include <QMimeData>
|
---|
| 57 | #include <QMouseEvent>
|
---|
| 58 | #include <QPixmap>
|
---|
| 59 | #include <QPushButton>
|
---|
| 60 | #include <QWidget>
|
---|
| 61 |
|
---|
| 62 | #include "screenwidget.h"
|
---|
| 63 |
|
---|
| 64 | /*!
|
---|
| 65 | Initializes the paint color, the mask color (cyan, magenta,
|
---|
| 66 | or yellow), connects the color selector and invert checkbox to functions,
|
---|
| 67 | and creates a two-by-two grid layout.
|
---|
| 68 | */
|
---|
| 69 |
|
---|
| 70 | ScreenWidget::ScreenWidget(QWidget *parent, QColor initialColor,
|
---|
| 71 | const QString &name, Separation mask,
|
---|
| 72 | const QSize &labelSize)
|
---|
| 73 | : QFrame(parent)
|
---|
| 74 | {
|
---|
| 75 | paintColor = initialColor;
|
---|
| 76 | maskColor = mask;
|
---|
| 77 | inverted = false;
|
---|
| 78 |
|
---|
| 79 | imageLabel = new QLabel;
|
---|
| 80 | imageLabel->setFrameShadow(QFrame::Sunken);
|
---|
| 81 | imageLabel->setFrameShape(QFrame::StyledPanel);
|
---|
| 82 | imageLabel->setMinimumSize(labelSize);
|
---|
| 83 |
|
---|
| 84 | nameLabel = new QLabel(name);
|
---|
| 85 | colorButton = new QPushButton(tr("Modify..."));
|
---|
| 86 | colorButton->setBackgroundRole(QPalette::Button);
|
---|
| 87 | colorButton->setMinimumSize(32, 32);
|
---|
| 88 |
|
---|
| 89 | QPalette palette(colorButton->palette());
|
---|
| 90 | palette.setColor(QPalette::Button, initialColor);
|
---|
| 91 | colorButton->setPalette(palette);
|
---|
| 92 |
|
---|
| 93 | invertButton = new QPushButton(tr("Invert"));
|
---|
| 94 | //invertButton->setToggleButton(true);
|
---|
| 95 | //invertButton->setOn(inverted);
|
---|
| 96 | invertButton->setEnabled(false);
|
---|
| 97 |
|
---|
| 98 | connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
|
---|
| 99 | connect(invertButton, SIGNAL(clicked()), this, SLOT(invertImage()));
|
---|
| 100 |
|
---|
| 101 | QGridLayout *gridLayout = new QGridLayout;
|
---|
| 102 | gridLayout->addWidget(imageLabel, 0, 0, 1, 2);
|
---|
| 103 | gridLayout->addWidget(nameLabel, 1, 0);
|
---|
| 104 | gridLayout->addWidget(colorButton, 1, 1);
|
---|
| 105 | gridLayout->addWidget(invertButton, 2, 1, 1, 1);
|
---|
| 106 | setLayout(gridLayout);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /*!
|
---|
| 110 | Creates a new image by separating out the cyan, magenta, or yellow
|
---|
| 111 | component, depending on the mask color specified in the constructor.
|
---|
| 112 |
|
---|
| 113 | The amount of the component found in each pixel of the image is used
|
---|
| 114 | to determine how much of a user-selected ink is used for each pixel
|
---|
| 115 | in the new image for the label widget.
|
---|
| 116 | */
|
---|
| 117 |
|
---|
| 118 | void ScreenWidget::createImage()
|
---|
| 119 | {
|
---|
| 120 | newImage = originalImage.copy();
|
---|
| 121 |
|
---|
| 122 | // Create CMY components for the ink being used.
|
---|
| 123 | float cyanInk = (255 - paintColor.red())/255.0;
|
---|
| 124 | float magentaInk = (255 - paintColor.green())/255.0;
|
---|
| 125 | float yellowInk = (255 - paintColor.blue())/255.0;
|
---|
| 126 |
|
---|
| 127 | int (*convert)(QRgb);
|
---|
| 128 |
|
---|
| 129 | switch (maskColor) {
|
---|
| 130 | case Cyan:
|
---|
| 131 | convert = qRed;
|
---|
| 132 | break;
|
---|
| 133 | case Magenta:
|
---|
| 134 | convert = qGreen;
|
---|
| 135 | break;
|
---|
| 136 | case Yellow:
|
---|
| 137 | convert = qBlue;
|
---|
| 138 | break;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | for (int y = 0; y < newImage.height(); ++y) {
|
---|
| 142 | for (int x = 0; x < newImage.width(); ++x) {
|
---|
| 143 | QRgb p(originalImage.pixel(x, y));
|
---|
| 144 |
|
---|
| 145 | // Separate the source pixel into its cyan component.
|
---|
| 146 | int amount;
|
---|
| 147 |
|
---|
| 148 | if (inverted)
|
---|
| 149 | amount = convert(p);
|
---|
| 150 | else
|
---|
| 151 | amount = 255 - convert(p);
|
---|
| 152 |
|
---|
| 153 | QColor newColor(
|
---|
| 154 | 255 - qMin(int(amount * cyanInk), 255),
|
---|
| 155 | 255 - qMin(int(amount * magentaInk), 255),
|
---|
| 156 | 255 - qMin(int(amount * yellowInk), 255));
|
---|
| 157 |
|
---|
| 158 | newImage.setPixel(x, y, newColor.rgb());
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | imageLabel->setPixmap(QPixmap::fromImage(newImage));
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*!
|
---|
| 166 | Returns a pointer to the modified image.
|
---|
| 167 | */
|
---|
| 168 |
|
---|
| 169 | QImage* ScreenWidget::image()
|
---|
| 170 | {
|
---|
| 171 | return &newImage;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /*!
|
---|
| 175 | Sets whether the amount of ink applied to the canvas is to be inverted
|
---|
| 176 | (subtracted from the maximum value) before the ink is applied.
|
---|
| 177 | */
|
---|
| 178 |
|
---|
| 179 | void ScreenWidget::invertImage()
|
---|
| 180 | {
|
---|
| 181 | //inverted = invertButton->isOn();
|
---|
| 182 | inverted = !inverted;
|
---|
| 183 | createImage();
|
---|
| 184 | emit imageChanged();
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /*!
|
---|
| 188 | Separate the current image into cyan, magenta, and yellow components.
|
---|
| 189 | Create a representation of how each component might appear when applied
|
---|
| 190 | to a blank white piece of paper.
|
---|
| 191 | */
|
---|
| 192 |
|
---|
| 193 | void ScreenWidget::setColor()
|
---|
| 194 | {
|
---|
| 195 | QColor newColor = QColorDialog::getColor(paintColor);
|
---|
| 196 |
|
---|
| 197 | if (newColor.isValid()) {
|
---|
| 198 | paintColor = newColor;
|
---|
| 199 | QPalette palette(colorButton->palette());
|
---|
| 200 | palette.setColor(QPalette::Button, paintColor);
|
---|
| 201 | colorButton->setPalette(palette);
|
---|
| 202 | createImage();
|
---|
| 203 | emit imageChanged();
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /*!
|
---|
| 208 | Records the original image selected by the user, creates a color
|
---|
| 209 | separation, and enables the invert image checkbox.
|
---|
| 210 | */
|
---|
| 211 |
|
---|
| 212 | void ScreenWidget::setImage(QImage &image)
|
---|
| 213 | {
|
---|
| 214 | originalImage = image;
|
---|
| 215 | createImage();
|
---|
| 216 | invertButton->setEnabled(true);
|
---|
| 217 | }
|
---|