2009-09-19 12:13:35 +04:00
/*
Zint Barcode Generator - the open source barcode generator
2022-06-16 18:47:34 +03:00
Copyright ( C ) 2009 - 2022 Robin Stuart < rstuart114 @ gmail . com >
2009-09-19 12:13:35 +04:00
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 .
*/
2022-06-16 18:47:34 +03:00
/* SPDX-License-Identifier: GPL-3.0-or-later */
2009-09-19 12:13:35 +04:00
2020-10-27 18:21:50 +03:00
//#include <QDebug>
2009-09-19 12:13:35 +04:00
# include <QUiLoader>
2009-09-20 19:40:46 +04:00
# include <QFileDialog>
# include <QMessageBox>
2017-05-11 23:14:38 +03:00
# include <QSettings>
2021-11-23 22:12:48 +03:00
# include <QStringBuilder>
2009-09-19 12:13:35 +04:00
# include "exportwindow.h"
2021-11-23 22:12:48 +03:00
// Shorthand
# define QSL QStringLiteral
2022-06-16 18:47:34 +03:00
ExportWindow : : ExportWindow ( BarcodeItem * bc , const QString & output_data ) : m_bc ( bc ) , m_output_data ( output_data ) , m_lines ( 0 )
2009-09-19 12:13:35 +04:00
{
2021-11-23 22:12:48 +03:00
setupUi ( this ) ;
2017-05-11 23:14:38 +03:00
QSettings settings ;
2021-01-11 21:11:41 +03:00
# if QT_VERSION < 0x60000
2020-10-27 18:11:33 +03:00
settings . setIniCodec ( " UTF-8 " ) ;
2021-01-11 21:11:41 +03:00
# endif
2017-05-11 23:14:38 +03:00
2021-11-23 22:12:48 +03:00
QByteArray geometry = settings . value ( QSL ( " studio/export/window_geometry " ) ) . toByteArray ( ) ;
restoreGeometry ( geometry ) ;
linDestPath - > setText ( settings . value ( QSL ( " studio/export/destination " ) ,
Add Structured Append support for AZTEC, CODEONE, DATAMATRIX, DOTCODE,
GRIDMATRIX, MAXICODE, MICROPDF417, PDF417, QRCODE, ULTRA
DOTCODE: use pre-calculated generator poly coeffs in Reed-Solomon for
performance improvement
PDF417/MICROPDF417: use common routine pdf417_initial()
GUI: code lines <= 118, shorthand widget_obj(),
shorten calling upcean_addon_gap(), upcean_guard_descent()
various backend: var name debug -> debug_print
2021-09-28 23:42:44 +03:00
QDir : : toNativeSeparators ( QDir : : homePath ( ) ) ) . toString ( ) ) ;
2021-11-23 22:12:48 +03:00
linPrefix - > setText ( settings . value ( QSL ( " studio/export/file_prefix " ) , QSL ( " bcs_ " ) ) . toString ( ) ) ;
2022-05-25 15:44:45 +03:00
linPostfix - > setText ( settings . value ( QSL ( " studio/export/file_postfix " ) , QSL ( " " ) ) . toString ( ) ) ;
2021-11-23 22:12:48 +03:00
cmbFileName - > setCurrentIndex ( settings . value ( QSL ( " studio/export/name_format " ) , 0 ) . toInt ( ) ) ;
cmbFileFormat - > setCurrentIndex ( settings . value ( QSL ( " studio/export/filetype " ) , 0 ) . toInt ( ) ) ;
2017-10-23 22:37:52 +03:00
2021-11-23 22:12:48 +03:00
QIcon closeIcon ( QIcon : : fromTheme ( QSL ( " window-close " ) , QIcon ( QSL ( " :res/x.svg " ) ) ) ) ;
btnCancel - > setIcon ( closeIcon ) ;
connect ( btnCancel , SIGNAL ( clicked ( bool ) ) , SLOT ( close ( ) ) ) ;
2017-07-27 12:21:46 +03:00
connect ( btnOK , SIGNAL ( clicked ( bool ) ) , SLOT ( process ( ) ) ) ;
connect ( btnDestPath , SIGNAL ( clicked ( bool ) ) , SLOT ( get_directory ( ) ) ) ;
2022-06-16 18:47:34 +03:00
m_dataList = m_output_data . split ( ' \n ' ) ;
m_lines = m_dataList . size ( ) ;
if ( m_lines & & m_dataList [ m_lines - 1 ] . isEmpty ( ) ) {
m_lines - - ;
}
/*: %1 is number of sequences */
lblFeedback - > setText ( tr ( " Export Results (%1): " ) . arg ( m_lines ) ) ;
2009-09-19 12:13:35 +04:00
}
ExportWindow : : ~ ExportWindow ( )
{
2017-05-11 23:14:38 +03:00
QSettings settings ;
2021-01-11 21:11:41 +03:00
# if QT_VERSION < 0x60000
2020-10-27 18:11:33 +03:00
settings . setIniCodec ( " UTF-8 " ) ;
2021-01-11 21:11:41 +03:00
# endif
2021-11-23 22:12:48 +03:00
settings . setValue ( QSL ( " studio/export/window_geometry " ) , saveGeometry ( ) ) ;
2017-05-11 23:14:38 +03:00
2021-11-23 22:12:48 +03:00
settings . setValue ( QSL ( " studio/export/destination " ) , linDestPath - > text ( ) ) ;
settings . setValue ( QSL ( " studio/export/file_prefix " ) , linPrefix - > text ( ) ) ;
2022-05-25 15:44:45 +03:00
settings . setValue ( QSL ( " studio/export/file_postfix " ) , linPostfix - > text ( ) ) ;
2021-11-23 22:12:48 +03:00
settings . setValue ( QSL ( " studio/export/name_format " ) , cmbFileName - > currentIndex ( ) ) ;
settings . setValue ( QSL ( " studio/export/filetype " ) , cmbFileFormat - > currentIndex ( ) ) ;
2009-09-20 19:40:46 +04:00
}
void ExportWindow : : get_directory ( )
{
2017-07-27 12:21:46 +03:00
QSettings settings ;
2021-01-11 21:11:41 +03:00
# if QT_VERSION < 0x60000
2020-10-27 18:11:33 +03:00
settings . setIniCodec ( " UTF-8 " ) ;
2021-01-11 21:11:41 +03:00
# endif
2017-07-27 12:21:46 +03:00
QString directory ;
QFileDialog fdialog ;
fdialog . setFileMode ( QFileDialog : : Directory ) ;
2021-11-23 22:12:48 +03:00
fdialog . setDirectory ( settings . value ( QSL ( " studio/default_dir " ) ,
QDir : : toNativeSeparators ( QDir : : homePath ( ) ) ) . toString ( ) ) ;
2017-07-27 12:21:46 +03:00
2021-11-23 22:12:48 +03:00
if ( fdialog . exec ( ) ) {
2017-07-27 12:21:46 +03:00
directory = fdialog . selectedFiles ( ) . at ( 0 ) ;
} else {
return ;
}
linDestPath - > setText ( QDir : : toNativeSeparators ( directory ) ) ;
2021-11-23 22:12:48 +03:00
settings . setValue ( QSL ( " studio/default_dir " ) , directory ) ;
2009-09-20 19:40:46 +04:00
}
void ExportWindow : : process ( )
{
2021-11-23 22:12:48 +03:00
const QRegularExpression urlRE ( QSL ( " [ \\ /:*? \" <>|%] " ) ) ;
txtFeedback - > setPlainText ( tr ( " Processing... " ) ) ;
txtFeedback - > moveCursor ( QTextCursor : : End , QTextCursor : : MoveAnchor ) ;
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
QString biggest ;
bool needUrlEscape = false ;
2017-10-23 22:37:52 +03:00
2021-11-23 22:12:48 +03:00
const int fileNameIdx = cmbFileName - > currentIndex ( ) ;
if ( fileNameIdx = = 1 ) {
2022-06-16 18:47:34 +03:00
biggest = QString : : number ( m_lines + 1 ) ;
2021-11-23 22:12:48 +03:00
} else {
needUrlEscape = m_output_data . contains ( urlRE ) ;
}
2017-10-23 22:37:52 +03:00
2021-11-23 22:12:48 +03:00
QString suffix ;
switch ( cmbFileFormat - > currentIndex ( ) ) {
2015-08-18 14:50:42 +03:00
# ifdef NO_PNG
2021-11-23 22:12:48 +03:00
case 0 : suffix = QSL ( " .eps " ) ; break ;
case 1 : suffix = QSL ( " .gif " ) ; break ;
case 2 : suffix = QSL ( " .svg " ) ; break ;
case 3 : suffix = QSL ( " .bmp " ) ; break ;
case 4 : suffix = QSL ( " .pcx " ) ; break ;
case 5 : suffix = QSL ( " .emf " ) ; break ;
case 6 : suffix = QSL ( " .tif " ) ; break ;
2015-08-18 14:50:42 +03:00
# else
2021-11-23 22:12:48 +03:00
case 0 : suffix = QSL ( " .png " ) ; break ;
case 1 : suffix = QSL ( " .eps " ) ; break ;
case 2 : suffix = QSL ( " .gif " ) ; break ;
case 3 : suffix = QSL ( " .svg " ) ; break ;
case 4 : suffix = QSL ( " .bmp " ) ; break ;
case 5 : suffix = QSL ( " .pcx " ) ; break ;
case 6 : suffix = QSL ( " .emf " ) ; break ;
case 7 : suffix = QSL ( " .tif " ) ; break ;
2015-08-18 14:50:42 +03:00
# endif
2017-05-11 23:14:38 +03:00
}
2017-10-23 22:37:52 +03:00
2021-11-23 22:12:48 +03:00
QString filePathPrefix = linDestPath - > text ( ) % QDir : : separator ( ) % linPrefix - > text ( ) ;
2022-05-25 15:44:45 +03:00
QString postfix = linPostfix - > text ( ) ;
2021-11-23 22:12:48 +03:00
QStringList Feedback ;
int successCount = 0 , errorCount = 0 ;
2022-06-16 18:47:34 +03:00
for ( int i = 0 ; i < m_lines ; i + + ) {
const QString & dataString = m_dataList [ i ] ;
2021-11-23 22:12:48 +03:00
QString fileName ;
switch ( fileNameIdx ) {
case 0 : /* Same as Data (URL Escaped) */
if ( needUrlEscape ) {
2020-10-27 18:21:50 +03:00
QString url_escaped ;
2021-11-23 22:12:48 +03:00
for ( int m = 0 ; m < dataString . length ( ) ; m + + ) {
QChar name_qchar = dataString [ m ] ;
2017-09-10 18:03:09 +03:00
char name_char = name_qchar . toLatin1 ( ) ;
2017-10-23 22:37:52 +03:00
2021-11-23 22:12:48 +03:00
switch ( name_char ) {
case ' \\ ' : url_escaped + = QSL ( " %5C " ) ; break ;
case ' / ' : url_escaped + = QSL ( " %2F " ) ; break ;
case ' : ' : url_escaped + = QSL ( " %3A " ) ; break ;
case ' * ' : url_escaped + = QSL ( " %2A " ) ; break ;
case ' ? ' : url_escaped + = QSL ( " %3F " ) ; break ;
case ' " ' : url_escaped + = QSL ( " %22 " ) ; break ;
case ' < ' : url_escaped + = QSL ( " %3C " ) ; break ;
case ' > ' : url_escaped + = QSL ( " %3E " ) ; break ;
case ' | ' : url_escaped + = QSL ( " %7C " ) ; break ;
case ' % ' : url_escaped + = QSL ( " %25 " ) ; break ;
2020-10-27 18:21:50 +03:00
default : url_escaped + = name_qchar ; break ;
}
}
2022-05-25 15:44:45 +03:00
fileName = filePathPrefix % url_escaped % postfix % suffix ;
2021-11-23 22:12:48 +03:00
} else {
2022-05-25 15:44:45 +03:00
fileName = filePathPrefix % dataString % postfix % suffix ;
2017-08-12 00:36:40 +03:00
}
2020-10-27 18:21:50 +03:00
break ;
case 1 : { /* Formatted Serial Number */
2021-11-23 22:12:48 +03:00
QString this_val , pad ;
2020-10-27 18:21:50 +03:00
this_val = QString : : number ( i + 1 ) ;
2021-11-23 22:12:48 +03:00
pad . fill ( ' 0 ' , biggest . length ( ) - this_val . length ( ) ) ;
2017-10-23 22:37:52 +03:00
2022-05-25 15:44:45 +03:00
fileName = filePathPrefix % pad % this_val % postfix % suffix ;
2020-10-27 18:21:50 +03:00
}
break ;
}
2021-11-23 22:12:48 +03:00
m_bc - > bc . setText ( dataString ) ;
m_bc - > bc . save_to_file ( fileName ) ;
if ( m_bc - > bc . hasErrors ( ) ) {
/*: %1 is line number, %2 is error message */
Feedback < < tr ( " Line %1: %2 " ) . arg ( i + 1 ) . arg ( m_bc - > bc . error_message ( ) ) ;
errorCount + + ;
2020-10-27 18:21:50 +03:00
} else {
2021-11-23 22:12:48 +03:00
/*: %1 is line number */
Feedback < < tr ( " Line %1: Success " ) . arg ( i + 1 ) ;
successCount + + ;
2020-10-27 18:21:50 +03:00
}
2021-11-23 22:12:48 +03:00
if ( i & & ( i % 100 = = 0 ) ) {
txtFeedback - > appendPlainText ( Feedback . join ( ' \n ' ) ) ;
txtFeedback - > moveCursor ( QTextCursor : : End , QTextCursor : : MoveAnchor ) ;
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
Feedback . clear ( ) ;
}
}
QString summary ;
if ( errorCount & & successCount ) {
/*: %1 is total no. of items processed, %2 is no. of failures, %3 is no. of successes */
summary = tr ( " Total %1, %2 failed, %3 succeeded. " ) . arg ( errorCount + successCount ) . arg ( errorCount )
. arg ( successCount ) ;
} else if ( errorCount ) {
/*: %1 is no. of failures */
summary = tr ( " All %1 failed. " ) . arg ( errorCount ) ;
} else if ( successCount ) {
/*: %1 is no. of successes */
summary = tr ( " All %1 succeeded. " ) . arg ( successCount ) ;
} else {
summary = tr ( " No items. " ) ;
}
if ( Feedback . size ( ) ) {
txtFeedback - > appendPlainText ( Feedback . join ( ' \n ' ) + ' \n ' + summary + ' \n ' ) ;
} else {
txtFeedback - > appendPlainText ( summary + ' \n ' ) ;
2020-10-27 18:21:50 +03:00
}
2021-11-23 22:12:48 +03:00
txtFeedback - > moveCursor ( QTextCursor : : End , QTextCursor : : MoveAnchor ) ;
2020-10-27 18:21:50 +03:00
}
2022-06-16 18:47:34 +03:00
/* vim: set ts=4 sw=4 et : */