comparison mupdf-source/thirdparty/zint/frontend_qt/scalewindow.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 Zint Barcode Generator - the open source barcode generator
3 Copyright (C) 2022-2024 Robin Stuart <rstuart114@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /* SPDX-License-Identifier: GPL-3.0-or-later */
20
21 //#include <QDebug>
22 #include <QSettings>
23 #include <QUiLoader>
24
25 #include <math.h>
26 #include "scalewindow.h"
27
28 // Shorthand
29 #define QSL QStringLiteral
30
31 /* "Standard" dpmm/dpi equivalents */
32 static int resolution_standard(int inch, int val)
33 {
34 /* Note toss-up between rounding 2400 dpi to 94 or 95 (~94.48818897637795275591) */
35 static int standards[9][2] = {
36 { 4, 100 }, { 6, 150 }, { 8, 200 }, { 12, 300 }, { 16, 400 },
37 { 24, 600 }, { 47, 1200 }, { 94, 2400 }, { 189, 4800 },
38 };
39 for (int i = 0; i < 8; i++) {
40 if (standards[i][inch] == val) {
41 return standards[i][inch ? 0 : 1];
42 }
43 }
44 return 0;
45 }
46
47 ScaleWindow::ScaleWindow(BarcodeItem *bc, Zint::QZintXdimDpVars *vars, double originalScale)
48 : m_bc(bc), Valid(false), m_vars(*vars), m_originalScale(originalScale), m_unset(false)
49 {
50 setupUi(this);
51
52 if (m_bc->bc.noPng()) {
53 cmbFileType->setItemText(0, QSL("Raster (BMP/GIF/PCX/TIF)"));
54 }
55 if (m_bc->bc.symbol() != BARCODE_MAXICODE) {
56 cmbFileType->setItemText(1, QSL("Vector (EMF/EPS/SVG)"));
57 cmbFileType->removeItem(2); // EMF
58 }
59
60 QSettings settings;
61 #if QT_VERSION < 0x60000
62 settings.setIniCodec("UTF-8");
63 #endif
64
65 QByteArray geometry = settings.value(QSL("studio/scale/window_geometry")).toByteArray();
66 restoreGeometry(geometry);
67
68 spnXdim->setValue(m_vars.x_dim);
69 cmbXdimUnits->setCurrentIndex(m_vars.x_dim_units);
70 spnResolution->setValue(m_vars.resolution);
71 cmbResolutionUnits->setCurrentIndex(m_vars.resolution_units);
72
73 if (m_bc->bc.symbol() == BARCODE_MAXICODE) {
74 cmbFileType->setCurrentIndex(std::min(m_vars.filetype_maxicode, cmbFileType->count() - 1));
75 } else {
76 cmbFileType->setCurrentIndex(std::min(m_vars.filetype, cmbFileType->count() - 1));
77 }
78
79 if (cmbXdimUnits->currentIndex() == 1) { // Inches
80 spnXdim->setSingleStep(0.001);
81 } else {
82 spnXdim->setSingleStep(0.01);
83 }
84 if (cmbResolutionUnits->currentIndex() == 1) { // Inches
85 spnResolution->setSingleStep(50);
86 } else {
87 spnResolution->setSingleStep(1);
88 }
89 set_maxima();
90
91 size_msg_ui_set();
92
93 QIcon closeIcon(QIcon::fromTheme(QSL("window-close"), QIcon(QSL(":res/x.svg"))));
94 QIcon unsetIcon(QSL(":res/delete.svg"));
95 QIcon okIcon(QIcon(QSL(":res/check.svg")));
96 btnCancel->setIcon(closeIcon);
97 btnScaleUnset->setIcon(unsetIcon);
98 btnScaleUnset->setEnabled(m_vars.set);
99 btnOK->setIcon(okIcon);
100
101 connect(btnCancel, SIGNAL(clicked(bool)), SLOT(close()));
102 connect(btnScaleUnset, SIGNAL(clicked(bool)), SLOT(unset_scale()));
103 connect(btnOK, SIGNAL(clicked(bool)), SLOT(okay()));
104 connect(spnXdim, SIGNAL(valueChanged(double)), SLOT(update_scale()));
105 connect(cmbXdimUnits, SIGNAL(currentIndexChanged(int)), SLOT(x_dim_units_change()));
106 connect(btnXdimDefault, SIGNAL(clicked(bool)), SLOT(x_dim_default()));
107 connect(spnResolution, SIGNAL(valueChanged(int)), SLOT(update_scale()));
108 connect(cmbResolutionUnits, SIGNAL(currentIndexChanged(int)), SLOT(resolution_units_change()));
109 connect(btnResolutionDefault, SIGNAL(clicked(bool)), SLOT(resolution_default()));
110 connect(cmbFileType, SIGNAL(currentIndexChanged(int)), SLOT(update_scale()));
111 }
112
113 ScaleWindow::~ScaleWindow()
114 {
115 QSettings settings;
116 #if QT_VERSION < 0x60000
117 settings.setIniCodec("UTF-8");
118 #endif
119 settings.setValue(QSL("studio/scale/window_geometry"), saveGeometry());
120 }
121
122 void ScaleWindow::size_msg_ui_set()
123 {
124 msgPrintingScale->setText(QString::asprintf("%.2f", m_bc->bc.scale()));
125 float width_x_dim, height_x_dim;
126 if (m_bc->bc.getWidthHeightXdim((float) m_vars.x_dim, width_x_dim, height_x_dim)) {
127 const char *fmt = cmbXdimUnits->currentIndex() == 1 ? "%.3f x %.3f in" : "%.2f x %.2f mm";
128 msgWidthHeight->setText(QString::asprintf(fmt, width_x_dim, height_x_dim));
129 } else {
130 msgWidthHeight->clear();
131 }
132
133 bool defaultDisable;
134 QWidget *focus = QApplication::focusWidget();
135
136 // This doesn't work well due to rounding errors TODO: fix
137 defaultDisable = m_bc->bc.defaultXdim() == (float) get_x_dim_mm();
138 btnXdimDefault->setEnabled(!defaultDisable);
139 if (focus == btnXdimDefault && defaultDisable) {
140 spnXdim->setFocus();
141 }
142
143 defaultDisable = (cmbResolutionUnits->currentIndex() == 0 && spnResolution->value() == 12)
144 || (cmbResolutionUnits->currentIndex() == 1 && spnResolution->value() == 300);
145 btnResolutionDefault->setEnabled(!defaultDisable);
146 if (focus == btnResolutionDefault && defaultDisable) {
147 spnResolution->setFocus();
148 }
149 }
150
151 void ScaleWindow::unset_scale()
152 {
153 m_vars.x_dim = std::min(m_bc->bc.getXdimDpFromScale(m_originalScale, get_dpmm(), getFileType()), 10.0f);
154 m_vars.set = 0;
155
156 if (cmbXdimUnits->currentIndex() == 1) { // Inches
157 spnXdim->setValue(m_vars.x_dim / 25.4);
158 } else {
159 spnXdim->setValue(m_vars.x_dim);
160 }
161 m_unset = true;
162 btnScaleUnset->setEnabled(false);
163 }
164
165 void ScaleWindow::okay()
166 {
167 if (update_vars()) {
168 Valid = true;
169 m_vars.set = m_unset ? 0 : 1;
170 }
171 close();
172 }
173
174 void ScaleWindow::update_scale()
175 {
176 float scale = update_vars();
177 if (scale) {
178 // Need up-to-date `vectorWidth()` and `vectorHeight()` to estimate size including borders, whitespace & text,
179 // so tell main window to encode and it will update UI here via `size_msg_ui_set()`
180 emit scaleChanged(scale);
181 m_unset = false;
182 btnScaleUnset->setEnabled(true);
183 set_maxima();
184 }
185 }
186
187 void ScaleWindow::x_dim_units_change()
188 {
189 if (cmbXdimUnits->currentIndex() == 1) { // Inches
190 spnXdim->setValue(spnXdim->value() / 25.4);
191 spnXdim->setSingleStep(0.001);
192 } else {
193 spnXdim->setValue(spnXdim->value() * 25.4);
194 spnXdim->setSingleStep(0.01);
195 }
196 update_scale();
197 }
198
199 void ScaleWindow::x_dim_default()
200 {
201 if (cmbXdimUnits->currentIndex() == 1) { // Inches
202 spnXdim->setValue(m_bc->bc.defaultXdim() / 25.4);
203 } else {
204 spnXdim->setValue(m_bc->bc.defaultXdim());
205 }
206 update_scale();
207 }
208
209 void ScaleWindow::resolution_units_change()
210 {
211 int value;
212 if (cmbResolutionUnits->currentIndex() == 1) { // Inches
213 if ((value = resolution_standard(0, spnResolution->value()))) {
214 spnResolution->setValue(value);
215 } else {
216 spnResolution->setValue((int) roundf(spnResolution->value() * 25.4f));
217 }
218 spnResolution->setSingleStep(50);
219 } else {
220 if ((value = resolution_standard(1, spnResolution->value()))) {
221 spnResolution->setValue(value);
222 } else {
223 spnResolution->setValue((int) roundf(spnResolution->value() / 25.4f));
224 }
225 spnResolution->setSingleStep(1);
226 }
227 update_scale();
228 }
229
230 void ScaleWindow::resolution_default()
231 {
232 spnResolution->setValue(cmbResolutionUnits->currentIndex() == 1 ? 300 : 12);
233 update_scale();
234 }
235
236 float ScaleWindow::get_x_dim_mm() const
237 {
238 return (float) (cmbXdimUnits->currentIndex() == 1 ? spnXdim->value() * 25.4 : spnXdim->value());
239
240 }
241
242 float ScaleWindow::get_dpmm() const
243 {
244 return (float) (cmbResolutionUnits->currentIndex() == 1 ? spnResolution->value() / 25.4 : spnResolution->value());
245 }
246
247 const char *ScaleWindow::getFileType() const
248 {
249 static const char *filetypes[3] = { "GIF", "SVG", "EMF" };
250 return filetypes[std::max(std::min(cmbFileType->currentIndex(), 2), 0)];
251 }
252
253 void ScaleWindow::set_maxima()
254 {
255 float maxXdim = std::min(m_bc->bc.getXdimDpFromScale(200.0f, get_dpmm(), getFileType()), 10.0f);
256 if (cmbXdimUnits->currentIndex() == 1) { // Inches
257 spnXdim->setMaximum(maxXdim / 25.4);
258 } else {
259 spnXdim->setMaximum(maxXdim);
260 }
261 float maxRes = m_bc->bc.getXdimDpFromScale(200.0f, get_x_dim_mm(), getFileType());
262 if (cmbResolutionUnits->currentIndex() == 1) { // Inches
263 spnResolution->setMaximum(maxRes * 25.4);
264 } else {
265 spnResolution->setMaximum(maxRes);
266 }
267 }
268
269 double ScaleWindow::update_vars()
270 {
271 double scale = (double) m_bc->bc.getScaleFromXdimDp(get_x_dim_mm(), get_dpmm(), getFileType());
272 if (scale != 0.0) {
273 m_vars.x_dim = spnXdim->value();
274 m_vars.x_dim_units = cmbXdimUnits->currentIndex();
275 m_vars.resolution = spnResolution->value();
276 m_vars.resolution_units = cmbResolutionUnits->currentIndex();
277 if (m_bc->bc.symbol() == BARCODE_MAXICODE) {
278 m_vars.filetype_maxicode = cmbFileType->currentIndex();
279 } else {
280 m_vars.filetype = cmbFileType->currentIndex();
281 }
282 }
283 return scale;
284 }
285
286 /* vim: set ts=4 sw=4 et : */