Subversion Repositories Scribus

Rev

Rev 25110 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4460 subik 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
20185 craig 8
 
10212 cbradney 9
#include "../formatidlist.h"
4460 subik 10
#include "barcodegenerator.h"
10212 cbradney 11
#include "commonstrings.h"
20185 craig 12
#include "iconmanager.h"
10212 cbradney 13
#include "loadsaveplugin.h"
14
#include "scpaths.h"
12042 subik 15
#include "scribuscore.h"
21926 craig 16
#include "ui/colorsandfills.h"
4685 subik 17
#include "undomanager.h"
4460 subik 18
 
22002 craig 19
#include <QDebug>
9461 subik 20
#include <QTextStream>
20308 craig 21
#include <QStandardItemModel>
20325 craig 22
#include <QTimer>
20340 craig 23
#include <QThread>
9461 subik 24
 
20308 craig 25
BarcodeType::BarcodeType(const QString &cmd, const QString &exa, const QString &exaop)
24744 jghali 26
           : command(cmd),
27
             exampleContents(exa),
28
             exampleOptions(exaop)
4460 subik 29
{
24744 jghali 30
 
4460 subik 31
}
32
 
33
BarcodeGenerator::BarcodeGenerator(QWidget* parent, const char* name)
24744 jghali 34
        : QDialog(parent)
