comparison mupdf-source/thirdparty/zint/frontend_qt/datawindow.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 <QRegularExpression>
25 #include <QSettings>
26 #include <QStringList>
27 #include <QUiLoader>
28
29 #include "datawindow.h"
30
31 // Shorthand
32 #define QSL QStringLiteral
33
34 static const int tempMessageTimeout = 2000;
35
36 DataWindow::DataWindow(const QString &input, bool isEscaped, int seg_no) : Valid(false), Escaped(false),
37 m_isEscaped(isEscaped), m_seg_no(seg_no)
38 {
39 setupUi(this);
40 QSettings settings;
41 #if QT_VERSION < 0x60000
42 settings.setIniCodec("UTF-8");
43 #endif
44
45 QByteArray geometry = settings.value(QSL("studio/data/window_geometry")).toByteArray();
46 restoreGeometry(geometry);
47
48 QIcon closeIcon(QIcon::fromTheme(QSL("window-close"), QIcon(QSL(":res/x.svg"))));
49 QIcon clearIcon(QSL(":res/delete.svg"));
50 QIcon okIcon(QIcon(QSL(":res/check.svg")));
51 btnCancel->setIcon(closeIcon);
52 btnDataClear->setIcon(clearIcon);
53 btnOK->setIcon(okIcon);
54
55 if (isEscaped && input.contains(QSL("\\n"))) {
56 // Substitute escaped Line Feeds with actual Line Feeds
57 QString out;
58 out.reserve(input.length());
59 int lastPosn = 0;
60 QRegularExpression escRE(QSL("\\\\(?:[0EabtnvfreGR\\\\]|d[0-9]{3}|o[0-7]{3}|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}"
61 "|U[0-9A-Fa-f]{6})"));
62 QRegularExpressionMatchIterator matchI = escRE.globalMatch(input);
63 while (matchI.hasNext()) {
64 QRegularExpressionMatch match = matchI.next();
65 if (match.captured(0) == QSL("\\n")) {
66 out += input.mid(lastPosn, match.capturedStart(0) - lastPosn) + '\n';
67 lastPosn = match.capturedEnd(0);
68 }
69 }
70 out += input.mid(lastPosn);
71 txtDataInput->setPlainText(out);
72 statusBarData->showMessage(tr("Converted LFs"), tempMessageTimeout);
73 } else {
74 txtDataInput->setPlainText(input);
75 }
76 txtDataInput->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
77
78 connect(btnCancel, SIGNAL(clicked(bool)), SLOT(close()));
79 connect(btnDataClear, SIGNAL(clicked(bool)), SLOT(clear_data()));
80 connect(btnOK, SIGNAL(clicked(bool)), SLOT(okay()));
81 connect(btnFromFile, SIGNAL(clicked(bool)), SLOT(from_file()));
82 connect(txtDataInput, SIGNAL(textChanged()), this, SLOT(text_changed()));
83
84 btnDataClear->setEnabled(!txtDataInput->toPlainText().isEmpty());
85 }
86
87 DataWindow::~DataWindow()
88 {
89 QSettings settings;
90 #if QT_VERSION < 0x60000
91 settings.setIniCodec("UTF-8");
92 #endif
93 settings.setValue(QSL("studio/data/window_geometry"), saveGeometry());
94 }
95
96 void DataWindow::clear_data()
97 {
98 txtDataInput->clear();
99 }
100
101 void DataWindow::text_changed()
102 {
103 bool escaped = m_isEscaped;
104 const QString &text = escapedData(escaped);
105 btnDataClear->setEnabled(!text.isEmpty());
106 emit dataChanged(text, escaped, m_seg_no);
107 }
108
109 void DataWindow::okay()
110 {
111 Valid = true;
112 DataOutput = escapedData(Escaped);
113 close();
114 }
115
116 QString DataWindow::escapedData(bool &escaped)
117 {
118 QString text = txtDataInput->toPlainText();
119 if (text.contains('\n')) {
120 // Escape Line Feeds
121 text.replace('\n', QSL("\\n"));
122 escaped = true;
123 }
124 return text;
125 }
126
127 void DataWindow::from_file()
128 {
129 QSettings settings;
130 #if QT_VERSION < 0x60000
131 settings.setIniCodec("UTF-8");
132 #endif
133 QFileDialog open_dialog;
134 QString filename;
135 QFile file;
136 QByteArray outstream;
137 open_dialog.setWindowTitle("Import File");
138 open_dialog.setDirectory(settings.value("studio/default_dir",
139 QDir::toNativeSeparators(QDir::homePath())).toString());
140
141 if (open_dialog.exec()) {
142 filename = open_dialog.selectedFiles().at(0);
143 } else {
144 return;
145 }
146
147 file.setFileName(filename);
148 if (!file.open(QIODevice::ReadOnly)) {
149 QMessageBox::critical(this, tr("Open Error"), tr("Could not open selected file."));
150 return;
151 }
152
153 outstream = file.readAll();
154
155 /* Allow some non-printing (control) characters to be read from file
156 by converting them to escape sequences */
157 QString escape_string(outstream); // Converts to UTF-8 (NOTE: QString can't handle embedded NULs)
158
159 QRegularExpression escRE(QSL("[\\x04\\x07\\x08\\x09\\x0B\\x0C\\x0D\\x1B\\x1D\\x1E\\x5C]"));
160 if (escape_string.contains(escRE)) {
161 escape_string.replace((QChar)'\\', QSL("\\\\"));
162 escape_string.replace((QChar)0x04, QSL("\\E")); /* End of Transmission */
163 escape_string.replace((QChar)'\a', QSL("\\a")); /* Bell (0x07) */
164 escape_string.replace((QChar)'\b', QSL("\\b")); /* Backspace (0x08) */
165 escape_string.replace((QChar)'\t', QSL("\\t")); /* Horizontal tab (0x09) */
166 // Leaving Line Feed (0x0A)
167 escape_string.replace((QChar)'\v', QSL("\\v")); /* Vertical tab (0x0B) */
168 escape_string.replace((QChar)'\f', QSL("\\f")); /* Form feed (0x0C) */
169 escape_string.replace((QChar)'\r', QSL("\\r")); /* Carriage return (0x0D) */
170 escape_string.replace((QChar)0x1b, QSL("\\e")); /* Escape */
171 escape_string.replace((QChar)0x1d, QSL("\\G")); /* Group Separator */
172 escape_string.replace((QChar)0x1e, QSL("\\R")); /* Record Separator */
173 Escaped = true;
174 statusBarData->showMessage(tr("Escaped data"), tempMessageTimeout);
175 }
176
177 txtDataInput->setPlainText(escape_string);
178 file.close();
179
180 settings.setValue("studio/default_dir", filename.mid(0, filename.lastIndexOf(QDir::separator())));
181 }
182
183 /* vim: set ts=4 sw=4 et : */