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
54//! [Window constructor start]
55Window::Window(QWidget *parent)
56 : QWidget(parent), thread(new RenderThread(this))
57{
58//! [Window constructor start] //! [set up widgets and connections]
59
60 label = new QLabel(this);
61 label->setAlignment(Qt::AlignCenter);
62
63 loadButton = new QPushButton(tr(s: "&Load image..."), this);
64 resetButton = new QPushButton(tr(s: "&Stop"), this);
65 resetButton->setEnabled(false);
66
67 connect(sender: loadButton, signal: &QPushButton::clicked,
68 receiver: this, slot: QOverload<>::of(ptr: &Window::loadImage));
69 connect(sender: resetButton, signal: &QPushButton::clicked,
70 receiver: thread, slot: &RenderThread::stopProcess);
71 connect(sender: thread, signal: &RenderThread::finished,
72 receiver: this, slot: &Window::resetUi);
73//! [set up widgets and connections] //! [connecting signal with custom type]
74 connect(sender: thread, signal: &RenderThread::sendBlock,
75 receiver: this, slot: &Window::addBlock);
76//! [connecting signal with custom type]
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(layout: buttonLayout);
87
88//! [Window constructor finish]
89 setWindowTitle(tr(s: "Queued Custom Type"));
90}
91//! [Window constructor finish]
92
93void 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(t: QLatin1String("*.") + QString::fromLatin1(str: format));
100
101 QString newPath = QFileDialog::getOpenFileName(parent: this, caption: tr(s: "Open Image"),
102 dir: path, filter: tr(s: "Image files (%1)").arg(a: formats.join(sep: ' ')));
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
114void Window::loadImage(const QImage &image)
115{
116 QImage useImage;
117 QRect space = QGuiApplication::primaryScreen()->availableGeometry();
118 if (image.width() > 0.75*space.width() || image.height() > 0.75*space.height())
119 useImage = image.scaled(w: 0.75*space.width(), h: 0.75*space.height(),
120 aspectMode: Qt::KeepAspectRatio, mode: Qt::SmoothTransformation);
121 else
122 useImage = image;
123
124 pixmap = QPixmap(useImage.width(), useImage.height());
125 pixmap.fill(fillColor: qRgb(r: 255, g: 255, b: 255));
126 label->setPixmap(pixmap);
127 loadButton->setEnabled(false);
128 resetButton->setEnabled(true);
129 thread->processImage(image: useImage);
130}
131
132//! [Adding blocks to the display]
133void Window::addBlock(const Block &block)
134{
135 QColor color = block.color();
136 color.setAlpha(64);
137
138 QPainter painter;
139 painter.begin(&pixmap);
140 painter.fillRect(block.rect(), color);
141 painter.end();
142 label->setPixmap(pixmap);
143}
144//! [Adding blocks to the display]
145
146void Window::resetUi()
147{
148 loadButton->setEnabled(true);
149 resetButton->setEnabled(false);
150}
151

source code of qtbase/examples/corelib/threads/queuedcustomtype/window.cpp