4460 subik 35
{
8513 cbradney 36
        ui.setupUi(this);
10593 fschmid 37
        setObjectName(name);
38
        setModal(true);
4527 subik 39
 
24452 jghali 40
        ui.bcodeBox->layout()->setAlignment(Qt::AlignTop);
41
        ui.colorBox->layout()->setAlignment(Qt::AlignTop);
42
 
20340 craig 43
        connect(&thread, SIGNAL(renderedImage(QString)),this, SLOT(updatePreview(QString)));
44
 
20308 craig 45
        /*
46
         *  We extract the barcode information from the BWIPP metadata which looks like this:
47
         *
48
         *  % --BEGIN ENCODER gs1-128--
49
         *  % --REQUIRES preamble raiseerror renlinear code128--
50
         *  % --DESC: GS1-128
51
         *  % --EXAM: (01)95012345678903(3103)000123
52
         *  % --EXOP: includetext
53
         *  % --RNDR: renlinear
54
         *
55
         */
18612 jghali 56
 
21994 craig 57
        QFile f(ScPaths::instance().shareDir() + QString("/plugins/barcode.ps"));
58
        if(!f.open(QIODevice::ReadOnly))
59
        {
60
                qDebug()<<"Barcodegenerator unable to open "<<f.fileName();
61
                return;
62
        }
20308 craig 63
        QTextStream ts(&f);
64
        QString bwipp = ts.readAll();
65
        f.close();
66
 
67
        QRegExp rx(
22638 craig 68
                                "[\\r\\n]+% --BEGIN (RESOURCE|RENDERER|ENCODER) ([\\w-]+)--[\\r\\n]+"
69
                                "(.*[\\r\\n]+)?"
70
                                "(%%BeginResource.*[\\r\\n]+)"
71
                                "% --END \\1 \\2--[\\r\\n]+");
20308 craig 72
        rx.setMinimal(true);
73
        int pos = 0;
74
        while ( (pos = rx.indexIn(bwipp, pos)) != -1 )
75
        {
23724 jghali 76
                int len = rx.matchedLength();
23725 jghali 77
                QString restype = rx.cap(1);
78
                QString resname = rx.cap(2);
79
                QString reshead = rx.cap(3);
80
                QString resbody = rx.cap(4);
20308 craig 81
 
23725 jghali 82
                resbodys[resname] = resbody;
20308 craig 83
 
22638 craig 84
                if (restype=="ENCODER")
20308 craig 85
                {
20311 jghali 86
                        QRegExp rxhead(
22638 craig 87
                                                "% --REQUIRES (.*)--[\\r\\n]+"
88
                                                "% --DESC:(.*)[\\r\\n]+"
89
                                                "% --EXAM:(.*)[\\r\\n]+"
90
                                                "% --EXOP:(.*)[\\r\\n]+"
91
                                                "% --RNDR:(.*)[\\r\\n]+"
92
                                                );
23724 jghali 93
                        if (rxhead.indexIn(reshead) >= 0)
94
                        {
95
                                resreqs[resname] = rxhead.cap(1).trimmed();
96
                                resdescs[resname] = rxhead.cap(2).trimmed();
97
                                resexams[resname] = rxhead.cap(3).trimmed();
98
                                resexops[resname] = rxhead.cap(4).trimmed();
99
                                resrndrs[resname] = rxhead.cap(5).trimmed();
100
                                encoderlist.append(resname);
101
                        }
22638 craig 102
                }
23724 jghali 103
                pos += len;
20311 jghali 104
        }
20308 craig 105
 
21994 craig 106
        foreach (const QString& enc, encoderlist)
107
                map[resdescs[enc]] = BarcodeType(enc, resexams[enc], resexops[enc]);
20308 craig 108
 
109
        /*
110
         *  Ultimately all of this static data about the capabilities of each barcode
22638 craig 111
         *  encoder will be replaced by data read from the barcode.ps metadata, when
20308 craig 112
         *  such data exists...
113
         *
22638 craig 114
         */
20308 craig 115
 
116
        // Content for the version and ecc combos
23725 jghali 117
        resvers["qrcode"] = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40";
118
        resecls["qrcode"] = "L,Q,M,H";
119
        resvers["gs1qrcode"] = resvers["qrcode"];
120
        resecls["gs1qrcode"] = resecls["qrcode"];
25125 craig 121
        resvers["gs1dlqrcode"] = resvers["qrcode"];
122
        resecls["gs1dlqrcode"] = resecls["qrcode"];
23725 jghali 123
        resvers["hibcqrcode"] = resvers["qrcode"];
124
        resecls["hibcqrcode"] = resecls["qrcode"];
125
        resvers["microqrcode"] = "M1,M2,M3,M4";
126
        resecls["microqrcode"] = "L,Q,M,H";
127
        resvers["datamatrix"] = "10x10,12x12,14x14,16x16,18x18,20x20,22x22,24x24,26x26,32x32,36x36,40x40,44x44,48x48,52x52,64x64,72x72,80x80,88x88,96x96,104x104,120x120,132x132,144x144,8x18,8x32,12x26,12x36,16x36,16x48";
128
        resecls["datamatrix"] = "";
129
        resvers["gs1datamatrix"] = resvers["datamatrix"];
130
        resecls["gs1datamatrix"] = resvers["datamatrix"];
25125 craig 131
        resvers["gs1dldatamatrix"] = resvers["datamatrix"];
132
        resecls["gs1dldatamatrix"] = resvers["datamatrix"];
23725 jghali 133
        resvers["hibcdatamatrix"] = resvers["datamatrix"];
134
        resecls["hibcdatamatrix"] = resecls["datamatrix"];
135
        resvers["azteccode"] = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32";
136
        resvlbl["azteccode"] = "Layers";
137
        resecls["azteccode"] = "5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95";
138
        resvers["azteccodecompact"] = "1,2,3,4";
139
        resvlbl["azteccodecompact"] = resvlbl["azteccode"];
140
        resecls["azteccodecompact"] = resecls["azteccode"];
141
        resvers["pdf417"] = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";
142
        resvlbl["pdf417"] = "Columns";
143
        resecls["pdf417"] = "1,2,3,4,5";
144
        resvers["pdf417compact"] = resvers["pdf417"];
145
        resvlbl["pdf417compact"] = resvlbl["pdf417"];
146
        resecls["pdf417compact"] = resecls["pdf417"];
147
        resvers["hibcpdf417"] = resvers["pdf417"];
148
        resvlbl["hibcpdf417"] = resvlbl["pdf417"];
149
        resecls["hibcpdf417"] = resecls["pdf417"];
150
        resvers["micropdf417"] = "1x11,1x14,1x17,1x20,1x24,1x28,2x8,2x11,2x14,2x17,2x20,2x23,2x26,3x6,3x8,3x10,3x12,3x15,3x20,3x26,3x32,3x38,3x44,4x4,4x6,4x8,4x10,4x12,4x15,4x20,4x26,4x32,4x38,4x44";
151
        resecls["micropdf417"] = "";
152
        resvers["hibcmicropdf417"] = resvers["micropdf417"];
153
        resecls["hibcmicropdf417"] = resecls["micropdf417"];
154
        resvers["hanxin"] = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84";
155
        resecls["hanxin"] = "L1,L2,L3,L4";
156
        resecls["ultracode"] = "EC0,EC1,EC2,EC3,EC4,EC5";
20308 craig 157
 
21413 craig 158
 
20308 craig 159
        // Which options checkboxes are enabled for each encoder
25110 craig 160
        // Check whether `includetext` option is available for each encoder
161
        for(const QString &enc : encoderlist)
162
                resincludetextAvail[enc] = resexops[enc].contains("includetext");
20308 craig 163
 
164
        QStringList guardwhitespaceAvail;
21994 craig 165
        guardwhitespaceAvail << "ean13" << "ean8" << "isbn" << "ismn" << "issn" << "ean13composite" << "ean8composite";
166
        foreach (const QString& enc, guardwhitespaceAvail)
23725 jghali 167
                resguardwhitespaceAvail[enc] = true;
20308 craig 168
 
20311 jghali 169
        QStringList includecheckAvail;
21994 craig 170
        includecheckAvail << "bc412" << "channelcode" << "code11" << "code2of5" << "coop2of5" << "datalogic2of5"
171
                                          << "iata2of5" << "industrial2of5" << "matrix2of5" << "code39" << "code39ext"
172
                                          << "code93" << "code93ext" << "interleaved2of5" << "msi" << "rationalizedCodabar";
173
        foreach (const QString& enc, includecheckAvail)
23725 jghali 174
                resincludecheckAvail[enc] = true;
20308 craig 175
 
20311 jghali 176
        QStringList includecheckintextAvail;
21994 craig 177
        includecheckintextAvail << "bc412" << "code11" << "code2of5" << "coop2of5" << "datalogic2of5" << "iata2of5"
178
                                                        << "industrial2of5" << "matrix2of5" << "code39" << "code39ext" << "interleaved2of5"
179
                                                        << "japanpost" << "msi" << "planet" << "plessey" << "postnet" << "rationalizedCodabar" << "royalmail";
180
        foreach (const QString& enc, includecheckintextAvail)
23725 jghali 181
                resincludecheckintextAvail[enc] = true;
20308 craig 182
 
20311 jghali 183
        QStringList parseAvail;
21994 craig 184
        parseAvail << "azteccode" << "azteccodecompact" << "codablockf" << "hibccodablockf" << "code128" << "hibccode128" << "code16k" << "code39ext" << "code49"
185
                           << "code93ext" << "codeone" << "datamatrix" << "hibcdatamatrix" << "maxicode" << "micropdf417" << "hibcmicropdf417" << "pdf417" << "hibcpdf417"
22439 craig 186
                           << "pdf417compact" << "posicode" << "qrcode" << "hibcqrcode" << "microqrcode" << "telepen" << "hanxin" << "dotcode" << "ultracode";
21994 craig 187
        foreach (const QString& enc, parseAvail)
23725 jghali 188
                resparseAvail[enc] = true;
20308 craig 189
 
20311 jghali 190
        QStringList parsefncAvail;
21994 craig 191
        parsefncAvail << "codablockf" << "code128" << "code16k" << "code49" << "code93" << "codeone"
22439 craig 192
                                  << "datamatrix" << "posicode" << "qrcode" << "microqrcode" << "dotcode" << "ultracode";
21994 craig 193
        foreach (const QString& enc, parsefncAvail)
23725 jghali 194
                resparsefncAvail[enc] = true;
20308 craig 195
 
20340 craig 196
        // Building up the bcFamilyCombo grouping the formats for readablity
197
        ui.bcFamilyCombo->addItem(tr("Select a barcode family")); // to prevent 1st gs call
198
        ui.bcFamilyCombo->insertSeparator(999);
199
 
20311 jghali 200
        // Building up the bcCombo grouping the formats for readablity
20308 craig 201
        ui.bcCombo->addItem(tr("Select a barcode format")); // to prevent 1st gs call
20311 jghali 202
        ui.bcCombo->insertSeparator(999);
20308 craig 203
 
20340 craig 204
        QString familyName;
205
        QStringList bcNames;
206
        bcNames << "EAN-13" << "EAN-8" << "UPC-A" << "UPC-E" << "ISBN" << "ISMN" << "ISSN";
207
        familyName = tr("Point of Sale");
208
        familyList.append(familyName);
209
        familyItems.insert(familyName, bcNames);
20308 craig 210
 
20340 craig 211
        bcNames.clear();
25125 craig 212
        bcNames << "GS1-14" << "GS1 Data Matrix" << "GS1 QR Code" << "GS1-128" << "ITF-14" << "SSCC-18"
213
                        << "GS1 Digital Link QR Code" << "GS1 Digital Link Data Matrix";
20340 craig 214
        familyName = tr("Supply Chain");
215
        familyList.append(familyName);
216
        familyItems.insert(familyName, bcNames);
20308 craig 217
 
20340 craig 218
        bcNames.clear();
21994 craig 219
        bcNames << "QR Code" << "Micro QR Code" << "Data Matrix" << "Aztec Code" << "Compact Aztec Code"
22439 craig 220
                        << "Aztec Runes" << "PDF417" << "Compact PDF417" << "MicroPDF417" << "Han Xin Code"
221
                        << "DotCode" << "Ultracode";
20340 craig 222
        familyName = tr("Two-dimensional symbols");
223
        familyList.append(familyName);
224
        familyItems.insert(familyName, bcNames);
20308 craig 225
 
20340 craig 226
        bcNames.clear();
21994 craig 227
        bcNames << "Code 128" << "Code 39" << "Code 39 Extended" << "Code 93" << "Code 93 Extended"
228
                        << "Interleaved 2 of 5 (ITF)";
20340 craig 229
        familyName = tr("One-dimensional symbols");
230
        familyList.append(familyName);
231
        familyItems.insert(familyName, bcNames);
20308 craig 232
 
20340 craig 233
        bcNames.clear();
21994 craig 234
        bcNames << "GS1 DataBar Omnidirectional" << "GS1 DataBar Stacked Omnidirectional"
235
                        << "GS1 DataBar Expanded" << "GS1 DataBar Expanded Stacked" << "GS1 DataBar Truncated"
22439 craig 236
                        << "GS1 DataBar Stacked" << "GS1 DataBar Limited" << "GS1 North American Coupon";
20340 craig 237
        familyName = tr("GS1 DataBar family");
238
        familyList.append(familyName);
239
        familyItems.insert(familyName, bcNames);
20308 craig 240
 
20340 craig 241
        bcNames.clear();
21994 craig 242
        bcNames << "AusPost 4 State Customer Code" << "Deutsche Post Identcode" << "Deutsche Post Leitcode"
243
                        << "Japan Post 4 State Customer Code" << "Royal Dutch TPG Post KIX"
244
                        << "Royal Mail 4 State Customer Code" << "USPS Intelligent Mail" << "USPS PLANET" << "USPS POSTNET";
20340 craig 245
        familyName = tr("Postal symbols");
246
        familyList.append(familyName);
247
        familyItems.insert(familyName, bcNames);
20308 craig 248
 
20340 craig 249
        bcNames.clear();
21994 craig 250
        bcNames << "Italian Pharmacode" << "Pharmaceutical Binary Code" << "Two-track Pharmacode"
251
                        << "Pharmazentralnummer (PZN)" << "HIBC Codablock F" << "HIBC Code 128" << "HIBC Code 39"
252
                        << "HIBC Data Matrix" << "HIBC MicroPDF417" << "HIBC PDF417" << "HIBC QR Code";
20340 craig 253
        familyName = tr("Pharmaceutical symbols");
254
        familyList.append(familyName);
255
        familyItems.insert(familyName, bcNames);
20308 craig 256
 
20340 craig 257
        bcNames.clear();
21994 craig 258
        bcNames << "Code 11" << "Codabar" << "Code 25" << "COOP 2 of 5" << "Datalogic 2 of 5" << "IATA 2 of 5"
259
                        << "Industrial 2 of 5" << "Matrix 2 of 5" << "MSI Modified Plessey" << "Plessey UK"
260
                        << "PosiCode" << "Telepen" << "Telepen Numeric"<< "Code 16K" << "Codablock F" << "Code 49"
261
                        << "Code One";
20340 craig 262
        familyName = tr("Less-used symbols");
263
        familyList.append(familyName);
264
        familyItems.insert(familyName, bcNames);
20308 craig 265
 
20340 craig 266
        bcNames.clear();
21994 craig 267
        bcNames << "EAN-13 Composite" << "EAN-8 Composite" << "UPC-A Composite" << "UPC-E Composite"
268
                        << "GS1 DataBar Omnidirectional Composite" << "GS1 DataBar Stacked Omnidirectional Composite"
269
                        << "GS1 DataBar Expanded Composite" << "GS1 DataBar Expanded Stacked Composite"
270
                        << "GS1 DataBar Truncated Composite" << "GS1 DataBar Stacked Composite"
271
                        << "GS1 DataBar Limited Composite" << "GS1-128 Composite";
20340 craig 272
        familyName = tr("GS1 Composite symbols");
273
        familyList.append(familyName);
274
        familyItems.insert(familyName, bcNames);
20308 craig 275
 
20340 craig 276
        ui.bcFamilyCombo->addItems(familyList);
277
 
20308 craig 278
        /*
22638 craig 279
         *  End of the hard-coded data
20308 craig 280
         *
281
         */
282
 
10593 fschmid 283
        guiColor = ui.codeEdit->palette().color(QPalette::Window);
20308 craig 284
 
8513 cbradney 285
        ui.okButton->setText(CommonStrings::tr_OK);
286
        ui.cancelButton->setText(CommonStrings::tr_Cancel);
23054 craig 287
        ui.resetButton->setIcon(IconManager::instance().loadIcon("u_undo16.png"));
13582 subik 288
 
289
        if (ScCore->primaryMainWindow()->doc->PageColors.contains("Black"))
290
        {
291
                lnColor = ScCore->primaryMainWindow()->doc->PageColors["Black"];
292
                txtColor = ScCore->primaryMainWindow()->doc->PageColors["Black"];
293
                ui.linesLabel->setToolTip("Black");
294
                ui.txtLabel->setToolTip("Black");
295
        }
296
        else
297
        {
298
                ui.linesLabel->setToolTip("n.a.");
299
                ui.txtLabel->setToolTip("n.a.");
300
        }
301
        if (ScCore->primaryMainWindow()->doc->PageColors.contains("White"))
302
        {
303
                bgColor = ScCore->primaryMainWindow()->doc->PageColors["White"];
304
                ui.bgLabel->setToolTip("White");
305
        }
306
        else
307
                ui.bgLabel->setToolTip("n.a.");
308
 
8513 cbradney 309
        paintColorSample(ui.linesLabel, lnColor);
310
        paintColorSample(ui.txtLabel, txtColor);
311
        paintColorSample(ui.bgLabel, bgColor);
4460 subik 312
 
24744 jghali 313
        paintBarcodeTimer = new QTimer(this);
20325 craig 314
        paintBarcodeTimer->setSingleShot(true);
315
        connect(paintBarcodeTimer, SIGNAL(timeout()), this, SLOT(paintBarcode()));
316
 
20340 craig 317
        connect(ui.bcFamilyCombo, SIGNAL(activated(QString)), this, SLOT(bcFamilyComboChanged()));
318
        connect(ui.bcCombo, SIGNAL(activated(QString)), this, SLOT(bcComboChanged()));
319
        connect(ui.bgColorButton, SIGNAL(clicked()), this, SLOT(bgColorButton_pressed()));
320
        connect(ui.lnColorButton, SIGNAL(clicked()), this, SLOT(lnColorButton_pressed()));
321
        connect(ui.txtColorButton, SIGNAL(clicked()), this, SLOT(txtColorButton_pressed()));
322
        connect(ui.okButton, SIGNAL(clicked()), this, SLOT(okButton_pressed()));
323
        connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(cancelButton_pressed()));
