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] |
65 | MainWindow::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("Pixel size:" )); |
90 | QSpinBox *pixelSizeSpinBox = new QSpinBox; |
91 | pixelSizeSpinBox->setMinimum(4); |
92 | pixelSizeSpinBox->setMaximum(32); |
93 | pixelSizeSpinBox->setValue(12); |
94 | //! [3] |
95 | |
96 | QMenu * = new QMenu(tr("&File" ), this); |
97 | QAction *openAction = fileMenu->addAction(tr("&Open..." )); |
98 | openAction->setShortcuts(QKeySequence::Open); |
99 | |
100 | printAction = fileMenu->addAction(tr("&Print..." )); |
101 | printAction->setEnabled(false); |
102 | printAction->setShortcut(QKeySequence::Print); |
103 | |
104 | QAction *quitAction = fileMenu->addAction(tr("E&xit" )); |
105 | quitAction->setShortcuts(QKeySequence::Quit); |
106 | |
107 | QMenu * = new QMenu(tr("&Help" ), this); |
108 | QAction *aboutAction = helpMenu->addAction(tr("&About" )); |
109 | |
110 | menuBar()->addMenu(fileMenu); |
111 | menuBar()->addSeparator(); |
112 | menuBar()->addMenu(helpMenu); |
113 | |
114 | connect(openAction, &QAction::triggered, this, &MainWindow::chooseImage); |
115 | connect(printAction, &QAction::triggered, this, &MainWindow::printImage); |
116 | connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); |
117 | connect(aboutAction, &QAction::triggered, this, &MainWindow::showAboutBox); |
118 | //! [4] |
119 | connect(pixelSizeSpinBox, &QSpinBox::valueChanged, |
120 | delegate, &PixelDelegate::setPixelSize); |
121 | connect(pixelSizeSpinBox, &QSpinBox::valueChanged, |
122 | this, &MainWindow::updateView); |
123 | //! [4] |
124 | |
125 | QHBoxLayout *controlsLayout = new QHBoxLayout; |
126 | controlsLayout->addWidget(pixelSizeLabel); |
127 | controlsLayout->addWidget(pixelSizeSpinBox); |
128 | controlsLayout->addStretch(1); |
129 | |
130 | QVBoxLayout *mainLayout = new QVBoxLayout; |
131 | mainLayout->addWidget(view); |
132 | mainLayout->addLayout(controlsLayout); |
133 | centralWidget->setLayout(mainLayout); |
134 | |
135 | setCentralWidget(centralWidget); |
136 | |
137 | setWindowTitle(tr("Pixelator" )); |
138 | resize(640, 480); |
139 | //! [5] |
140 | } |
141 | //! [5] |
142 | |
143 | void MainWindow::chooseImage() |
144 | { |
145 | QString fileName = QFileDialog::getOpenFileName(this, |
146 | tr("Choose an image" ), currentPath, "*" ); |
147 | |
148 | if (!fileName.isEmpty()) |
149 | openImage(fileName); |
150 | } |
151 | |
152 | void MainWindow::openImage(const QString &fileName) |
153 | { |
154 | QImage image; |
155 | |
156 | if (image.load(fileName)) { |
157 | model->setImage(image); |
158 | if (!fileName.startsWith(":/" )) { |
159 | currentPath = fileName; |
160 | setWindowTitle(tr("%1 - Pixelator" ).arg(currentPath)); |
161 | } |
162 | |
163 | printAction->setEnabled(true); |
164 | updateView(); |
165 | } |
166 | } |
167 | |
168 | void MainWindow::printImage() |
169 | { |
170 | #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) |
171 | if (model->rowCount(QModelIndex())*model->columnCount(QModelIndex()) > 90000) { |
172 | QMessageBox::StandardButton answer; |
173 | answer = QMessageBox::question(this, tr("Large Image Size" ), |
174 | tr("The printed image may be very large. Are you sure that " |
175 | "you want to print it?" ), |
176 | 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("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(QModelIndex()); |
194 | int columns = model->columnCount(QModelIndex()); |
195 | int sourceWidth = (columns + 1) * ItemSize; |
196 | int sourceHeight = (rows + 1) * ItemSize; |
197 | |
198 | painter.save(); |
199 | |
200 | double xscale = printer.pageRect(QPrinter::DevicePixel).width() / double(sourceWidth); |
201 | double yscale = printer.pageRect(QPrinter::DevicePixel).height() / double(sourceHeight); |
202 | double scale = qMin(xscale, yscale); |
203 | |
204 | painter.translate(printer.paperRect(QPrinter::DevicePixel).x() + printer.pageRect(QPrinter::DevicePixel).width() / 2, |
205 | printer.paperRect(QPrinter::DevicePixel).y() + printer.pageRect(QPrinter::DevicePixel).height() / 2); |
206 | painter.scale(scale, scale); |
207 | painter.translate(-sourceWidth / 2, -sourceHeight / 2); |
208 | |
209 | QStyleOptionViewItem option; |
210 | QModelIndex parent = QModelIndex(); |
211 | |
212 | QProgressDialog progress(tr("Printing..." ), tr("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, option, |
227 | 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(this, tr("Printing canceled" ), |
239 | tr("The printing process was canceled." ), 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 | |
247 | void MainWindow::showAboutBox() |
248 | { |
249 | QMessageBox::about(this, tr("About the Pixelator example" ), |
250 | tr("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] |
256 | void MainWindow::updateView() |
257 | { |
258 | view->resizeColumnsToContents(); |
259 | view->resizeRowsToContents(); |
260 | } |
261 | //! [6] |
262 | |