1 /*
2     Zint Barcode Generator - the open source barcode generator
3     Copyright (C) 2009 - 2021 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 /* vim: set ts=4 sw=4 et : */
20 
21 //#include <QDebug>
22 #include <QFile>
23 #include <QUiLoader>
24 #include <QFileDialog>
25 #include <QMessageBox>
26 #include <QSettings>
27 
28 #include "sequencewindow.h"
29 #include "exportwindow.h"
30 
SequenceWindow()31 SequenceWindow::SequenceWindow()
32 {
33     setupUi(this);
34     QSettings settings;
35 #if QT_VERSION < 0x60000
36     settings.setIniCodec("UTF-8");
37 #endif
38     QValidator *intvalid = new QIntValidator(this);
39 
40     linStartVal->setText(settings.value("studio/sequence/start_value", "1").toString());
41     linEndVal->setText(settings.value("studio/sequence/end_value", "10").toString());
42     linIncVal->setText(settings.value("studio/sequence/increment", "1").toString());
43     linFormat->setText(settings.value("studio/sequence/format", "$$$$$$").toString());
44 
45     linStartVal->setValidator(intvalid);
46     linEndVal->setValidator(intvalid);
47     linIncVal->setValidator(intvalid);
48     connect(btnClose, SIGNAL( clicked( bool )), SLOT(quit_now()));
49     connect(btnReset, SIGNAL( clicked( bool )), SLOT(reset_preview()));
50     connect(btnCreate, SIGNAL( clicked( bool )), SLOT(create_sequence()));
51     connect(txtPreview, SIGNAL( textChanged()), SLOT(check_generate()));
52     connect(btnImport, SIGNAL( clicked( bool )), SLOT(import()));
53     connect(btnExport, SIGNAL( clicked( bool )), SLOT(generate_sequence()));
54 }
55 
~SequenceWindow()56 SequenceWindow::~SequenceWindow()
57 {
58     QSettings settings;
59 #if QT_VERSION < 0x60000
60     settings.setIniCodec("UTF-8");
61 #endif
62 
63     settings.setValue("studio/sequence/start_value", linStartVal->text());
64     settings.setValue("studio/sequence/end_value", linEndVal->text());
65     settings.setValue("studio/sequence/increment", linIncVal->text());
66     settings.setValue("studio/sequence/format", linFormat->text());
67 }
68 
quit_now()69 void SequenceWindow::quit_now()
70 {
71     close();
72 }
73 
reset_preview()74 void SequenceWindow::reset_preview()
75 {
76     txtPreview->clear();
77 }
78 
apply_format(QString raw_number)79 QString SequenceWindow::apply_format(QString raw_number)
80 {
81     QString adjusted, reversed;
82     QString format;
83     int format_len, input_len, i, inpos;
84     QChar format_qchar;
85 
86     format = linFormat->text();
87     input_len = raw_number.length();
88     format_len = format.length();
89 
90     inpos = input_len;
91 
92     for(i = format_len; i > 0; i--) {
93         format_qchar = format[i - 1];
94         char format_char = format_qchar.toLatin1();
95         switch(format_char) {
96             case '#':
97                 if (inpos > 0) {
98                     adjusted += raw_number[inpos - 1];
99                     inpos--;
100                 } else {
101                     adjusted += ' ';
102                 }
103                 break;
104             case '$':
105                 if (inpos > 0) {
106                     adjusted += raw_number[inpos - 1];
107                     inpos--;
108                 } else {
109                     adjusted += '0';
110                 }
111                 break;
112             case '*':
113                 if (inpos > 0) {
114                     adjusted += raw_number[inpos - 1];
115                     inpos--;
116                 } else {
117                     adjusted += '*';
118                 }
119                 break;
120             default:
121                 adjusted += format_char;
122                 break;
123         }
124     }
125 
126     for(i = format_len; i > 0; i--) {
127         reversed += adjusted[i - 1];
128     }
129 
130     return reversed;
131 }
132 
create_sequence()133 void SequenceWindow::create_sequence()
134 {
135     QString startval, endval, incval, part, outputtext;
136     int start, stop, step, i;
137     bool ok;
138 
139     startval = linStartVal->text();
140     endval = linEndVal->text();
141     incval = linIncVal->text();
142     start = startval.toInt(&ok, 10);
143     stop = endval.toInt(&ok, 10);
144     step = incval.toInt(&ok, 10);
145 
146     if((stop <= start) || (step <= 0)) {
147         QMessageBox::critical(this, tr("Sequence Error"), tr("One or more of the input values is incorrect."));
148         return;
149     }
150 
151     for(i = start; i <= stop; i += step) {
152         part = apply_format(QString::number(i, 10));
153         part += '\n';
154         outputtext += part;
155     }
156 
157     txtPreview->setPlainText(outputtext);
158 }
159 
check_generate()160 void SequenceWindow::check_generate()
161 {
162     QString preview_copy;
163 
164     preview_copy = txtPreview->toPlainText();
165     if(preview_copy.isEmpty()) {
166         btnExport->setEnabled(false);
167         lblExport->setEnabled(false);
168     } else {
169         btnExport->setEnabled(true);
170         lblExport->setEnabled(true);
171     }
172 }
173 
import()174 void SequenceWindow::import()
175 {
176     QSettings settings;
177 #if QT_VERSION < 0x60000
178     settings.setIniCodec("UTF-8");
179 #endif
180     QFileDialog import_dialog;
181     QString filename;
182     QFile file;
183     QByteArray outstream;
184 
185     import_dialog.setWindowTitle("Import File");
186     import_dialog.setDirectory(settings.value("studio/default_dir", QDir::toNativeSeparators(QDir::homePath())).toString());
187 
188     if (import_dialog.exec()) {
189         filename = import_dialog.selectedFiles().at(0);
190     } else {
191         return;
192     }
193 
194     file.setFileName(filename);
195     if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
196         QMessageBox::critical(this, tr("Open Error"), tr("Could not open selected file."));
197         return;
198     }
199 
200     outstream = file.readAll();
201 
202     txtPreview->setPlainText(QString(outstream));
203     file.close();
204 
205     settings.setValue("studio/default_dir", filename.mid(0, filename.lastIndexOf(QDir::separator())));
206 }
207 
generate_sequence()208 void SequenceWindow::generate_sequence()
209 {
210     ExportWindow dlg;
211     dlg.barcode = barcode;
212     dlg.output_data = txtPreview->toPlainText();
213     dlg.exec();
214 }
215