QtBase  v6.3.1
window.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "window.h"
52 #include <QtWidgets>
53 
56  : QWidget(parent), thread(new RenderThread(this))
57 {
59 
60  label = new QLabel(this);
61  label->setAlignment(Qt::AlignCenter);
62 
63  loadButton = new QPushButton(tr("&Load image..."), this);
64  resetButton = new QPushButton(tr("&Stop"), this);
65  resetButton->setEnabled(false);
66 
67  connect(loadButton, &QPushButton::clicked,
68  this, QOverload<>::of(&Window::loadImage));
69  connect(resetButton, &QPushButton::clicked,
70  thread, &RenderThread::stopProcess);
72  this, &Window::resetUi);
75  this, &Window::addBlock);
77 
78  QHBoxLayout *buttonLayout = new QHBoxLayout;
79  buttonLayout->addStretch();
80  buttonLayout->addWidget(loadButton);
81  buttonLayout->addWidget(resetButton);
82  buttonLayout->addStretch();
83 
84  QVBoxLayout *layout = new QVBoxLayout(this);
85  layout->addWidget(label);
86  layout->addLayout(buttonLayout);
87 
89  setWindowTitle(tr("Queued Custom Type"));
90 }
92 
93 void Window::loadImage()
94 {
95  QStringList formats;
96  const QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
97  for (const QByteArray &format : supportedFormats)
98  if (format.toLower() == format)
99  formats.append(QLatin1String("*.") + QString::fromLatin1(format));
100 
101  QString newPath = QFileDialog::getOpenFileName(this, tr("Open Image"),
102  path, tr("Image files (%1)").arg(formats.join(' ')));
103 
104  if (newPath.isEmpty())
105  return;
106 
107  QImage image(newPath);
108  if (!image.isNull()) {
109  loadImage(image);
110  path = newPath;
111  }
112 }
113 
115 {
116  QImage useImage;
118  if (image.width() > 0.75*space.width() || image.height() > 0.75*space.height())
119  useImage = image.scaled(0.75*space.width(), 0.75*space.height(),
121  else
122  useImage = image;
123 
124  pixmap = QPixmap(useImage.width(), useImage.height());
125  pixmap.fill(qRgb(255, 255, 255));
126  label->setPixmap(pixmap);
127  loadButton->setEnabled(false);
128  resetButton->setEnabled(true);
129  thread->processImage(useImage);
130 }
131 
134 {
135  QColor color = block.color();
136  color.setAlpha(64);
137 
139  painter.begin(&pixmap);
140  painter.fillRect(block.rect(), color);
141  painter.end();
142  label->setPixmap(pixmap);
143 }
145 
146 void Window::resetUi()
147 {
148  loadButton->setEnabled(true);
149  resetButton->setEnabled(false);
150 }
[custom type definition and meta-type declaration]
Definition: block.h:60
void clicked(bool checked=false)
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
void addStretch(int stretch=0)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
friend class QPushButton
[1]
Definition: qdialog.h:57
static QString getOpenFileName(QWidget *parent=nullptr, const QString &caption=QString(), const QString &dir=QString(), const QString &filter=QString(), QString *selectedFilter=nullptr, Options options=Options())
void setWindowTitle(const QString &title)
QGraphicsLayout * layout
The layout of the widget.
QScreen * primaryScreen
the primary (or default) screen of the application.
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:114
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
int width() const
Definition: qimage.cpp:1331
int height() const
Definition: qimage.cpp:1343
static QList< QByteArray > supportedImageFormats()
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
bool begin(QPaintDevice *)
Definition: qpainter.cpp:1709
bool end()
Definition: qpainter.cpp:1877
void fillRect(const QRectF &, const QBrush &)
Definition: qpainter.cpp:6644
void fill(const QColor &fillColor=Qt::white)
Definition: qpixmap.cpp:883
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
constexpr int height() const noexcept
Definition: qrect.h:266
constexpr int width() const noexcept
Definition: qrect.h:263
QRect availableGeometry
the screen's available geometry in pixels
Definition: qscreen.h:82
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString fromLatin1(QByteArrayView ba)
Definition: qstring.cpp:5488
bool isEmpty() const
Definition: qstring.h:1216
The QStringList class provides a list of strings.
void finished(QPrivateSignal)
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setEnabled(bool)
Definition: qwidget.cpp:3368
friend class QPixmap
Definition: qwidget.h:778
void stopProcess()
[processing the image (finish)]
void sendBlock(const Block &block)
void processImage(const QImage &image)
[processing the image (start)]
void loadImage(const QImage &image)
Definition: window.cpp:114
Window()
[0]
Definition: window.cpp:60
void addBlock(const Block &block)
[Adding blocks to the display]
Definition: window.cpp:133
#define this
Definition: dialogs.cpp:56
JOCTET JCOEFPTR block
Definition: jsimd.h:109
@ AlignCenter
Definition: qnamespace.h:188
@ SmoothTransformation
Definition: qnamespace.h:1351
@ KeepAspectRatio
Definition: qnamespace.h:1213
Definition: image.cpp:51
GLuint color
[2]
GLuint GLsizei const GLchar * label
[43]
GLint GLsizei GLsizei GLenum format
GLeglImageOES image
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
constexpr QRgb qRgb(int r, int g, int b)
Definition: qrgb.h:66
SSL_CTX int(*) void arg)
#define tr(X)
QPainter painter(this)
[7]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent