1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*
|
---|
43 | screenwidget.cpp
|
---|
44 |
|
---|
45 | A widget to display colour components from an image using independently
|
---|
46 | selected colors. Controls are provided to allow the image to be inverted, and
|
---|
47 | the color to be selection via a standard dialog. The image is displayed in a
|
---|
48 | label widget.
|
---|
49 | */
|
---|
50 |
|
---|
51 | #include <QApplication>
|
---|
52 | #include <QColorDialog>
|
---|
53 | #include <QGridLayout>
|
---|
54 | #include <QImage>
|
---|
55 | #include <QLabel>
|
---|
56 | #include <QMenu>
|
---|
57 | #include <QMimeData>
|
---|
58 | #include <QMouseEvent>
|
---|
59 | #include <QPixmap>
|
---|
60 | #include <QPushButton>
|
---|
61 | #include <QWidget>
|
---|
62 |
|
---|
63 | #include "screenwidget.h"
|
---|
64 |
|
---|
65 | /*!
|
---|
66 | Initializes the paint color, the mask color (cyan, magenta,
|
---|
67 | or yellow), connects the color selector and invert checkbox to functions,
|
---|
68 | and creates a two-by-two grid layout.
|
---|
69 | */
|
---|
70 |
|
---|
71 | ScreenWidget::ScreenWidget(QWidget *parent, QColor initialColor,
|
---|
72 | const QString &name, Separation mask,
|
---|
73 | const QSize &labelSize)
|
---|
74 | : QFrame(parent)
|
---|
75 | {
|
---|
76 | paintColor = initialColor;
|
---|
77 | maskColor = mask;
|
---|
78 | inverted = false;
|
---|
79 |
|
---|
80 | imageLabel = new QLabel;
|
---|
81 | imageLabel->setFrameShadow(QFrame::Sunken);
|
---|
82 | imageLabel->setFrameShape(QFrame::StyledPanel);
|
---|
83 | imageLabel->setMinimumSize(labelSize);
|
---|
84 |
|
---|
85 | nameLabel = new QLabel(name);
|
---|
86 | colorButton = new QPushButton(tr("Modify..."));
|
---|
87 | colorButton->setBackgroundRole(QPalette::Button);
|
---|
88 | colorButton->setMinimumSize(32, 32);
|
---|
89 |
|
---|
90 | QPalette palette(colorButton->palette());
|
---|
91 | palette.setColor(QPalette::Button, initialColor);
|
---|
92 | colorButton->setPalette(palette);
|
---|
93 |
|
---|
94 | invertButton = new QPushButton(tr("Invert"));
|
---|
95 | //invertButton->setToggleButton(true);
|
---|
96 | //invertButton->setOn(inverted);
|
---|
97 | invertButton->setEnabled(false);
|
---|
98 |
|
---|
99 | connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
|
---|
100 | connect(invertButton, SIGNAL(clicked()), this, SLOT(invertImage()));
|
---|
101 |
|
---|
102 | QGridLayout *gridLayout = new QGridLayout;
|
---|
103 | gridLayout->addWidget(imageLabel, 0, 0, 1, 2);
|
---|
104 | gridLayout->addWidget(nameLabel, 1, 0);
|
---|
105 | gridLayout->addWidget(colorButton, 1, 1);
|
---|
106 | gridLayout->addWidget(invertButton, 2, 1, 1, 1);
|
---|
107 | setLayout(gridLayout);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*!
|
---|
111 | Creates a new image by separating out the cyan, magenta, or yellow
|
---|
112 | component, depending on the mask color specified in the constructor.
|
---|
113 |
|
---|
114 | The amount of the component found in each pixel of the image is used
|
---|
115 | to determine how much of a user-selected ink is used for each pixel
|
---|
116 | in the new image for the label widget.
|
---|
117 | */
|
---|
118 |
|
---|
119 | void ScreenWidget::createImage()
|
---|
120 | {
|
---|
121 | newImage = originalImage.copy();
|
---|
122 |
|
---|
123 | // Create CMY components for the ink being used.
|
---|
124 | float cyanInk = (255 - paintColor.red())/255.0;
|
---|
125 | float magentaInk = (255 - paintColor.green())/255.0;
|
---|
126 | float yellowInk = (255 - paintColor.blue())/255.0;
|
---|
127 |
|
---|
128 | int (*convert)(QRgb);
|
---|
129 |
|
---|
130 | switch (maskColor) {
|
---|
131 | case Cyan:
|
---|
132 | convert = qRed;
|
---|
133 | break;
|
---|
134 | case Magenta:
|
---|
135 | convert = qGreen;
|
---|
136 | break;
|
---|
137 | case Yellow:
|
---|
138 | convert = qBlue;
|
---|
139 | break;
|
---|
140 | }
|
---|
141 |
|
---|
142 | for (int y = 0; y < newImage.height(); ++y) {
|
---|
143 | for (int x = 0; x < newImage.width(); ++x) {
|
---|
144 | QRgb p(originalImage.pixel(x, y));
|
---|
145 |
|
---|
146 | // Separate the source pixel into its cyan component.
|
---|
147 | int amount;
|
---|
148 |
|
---|
149 | if (inverted)
|
---|
150 | amount = convert(p);
|
---|
151 | else
|
---|
152 | amount = 255 - convert(p);
|
---|
153 |
|
---|
154 | QColor newColor(
|
---|
155 | 255 - qMin(int(amount * cyanInk), 255),
|
---|
156 | 255 - qMin(int(amount * magentaInk), 255),
|
---|
157 | 255 - qMin(int(amount * yellowInk), 255));
|
---|
158 |
|
---|
159 | newImage.setPixel(x, y, newColor.rgb());
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | imageLabel->setPixmap(QPixmap::fromImage(newImage));
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*!
|
---|
167 | Returns a pointer to the modified image.
|
---|
168 | */
|
---|
169 |
|
---|
170 | QImage* ScreenWidget::image()
|
---|
171 | {
|
---|
172 | return &newImage;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*!
|
---|
176 | Sets whether the amount of ink applied to the canvas is to be inverted
|
---|
177 | (subtracted from the maximum value) before the ink is applied.
|
---|
178 | */
|
---|
179 |
|
---|
180 | void ScreenWidget::invertImage()
|
---|
181 | {
|
---|
182 | //inverted = invertButton->isOn();
|
---|
183 | inverted = !inverted;
|
---|
184 | createImage();
|
---|
185 | emit imageChanged();
|
---|
186 | }
|
---|
187 |
|
---|
188 | /*!
|
---|
189 | Separate the current image into cyan, magenta, and yellow components.
|
---|
190 | Create a representation of how each component might appear when applied
|
---|
191 | to a blank white piece of paper.
|
---|
192 | */
|
---|
193 |
|
---|
194 | void ScreenWidget::setColor()
|
---|
195 | {
|
---|
196 | QColor newColor = QColorDialog::getColor(paintColor);
|
---|
197 |
|
---|
198 | if (newColor.isValid()) {
|
---|
199 | paintColor = newColor;
|
---|
200 | QPalette palette(colorButton->palette());
|
---|
201 | palette.setColor(QPalette::Button, paintColor);
|
---|
202 | colorButton->setPalette(palette);
|
---|
203 | createImage();
|
---|
204 | emit imageChanged();
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*!
|
---|
209 | Records the original image selected by the user, creates a color
|
---|
210 | separation, and enables the invert image checkbox.
|
---|
211 | */
|
---|
212 |
|
---|
213 | void ScreenWidget::setImage(QImage &image)
|
---|
214 | {
|
---|
215 | originalImage = image;
|
---|
216 | createImage();
|
---|
217 | invertButton->setEnabled(true);
|
---|
218 | }
|
---|