Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

barcodegenerator.cpp

Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 
00008 #include "../formatidlist.h"
00009 #include "barcodegenerator.h"
00010 #include "commonstrings.h"
00011 #include "loadsaveplugin.h"
00012 #include "scpaths.h"
00013 #include "scribus.h"
00014 #include "undomanager.h"
00015 #include "util_ghostscript.h"
00016 #include "util_icon.h"
00017 
00018 
00019 #include <QTextStream>
00020 #include <QColorDialog>
00021 
00022 BarcodeType::BarcodeType(QString cmd, QString exa,
00023                          QString comm, QString regExp,
00024                          bool includeCheck, bool includeCheckInText)
00025 {
00026     command = cmd;
00027     example = exa;
00028     comment = comm;
00029     regularExp = regExp;
00030     this->includeCheck = includeCheck;
00031     this->includeCheckInText = includeCheckInText;
00032 }
00033 
00034 BarcodeGenerator::BarcodeGenerator(QWidget* parent, const char* name)
00035     : QDialog(parent)
00036 {
00037     ui.setupUi(this);
00038     setObjectName(name);
00039     setModal(true);
00040     map["EAN-13"] = BarcodeType("ean13", "9781860742712", tr("12 or 13 digits"),
00041                                 "[0-9]{12,13}");
00042     map["EAN-8"] = BarcodeType("ean8", "12345678", tr("8 digits"),
00043                                "[0-9]{8,8}");
00044     map["UPC-A"] = BarcodeType("upca", "78858101497", tr("11 or 12 digits"),
00045                                "[0-9]{11,12}");
00046     map["UPC-E"] = BarcodeType("upce", "0123456", tr("7 or 8 digits"),
00047                                "[0-9]{7,8}");
00048     map["EAN-5"] = BarcodeType("ean5", "90200", tr("5 digits"),
00049                                "[0-9]{5,5}");
00050     map["EAN-2"] = BarcodeType("ean2", "42", tr("2 digits"),
00051                                "[0-9]{2,2}");
00052     map["ISBN"] = BarcodeType("isbn", "1-58880-149",
00053                               tr("12 or 13 digits with dashes. The legacy ISBN-10 format accepts 9 or 10 digits with dashes, but this standard was depreciated for public use after 1st January 2007. (Note: To convert an old ISBN-10 to a new ISBN-13, prefix 978- to the first 9 digits, e.g. 1-56592-479-7 -> 978-1-56592-479. The final check-digit will be calculated automatically.)"),
00054                               "[0-9]*\\-[0-9]*\\-[0-9]*");
00055 //    "Code-11"] = "code11"
00056     map["Code-39"] = BarcodeType("code39", "CODE-39",
00057                                  tr("Variable number of characters, digits and any of the symbols -. *$/+%."),
00058                                  "[0-9a-zA-Z\\-\\.\\ \\*\\$\\/\\+\\%]*",
00059                                 true, true);
00060 //    "Code-93"] = "code93"
00061     map["Code-128"] = BarcodeType("code128", "^104^102Count^0991234^101!",
00062                                   tr("Variable number of ASCII characters and special function symbols, starting with the appropriate start character for the initial character set. UCC/EAN-128s must have a mandatory FNC 1 symbol immediately following the start character."),
00063                                   "\\^[0-9a-zA-Z\\^\\!]*",
00064                                   true, true);
00065     map["UCC/EAN-128"] = BarcodeType("code128", "^104^102Count^0991234^101!",
00066                                      tr("Variable number of ASCII characters and special function symbols, starting with the appropriate start character for the initial character set. UCC/EAN-128s must have a mandatory FNC 1 symbol immediately following the start character."),
00067                                      "\\^[0-9a-zA-Z\\^\\!]*");
00068     map["Rationalized Codabar"] = BarcodeType("rationalizedCodabar", "0123456789",
00069                                               tr("Variable number of digits and any of the symbols -$:/.+ABCD."),
00070                                               "[0-9A-D\\-\\$\\:\\/\\.\\+]*",
00071                                              true, true);
00072     map["Interleaved 2 of 5"] = BarcodeType("interleaved2of5", "05012345678900",
00073                                             tr("Variable number of digits"),
00074                                             "[0-9]*",
00075                                            true, true);
00076     map["ITF-14"] = BarcodeType("interleaved2of5", "05012345678900",
00077                                 tr("Variable number of digits. An ITF-14 is 14 characters and does not have a check digit"),
00078                                 "[0-9]*",
00079                                true, true);
00080     map["Code 2 of 5"] = BarcodeType("code2of5", "0123456789",
00081                                      tr("Variable number of digits"),
00082                                      "[0-9]*");
00083     map["Postnet"] = BarcodeType("postnet", "01234567",
00084                                  tr("Variable number of digits"),
00085                                  "[0-9]*",
00086                                 false, true);
00087     map["Royal Mail"] = BarcodeType("royalmail", "LE28HS9Z",
00088                                     tr("Variable number of digits and capital letters"),
00089                                     "[0-9A-Z]*",
00090                                     false, true);
00091 //    "Auspost"] = "auspost"
00092     map["MSI"] = BarcodeType("msi", "0120823635162", tr("Variable number of digits"),
00093                              "[0-9]*",
00094                             true, true);
00095 //    "KIX"] = "kix"
00096     map["Plessey"] = BarcodeType("plessey", "012345ABCDEF",
00097                                  tr("Variable number of hexadecimal characters"),
00098                                  "[0-9A-F]*",
00099                                 false, true);
00100     //    "Symbol"] = "symbol"
00101 
00102     useSamples = true;
00103     guiColor = ui.codeEdit->palette().color(QPalette::Window);
00104     ui.bcCombo->addItem(tr("Select Type")); // to prevent 1st gs call
00105     ui.bcCombo->addItems(map.keys());
00106     ui.okButton->setText(CommonStrings::tr_OK);
00107     ui.cancelButton->setText(CommonStrings::tr_Cancel);
00108     ui.resetButton->setIcon(loadIcon("u_undo16.png"));
00109     lnColor = Qt::black;
00110     txtColor = Qt::black;
00111     bgColor = Qt::white;
00112     paintColorSample(ui.linesLabel, lnColor);
00113     paintColorSample(ui.txtLabel, txtColor);
00114     paintColorSample(ui.bgLabel, bgColor);
00115 
00116     tmpFile = QDir::convertSeparators(ScPaths::getTempFileDir() + "bcode.png");
00117     psFile = QDir::convertSeparators(ScPaths::getTempFileDir() + "bcode.ps");
00118 
00119     // PS engine
00120     psCommand.append("%!PS-Adobe-2.0 EPSF-2.0\n");
00121     QFile f( ScPaths::instance().shareDir() + QString("/plugins/barcode.ps") );
00122     f.open(QIODevice::ReadOnly);
00123     QTextStream ts(&f);
00124     QString s = ts.readAll();
00125     int begin = s.indexOf("% --BEGIN TEMPLATE--");
00126     int end = s.indexOf("% --END TEMPLATE--");
00127     psCommand.append(s.mid(begin, end));
00128     f.close();
00129     psCommand.append("\n\n%command\n");
00130     bcComboChanged();
00131 }
00132 
00133 BarcodeGenerator::~BarcodeGenerator()
00134 {
00135     QFile::remove(psFile);
00136     QFile::remove(tmpFile);
00137 }
00138 
00139 void BarcodeGenerator::bcComboChanged()
00140 {
00141     if (ui.bcCombo->currentIndex() == 0)
00142     {
00143         ui.okButton->setEnabled(false);
00144         ui.sampleLabel->setText(tr("Select Barcode Type"));
00145         return;
00146     }
00147     else
00148         ui.okButton->setEnabled(true);
00149 
00150     QString s = ui.bcCombo->currentText();
00151     ui.commentEdit->setText(map[s].comment);
00152     if (useSamples)
00153     {
00154         disconnect(ui.codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&)));
00155         ui.codeEdit->setText(map[s].example);
00156         connect(ui.codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&)));
00157     }
00158 
00159     ui.includeCheck->setEnabled(map[s].includeCheck ? true : false);
00160     if (ui.textCheck->isChecked())
00161         ui.includeCheckInText->setEnabled(map[s].includeCheckInText ? true : false);
00162     else
00163         ui.includeCheckInText->setEnabled(false);
00164 
00165     codeEdit_check(ui.codeEdit->text());
00166     paintBarcode();
00167 }
00168 
00169 void BarcodeGenerator::textCheck_changed()
00170 {
00171     bool s = ui.textCheck->checkState();
00172     ui.txtColorButton->setEnabled(s);
00173     ui.includeCheckInText->setEnabled(s);
00174     paintBarcode();
00175 }
00176 
00177 void BarcodeGenerator::guardCheck_changed()
00178 {
00179     paintBarcode();
00180 }
00181 
00182 void BarcodeGenerator::includeCheck_stateChanged(int)
00183 {
00184     paintBarcode();
00185 }
00186 
00187 void BarcodeGenerator::includeCheckInText_stateChanged(int)
00188 {
00189     paintBarcode();
00190 }
00191 
00192 void BarcodeGenerator::paintColorSample(QLabel *l, QColor c)
00193 {
00194     QRect rect = l->frameRect();
00195     QPixmap pm(rect.width(), rect.height());
00196     pm.fill(c);
00197     l->setPixmap(pm);
00198 }
00199 
00200 void BarcodeGenerator::bgColorButton_pressed()
00201 {
00202     bgColor = QColorDialog::getColor(bgColor, this);
00203     if (bgColor.isValid())
00204     {
00205         paintColorSample(ui.bgLabel, bgColor);
00206         paintBarcode();
00207     }
00208 }
00209 
00210 void BarcodeGenerator::lnColorButton_pressed()
00211 {
00212     lnColor = QColorDialog::getColor(lnColor, this);
00213     if (lnColor.isValid())
00214     {
00215         paintColorSample(ui.linesLabel, lnColor);
00216         paintBarcode();
00217     }
00218 }
00219 
00220 void BarcodeGenerator::txtColorButton_pressed()
00221 {
00222     txtColor = QColorDialog::getColor(txtColor, this);
00223     if (txtColor.isValid())
00224     {
00225         paintColorSample(ui.txtLabel, txtColor);
00226         paintBarcode();
00227     }
00228 }
00229 
00230 void BarcodeGenerator::okButton_pressed()
00231 {
00232     // no need to call paintBarcode(tmpFile, 300); because
00233     // it's created by previous run...
00234     hide();
00235     const FileFormat * fmt = LoadSavePlugin::getFormatById(FORMATID_PSIMPORT);
00236     if( fmt )
00237         fmt->loadFile(psFile, LoadSavePlugin::lfUseCurrentPage|LoadSavePlugin::lfInteractive);
00238     accept();
00239 }
00240 
00241 void BarcodeGenerator::cancelButton_pressed()
00242 {
00243     reject();
00244 }
00245 
00246 bool BarcodeGenerator::codeEdit_check(const QString& )//s)
00247 {
00248     /* probably not needed as the backend do it for us (PV)
00249     QRegExp rx(map[bcCombo->currentText()].regularExp);
00250     if (!rx.exactMatch(s))
00251     {
00252         ui.codeEdit->setPaletteBackgroundColor(QColor(255, 0, 0));
00253         ui.sampleLabel->setText("<qt>" + tr("Barcode incomplete") + "</qt>");
00254         ui.okButton->setEnabled(false);
00255         return false;
00256     }
00257     else
00258     {
00259         ui.codeEdit->setPaletteBackgroundColor(guiColor);
00260         ui.okButton->setEnabled(true);
00261         paintBarcode();
00262         return true;
00263     } */
00264     paintBarcode();
00265     return true;
00266 }
00267 
00268 void BarcodeGenerator::codeEdit_textChanged(const QString& s)
00269 {
00270     useSamples = false;
00271     codeEdit_check(s);
00272 }
00273 
00274 bool BarcodeGenerator::paintBarcode(QString fileName, int dpi)
00275 {
00276     if (fileName == QString::null)
00277         fileName = tmpFile;
00278     QString opts("barcolor=%1 showbackground backgroundcolor=%2 textcolor=%3");
00279     opts = opts.arg(lnColor.name().replace('#', "")) \
00280             .arg(bgColor.name().replace('#', "")) \
00281             .arg(txtColor.name().replace('#', ""));
00282     if (ui.textCheck->isChecked())
00283         opts += " includetext";
00284     if (ui.guardCheck->isChecked())
00285         opts += " guardwhitespace";
00286     if (ui.includeCheckInText->isChecked() & ui.includeCheckInText->isEnabled())
00287         opts += " includecheckintext";
00288     if (ui.includeCheck->isChecked() & ui.includeCheck->isEnabled())
00289         opts += " includecheck";
00290     QString comm("15 10 moveto (%1) (%2) %3 barcode");
00291     comm = comm.arg(ui.codeEdit->text()).arg(opts).arg(map[ui.bcCombo->currentText()].command);
00292     comm = psCommand + comm;
00293     QFile f(psFile);
00294     if (!f.open(QIODevice::WriteOnly))
00295     {
00296         ui.sampleLabel->setText("<qt>" + tr("Error opening file: %1").arg(psFile) + "</qt>");
00297         return false;
00298     }
00299     QTextStream ts(&f);
00300     ts << comm;
00301     f.close();
00302 
00303     QStringList gargs;
00304     // limit the area only for preview. EPS importer bounds the box itself.
00305     if (fileName == tmpFile)
00306     {
00307         gargs.append("-dDEVICEWIDTHPOINTS=200");
00308         gargs.append("-dDEVICEHEIGHTPOINTS=150");
00309     }
00310     gargs.append( QString("-r%1").arg(dpi) );
00311     gargs.append( QString("-sOutputFile=%1").arg(fileName) );
00312     gargs.append( psFile );
00313     qApp->changeOverrideCursor(Qt::WaitCursor);
00314     int gs = callGS(gargs);
00315     bool retval = true;
00316     if (gs != 0)
00317         retval = false;
00318     qApp->changeOverrideCursor(Qt::ArrowCursor);
00319 //  qApp->restoreOverrideCursor();
00320     // setup only preview
00321     if (fileName != tmpFile)
00322         return retval;
00323     if (gs == 0)
00324     {
00325         ui.sampleLabel->setPixmap(QPixmap(fileName));
00326         ui.okButton->setEnabled(true);
00327     }
00328     else
00329     {
00330         ui.sampleLabel->setText("<qt>" + tr("Barcode incomplete") + "</qt>");
00331         ui.okButton->setEnabled(false);
00332     }
00333     return retval;
00334 }
00335 
00336 void BarcodeGenerator::resetButton_clicked()
00337 {
00338     useSamples = true;
00339     bcComboChanged();
00340 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.