1
0
mirror of https://github.com/woo-j/zint.git synced 2024-12-23 13:33:55 +03:00
zint/frontend_qt/sequencewindow.cpp

216 lines
6.5 KiB
C++
Raw Normal View History

/*
Zint Barcode Generator - the open source barcode generator
Copyright (C) 2009 - 2021 Robin Stuart <rstuart114@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* vim: set ts=4 sw=4 et : */
//#include <QDebug>
#include <QFile>
#include <QUiLoader>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include "sequencewindow.h"
#include "exportwindow.h"
// Shorthand
#define QSL QStringLiteral
SequenceWindow::SequenceWindow(BarcodeItem *bc) : m_bc(bc)
{
setupUi(this);
QSettings settings;
#if QT_VERSION < 0x60000
settings.setIniCodec("UTF-8");
#endif
QByteArray geometry = settings.value(QSL("studio/sequence/window_geometry")).toByteArray();
restoreGeometry(geometry);
spnSeqStartVal->setValue(settings.value(QSL("studio/sequence/start_value"), 1).toInt());
spnSeqEndVal->setValue(settings.value(QSL("studio/sequence/end_value"), 10).toInt());
spnSeqIncVal->setValue(settings.value(QSL("studio/sequence/increment"), 1).toInt());
linSeqFormat->setText(settings.value(QSL("studio/sequence/format"), QSL("$$$$$$")).toString());
QIcon closeIcon(QIcon::fromTheme(QSL("window-close"), QIcon(QSL(":res/x.svg"))));
QIcon clearIcon(QIcon::fromTheme(QSL("edit-clear"), QIcon(QSL(":res/delete.svg"))));
btnSeqClose->setIcon(closeIcon);
btnSeqClear->setIcon(clearIcon);
connect(btnSeqClose, SIGNAL( clicked( bool )), SLOT(close()));
connect(btnSeqClear, SIGNAL( clicked( bool )), SLOT(clear_preview()));
connect(btnSeqCreate, SIGNAL( clicked( bool )), SLOT(create_sequence()));
connect(txtSeqPreview, SIGNAL( textChanged()), SLOT(check_generate()));
connect(btnSeqImport, SIGNAL( clicked( bool )), SLOT(import()));
connect(btnSeqExport, SIGNAL( clicked( bool )), SLOT(generate_sequence()));
}
SequenceWindow::~SequenceWindow()
{
QSettings settings;
#if QT_VERSION < 0x60000
settings.setIniCodec("UTF-8");
#endif
settings.setValue(QSL("studio/sequence/window_geometry"), saveGeometry());
2017-10-23 22:37:52 +03:00
settings.setValue(QSL("studio/sequence/start_value"), spnSeqStartVal->value());
settings.setValue(QSL("studio/sequence/end_value"), spnSeqEndVal->value());
settings.setValue(QSL("studio/sequence/increment"), spnSeqIncVal->value());
settings.setValue(QSL("studio/sequence/format"), linSeqFormat->text());
}
void SequenceWindow::clear_preview()
{
txtSeqPreview->clear();
}
QString SequenceWindow::apply_format(const QString& raw_number)
{
QString adjusted, reversed;
QString format;
int format_len, input_len, i, inpos;
QChar format_qchar;
2017-10-23 22:37:52 +03:00
format = linSeqFormat->text();
input_len = raw_number.length();
format_len = format.length();
2017-10-23 22:37:52 +03:00
inpos = input_len;
2017-10-23 22:37:52 +03:00
for (i = format_len; i > 0; i--) {
format_qchar = format[i - 1];
2017-09-10 18:03:09 +03:00
char format_char = format_qchar.toLatin1();
switch (format_char) {
case '#':
if (inpos > 0) {
adjusted += raw_number[inpos - 1];
inpos--;
} else {
adjusted += ' ';
}
break;
case '$':
if (inpos > 0) {
adjusted += raw_number[inpos - 1];
inpos--;
} else {
adjusted += '0';
}
break;
case '*':
if (inpos > 0) {
adjusted += raw_number[inpos - 1];
inpos--;
} else {
adjusted += '*';
}
break;
default:
adjusted += format_char;
break;
}
}
for(i = format_len; i > 0; i--) {
reversed += adjusted[i - 1];
}
return reversed;
}
void SequenceWindow::create_sequence()
{
QStringList outputtext;
int start, stop, step, i;
start = spnSeqStartVal->value();
stop = spnSeqEndVal->value();
step = spnSeqIncVal->value();
if (stop < start) {
QMessageBox::critical(this, tr("Sequence Error"),
tr("End Value must be greater than or equal to Start Value."));
return;
}
if ((stop - start) / step > 10000) {
QMessageBox::critical(this, tr("Sequence Error"), tr("The maximum sequence allowed is 10,000 items."));
return;
}
for (i = start; i <= stop; i += step) {
outputtext << apply_format(QString::number(i, 10));
}
txtSeqPreview->setPlainText(outputtext.join('\n'));
}
void SequenceWindow::check_generate()
{
QString preview_copy;
preview_copy = txtSeqPreview->toPlainText();
if (preview_copy.isEmpty()) {
btnSeqExport->setEnabled(false);
lblSeqExport->setEnabled(false);
} else {
btnSeqExport->setEnabled(true);
lblSeqExport->setEnabled(true);
}
}
void SequenceWindow::import()
{
QSettings settings;
#if QT_VERSION < 0x60000
settings.setIniCodec("UTF-8");
#endif
QFileDialog import_dialog;
QString filename;
QFile file;
QByteArray outstream;
2017-10-23 22:37:52 +03:00
import_dialog.setWindowTitle(tr("Import File"));
import_dialog.setDirectory(settings.value(QSL("studio/default_dir"),
QDir::toNativeSeparators(QDir::homePath())).toString());
2017-10-23 22:37:52 +03:00
if (import_dialog.exec()) {
filename = import_dialog.selectedFiles().at(0);
} else {
return;
}
2017-10-23 22:37:52 +03:00
file.setFileName(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this, tr("Open Error"), tr("Could not open selected file."));
return;
}
outstream = file.readAll();
txtSeqPreview->setPlainText(QString(outstream));
file.close();
2017-10-23 22:37:52 +03:00
settings.setValue(QSL("studio/default_dir"), filename.mid(0, filename.lastIndexOf(QDir::separator())));
}
void SequenceWindow::generate_sequence()
{
ExportWindow dlg(m_bc, txtSeqPreview->toPlainText());
dlg.exec();
}