324
        connect(ui.codeEdit, SIGNAL(textChanged(QString)), this, SLOT(codeEdit_textChanged(QString)));
325
        connect(ui.resetButton, SIGNAL(clicked()), this, SLOT(resetButton_clicked()));
4460 subik 326
        bcComboChanged();
20325 craig 327
 
4460 subik 328
}
329
 
330
BarcodeGenerator::~BarcodeGenerator()
331
{
21994 craig 332
        if (!paintBarcodeTimer)
333
                return;
334
        delete paintBarcodeTimer;
23725 jghali 335
        paintBarcodeTimer = nullptr;
4460 subik 336
}
337
 
20308 craig 338
void BarcodeGenerator::updateOptions()
18612 jghali 339
{
20308 craig 340
        QString enc=map[ui.bcCombo->currentText()].command;
18612 jghali 341
 
23726 jghali 342
        ui.formatLabel->setText(resvlbl.contains(enc) ? resvlbl[enc] + ":" : "Version:");
20308 craig 343
        ui.formatCombo->blockSignals(true);
344
        ui.formatCombo->clear();
345
        ui.formatCombo->addItem("Auto");
22856 jghali 346
        if (resvers.contains(enc))
20308 craig 347
        {
348
                ui.formatCombo->insertSeparator(999);
349
                ui.formatCombo->addItems(resvers[enc].split(","));
350
                ui.formatLabel->setEnabled(true);
351
                ui.formatCombo->setEnabled(true);
352
        }
353
        else
354
        {
355
                ui.formatLabel->setEnabled(false);
356
                ui.formatCombo->setEnabled(false);
357
        }
20311 jghali 358
        ui.formatCombo->blockSignals(false);
18612 jghali 359
 
20308 craig 360
        ui.eccCombo->blockSignals(true);
20311 jghali 361
        ui.eccCombo->clear();
362
        ui.eccCombo->addItem("Auto");
22856 jghali 363
        if (resecls.contains(enc))
20308 craig 364
        {
365
                ui.eccCombo->insertSeparator(999);
20311 jghali 366
                ui.eccCombo->addItems(resecls[enc].split(","));
367
                ui.eccLabel->setEnabled(true);
368
                ui.eccCombo->setEnabled(true);
369
        }
20308 craig 370
        else
371
        {
372
                ui.eccLabel->setEnabled(false);
373
                ui.eccCombo->setEnabled(false);
374
        }
20311 jghali 375
        ui.eccCombo->blockSignals(false);
18612 jghali 376
 
377
}
378
 
