Subversion Repositories Scribus

Rev

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