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 "mainwindow.h"
52#include "imagemodel.h"
53#include "pixeldelegate.h"
54
55#include <QtWidgets>
56#if defined(QT_PRINTSUPPORT_LIB)
57#include <QtPrintSupport/qtprintsupportglobal.h>
58#if QT_CONFIG(printdialog)
59#include <QPrinter>
60#include <QPrintDialog>
61#endif
62#endif
63
64//! [0]
65MainWindow::MainWindow()
66{
67//! [0]
68 currentPath = QDir::homePath();
69 model = new ImageModel(this);
70
71 QWidget *centralWidget = new QWidget;
72
73//! [1]
74 view = new QTableView;
75 view->setShowGrid(false);
76 view->horizontalHeader()->hide();
77 view->verticalHeader()->hide();
78 view->horizontalHeader()->setMinimumSectionSize(1);
79 view->verticalHeader()->setMinimumSectionSize(1);
80 view->setModel(model);
81//! [1]
82
83//! [2]
84 PixelDelegate *delegate = new PixelDelegate(this);
85 view->setItemDelegate(delegate);
86//! [2]
87
88//! [3]
89 QLabel *pixelSizeLabel = new QLabel(tr(s: "Pixel size:"));
90 QSpinBox *pixelSizeSpinBox = new QSpinBox;
91 pixelSizeSpinBox->setMinimum(4);
92 pixelSizeSpinBox->setMaximum(32);
93 pixelSizeSpinBox->setValue(12);
94//! [3]
95
96 QMenu *fileMenu = new QMenu(tr(s: "&File"), this);
97 QAction *openAction = fileMenu->addAction(text: tr(s: "&Open..."));
98 openAction->setShortcuts(QKeySequence::Open);
99
100 printAction = fileMenu->addAction(text: tr(s: "&Print..."));
101 printAction->setEnabled(false);
102 printAction->setShortcut(QKeySequence::Print);
103
104 QAction *quitAction = fileMenu->addAction(text: tr(s: "E&xit"));
105 quitAction->setShortcuts(QKeySequence::Quit);
106
107 QMenu *helpMenu = new QMenu(tr(s: "&Help"), this);
108 QAction *aboutAction = helpMenu->addAction(text: tr(s: "&About"));
109
110 menuBar()->addMenu(menu: fileMenu);
111 menuBar()->addSeparator();
112 menuBar()->addMenu(menu: helpMenu);
113
114 connect(sender: openAction, signal: &QAction::triggered, receiver: this, slot: &MainWindow::chooseImage);
115 connect(sender: printAction, signal: &QAction::triggered, receiver: this, slot: &MainWindow::printImage);
116 connect(sender: quitAction, signal: &QAction::triggered, qApp, slot: &QCoreApplication::quit);
117 connect(sender: aboutAction, signal: &QAction::triggered, receiver: this, slot: &MainWindow::showAboutBox);
118//! [4]
119 connect(sender: pixelSizeSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged),
120 receiver: delegate, slot: &PixelDelegate::setPixelSize);
121 connect(sender: pixelSizeSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged),
122 receiver: this, slot: &MainWindow::updateView);
123//! [4]
124
125 QHBoxLayout *controlsLayout = new QHBoxLayout;
126 controlsLayout->addWidget(pixelSizeLabel);
127 controlsLayout->addWidget(pixelSizeSpinBox);
128 controlsLayout->addStretch(stretch: 1);
129
130 QVBoxLayout *mainLayout = new QVBoxLayout;
131 mainLayout->addWidget(view);
132 mainLayout->addLayout(layout: controlsLayout);
133 centralWidget->setLayout(mainLayout);
134
135 setCentralWidget(centralWidget);
136
137 setWindowTitle(tr(s: "Pixelator"));
138 resize(w: 640, h: 480);
139//! [5]
140}
141//! [5]
142
143void MainWindow::chooseImage()
144{
145 QString fileName = QFileDialog::getOpenFileName(parent: this,
146 caption: tr(s: "Choose an image"), dir: currentPath, filter: "*");
147
148 if (!fileName.isEmpty())
149 openImage(fileName);
150}
151
152void MainWindow::openImage(const QString &fileName)
153{
154 QImage image;
155
156 if (image.load(fileName)) {
157 model->setImage(image);
158 if (!fileName.startsWith(s: ":/")) {
159 currentPath = fileName;
160 setWindowTitle(tr(s: "%1 - Pixelator").arg(a: currentPath));
161 }
162
163 printAction->setEnabled(true);
164 updateView();
165 }
166}
167
168void MainWindow::printImage()
169{
170#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
171 if (model->rowCount(parent: QModelIndex())*model->columnCount(parent: QModelIndex()) > 90000) {
172 QMessageBox::StandardButton answer;
173 answer = QMessageBox::question(parent: this, title: tr(s: "Large Image Size"),
174 text: tr(s: "The printed image may be very large. Are you sure that "
175 "you want to print it?"),
176 buttons: QMessageBox::Yes | QMessageBox::No);
177 if (answer == QMessageBox::No)
178 return;
179 }
180
181 QPrinter printer(QPrinter::HighResolution);
182
183 QPrintDialog dlg(&printer, this);
184 dlg.setWindowTitle(tr(s: "Print Image"));
185
186 if (dlg.exec() != QDialog::Accepted) {
187 return;
188 }
189
190 QPainter painter;
191 painter.begin(&printer);
192
193 int rows = model->rowCount(parent: QModelIndex());
194 int columns = model->columnCount(parent: QModelIndex());
195 int sourceWidth = (columns + 1) * ItemSize;
196 int sourceHeight = (rows + 1) * ItemSize;
197
198 painter.save();
199
200 double xscale = printer.pageRect().width() / double(sourceWidth);
201 double yscale = printer.pageRect().height() / double(sourceHeight);
202 double scale = qMin(a: xscale, b: yscale);
203
204 painter.translate(dx: printer.paperRect().x() + printer.pageRect().width() / 2,
205 dy: printer.paperRect().y() + printer.pageRect().height() / 2);
206 painter.scale(sx: scale, sy: scale);
207 painter.translate(dx: -sourceWidth / 2, dy: -sourceHeight / 2);
208
209 QStyleOptionViewItem option;
210 QModelIndex parent = QModelIndex();
211
212 QProgressDialog progress(tr(s: "Printing..."), tr(s: "Cancel"), 0, rows, this);
213 progress.setWindowModality(Qt::ApplicationModal);
214 float y = ItemSize / 2;
215
216 for (int row = 0; row < rows; ++row) {
217 progress.setValue(row);
218 qApp->processEvents();
219 if (progress.wasCanceled())
220 break;
221
222 float x = ItemSize / 2;
223
224 for (int column = 0; column < columns; ++column) {
225 option.rect = QRect(int(x), int(y), ItemSize, ItemSize);
226 view->itemDelegate()->paint(painter: &painter, option,
227 index: model->index(row, column, parent));
228 x = x + ItemSize;
229 }
230 y = y + ItemSize;
231 }
232 progress.setValue(rows);
233
234 painter.restore();
235 painter.end();
236
237 if (progress.wasCanceled()) {
238 QMessageBox::information(parent: this, title: tr(s: "Printing canceled"),
239 text: tr(s: "The printing process was canceled."), button0: QMessageBox::Cancel);
240 }
241#else
242 QMessageBox::information(this, tr("Printing canceled"),
243 tr("Printing is not supported on this Qt build"), QMessageBox::Cancel);
244#endif
245}
246
247void MainWindow::showAboutBox()
248{
249 QMessageBox::about(parent: this, title: tr(s: "About the Pixelator example"),
250 text: tr(s: "This example demonstrates how a standard view and a custom\n"
251 "delegate can be used to produce a specialized representation\n"
252 "of data in a simple custom model."));
253}
254
255//! [6]
256void MainWindow::updateView()
257{
258 view->resizeColumnsToContents();
259 view->resizeRowsToContents();
260}
261//! [6]
262

source code of qtbase/examples/widgets/itemviews/pixelator/mainwindow.cpp