20340 craig 379
 
380
void BarcodeGenerator::bcFamilyComboChanged()
381
{
382
        ui.bcCombo->blockSignals(true);
383
        ui.bcCombo->clear();
384
        ui.bcCombo->addItem(tr("Select a barcode format")); // to prevent 1st gs call
385
        ui.bcCombo->insertSeparator(999);
386
        ui.bcCombo->addItems(familyItems[ui.bcFamilyCombo->currentText()]);
387
        ui.bcCombo->blockSignals(false);
388
        bcComboChanged();
389
}
390
 
24744 jghali 391
void BarcodeGenerator::bcComboChanged(int)
18612 jghali 392
{
393
        bcComboChanged();
394
}
395
 
4460 subik 396
void BarcodeGenerator::bcComboChanged()
397
{
20308 craig 398
        updateOptions();
399
 
11228 subik 400
        if (ui.bcCombo->currentIndex() == 0)
401
        {
402
                ui.okButton->setEnabled(false);
14832 cbradney 403
                ui.sampleLabel->setText(tr("Select Type"));
20311 jghali 404
                ui.codeEdit->clear();
20308 craig 405
                ui.codeEdit->setEnabled(false);
20311 jghali 406
                ui.optionsEdit->clear();
20308 craig 407
                ui.optionsEdit->setEnabled(false);
20311 jghali 408
                ui.includetextCheck->setEnabled(false);
409
                ui.guardwhitespaceCheck->setEnabled(false);
410
                ui.includecheckCheck->setEnabled(false);
411
                ui.includecheckintextCheck->setEnabled(false);
412
                ui.parseCheck->setEnabled(false);
413
                ui.parsefncCheck->setEnabled(false);
414
                ui.formatLabel->setEnabled(false);
415
                ui.formatCombo->setEnabled(false);
416
                ui.eccLabel->setEnabled(false);
417
                ui.eccCombo->setEnabled(false);
20308 craig 418
                ui.bgColorButton->setEnabled(false);
419
                ui.lnColorButton->setEnabled(false);
420
                ui.txtColorButton->setEnabled(false);
11228 subik 421
                return;
422
        }
18612 jghali 423
 
20308 craig 424
        ui.codeEdit->setEnabled(true);
425
        ui.optionsEdit->setEnabled(true);
426
        ui.bgColorButton->setEnabled(true);
427
        ui.lnColorButton->setEnabled(true);
428
        ui.txtColorButton->setEnabled(true);
19688 jghali 429
        ui.okButton->setEnabled(true);
11228 subik 430
 
8513 cbradney 431
        QString s = ui.bcCombo->currentText();
20325 craig 432
        ui.codeEdit->blockSignals(true);
433
        ui.codeEdit->setText(map[s].exampleContents);
434
        ui.codeEdit->blockSignals(false);
435
        ui.optionsEdit->blockSignals(true);
436
        ui.optionsEdit->setText(map[s].exampleOptions);
437
        ui.optionsEdit->blockSignals(false);
4835 subik 438
 
23725 jghali 439
        QString enc = map[s].command;
20311 jghali 440
        ui.includetextCheck->setEnabled(resincludetextAvail[enc]);
441
        ui.guardwhitespaceCheck->setEnabled(resguardwhitespaceAvail[enc]);
442
        ui.includecheckCheck->setEnabled(resincludecheckAvail[enc]);
443
        ui.includecheckintextCheck->setEnabled(resincludetextAvail[enc] && resincludecheckintextAvail[enc]);
444
        ui.parseCheck->setEnabled(resparseAvail[enc]);
445
        ui.parsefncCheck->setEnabled(resparsefncAvail[enc]);
4835 subik 446
 
20308 craig 447
        updateUIFromOptionsText();
18612 jghali 448
 
20325 craig 449
        enqueuePaintBarcode(0);
4460 subik 450
}
451
 
