2009-11-22 18:38:15 +03:00
/*
Zint Barcode Generator - the open source barcode generator
2021-01-11 21:11:41 +03:00
Copyright ( C ) 2009 - 2021 Robin Stuart < rstuart114 @ gmail . com >
2009-11-22 18:38:15 +03: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 .
*/
2020-10-27 18:21:50 +03:00
/* vim: set ts=4 sw=4 et : */
2009-11-22 18:38:15 +03:00
2020-10-27 18:21:50 +03:00
//#include <QDebug>
2009-11-22 18:38:15 +03:00
# include <QFileDialog>
# include <QUiLoader>
# include <QStringList>
# include <QMessageBox>
2017-07-27 12:21:46 +03:00
# include <QSettings>
2009-11-22 18:38:15 +03:00
# include "datawindow.h"
DataWindow : : DataWindow ( )
{
2020-10-27 18:21:50 +03:00
setupUi ( this ) ;
2009-11-22 18:38:15 +03:00
2020-10-27 18:21:50 +03:00
connect ( btnCancel , SIGNAL ( clicked ( bool ) ) , SLOT ( quit_now ( ) ) ) ;
connect ( btnReset , SIGNAL ( clicked ( bool ) ) , SLOT ( clear_data ( ) ) ) ;
connect ( btnOK , SIGNAL ( clicked ( bool ) ) , SLOT ( okay ( ) ) ) ;
2009-11-22 18:38:15 +03:00
}
2017-09-10 18:03:09 +03:00
DataWindow : : DataWindow ( const QString & input )
2009-11-22 18:38:15 +03:00
{
2020-10-27 18:21:50 +03:00
setupUi ( this ) ;
txtDataInput - > setPlainText ( input ) ;
txtDataInput - > moveCursor ( QTextCursor : : End , QTextCursor : : MoveAnchor ) ;
2017-10-23 22:37:52 +03:00
2020-10-27 18:21:50 +03:00
connect ( btnCancel , SIGNAL ( clicked ( bool ) ) , SLOT ( quit_now ( ) ) ) ;
connect ( btnReset , SIGNAL ( clicked ( bool ) ) , SLOT ( clear_data ( ) ) ) ;
connect ( btnOK , SIGNAL ( clicked ( bool ) ) , SLOT ( okay ( ) ) ) ;
connect ( btnFromFile , SIGNAL ( clicked ( bool ) ) , SLOT ( from_file ( ) ) ) ;
2009-11-22 18:38:15 +03:00
}
DataWindow : : ~ DataWindow ( )
{
}
void DataWindow : : quit_now ( )
{
2020-10-27 18:21:50 +03:00
Valid = 0 ;
close ( ) ;
2009-11-22 18:38:15 +03:00
}
void DataWindow : : clear_data ( )
{
2020-10-27 18:21:50 +03:00
txtDataInput - > clear ( ) ;
2009-11-22 18:38:15 +03:00
}
void DataWindow : : okay ( )
{
2020-10-27 18:21:50 +03:00
Valid = 1 ;
DataOutput = txtDataInput - > toPlainText ( ) ;
close ( ) ;
2009-11-22 18:38:15 +03:00
}
void DataWindow : : from_file ( )
{
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
QFileDialog open_dialog ;
QString filename ;
QFile file ;
QByteArray outstream ;
2017-10-22 12:55:50 +03:00
QString escape_string ;
2017-07-27 12:21:46 +03:00
open_dialog . setWindowTitle ( " Open File " ) ;
open_dialog . setDirectory ( settings . value ( " studio/default_dir " , QDir : : toNativeSeparators ( QDir : : homePath ( ) ) ) . toString ( ) ) ;
2017-10-23 22:37:52 +03:00
2017-07-27 12:21:46 +03:00
if ( open_dialog . exec ( ) ) {
filename = open_dialog . selectedFiles ( ) . at ( 0 ) ;
} else {
return ;
}
file . setFileName ( filename ) ;
if ( ! file . open ( QIODevice : : ReadOnly ) ) {
QMessageBox : : critical ( this , tr ( " Open Error " ) , tr ( " Could not open selected file. " ) ) ;
return ;
}
2017-10-23 22:37:52 +03:00
2017-07-27 12:21:46 +03:00
outstream = file . readAll ( ) ;
2017-10-22 12:55:50 +03:00
/* Allow some non-printing (control) characters to be read from file
by converting them to escape sequences */
escape_string . clear ( ) ;
escape_string . append ( QString ( outstream ) ) ;
2017-10-23 22:37:52 +03:00
2017-10-22 12:55:50 +03:00
escape_string . replace ( ( QChar ) ' \\ ' , ( QString ) " \\ \\ " , Qt : : CaseInsensitive ) ;
escape_string . replace ( ( QChar ) 0x04 , ( QString ) " \\ E " , Qt : : CaseInsensitive ) ; /* End of Transmission */
escape_string . replace ( ( QChar ) ' \a ' , ( QString ) " \\ a " , Qt : : CaseInsensitive ) ; /* Bell */
escape_string . replace ( ( QChar ) ' \b ' , ( QString ) " \\ b " , Qt : : CaseInsensitive ) ; /* Backspace */
escape_string . replace ( ( QChar ) ' \t ' , ( QString ) " \\ t " , Qt : : CaseInsensitive ) ; /* Horizontal tab */
escape_string . replace ( ( QChar ) ' \v ' , ( QString ) " \\ v " , Qt : : CaseInsensitive ) ; /* Vertical tab */
escape_string . replace ( ( QChar ) ' \f ' , ( QString ) " \\ f " , Qt : : CaseInsensitive ) ; /* Form feed */
escape_string . replace ( ( QChar ) ' \r ' , ( QString ) " \\ r " , Qt : : CaseInsensitive ) ; /* Carriage return */
escape_string . replace ( ( QChar ) 0x1b , ( QString ) " \\ e " , Qt : : CaseInsensitive ) ; /* Escape */
escape_string . replace ( ( QChar ) 0x1d , ( QString ) " \\ G " , Qt : : CaseInsensitive ) ; /* Group Separator */
escape_string . replace ( ( QChar ) 0x1e , ( QString ) " \\ R " , Qt : : CaseInsensitive ) ; /* Record Separator */
2017-10-23 22:37:52 +03:00
2017-10-22 12:55:50 +03:00
txtDataInput - > setPlainText ( QString ( escape_string ) ) ;
2017-07-27 12:21:46 +03:00
file . close ( ) ;
2017-10-23 22:37:52 +03:00
2017-07-27 12:21:46 +03:00
settings . setValue ( " studio/default_dir " , filename . mid ( 0 , filename . lastIndexOf ( QDir : : separator ( ) ) ) ) ;
2017-10-23 22:37:52 +03:00
}