Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
screenwidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4/*
5screenwidget.cpp
6
7A widget to display colour components from an image using independently
8selected colors. Controls are provided to allow the image to be inverted, and
9the color to be selection via a standard dialog. The image is displayed in a
10label widget.
11*/
12
13#include <QApplication>
14#include <QColorDialog>
15#include <QGridLayout>
16#include <QImage>
17#include <QLabel>
18#include <QMenu>
19#include <QMimeData>
20#include <QMouseEvent>
21#include <QPixmap>
22#include <QPushButton>
23#include <QWidget>
24
25#include "screenwidget.h"
26
27/*!
28Initializes the paint color, the mask color (cyan, magenta,
29or yellow), connects the color selector and invert checkbox to functions,
30and creates a two-by-two grid layout.
31*/
32
33ScreenWidget::ScreenWidget(QWidget *parent, QColor initialColor,
34 const QString &name, Separation mask,
35 const QSize &labelSize)
36 : QFrame(parent)
37{
38 paintColor = initialColor;
39 maskColor = mask;
40 inverted = false;
41
42 imageLabel = new QLabel;
43 imageLabel->setFrameShadow(QFrame::Sunken);
44 imageLabel->setFrameShape(QFrame::StyledPanel);
45 imageLabel->setMinimumSize(labelSize);
46
47 nameLabel = new QLabel(name);
48 colorButton = new QPushButton(tr("Modify..."));
49 colorButton->setBackgroundRole(QPalette::Button);
50 colorButton->setMinimumSize(32, 32);
51
52 QPalette palette(colorButton->palette());
53 palette.setColor(QPalette::Button, initialColor);
54 colorButton->setPalette(palette);
55
56 invertButton = new QPushButton(tr("Invert"));
57 //invertButton->setToggleButton(true);
58 //invertButton->setOn(inverted);
59 invertButton->setEnabled(false);
60
61 connect(colorButton, &QPushButton::clicked, this, &ScreenWidget::setColor);
62 connect(invertButton, &QPushButton::clicked, this, &ScreenWidget::invertImage);
63
64 QGridLayout *gridLayout = new QGridLayout(this);
65 gridLayout->addWidget(imageLabel, 0, 0, 1, 2);
66 gridLayout->addWidget(nameLabel, 1, 0);
67 gridLayout->addWidget(colorButton, 1, 1);
68 gridLayout->addWidget(invertButton, 2, 1, 1, 1);
69}
70
71/*!
72 Creates a new image by separating out the cyan, magenta, or yellow
73 component, depending on the mask color specified in the constructor.
74
75 The amount of the component found in each pixel of the image is used
76 to determine how much of a user-selected ink is used for each pixel
77 in the new image for the label widget.
78*/
79
80void ScreenWidget::createImage()
81{
82 newImage = originalImage.copy();
83
84 // Create CMY components for the ink being used.
85 float cyanInk = (255 - paintColor.red())/255.0;
86 float magentaInk = (255 - paintColor.green())/255.0;
87 float yellowInk = (255 - paintColor.blue())/255.0;
88
89 int (*convert)(QRgb);
90
91 switch (maskColor) {
92 case Cyan:
93 convert = qRed;
94 break;
95 case Magenta:
96 convert = qGreen;
97 break;
98 case Yellow:
99 convert = qBlue;
100 break;
101 }
102
103 for (int y = 0; y < newImage.height(); ++y) {
104 for (int x = 0; x < newImage.width(); ++x) {
105 QRgb p(originalImage.pixel(x, y));
106
107 // Separate the source pixel into its cyan component.
108 int amount;
109
110 if (inverted)
111 amount = convert(p);
112 else
113 amount = 255 - convert(p);
114
115 QColor newColor(
116 255 - qMin(int(amount * cyanInk), 255),
117 255 - qMin(int(amount * magentaInk), 255),
118 255 - qMin(int(amount * yellowInk), 255));
119
120 newImage.setPixel(x, y, newColor.rgb());
121 }
122 }
123
124 imageLabel->setPixmap(QPixmap::fromImage(newImage));
125}
126
127/*!
128 Returns a pointer to the modified image.
129*/
130
132{
133 return &newImage;
134}
135
136/*!
137 Sets whether the amount of ink applied to the canvas is to be inverted
138 (subtracted from the maximum value) before the ink is applied.
139*/
140
142{
143 //inverted = invertButton->isOn();
144 inverted = !inverted;
145 createImage();
146 emit imageChanged();
147}
148
149/*!
150 Separate the current image into cyan, magenta, and yellow components.
151 Create a representation of how each component might appear when applied
152 to a blank white piece of paper.
153*/
154
155void ScreenWidget::setColor()
156{
157 QColor newColor = QColorDialog::getColor(paintColor);
158
159 if (newColor.isValid()) {
160 paintColor = newColor;
161 QPalette palette(colorButton->palette());
162 palette.setColor(QPalette::Button, paintColor);
163 colorButton->setPalette(palette);
164 createImage();
165 emit imageChanged();
166 }
167}
168
169/*!
170 Records the original image selected by the user, creates a color
171 separation, and enables the invert image checkbox.
172*/
173
174void ScreenWidget::setImage(QImage &image)
175{
176 originalImage = image;
177 createImage();
178 invertButton->setEnabled(true);
179}
friend class QWidget
Definition qpainter.h:431
void invertImage()
Sets whether the amount of ink applied to the canvas is to be inverted (subtracted from the maximum v...
ScreenWidget(QWidget *parent, QColor initialColor, const QString &name, Separation mask, const QSize &labelSize)
Initializes the paint color, the mask color (cyan, magenta, or yellow), connects the color selector a...
QImage * image()
Returns a pointer to the modified image.
void setImage(QImage &image)
Records the original image selected by the user, creates a color separation, and enables the invert i...