20325 craig 452
void BarcodeGenerator::enqueuePaintBarcode(int delay)
453
{
454
        ui.okButton->setEnabled(false);
22638 craig 455
        //      paintBarcode();
20325 craig 456
        paintBarcodeTimer->start(delay);
457
}
458
 
20340 craig 459
void BarcodeGenerator::updateOptionsTextFromUI()
460
{
23725 jghali 461
        QString opts = ui.optionsEdit->text();
20308 craig 462
 
20340 craig 463
        if (ui.includetextCheck->isChecked())
464
        {
20311 jghali 465
                if (!opts.contains(QRegExp("\\bincludetext\\b")))
466
                        opts.append(" includetext");
20340 craig 467
        }
468
        else
469
        {
22638 craig 470
                opts.replace(QRegExp("\\bincludetext\\b")," ");
20311 jghali 471
        }
20308 craig 472
 
20340 craig 473
        if (ui.guardwhitespaceCheck->isChecked())
474
        {
22638 craig 475
                if (!opts.contains(QRegExp("\\bguardwhitespace\\b")))
20311 jghali 476
                        opts.append(" guardwhitespace");
20340 craig 477
        }
478
        else
479
        {
22638 craig 480
                opts.replace(QRegExp("\\bguardwhitespace\\b")," ");
20311 jghali 481
        }
20308 craig 482
 
20340 craig 483
        if (ui.includecheckCheck->isChecked())
484
        {
20311 jghali 485
                if (!opts.contains(QRegExp("\\bincludecheck\\b")))
486
                        opts.append(" includecheck");
20340 craig 487
        }
488
        else
489
        {
20311 jghali 490
                opts.replace(QRegExp("\\bincludecheck\\b")," ");
491
        }
20308 craig 492
 
20340 craig 493
        if (ui.includecheckintextCheck->isChecked())
494
        {
20311 jghali 495
                if (!opts.contains(QRegExp("\\bincludecheckintext\\b")))
496
                        opts.append(" includecheckintext");
20340 craig 497
        }
498
        else
499
        {
20311 jghali 500
                opts.replace(QRegExp("\\bincludecheckintext\\b")," ");
501
        }
20308 craig 502
 
20340 craig 503
        if (ui.parseCheck->isChecked())
504
        {
20311 jghali 505
                if (!opts.contains(QRegExp("\\bparse\\b")))
506
                        opts.append(" parse");
20340 craig 507
        }
508
        else
509
        {
20311 jghali 510
                opts.replace(QRegExp("\\bparse\\b")," ");
511
        }
20308 craig 512
 
20340 craig 513
        if (ui.parsefncCheck->isChecked())
514
        {
22638 craig 515
                if (!opts.contains(QRegExp("\\bparsefnc\\b")))
20311 jghali 516
                        opts.append(" parsefnc");
20340 craig 517
        }
518
        else
519
        {
20311 jghali 520
                opts.replace(QRegExp("\\bparsefnc\\b")," ");
521
        }
20308 craig 522
 
23725 jghali 523
        QString enc = map[ui.bcCombo->currentText()].command;
524
        QString vlbl = resvlbl.contains(enc) ? resvlbl[enc].toLower() : "version";
20308 craig 525
 
22638 craig 526
        if (ui.formatCombo->currentIndex() != 0)
20311 jghali 527
        {
23725 jghali 528
                QString t = ui.formatCombo->currentText();
529
                if (!opts.contains(QRegExp("\\b" + QRegExp::escape(vlbl) + "=.*\\b")))
530
                        opts.append(" " + vlbl + "=" + t);
20311 jghali 531
                else
23725 jghali 532
                        opts.replace(QRegExp("\\b" + QRegExp::escape(vlbl) + "=\\S*\\b"), vlbl + "=" + t);
20340 craig 533
        }
534
        else
535
        {
23725 jghali 536
                opts.replace(QRegExp("\\b" + QRegExp::escape(vlbl) + "=\\S*\\b"), " ");
20311 jghali 537
        }
20308 craig 538
 
20340 craig 539
        if (ui.eccCombo->currentIndex() != 0)
540
        {
23725 jghali 541
                QString t = ui.eccCombo->currentText();
22638 craig 542
                if (!opts.contains(QRegExp("\\beclevel=.*\\b")))
23725 jghali 543
                        opts.append(" eclevel=" + t);
20311 jghali 544
                else
23725 jghali 545
                        opts.replace(QRegExp("\\beclevel=\\S*\\b"), "eclevel=" + t);
20340 craig 546
        }
547
        else
548
        {
20311 jghali 549
                opts.replace(QRegExp("\\beclevel=\\S*\\b")," ");
550
        }
20308 craig 551
 
552
        ui.optionsEdit->blockSignals(true);
20311 jghali 553
        ui.optionsEdit->setText(opts.simplified());
20308 craig 554
        ui.optionsEdit->blockSignals(false);
555
 
556
}
557
 
