Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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);
45 imageLabel->setMinimumSize(labelSize);
46
47 nameLabel = new QLabel(name);
48 colorButton = new QPushButton(tr("Modify..."));
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
63
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
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:
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
132{
133 return &newImage;
134}
135
142{
143 //inverted = invertButton->isOn();
144 inverted = !inverted;
145 createImage();
147}
148
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();
166 }
167}
168
175{
176 originalImage = image;
177 createImage();
178 invertButton->setEnabled(true);
179}
void clicked(bool checked=false)
This signal is emitted when the button is activated (i.e., pressed down then released while the mouse...
static QColor getColor(const QColor &initial=Qt::white, QWidget *parent=nullptr, const QString &title=QString(), ColorDialogOptions options=ColorDialogOptions())
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
int red() const noexcept
Returns the red color component of this color.
Definition qcolor.cpp:1528
int blue() const noexcept
Returns the blue color component of this color.
Definition qcolor.cpp:1583
int green() const noexcept
Returns the green color component of this color.
Definition qcolor.cpp:1555
The QFrame class is the base class of widgets that can have a frame.
Definition qframe.h:17
@ Sunken
Definition qframe.h:51
void setFrameShape(Shape)
Definition qframe.cpp:257
@ StyledPanel
Definition qframe.h:45
void setFrameShadow(Shadow)
Definition qframe.cpp:276
The QGridLayout class lays out widgets in a grid.
Definition qgridlayout.h:21
void addWidget(QWidget *w)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qgridlayout.h:64
\inmodule QtGui
Definition qimage.h:37
void setPixel(int x, int y, uint index_or_rgb)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2588
QRgb pixel(int x, int y) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2493
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
The QLabel widget provides a text or image display.
Definition qlabel.h:20
void setPixmap(const QPixmap &)
Definition qlabel.cpp:339
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
void setColor(ColorGroup cg, ColorRole cr, const QColor &color)
Sets the color in the specified color group, used for the given color role, to the specified solid co...
Definition qpalette.h:146
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition qpixmap.cpp:1437
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setBackgroundRole(QPalette::ColorRole)
Sets the background role of the widget to role.
Definition qwidget.cpp:4391
void setMinimumSize(const QSize &)
Definition qwidget.h:832
void setEnabled(bool)
Definition qwidget.cpp:3358
void setPalette(const QPalette &)
Definition qwidget.cpp:4530
QPalette palette
the widget's palette
Definition qwidget.h:132
int y
the y coordinate of the widget relative to its parent and including any window frame
Definition qwidget.h:110
int x
the x coordinate of the widget relative to its parent including any window frame
Definition qwidget.h:109
QRegion mask() const
Returns the mask currently set on a widget.
void setColor()
Separate the current image into cyan, magenta, and yellow components.
void imageChanged()
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...
QPushButton
[1]
Definition image.cpp:4
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLint GLint GLint GLint GLint x
[0]
GLuint name
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint y
GLfloat GLfloat p
[1]
static constexpr To convert(const std::array< Mapping, N > &mapping, From Mapping::*from, To Mapping::*to, From value, To defaultValue)
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
constexpr int qRed(QRgb rgb)
Definition qrgb.h:18
constexpr int qGreen(QRgb rgb)
Definition qrgb.h:21
constexpr int qBlue(QRgb rgb)
Definition qrgb.h:24
#define tr(X)
#define emit
QGridLayout * gridLayout
[0]