Subversion Repositories Scribus

Rev

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