Subversion Repositories Scribus

Rev

Rev 4685 | Rev 4762 | 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
 
8
#include "barcodegenerator.h"
4527 subik 9
#include "barcodegenerator.moc"
4506 cbradney 10
#include "gsutil.h"
4460 subik 11
#include "util.h"
4468 subik 12
#include "scribus.h"
4527 subik 13
#include "scpaths.h"
4460 subik 14
#include "commonstrings.h"
4685 subik 15
#include "undomanager.h"
16
#include "../psimport/importps.h"
4460 subik 17
#include <qcombobox.h>
18
#include <qtextedit.h>
19
#include <qlineedit.h>
20
#include <qpushbutton.h>
21
#include <qcheckbox.h>
22
#include <qcolor.h>
23
#include <qcolordialog.h>
24
#include <qlabel.h>
25
#include <qfile.h>
26
#include <qdir.h>
4527 subik 27
#include <qfiledialog.h>
4460 subik 28
 
29
 
30
BarcodeType::BarcodeType(QString cmd, QString exa, QString comm)
31
{
32
        command = cmd;
33
        example = exa;
34
        comment = comm;
35
}
36
 
37
BarcodeGenerator::BarcodeGenerator(QWidget* parent, const char* name)
38
        : BarcodeGeneratorBase(parent, name, true)
39
{
40
        map["EAN-13"] = BarcodeType("ean13", "9781860742712", "12 or 13 digits");
41
        map["EAN-8"] = BarcodeType("ean8", "12345678", "8 digits");
42
        map["UPC-A"] = BarcodeType("upca", "78858101497", "11 or 12 digits");
43
        map["UPC-E"] = BarcodeType("upce", "0123456", "7 or 8 digits");
44
        map["EAN-5"] = BarcodeType("ean5", "90200", "5 digits");
45
        map["EAN-2"] = BarcodeType("ean2", "42", "2 digits");
46
        map["ISBN"] = BarcodeType("isbn", "1-58880-149", "9 or 10 digits seperated appropriately with dashes");
47
//    "Code-11"] = "code11"
48
        map["Code-39"] = BarcodeType("code39", "CODE-39", "Variable number of characters, digits and any of the symbols -. *$/+%.");
49
//    "Code-93"] = "code93"
50
        map["Code-128"] = BarcodeType("code128", "^104^102Count^0991234^101!", "Variable number of ASCII characters and special funtion symbols, starting with the approriate start character for the initial character set. UCC/EAN-128s must have a manditory FNC 1 symbol immediately following the start character.");
51
        map["UCC/EAN-128"] = BarcodeType("code128", "^104^102Count^0991234^101!", "Variable number of ASCII characters and special funtion symbols, starting with the approriate start character for the initial character set. UCC/EAN-128s must have a manditory FNC 1 symbol immediately following the start character.");
52
        map["Rationalized Codabar"] = BarcodeType("rationalizedCodabar", "0123456789", "Variable number of digits and any of the symbols -$:/.+ABCD.");
53
        map["Interleaved 2 of 5"] = BarcodeType("interleaved2of5", "05012345678900", "Variable number of digits. An ITF-14 is 14 characters and does not have a check digit");
54
        map["ITF-14"] = BarcodeType("interleaved2of5", "05012345678900", "Variable number of digits. An ITF-14 is 14 characters and does not have a check digit");
55
        map["Code 2 of 5"] = BarcodeType("code2of5", "0123456789", "Variable number of digits");
56
        map["Postnet"] = BarcodeType("postnet", "01234567", "Variable number digits");
57
        map["Royal Mail"] = BarcodeType("royalmail", "LE28HS9Z", "Variable number digits and capital letters");
58
//    "Auspost"] = "auspost"
59
        map["MSI"] = BarcodeType("msi", "0120823635162", "Variable number digits");
60
//    "KIX"] = "kix"
61
        map["Plessey"] = BarcodeType("plessey", "012345ABCDEF", "Variable number of hexadecimal characters");
62
//    "Symbol"] = "symbol"
4527 subik 63
 
4468 subik 64
        useSamples = true;
4460 subik 65
        bcCombo->insertStringList(map.keys());
66
        okButton->setText(CommonStrings::tr_OK);
67
        cancelButton->setText(CommonStrings::tr_Cancel);
4468 subik 68
        resetButton->setPixmap(loadIcon("u_undo16.png"));
4460 subik 69
        lnColor = Qt::black;
70
        txtColor = Qt::black;
71
        bgColor = Qt::white;
72
        paintColorSample(linesLabel, lnColor);
73
        paintColorSample(txtLabel, txtColor);
74
        paintColorSample(bgLabel, bgColor);
75
 
76
        tmpFile = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/bcode.png");
77
        psFile = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/bcode.ps");
78
 
79
        // PS engine
80
        psCommand.append("%!PS-Adobe-2.0 EPSF-2.0\n");
4527 subik 81
        QFile f( ScPaths::instance().shareDir() + QString("/plugins/barcode.ps") );
4460 subik 82
        f.open(IO_ReadOnly);
83
        QTextStream ts(&f);
84
        QString s = ts.read();
85
        int begin = s.find("% --BEGIN TEMPLATE--");
86
        int end = s.find("% --END TEMPLATE--");
87
        psCommand.append(s.mid(begin, end));
88
        f.close();
89
        psCommand.append("\n\n%command\n");
90
        bcComboChanged();
91
}
92
 
93
BarcodeGenerator::~BarcodeGenerator()
94
{
95
        QFile::remove(psFile);
96
        QFile::remove(tmpFile);
97
}
98
 