558
void BarcodeGenerator::updateUIFromOptionsText()
4460 subik 559
{
22638 craig 560
        ui.includetextCheck->blockSignals(true);
20308 craig 561
        ui.includetextCheck->setChecked(ui.optionsEdit->text().contains(QRegExp("\\bincludetext\\b")));
22638 craig 562
        ui.includetextCheck->blockSignals(false);
20308 craig 563
 
22638 craig 564
        ui.guardwhitespaceCheck->blockSignals(true);
20308 craig 565
        ui.guardwhitespaceCheck->setChecked(ui.optionsEdit->text().contains(QRegExp("\\bguardwhitespace\\b")));
22638 craig 566
        ui.guardwhitespaceCheck->blockSignals(false);
20308 craig 567
 
22638 craig 568
        ui.includecheckCheck->blockSignals(true);
20308 craig 569
        ui.includecheckCheck->setChecked(ui.optionsEdit->text().contains(QRegExp("\\bincludecheck\\b")));
22638 craig 570
        ui.includecheckCheck->blockSignals(false);
20308 craig 571
 
22638 craig 572
        ui.includecheckintextCheck->blockSignals(true);
20308 craig 573
        ui.includecheckintextCheck->setChecked(ui.optionsEdit->text().contains(QRegExp("\\bincludecheckintext\\b")));
22638 craig 574
        ui.includecheckintextCheck->blockSignals(false);
20308 craig 575
 
22638 craig 576
        ui.parseCheck->blockSignals(true);
20308 craig 577
        ui.parseCheck->setChecked(ui.optionsEdit->text().contains(QRegExp("\\bparse\\b")));
22638 craig 578
        ui.parseCheck->blockSignals(false);
20308 craig 579
 
22638 craig 580
        ui.parsefncCheck->blockSignals(true);
20308 craig 581
        ui.parsefncCheck->setChecked(ui.optionsEdit->text().contains(QRegExp("\\bparsefnc\\b")));
22638 craig 582
        ui.parsefncCheck->blockSignals(false);
20308 craig 583
 
23725 jghali 584
        QString enc = map[ui.bcCombo->currentText()].command;
585
        QString vlbl = resvlbl.contains(enc) ? resvlbl[enc].toLower() : "version";
20340 craig 586
 
23725 jghali 587
        QRegExp rxf("\\b" + QRegExp::escape(vlbl) + "=(\\S*)\\b");
22638 craig 588
        ui.formatCombo->blockSignals(true);
20340 craig 589
        if (ui.optionsEdit->text().contains(rxf))
590
        {
23725 jghali 591
                int idx = ui.formatCombo->findText(rxf.cap(1));
20311 jghali 592
                if (idx == -1)
23725 jghali 593
                        idx = 0;
20308 craig 594
                ui.formatCombo->setCurrentIndex(idx);
20340 craig 595
        }
596
        else
597
        {
20308 craig 598
                ui.formatCombo->setCurrentIndex(0);
20311 jghali 599
        }
22638 craig 600
        ui.formatCombo->blockSignals(false);
20308 craig 601
 
602
        QRegExp rxe("\\beclevel=(\\S*)\\b");
22638 craig 603
        ui.eccCombo->blockSignals(true);
20340 craig 604
        if (ui.optionsEdit->text().contains(rxe))
605
        {
20311 jghali 606
                int idx=ui.eccCombo->findText(rxe.cap(1));
607
                if (idx == -1)
23725 jghali 608
                        idx = 0;
20311 jghali 609
                ui.eccCombo->setCurrentIndex(idx);
20340 craig 610
        }
611
        else
612
        {
20308 craig 613
                ui.eccCombo->setCurrentIndex(0);
20311 jghali 614
        }
615
        ui.eccCombo->blockSignals(false);
20308 craig 616
}
617
 
