Rev 5588 | 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" |
5539 | mrdocs | 16 | #include "loadsaveplugin.h" |
17 | #include "../formatidlist.h" |
||
4460 | subik | 18 | #include <qcombobox.h> |
19 | #include <qtextedit.h> |
||
20 | #include <qlineedit.h> |
||
21 | #include <qpushbutton.h> |
||
22 | #include <qcheckbox.h> |
||
23 | #include <qcolor.h> |
||
24 | #include <qcolordialog.h> |
||
25 | #include <qlabel.h> |
||
26 | #include <qfile.h> |
||
27 | #include <qdir.h> |
||
4527 | subik | 28 | #include <qfiledialog.h> |
4762 | subik | 29 | #include <qregexp.h> |
4460 | subik | 30 | |
4835 | subik | 31 | BarcodeType::BarcodeType(QString cmd, QString exa, |
32 | QString comm, QString regExp, |
||
33 | bool includeCheck, bool includeCheckInText) |
||
4460 | subik | 34 | { |
35 | command = cmd; |
||
36 | example = exa; |
||
37 | comment = comm; |
||
4762 | subik | 38 | regularExp = regExp; |
4835 | subik | 39 | this->includeCheck = includeCheck; |
40 | this->includeCheckInText = includeCheckInText; |
||
4460 | subik | 41 | } |
42 | |||
43 | BarcodeGenerator::BarcodeGenerator(QWidget* parent, const char* name) |
||
44 | : BarcodeGeneratorBase(parent, name, true) |
||
45 | { |
||
4818 | subik | 46 | map["EAN-13"] = BarcodeType("ean13", "9781860742712", tr("12 or 13 digits"), |
4762 | subik | 47 | "[0-9]{12,13}"); |
4818 | subik | 48 | map["EAN-8"] = BarcodeType("ean8", "12345678", tr("8 digits"), |
4762 | subik | 49 | "[0-9]{8,8}"); |
4818 | subik | 50 | map["UPC-A"] = BarcodeType("upca", "78858101497", tr("11 or 12 digits"), |
4762 | subik | 51 | "[0-9]{11,12}"); |
4818 | subik | 52 | map["UPC-E"] = BarcodeType("upce", "0123456", tr("7 or 8 digits"), |
4762 | subik | 53 | "[0-9]{7,8}"); |
4818 | subik | 54 | map["EAN-5"] = BarcodeType("ean5", "90200", tr("5 digits"), |
4762 | subik | 55 | "[0-9]{5,5}"); |
4818 | subik | 56 | map["EAN-2"] = BarcodeType("ean2", "42", tr("2 digits"), |
4762 | subik | 57 | "[0-9]{2,2}"); |
58 | map["ISBN"] = BarcodeType("isbn", "1-58880-149", |
||
5495 | subik | 59 | tr("For ISBN-10 the data should contain 9 or 10 \ |
60 | digits separated appropriately by dash characters.\n" |
||
61 | "For ISBN-13 the data should contain 12 or 13 \ |
||
62 | digits separated appropriately by dash characters.\n" |
||
63 | "If the last digit is not given then the ISBN \ |
||
64 | checkdigit is calculated automatically."), |
||
4762 | subik | 65 | "[0-9]*\\-[0-9]*\\-[0-9]*"); |
4460 | subik | 66 | // "Code-11"] = "code11" |
4762 | subik | 67 | map["Code-39"] = BarcodeType("code39", "CODE-39", |
4818 | subik | 68 | tr("Variable number of characters, digits and any of the symbols -. *$/+%."), |
4835 | subik | 69 | "[0-9a-zA-Z\\-\\.\\ \\*\\$\\/\\+\\%]*", |
70 | true, true); |
||
4460 | subik | 71 | // "Code-93"] = "code93" |
4762 | subik | 72 | map["Code-128"] = BarcodeType("code128", "^104^102Count^0991234^101!", |
4818 | subik | 73 | tr("Variable number of ASCII characters and special function symbols, starting with the appropriate start character for the initial character set. UCC/EAN-128s must have a mandatory FNC 1 symbol immediately following the start character."), |
4835 | subik | 74 | "\\^[0-9a-zA-Z\\^\\!]*", |
75 | true, true); |
||
4762 | subik | 76 | map["UCC/EAN-128"] = BarcodeType("code128", "^104^102Count^0991234^101!", |
4818 | subik | 77 | tr("Variable number of ASCII characters and special function symbols, starting with the appropriate start character for the initial character set. UCC/EAN-128s must have a mandatory FNC 1 symbol immediately following the start character."), |
4762 | subik | 78 | "\\^[0-9a-zA-Z\\^\\!]*"); |
79 | map["Rationalized Codabar"] = BarcodeType("rationalizedCodabar", "0123456789", |
||
4818 | subik | 80 | tr("Variable number of digits and any of the symbols -$:/.+ABCD."), |
4835 | subik | 81 | "[0-9A-D\\-\\$\\:\\/\\.\\+]*", |
82 | true, true); |
||
4762 | subik | 83 | map["Interleaved 2 of 5"] = BarcodeType("interleaved2of5", "05012345678900", |
4883 | subik | 84 | tr("Variable number of digits"), |
4835 | subik | 85 | "[0-9]*", |
86 | true, true); |
||
4762 | subik | 87 | map["ITF-14"] = BarcodeType("interleaved2of5", "05012345678900", |
4818 | subik | 88 | tr("Variable number of digits. An ITF-14 is 14 characters and does not have a check digit"), |
4835 | subik | 89 | "[0-9]*", |
90 | true, true); |
||
4762 | subik | 91 | map["Code 2 of 5"] = BarcodeType("code2of5", "0123456789", |
4818 | subik | 92 | tr("Variable number of digits"), |
4762 | subik | 93 | "[0-9]*"); |
94 | map["Postnet"] = BarcodeType("postnet", "01234567", |
||
4818 | subik | 95 | tr("Variable number of digits"), |
4835 | subik | 96 | "[0-9]*", |
97 | false, true); |
||
4762 | subik | 98 | map["Royal Mail"] = BarcodeType("royalmail", "LE28HS9Z", |
4818 | subik | 99 | tr("Variable number of digits and capital letters"), |
4835 | subik | 100 | "[0-9A-Z]*", |
101 | false, true); |
||
4460 | subik | 102 | // "Auspost"] = "auspost" |
4818 | subik | 103 | map["MSI"] = BarcodeType("msi", "0120823635162", tr("Variable number of digits"), |
4835 | subik | 104 | "[0-9]*", |
105 | true, true); |
||
4460 | subik | 106 | // "KIX"] = "kix" |
4762 | subik | 107 | map["Plessey"] = BarcodeType("plessey", "012345ABCDEF", |
4818 | subik | 108 | tr("Variable number of hexadecimal characters"), |
4835 | subik | 109 | "[0-9A-F]*", |
110 | false, true); |
||
4762 | subik | 111 | // "Symbol"] = "symbol" |
4527 | subik | 112 | |
4468 | subik | 113 | useSamples = true; |
4762 | subik | 114 | guiColor = codeEdit->paletteBackgroundColor(); |
4460 | subik | 115 | bcCombo->insertStringList(map.keys()); |
116 | okButton->setText(CommonStrings::tr_OK); |
||
117 | cancelButton->setText(CommonStrings::tr_Cancel); |
||
4468 | subik | 118 | resetButton->setPixmap(loadIcon("u_undo16.png")); |
4460 | subik | 119 | lnColor = Qt::black; |
120 | txtColor = Qt::black; |
||
121 | bgColor = Qt::white; |
||
122 | paintColorSample(linesLabel, lnColor); |
||
123 | paintColorSample(txtLabel, txtColor); |
||
124 | paintColorSample(bgLabel, bgColor); |
||
125 | |||
5917 | jghali | 126 | tmpFile = QDir::convertSeparators(ScPaths::getTempFileDir() + "bcode.png"); |
127 | psFile = QDir::convertSeparators(ScPaths::getTempFileDir() + "bcode.ps"); |
||
4460 | subik | 128 | |
129 | // PS engine |
||
130 | psCommand.append("%!PS-Adobe-2.0 EPSF-2.0\n"); |
||
4527 | subik | 131 | QFile f( ScPaths::instance().shareDir() + QString("/plugins/barcode.ps") ); |
4460 | subik | 132 | f.open(IO_ReadOnly); |
133 | QTextStream ts(&f); |
||
134 | QString s = ts.read(); |
||
135 | int begin = s.find("% --BEGIN TEMPLATE--"); |
||
136 | int end = s.find("% --END TEMPLATE--"); |
||
137 | psCommand.append(s.mid(begin, end)); |
||
138 | f.close(); |
||
139 | psCommand.append("\n\n%command\n"); |
||
140 | bcComboChanged(); |
||
141 | } |
||
142 | |||
143 | BarcodeGenerator::~BarcodeGenerator() |
||
144 | { |
||
145 | QFile::remove(psFile); |
||
146 | QFile::remove(tmpFile); |
||
147 | } |
||
148 | |||
149 | void BarcodeGenerator::bcComboChanged() |
||
150 | { |
||
151 | QString s = bcCombo->currentText(); |
||
152 | commentEdit->setText(map[s].comment); |
||
4468 | subik | 153 | if (useSamples) |
154 | { |
||
155 | disconnect(codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&))); |
||
156 | codeEdit->setText(map[s].example); |
||
157 | connect(codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&))); |
||
158 | } |
||
4835 | subik | 159 | |
160 | includeCheck->setEnabled(map[s].includeCheck ? true : false); |
||
161 | if (textCheck->isChecked()) |
||
162 | includeCheckInText->setEnabled(map[s].includeCheckInText ? true : false); |
||
163 | else |
||
164 | includeCheckInText->setEnabled(false); |
||
165 | |||
4762 | subik | 166 | codeEdit_check(codeEdit->text()); |
4460 | subik | 167 | paintBarcode(); |
168 | } |
||
169 | |||
170 | void BarcodeGenerator::textCheck_changed() |
||
171 | { |
||
4835 | subik | 172 | bool s = textCheck->state(); |
173 | txtColorButton->setEnabled(s); |
||
174 | includeCheckInText->setEnabled(s); |
||
4460 | subik | 175 | paintBarcode(); |
176 | } |
||
177 | |||
178 | void BarcodeGenerator::guardCheck_changed() |
||
179 | { |
||
180 | paintBarcode(); |
||
181 | } |
||
182 | |||
4835 | subik | 183 | void BarcodeGenerator::includeCheck_stateChanged(int) |
184 | { |
||
185 | paintBarcode(); |
||
186 | } |
||
187 | |||
188 | void BarcodeGenerator::includeCheckInText_stateChanged(int) |
||
189 | { |
||
190 | paintBarcode(); |
||
191 | } |
||
192 | |||
4460 | subik | 193 | void BarcodeGenerator::paintColorSample(QLabel *l, QColor c) |
194 | { |
||
195 | QPixmap pm(1, 1); |
||
196 | pm.fill(c); |
||
197 | l->setPixmap(pm); |
||
198 | } |
||
199 | |||
200 | void BarcodeGenerator::bgColorButton_pressed() |
||
201 | { |
||
202 | bgColor = QColorDialog::getColor(bgColor, this); |
||
203 | if (bgColor.isValid()) |
||
204 | { |
||
205 | paintColorSample(bgLabel, bgColor); |
||
206 | paintBarcode(); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | void BarcodeGenerator::lnColorButton_pressed() |
||
211 | { |
||
212 | lnColor = QColorDialog::getColor(lnColor, this); |
||
213 | if (lnColor.isValid()) |
||
214 | { |
||
215 | paintColorSample(linesLabel, lnColor); |
||
216 | paintBarcode(); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | void BarcodeGenerator::txtColorButton_pressed() |
||
221 | { |
||
222 | txtColor = QColorDialog::getColor(txtColor, this); |
||
223 | if (txtColor.isValid()) |
||
224 | { |
||
225 | paintColorSample(txtLabel, txtColor); |
||
226 | paintBarcode(); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | void BarcodeGenerator::okButton_pressed() |
||
231 | { |
||
4685 | subik | 232 | // no need to call paintBarcode(tmpFile, 300); because |
233 | // it's created by previous run... |
||
4762 | subik | 234 | hide(); |
5539 | mrdocs | 235 | const FileFormat * fmt = LoadSavePlugin::getFormatById(FORMATID_PSIMPORT); |
236 | if( fmt ) |
||
237 | fmt->loadFile(QString::fromUtf8(psFile), LoadSavePlugin::lfUseCurrentPage|LoadSavePlugin::lfInteractive); |
||
4460 | subik | 238 | accept(); |
239 | } |
||
240 | |||
241 | void BarcodeGenerator::cancelButton_pressed() |
||
242 | { |
||
243 | reject(); |
||
244 | } |
||
245 | |||
4837 | subik | 246 | bool BarcodeGenerator::codeEdit_check(const QString& )//s) |
4460 | subik | 247 | { |
5588 | cbradney | 248 | /* probably not needed as the backend do it for us (PV) |
4762 | subik | 249 | QRegExp rx(map[bcCombo->currentText()].regularExp); |
250 | if (!rx.exactMatch(s)) |
||
251 | { |
||
252 | codeEdit->setPaletteBackgroundColor(QColor(255, 0, 0)); |
||
4763 | subik | 253 | sampleLabel->setText("<qt>" + tr("Barcode incomplete") + "</qt>"); |
4762 | subik | 254 | okButton->setEnabled(false); |
255 | return false; |
||
256 | } |
||
257 | else |
||
258 | { |
||
259 | codeEdit->setPaletteBackgroundColor(guiColor); |
||
260 | okButton->setEnabled(true); |
||
261 | paintBarcode(); |
||
262 | return true; |
||
4818 | subik | 263 | } */ |
264 | paintBarcode(); |
||
265 | return true; |
||
4762 | subik | 266 | } |
267 | |||
268 | void BarcodeGenerator::codeEdit_textChanged(const QString& s) |
||
269 | { |
||
4468 | subik | 270 | useSamples = false; |
4762 | subik | 271 | codeEdit_check(s); |
4460 | subik | 272 | } |
273 | |||
4468 | subik | 274 | bool BarcodeGenerator::paintBarcode(QString fileName, int dpi) |
4460 | subik | 275 | { |
4468 | subik | 276 | if (fileName == QString::null) |
277 | fileName = tmpFile; |
||
4460 | subik | 278 | QString opts("barcolor=%1 showbackground backgroundcolor=%2 textcolor=%3"); |
279 | opts = opts.arg(lnColor.name().replace('#', "")) \ |
||
280 | .arg(bgColor.name().replace('#', "")) \ |
||
281 | .arg(txtColor.name().replace('#', "")); |
||
282 | if (textCheck->isChecked()) |
||
283 | opts += " includetext"; |
||
284 | if (guardCheck->isChecked()) |
||
285 | opts += " guardwhitespace"; |
||
4835 | subik | 286 | if (includeCheckInText->isChecked() & includeCheckInText->isEnabled()) |
287 | opts += " includecheckintext"; |
||
288 | if (includeCheck->isChecked() & includeCheck->isEnabled()) |
||
289 | opts += " includecheck"; |
||
4460 | subik | 290 | QString comm("15 10 moveto (%1) (%2) %3 barcode"); |
291 | comm = comm.arg(codeEdit->text()).arg(opts).arg(map[bcCombo->currentText()].command); |
||
292 | comm = psCommand + comm; |
||
293 | QFile f(psFile); |
||
294 | if (!f.open(IO_WriteOnly)) |
||
295 | { |
||
296 | sampleLabel->setText("<qt>" + tr("Error opening file: %1").arg(psFile) + "</qt>"); |
||
4468 | subik | 297 | return false; |
4460 | subik | 298 | } |
299 | QTextStream ts(&f); |
||
300 | ts << comm; |
||
301 | f.close(); |
||
4527 | subik | 302 | |
303 | QStringList gargs; |
||
4818 | subik | 304 | // limit the area only for preview. EPS importer bounds the box itself. |
305 | if (fileName == tmpFile) |
||
306 | { |
||
307 | gargs.append("-dDEVICEWIDTHPOINTS=200"); |
||
308 | gargs.append("-dDEVICEHEIGHTPOINTS=150"); |
||
309 | } |
||
4527 | subik | 310 | gargs.append( QString("-r%1").arg(dpi) ); |
4685 | subik | 311 | gargs.append( QString("-sOutputFile=%1").arg(fileName) ); |
4527 | subik | 312 | gargs.append( psFile ); |
4460 | subik | 313 | int gs = callGS(gargs); |
4468 | subik | 314 | bool retval = true; |
315 | if (gs != 0) |
||
316 | retval = false; |
||
317 | // setup only preview |
||
318 | if (fileName != tmpFile) |
||
319 | return retval; |
||
4460 | subik | 320 | if (gs == 0) |
4762 | subik | 321 | { |
4468 | subik | 322 | sampleLabel->setPixmap(QPixmap(fileName)); |
4762 | subik | 323 | okButton->setEnabled(true); |
324 | } |
||
4460 | subik | 325 | else |
4762 | subik | 326 | { |
4818 | subik | 327 | sampleLabel->setText("<qt>" + tr("Barcode incomplete") + "</qt>"); |
4762 | subik | 328 | okButton->setEnabled(false); |
329 | } |
||
4468 | subik | 330 | return retval; |
4460 | subik | 331 | } |
4468 | subik | 332 | |
333 | void BarcodeGenerator::resetButton_clicked() |
||
334 | { |
||
335 | useSamples = true; |
||
336 | bcComboChanged(); |
||
337 | } |