99
void BarcodeGenerator::bcComboChanged()
100
{
101
        QString s = bcCombo->currentText();
102
        commentEdit->setText(map[s].comment);
4468 subik 103
        if (useSamples)
104
        {
105
                disconnect(codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&)));
106
                codeEdit->setText(map[s].example);
107
                connect(codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&)));
108
        }
4460 subik 109
        paintBarcode();
110
}
111
 
112
void BarcodeGenerator::textCheck_changed()
113
{
114
        txtColorButton->setEnabled(textCheck->state());
115
        paintBarcode();
116
}
117
 
118
void BarcodeGenerator::guardCheck_changed()
119
{
120
        paintBarcode();
121
}
122
 
123
void BarcodeGenerator::paintColorSample(QLabel *l, QColor c)
124
{
125
        QPixmap pm(1, 1);
126
        pm.fill(c);
127
        l->setPixmap(pm);
128
}
129
 
130
void BarcodeGenerator::bgColorButton_pressed()
131
{
132
        bgColor = QColorDialog::getColor(bgColor, this);
133
        if (bgColor.isValid())
134
        {
135
                paintColorSample(bgLabel, bgColor);
136
                paintBarcode();
137
        }
138
}
139
 
140
void BarcodeGenerator::lnColorButton_pressed()
141
{
142
        lnColor = QColorDialog::getColor(lnColor, this);
143
        if (lnColor.isValid())
144
        {
145
                paintColorSample(linesLabel, lnColor);
146
                paintBarcode();
147
        }
148
}
149
 
150
void BarcodeGenerator::txtColorButton_pressed()
151
{
152
        txtColor = QColorDialog::getColor(txtColor, this);
153
        if (txtColor.isValid())
154
        {
155
                paintColorSample(txtLabel, txtColor);
156
                paintBarcode();
157
        }
158
}
159
 
160
void BarcodeGenerator::okButton_pressed()
161
{
4685 subik 162
        // no need to call paintBarcode(tmpFile, 300); because
163
        // it's created by previous run...
164
        if (UndoManager::undoEnabled() && ScMW->HaveDoc)
4468 subik 165
        {
4685 subik 166
                UndoManager::instance()->beginTransaction(ScMW->doc->currentPage->getUName(),Um::IImageFrame,Um::ImportEPS, psFile, Um::IEPS);
4468 subik 167
        }
4685 subik 168
        else if (UndoManager::undoEnabled() && !ScMW->HaveDoc)
169
                UndoManager::instance()->setUndoEnabled(false);
4760 subik 170
        EPSPlug *dia = new EPSPlug(psFile, true);
4685 subik 171
        Q_CHECK_PTR(dia);
172
        if (UndoManager::undoEnabled())
173
                UndoManager::instance()->commit();
174
        else
175
                UndoManager::instance()->setUndoEnabled(true);
176
        delete dia;
4460 subik 177
        accept();
178
}
179
 
180
void BarcodeGenerator::cancelButton_pressed()
181
{
182
        reject();
183
}
184
 
185
void BarcodeGenerator::codeEdit_textChanged(const QString&)
186
{
4468 subik 187
        useSamples = false;
4460 subik 188
        paintBarcode();
189
}
190
 
4468 subik 191
bool BarcodeGenerator::paintBarcode(QString fileName, int dpi)
4460 subik 192
{
4468 subik 193
        if (fileName == QString::null)
194
                fileName = tmpFile;
4460 subik 195
        QString opts("barcolor=%1 showbackground backgroundcolor=%2 textcolor=%3");
196
        opts = opts.arg(lnColor.name().replace('#', "")) \
197
                        .arg(bgColor.name().replace('#', "")) \
198
                        .arg(txtColor.name().replace('#', ""));
199
        if (textCheck->isChecked())
200
                opts += " includetext";
201
        if (guardCheck->isChecked())
202
                opts += " guardwhitespace";
203
        QString comm("15 10 moveto (%1) (%2) %3 barcode");
204
        comm = comm.arg(codeEdit->text()).arg(opts).arg(map[bcCombo->currentText()].command);
205
        comm = psCommand + comm;
206
        QFile f(psFile);
207
        if (!f.open(IO_WriteOnly))
208
        {
209
                sampleLabel->setText("<qt>" + tr("Error opening file: %1").arg(psFile) + "</qt>");
4468 subik 210
                return false;
4460 subik 211
        }
212
        QTextStream ts(&f);
213
        ts << comm;
214
        f.close();
4527 subik 215
 
216
        QStringList gargs;
217
        gargs.append("-dDEVICEWIDTHPOINTS=200");
218
        gargs.append("-dDEVICEHEIGHTPOINTS=150");
219
        gargs.append( QString("-r%1").arg(dpi) );
4685 subik 220
        gargs.append( QString("-sOutputFile=%1").arg(fileName) );
4527 subik 221
        gargs.append( psFile );
4460 subik 222
        int gs = callGS(gargs);
4468 subik 223
        bool retval = true;
224
        if (gs != 0)
225
                retval = false;
226
        // setup only preview
227
        if (fileName != tmpFile)
228
                return retval;
4460 subik 229
    if (gs == 0)
4468 subik 230
                sampleLabel->setPixmap(QPixmap(fileName));
4460 subik 231
        else
232
                sampleLabel->setText("<qt>" + tr("Error creating preview") + "</qt>");
4468 subik 233
        return retval;
4460 subik 234
}
4468 subik 235
 
236
void BarcodeGenerator::resetButton_clicked()
237
{
238
        useSamples = true;
239
        bcComboChanged();
240
}