22635 craig 618
void BarcodeGenerator::updatePreview(const QString& errorMsg)
20340 craig 619
{
21526 craig 620
        QString pngFile = QDir::toNativeSeparators(ScPaths::tempFileDir() + "bcode.png");
23725 jghali 621
        if (errorMsg.isEmpty())
20340 craig 622
        {
623
                ui.sampleLabel->setPixmap(QPixmap(pngFile));
624
                ui.okButton->setEnabled(true);
625
        }
626
        else
627
        {
23725 jghali 628
                ui.sampleLabel->setText("<qt>" + errorMsg + "</qt>");
20340 craig 629
        }
630
}
631
 
20308 craig 632
void BarcodeGenerator::on_includetextCheck_stateChanged(int)
633
{
634
        updateOptionsTextFromUI();
20325 craig 635
        enqueuePaintBarcode(0);
4460 subik 636
}
637
 
20308 craig 638
void BarcodeGenerator::on_guardwhitespaceCheck_stateChanged(int)
4460 subik 639
{
20308 craig 640
        updateOptionsTextFromUI();
20325 craig 641
        enqueuePaintBarcode(0);
4460 subik 642
}
643
 
20308 craig 644
void BarcodeGenerator::on_includecheckCheck_stateChanged(int)
4835 subik 645
{
20308 craig 646
        updateOptionsTextFromUI();
20325 craig 647
        enqueuePaintBarcode(0);
4835 subik 648
}
649
 
20308 craig 650
void BarcodeGenerator::on_includecheckintextCheck_stateChanged(int)
4835 subik 651
{
20308 craig 652
        updateOptionsTextFromUI();
20325 craig 653
        enqueuePaintBarcode(0);
4835 subik 654
}
655
 
20308 craig 656
void BarcodeGenerator::on_parseCheck_stateChanged(int)
657
{
658
        updateOptionsTextFromUI();
20325 craig 659
        enqueuePaintBarcode(0);
20308 craig 660
}
661
 
662
void BarcodeGenerator::on_parsefncCheck_stateChanged(int)
663
{
664
        updateOptionsTextFromUI();
20325 craig 665
        enqueuePaintBarcode(0);
20308 craig 666
}
667
 
668
void BarcodeGenerator::on_formatCombo_currentIndexChanged(int)
669
{
670
        updateOptionsTextFromUI();
20325 craig 671
        enqueuePaintBarcode(0);
20308 craig 672
}
673
 
674
void BarcodeGenerator::on_eccCombo_currentIndexChanged(int)
675
{
676
        updateOptionsTextFromUI();
20325 craig 677
        enqueuePaintBarcode(0);
20308 craig 678
}
679
 
13582 subik 680
void BarcodeGenerator::paintColorSample(QLabel *l, const ScColor & c)
4460 subik 681
{
24453 jghali 682
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
683
        QPixmap currentPixmap = l->pixmap(Qt::ReturnByValue);
684
#else
24459 jghali 685
        QPixmap currentPixmap = l->pixmap() ? *(l->pixmap()) : QPixmap();
24453 jghali 686
#endif
687
 
688
        double pixelRatio = l->devicePixelRatioF();
689
        QSize pixmapSize(currentPixmap.width(), currentPixmap.height());
690
        if (currentPixmap.isNull())
691
        {
692
                QRect rect = l->frameRect();
693
                pixmapSize = QSize(rect.width() * pixelRatio, rect.height() * pixelRatio);
694
        }
695
        QPixmap pm(pixmapSize.width(), pixmapSize.height());
13582 subik 696
        pm.fill(c.getRawRGBColor()); // brute force sc2qt color convert for preview
4460 subik 697
        l->setPixmap(pm);
698
}
699
 
700
void BarcodeGenerator::bgColorButton_pressed()
701
{
21926 craig 702
        ColorsAndFillsDialog d(this, &ScCore->primaryMainWindow()->doc->docGradients, ScCore->primaryMainWindow()->doc->PageColors, "", &ScCore->primaryMainWindow()->doc->docPatterns, ScCore->primaryMainWindow()->doc, ScCore->primaryMainWindow());
13582 subik 703
        if (!d.exec())
704
                return;
24453 jghali 705
 
706
        QString selectedColorName = d.selectedColorName();
707
        if (selectedColorName == CommonStrings::None)
708
                return;
709
 
13582 subik 710
        bgColor = d.selectedColor();
711
        ui.bgLabel->setToolTip(d.selectedColorName());
712
        paintColorSample(ui.bgLabel, bgColor);
20325 craig 713
        enqueuePaintBarcode(0);
4460 subik 714
}
715
 
716
void BarcodeGenerator::lnColorButton_pressed()
717
{
21926 craig 718
        ColorsAndFillsDialog d(this, &ScCore->primaryMainWindow()->doc->docGradients, ScCore->primaryMainWindow()->doc->PageColors, "", &ScCore->primaryMainWindow()->doc->docPatterns, ScCore->primaryMainWindow()->doc, ScCore->primaryMainWindow());
13582 subik 719
        if (!d.exec())
720
                return;
24453 jghali 721
 
722
        QString selectedColorName = d.selectedColorName();
723
        if (selectedColorName == CommonStrings::None)
724
                return;
725
 
13582 subik 726
        lnColor = d.selectedColor();
727
        ui.linesLabel->setToolTip(d.selectedColorName());
728
        paintColorSample(ui.linesLabel, lnColor);
20325 craig 729
        enqueuePaintBarcode(0);
4460 subik 730
}
731
 
