comparison mupdf-source/thirdparty/zint/frontend_qt/exportwindow.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) 2009-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 <QFileDialog>
23 #include <QMessageBox>
24 #include <QSettings>
25 #include <QStringBuilder>
26 #include <QUiLoader>
27
28 #include "exportwindow.h"
29
30 // Shorthand
31 #define QSL QStringLiteral
32 #define QSEmpty QLatin1String("")
33
34 ExportWindow::ExportWindow(BarcodeItem *bc, const QString& output_data)
35 : m_bc(bc), m_output_data(output_data), m_lines(0)
36 {
37 setupUi(this);
38
39 if (m_bc->bc.noPng()) {
40 cmbFileType->removeItem(0); // PNG
41 }
42
43 QSettings settings;
44 #if QT_VERSION < 0x60000
45 settings.setIniCodec("UTF-8");
46 #endif
47
48 QByteArray geometry = settings.value(QSL("studio/export/window_geometry")).toByteArray();
49 restoreGeometry(geometry);
50
51 linDestPath->setText(settings.value(QSL("studio/export/destination"),
52 QDir::toNativeSeparators(QDir::homePath())).toString());
53 linPrefix->setText(settings.value(QSL("studio/export/file_prefix"), QSL("bcs_")).toString());
54 linPostfix->setText(settings.value(QSL("studio/export/file_postfix"), QSEmpty).toString());
55 cmbFileName->setCurrentIndex(settings.value(QSL("studio/export/name_format"), 0).toInt());
56 cmbFileType->setCurrentIndex(std::min(settings.value(QSL("studio/export/filetype"), 0).toInt(),
57 cmbFileType->count() - 1));
58
59 QIcon closeIcon(QIcon::fromTheme(QSL("window-close"), QIcon(QSL(":res/x.svg"))));
60 btnCancel->setIcon(closeIcon);
61 QIcon folderIcon(QIcon::fromTheme(QSL("folder"), QIcon(QSL(":res/folder.svg"))));
62 btnDestPath->setIcon(folderIcon);
63
64 connect(btnCancel, SIGNAL(clicked(bool)), SLOT(close()));
65 connect(btnOK, SIGNAL(clicked(bool)), SLOT(process()));
66 connect(btnDestPath, SIGNAL(clicked(bool)), SLOT(get_directory()));
67
68 m_dataList = m_output_data.split('\n');
69 m_lines = m_dataList.size();
70 if (m_lines && m_dataList[m_lines - 1].isEmpty()) {
71 m_lines--;
72 }
73 /*: %1 is number of sequences */
74 lblFeedback->setText(tr("Export Results (%1):").arg(m_lines));
75 }
76
77 ExportWindow::~ExportWindow()
78 {
79 QSettings settings;
80 #if QT_VERSION < 0x60000
81 settings.setIniCodec("UTF-8");
82 #endif
83 settings.setValue(QSL("studio/export/window_geometry"), saveGeometry());
84
85 settings.setValue(QSL("studio/export/destination"), linDestPath->text());
86 settings.setValue(QSL("studio/export/file_prefix"), linPrefix->text());
87 settings.setValue(QSL("studio/export/file_postfix"), linPostfix->text());
88 settings.setValue(QSL("studio/export/name_format"), cmbFileName->currentIndex());
89 settings.setValue(QSL("studio/export/filetype"), cmbFileType->currentIndex());
90 }
91
92 void ExportWindow::get_directory()
93 {
94 QSettings settings;
95 #if QT_VERSION < 0x60000
96 settings.setIniCodec("UTF-8");
97 #endif
98 QString directory;
99 QFileDialog fdialog;
100
101 fdialog.setFileMode(QFileDialog::Directory);
102 fdialog.setDirectory(settings.value(QSL("studio/default_dir"),
103 QDir::toNativeSeparators(QDir::homePath())).toString());
104
105 if (fdialog.exec()) {
106 directory = fdialog.selectedFiles().at(0);
107 } else {
108 return;
109 }
110
111 linDestPath->setText(QDir::toNativeSeparators(directory));
112 settings.setValue(QSL("studio/default_dir"), directory);
113 }
114
115 void ExportWindow::process()
116 {
117 const QRegularExpression urlRE(QSL("[\\/:*?\"<>|%]"));
118
119 txtFeedback->setPlainText(tr("Processing..."));
120 txtFeedback->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
121 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
122
123 QString biggest;
124 bool needUrlEscape = false;
125
126 const int fileNameIdx = cmbFileName->currentIndex();
127 if (fileNameIdx == 1) {
128 biggest = QString::number(m_lines + 1);
129 } else {
130 needUrlEscape = m_output_data.contains(urlRE);
131 }
132
133 QString suffix;
134 int suffixIdx = cmbFileType->currentText().lastIndexOf(QSL("(*."));
135 if (suffixIdx == -1) {
136 suffix = m_bc->bc.noPng() ? QSL(".gif") : QSL(".png");
137 } else {
138 suffix = cmbFileType->currentText().mid(suffixIdx + 2, 4);
139 }
140
141 QString filePathPrefix = linDestPath->text() % QDir::separator() % linPrefix->text();
142 QString postfix = linPostfix->text();
143
144 QStringList Feedback;
145 int successCount = 0, errorCount = 0;
146 for (int i = 0; i < m_lines; i++) {
147 const QString &dataString = m_dataList[i];
148 QString fileName;
149 switch (fileNameIdx) {
150 case 0: /* Same as Data (URL Escaped) */
151 if (needUrlEscape) {
152 QString url_escaped;
153
154 for (int m = 0; m < dataString.length(); m++) {
155 QChar name_qchar = dataString[m];
156 char name_char = name_qchar.toLatin1();
157
158 switch (name_char) {
159 case '\\': url_escaped += QSL("%5C"); break;
160 case '/': url_escaped += QSL("%2F"); break;
161 case ':': url_escaped += QSL("%3A"); break;
162 case '*': url_escaped += QSL("%2A"); break;
163 case '?': url_escaped += QSL("%3F"); break;
164 case '"': url_escaped += QSL("%22"); break;
165 case '<': url_escaped += QSL("%3C"); break;
166 case '>': url_escaped += QSL("%3E"); break;
167 case '|': url_escaped += QSL("%7C"); break;
168 case '%': url_escaped += QSL("%25"); break;
169 default: url_escaped += name_qchar; break;
170 }
171 }
172 fileName = filePathPrefix % url_escaped % postfix % suffix;
173 } else {
174 fileName = filePathPrefix % dataString % postfix % suffix;
175 }
176 break;
177 case 1: { /* Formatted Serial Number */
178 QString this_val, pad;
179
180 this_val = QString::number(i + 1);
181
182 pad.fill('0', biggest.length() - this_val.length());
183
184 fileName = filePathPrefix % pad % this_val % postfix % suffix;
185 }
186 break;
187 }
188 m_bc->bc.setText(dataString);
189 m_bc->bc.save_to_file(fileName);
190 if (m_bc->bc.hasErrors()) {
191 /*: %1 is line number, %2 is error message */
192 Feedback << tr("Line %1: %2").arg(i + 1).arg(m_bc->bc.error_message());
193 errorCount++;
194 } else {
195 /*: %1 is line number */
196 Feedback << tr("Line %1: Success").arg(i + 1);
197 successCount++;
198 }
199 if (i && (i % 100 == 0)) {
200 txtFeedback->appendPlainText(Feedback.join('\n'));
201 txtFeedback->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
202 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
203 Feedback.clear();
204 }
205 }
206
207 QString summary;
208 if (errorCount && successCount) {
209 /*: %1 is total no. of items processed, %2 is no. of failures, %3 is no. of successes */
210 summary = tr("Total %1, %2 failed, %3 succeeded.").arg(errorCount + successCount).arg(errorCount)
211 .arg(successCount);
212 } else if (errorCount) {
213 /*: %1 is no. of failures */
214 summary = tr("All %1 failed.").arg(errorCount);
215 } else if (successCount) {
216 /*: %1 is no. of successes */
217 summary = tr("All %1 succeeded.").arg(successCount);
218 } else {
219 summary = tr("No items.");
220 }
221 if (Feedback.size()) {
222 txtFeedback->appendPlainText(Feedback.join('\n') + '\n' + summary + '\n');
223 } else {
224 txtFeedback->appendPlainText(summary + '\n');
225 }
226 txtFeedback->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
227 }
228
229 /* vim: set ts=4 sw=4 et : */