732
void BarcodeGenerator::txtColorButton_pressed()
733
{
21926 craig 734
        ColorsAndFillsDialog d(this, &ScCore->primaryMainWindow()->doc->docGradients, ScCore->primaryMainWindow()->doc->PageColors, "", &ScCore->primaryMainWindow()->doc->docPatterns, ScCore->primaryMainWindow()->doc, ScCore->primaryMainWindow());
13582 subik 735
        if (!d.exec())
736
                return;
24453 jghali 737
 
738
        QString selectedColorName = d.selectedColorName();
739
        if (selectedColorName == CommonStrings::None)
740
                return;
741
 
13582 subik 742
        txtColor = d.selectedColor();
743
        ui.txtLabel->setToolTip(d.selectedColorName());
744
        paintColorSample(ui.txtLabel, txtColor);
20325 craig 745
        enqueuePaintBarcode(0);
4460 subik 746
}
747
 
748
void BarcodeGenerator::okButton_pressed()
749
{
21526 craig 750
        QString psFile = QDir::toNativeSeparators(ScPaths::tempFileDir() + "bcode.ps");
20340 craig 751
 
752
        // no need to call paintBarcode(pngFile, 300); because
4685 subik 753
        // it's created by previous run...
4762 subik 754
        hide();
18297 fschmid 755
        const FileFormat * fmt = LoadSavePlugin::getFormatByExt("ps");
12042 subik 756
 
19420 jghali 757
        UndoTransaction tran;
12042 subik 758
        if (UndoManager::undoEnabled())
759
        {
19420 jghali 760
                tran = UndoManager::instance()->beginTransaction(
22638 craig 761
                                   ScCore->primaryMainWindow()->doc->currentPage()->getUName(),
762
                                   Um::IImageFrame,
763
                                   Um::ImportBarcode,
764
                                   ui.bcCombo->currentText() + " (" + ui.codeEdit->text() + ")",
765
                                   Um::IEPS);
12042 subik 766
        }
767
 
768
        if (fmt)
769
        {
24454 jghali 770
                fmt->loadFile(psFile, LoadSavePlugin::lfUseCurrentPage|LoadSavePlugin::lfInteractive|LoadSavePlugin::lfNoDialogs);
12042 subik 771
                if (tran)
19420 jghali 772
                        tran.commit();
12042 subik 773
        }
4460 subik 774
        accept();
775
}
776
 
777
void BarcodeGenerator::cancelButton_pressed()
778
{
779
        reject();
780
}
781
 
24744 jghali 782
void BarcodeGenerator::codeEdit_textChanged(const QString&)
4460 subik 783
{
20340 craig 784
        enqueuePaintBarcode(0);
4762 subik 785
}
786
 
24744 jghali 787
void BarcodeGenerator::on_optionsEdit_textChanged(const QString&)
20308 craig 788
{
20311 jghali 789
        updateUIFromOptionsText();
20340 craig 790
        enqueuePaintBarcode(0);
20308 craig 791
}
792
 
20340 craig 793
void BarcodeGenerator::paintBarcode()
4460 subik 794
{
20308 craig 795
        QString coloropts("barcolor=%1 showbackground backgroundcolor=%2 textcolor=%3");
22638 craig 796
        coloropts = coloropts.arg(lnColor.name().replace('#', ""), bgColor.name().replace('#', ""), txtColor.name().replace('#', ""));
20308 craig 797
 
23726 jghali 798
        QString opts=ui.optionsEdit->text() + " " + coloropts;
20308 craig 799
 
20311 jghali 800
        // Assemble PS from encoder and requirement bodies
23726 jghali 801
        QString psCommand = "%!PS-Adobe-2.0 EPSF-2.0\n";
20311 jghali 802
        QString req;
23725 jghali 803
        QString enc = map[ui.bcCombo->currentText()].command;
22638 craig 804
        foreach (req, resreqs[enc].split(" "))
20311 jghali 805
                psCommand.append(resbodys[req]);
806
        psCommand.append(resbodys[enc]);
20329 craig 807
        psCommand.append(
22638 craig 808
                                "errordict begin\n"
809
                                "/handleerror {\n"
810
                                "$error begin\n"
811
                                "errorname dup length string cvs 0 6 getinterval (bwipp.) eq {\n"
812
                                "(%stderr) (w) file\n"
813
                                "dup (\nBWIPP ERROR: ) writestring\n"
814
                                "dup errorname dup length string cvs writestring\n"
815
                                "dup ( ) writestring\n"
816
                                "dup errorinfo dup length string cvs writestring\n"
817
                                "dup (\n) writestring\n"
818
                                "dup flushfile end quit\n"
819
                                "} if\n"
820
                                "end //handleerror exec\n"
821
                                "} bind def\n"
822
                                "end\n"
823
                                );
20308 craig 824
        QString comm("20 10 moveto <%1> <%2> /%3 /uk.co.terryburton.bwipp findresource exec\n");
25087 jghali 825
        QString bcString = ui.codeEdit->text();
25084 jghali 826
        QByteArray bcLatin1 = ui.codeEdit->text().toLatin1();
827
        QByteArray bcUtf8 = ui.codeEdit->text().toUtf8();
25087 jghali 828
        QByteArray bcArray = (bcString != QString::fromLatin1(bcLatin1)) ? ("\xef\xbb\xbf" + bcUtf8) : bcLatin1;
25084 jghali 829
        QString bcdata(bcArray.toHex());
20311 jghali 830
        QString bcopts(opts.toLatin1().toHex());
22638 craig 831
        comm = comm.arg(bcdata, bcopts, map[ui.bcCombo->currentText()].command);
20311 jghali 832
        psCommand.append(comm);
833
        psCommand.append("showpage\n");
20308 craig 834
 
20340 craig 835
        thread.render(psCommand);
836
}
4527 subik 837
 
20329 craig 838
 
4468 subik 839
void BarcodeGenerator::resetButton_clicked()
840
{
841
        bcComboChanged();
842
}