Rev 4837 | Rev 5000 | 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> |
4762 | subik | 28 | #include <qregexp.h> |
4460 | subik | 29 | |
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", |
||
4818 | subik | 59 | tr("9 or 10 digits separated appropriately with dashes"), |
4762 | subik | 60 | "[0-9]*\\-[0-9]*\\-[0-9]*"); |
4460 | subik | 61 | // "Code-11"] = "code11" |
4762 | subik | 62 | map["Code-39"] = BarcodeType("code39", "CODE-39", |
4818 | subik | 63 | tr("Variable number of characters, digits and any of the symbols -. *$/+%."), |
4835 | subik | 64 | "[0-9a-zA-Z\\-\\.\\ \\*\\$\\/\\+\\%]*", |
65 | true, true); |
||
4460 | subik | 66 | // "Code-93"] = "code93" |
4762 | subik | 67 | map["Code-128"] = BarcodeType("code128", "^104^102Count^0991234^101!", |
4818 | subik | 68 | 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 | 69 | "\\^[0-9a-zA-Z\\^\\!]*", |
70 | true, true); |
||
4762 | subik | 71 | map["UCC/EAN-128"] = BarcodeType("code128", "^104^102Count^0991234^101!", |
4818 | subik | 72 | 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 | 73 | "\\^[0-9a-zA-Z\\^\\!]*"); |
74 | map["Rationalized Codabar"] = BarcodeType("rationalizedCodabar", "0123456789", |
||
4818 | subik | 75 | tr("Variable number of digits and any of the symbols -$:/.+ABCD."), |
4835 | subik | 76 | "[0-9A-D\\-\\$\\:\\/\\.\\+]*", |
77 | true, true); |
||
4762 | subik | 78 | map["Interleaved 2 of 5"] = BarcodeType("interleaved2of5", "05012345678900", |
4883 | subik | 79 | tr("Variable number of digits"), |
4835 | subik | 80 | "[0-9]*", |
81 | true, true); |
||
4762 | subik | 82 | map["ITF-14"] = BarcodeType("interleaved2of5", "05012345678900", |
4818 | subik | 83 | tr("Variable number of digits. An ITF-14 is 14 characters and does not have a check digit"), |
4835 | subik | 84 | "[0-9]*", |
85 | true, true); |
||
4762 | subik | 86 | map["Code 2 of 5"] = BarcodeType("code2of5", "0123456789", |
4818 | subik | 87 | tr("Variable number of digits"), |
4762 | subik | 88 | "[0-9]*"); |
89 | map["Postnet"] = BarcodeType("postnet", "01234567", |
||
4818 | subik | 90 | tr("Variable number of digits"), |
4835 | subik | 91 | "[0-9]*", |
92 | false, true); |
||
4762 | subik | 93 | map["Royal Mail"] = BarcodeType("royalmail", "LE28HS9Z", |
4818 | subik | 94 | tr("Variable number of digits and capital letters"), |
4835 | subik | 95 | "[0-9A-Z]*", |
96 | false, true); |
||
4460 | subik | 97 | // "Auspost"] = "auspost" |
4818 | subik | 98 | map["MSI"] = BarcodeType("msi", "0120823635162", tr("Variable number of digits"), |
4835 | subik | 99 | "[0-9]*", |
100 | true, true); |
||
4460 | subik | 101 | // "KIX"] = "kix" |
4762 | subik | 102 | map["Plessey"] = BarcodeType("plessey", "012345ABCDEF", |
4818 | subik | 103 | tr("Variable number of hexadecimal characters"), |
4835 | subik | 104 | "[0-9A-F]*", |
105 | false, true); |
||
4762 | subik | 106 | // "Symbol"] = "symbol" |
4527 | subik | 107 | |
4468 | subik | 108 | useSamples = true; |
4762 | subik | 109 | guiColor = codeEdit->paletteBackgroundColor(); |
4460 | subik | 110 | bcCombo->insertStringList(map.keys()); |
111 | okButton->setText(CommonStrings::tr_OK); |
||
112 | cancelButton->setText(CommonStrings::tr_Cancel); |
||
4468 | subik | 113 | resetButton->setPixmap(loadIcon("u_undo16.png")); |
4460 | subik | 114 | lnColor = Qt::black; |
115 | txtColor = Qt::black; |
||
116 | bgColor = Qt::white; |
||
117 | paintColorSample(linesLabel, lnColor); |
||
118 | paintColorSample(txtLabel, txtColor); |
||
119 | paintColorSample(bgLabel, bgColor); |
||
120 | |||
121 | tmpFile = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/bcode.png"); |
||
122 | psFile = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/bcode.ps"); |
||
123 | |||
124 | // PS engine |
||
125 | psCommand.append("%!PS-Adobe-2.0 EPSF-2.0\n"); |
||
4527 | subik | 126 | QFile f( ScPaths::instance().shareDir() + QString("/plugins/barcode.ps") ); |
4460 | subik | 127 | f.open(IO_ReadOnly); |
128 | QTextStream ts(&f); |
||
129 | QString s = ts.read(); |
||
130 | int begin = s.find("% --BEGIN TEMPLATE--"); |
||
131 | int end = s.find("% --END TEMPLATE--"); |
||
132 | psCommand.append(s.mid(begin, end)); |
||
133 | f.close(); |
||
134 | psCommand.append("\n\n%command\n"); |
||
135 | bcComboChanged(); |
||
136 | } |
||
137 | |||
138 | BarcodeGenerator::~BarcodeGenerator() |
||
139 | { |
||
140 | QFile::remove(psFile); |
||
141 | QFile::remove(tmpFile); |
||
142 | } |
||
143 | |||
144 | void BarcodeGenerator::bcComboChanged() |
||
145 | { |
||
146 | QString s = bcCombo->currentText(); |
||
147 | commentEdit->setText(map[s].comment); |
||
4468 | subik | 148 | if (useSamples) |
149 | { |
||
150 | disconnect(codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&))); |
||
151 | codeEdit->setText(map[s].example); |
||
152 | connect(codeEdit, SIGNAL(textChanged(const QString&)), this, SLOT(codeEdit_textChanged(const QString&))); |
||
153 | } |
||
4835 | subik | 154 | |
155 | includeCheck->setEnabled(map[s].includeCheck ? true : false); |
||
156 | if (textCheck->isChecked()) |
||
157 | includeCheckInText->setEnabled(map[s].includeCheckInText ? true : false); |
||
158 | else |
||
159 | includeCheckInText->setEnabled(false); |
||
160 | |||
4762 | subik | 161 | codeEdit_check(codeEdit->text()); |
4460 | subik | 162 | paintBarcode(); |
163 | } |
||
164 | |||
165 | void BarcodeGenerator::textCheck_changed() |
||
166 | { |
||
4835 | subik | 167 | bool s = textCheck->state(); |
168 | txtColorButton->setEnabled(s); |
||
169 | includeCheckInText->setEnabled(s); |
||
4460 | subik | 170 | paintBarcode(); |
171 | } |
||
172 | |||
173 | void BarcodeGenerator::guardCheck_changed() |
||
174 | { |
||
175 | paintBarcode(); |
||
176 | } |
||
177 | |||
4835 | subik | 178 | void BarcodeGenerator::includeCheck_stateChanged(int) |
179 | { |
||
180 | paintBarcode(); |
||
181 | } |
||
182 | |||
183 | void BarcodeGenerator::includeCheckInText_stateChanged(int) |
||
184 | { |
||
185 | paintBarcode(); |
||
186 | } |
||
187 | |||
4460 | subik | 188 | void BarcodeGenerator::paintColorSample(QLabel *l, QColor c) |
189 | { |
||
190 | QPixmap pm(1, 1); |
||
191 | pm.fill(c); |
||
192 | l->setPixmap(pm); |
||
193 | } |
||
194 | |||
195 | void BarcodeGenerator::bgColorButton_pressed() |
||
196 | { |
||
197 | bgColor = QColorDialog::getColor(bgColor, this); |
||
198 | if (bgColor.isValid()) |
||
199 | { |
||
200 | paintColorSample(bgLabel, bgColor); |
||
201 | paintBarcode(); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | void BarcodeGenerator::lnColorButton_pressed() |
||
206 | { |
||
207 | lnColor = QColorDialog::getColor(lnColor, this); |
||
208 | if (lnColor.isValid()) |
||
209 | { |
||
210 | paintColorSample(linesLabel, lnColor); |
||
211 | paintBarcode(); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | void BarcodeGenerator::txtColorButton_pressed() |
||
216 | { |
||
217 | txtColor = QColorDialog::getColor(txtColor, this); |
||
218 | if (txtColor.isValid()) |
||
219 | { |
||
220 | paintColorSample(txtLabel, txtColor); |
||
221 | paintBarcode(); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | void BarcodeGenerator::okButton_pressed() |
||
226 | { |
||
4685 | subik | 227 | // no need to call paintBarcode(tmpFile, 300); because |
228 | // it's created by previous run... |
||
229 | if (UndoManager::undoEnabled() && ScMW->HaveDoc) |
||
4468 | subik | 230 | { |
4685 | subik | 231 | UndoManager::instance()->beginTransaction(ScMW->doc->currentPage->getUName(),Um::IImageFrame,Um::ImportEPS, psFile, Um::IEPS); |
4468 | subik | 232 | } |
4685 | subik | 233 | else if (UndoManager::undoEnabled() && !ScMW->HaveDoc) |
234 | UndoManager::instance()->setUndoEnabled(false); |
||
4762 | subik | 235 | hide(); |
4760 | subik | 236 | EPSPlug *dia = new EPSPlug(psFile, true); |
4685 | subik | 237 | Q_CHECK_PTR(dia); |
238 | if (UndoManager::undoEnabled()) |
||
239 | UndoManager::instance()->commit(); |
||
240 | else |
||
241 | UndoManager::instance()->setUndoEnabled(true); |
||
242 | delete dia; |
||
4460 | subik | 243 | accept(); |
244 | } |
||
245 | |||
246 | void BarcodeGenerator::cancelButton_pressed() |
||
247 | { |
||
248 | reject(); |
||
249 | } |
||
250 | |||
4837 | subik | 251 | bool BarcodeGenerator::codeEdit_check(const QString& )//s) |
4460 | subik | 252 | { |
4818 | subik | 253 | /* propably not needed as the backend do it for us (PV) |
4762 | subik | 254 | QRegExp rx(map[bcCombo->currentText()].regularExp); |
255 | if (!rx.exactMatch(s)) |
||
256 | { |
||
257 | codeEdit->setPaletteBackgroundColor(QColor(255, 0, 0)); |
||
4763 | subik | 258 | sampleLabel->setText("<qt>" + tr("Barcode incomplete") + "</qt>"); |
4762 | subik | 259 | okButton->setEnabled(false); |
260 | return false; |
||
261 | } |
||
262 | else |
||
263 | { |
||
264 | codeEdit->setPaletteBackgroundColor(guiColor); |
||
265 | okButton->setEnabled(true); |
||
266 | paintBarcode(); |
||
267 | return true; |
||
4818 | subik | 268 | } */ |
269 | paintBarcode(); |
||
270 | return true; |
||
4762 | subik | 271 | } |
272 | |||
273 | void BarcodeGenerator::codeEdit_textChanged(const QString& s) |
||
274 | { |
||
4468 | subik | 275 | useSamples = false; |
4762 | subik | 276 | codeEdit_check(s); |
4460 | subik | 277 | } |
278 | |||
4468 | subik | 279 | bool BarcodeGenerator::paintBarcode(QString fileName, int dpi) |
4460 | subik | 280 | { |
4468 | subik | 281 | if (fileName == QString::null) |
282 | fileName = tmpFile; |
||
4460 | subik | 283 | QString opts("barcolor=%1 showbackground backgroundcolor=%2 textcolor=%3"); |
284 | opts = opts.arg(lnColor.name().replace('#', "")) \ |
||
285 | .arg(bgColor.name().replace('#', "")) \ |
||
286 | .arg(txtColor.name().replace('#', "")); |
||
287 | if (textCheck->isChecked()) |
||
288 | opts += " includetext"; |
||
289 | if (guardCheck->isChecked()) |
||
290 | opts += " guardwhitespace"; |
||
4835 | subik | 291 | if (includeCheckInText->isChecked() & includeCheckInText->isEnabled()) |
292 | opts += " includecheckintext"; |
||
293 | if (includeCheck->isChecked() & includeCheck->isEnabled()) |
||
294 | opts += " includecheck"; |
||
4460 | subik | 295 | QString comm("15 10 moveto (%1) (%2) %3 barcode"); |
296 | comm = comm.arg(codeEdit->text()).arg(opts).arg(map[bcCombo->currentText()].command); |
||
297 | comm = psCommand + comm; |
||
298 | QFile f(psFile); |
||
299 | if (!f.open(IO_WriteOnly)) |
||
300 | { |
||
301 | sampleLabel->setText("<qt>" + tr("Error opening file: %1").arg(psFile) + "</qt>"); |
||
4468 | subik | 302 | return false; |
4460 | subik | 303 | } |
304 | QTextStream ts(&f); |
||
305 | ts << comm; |
||
306 | f.close(); |
||
4527 | subik | 307 | |
308 | QStringList gargs; |
||
4818 | subik | 309 | // limit the area only for preview. EPS importer bounds the box itself. |
310 | if (fileName == tmpFile) |
||
311 | { |
||
312 | gargs.append("-dDEVICEWIDTHPOINTS=200"); |
||
313 | gargs.append("-dDEVICEHEIGHTPOINTS=150"); |
||
314 | } |
||
4527 | subik | 315 | gargs.append( QString("-r%1").arg(dpi) ); |
4685 | subik | 316 | gargs.append( QString("-sOutputFile=%1").arg(fileName) ); |
4527 | subik | 317 | gargs.append( psFile ); |
4460 | subik | 318 | int gs = callGS(gargs); |
4468 | subik | 319 | bool retval = true; |
320 | if (gs != 0) |
||
321 | retval = false; |
||
322 | // setup only preview |
||
323 | if (fileName != tmpFile) |
||
324 | return retval; |
||
4460 | subik | 325 | if (gs == 0) |
4762 | subik | 326 | { |
4468 | subik | 327 | sampleLabel->setPixmap(QPixmap(fileName)); |
4762 | subik | 328 | okButton->setEnabled(true); |
329 | } |
||
4460 | subik | 330 | else |
4762 | subik | 331 | { |
4818 | subik | 332 | sampleLabel->setText("<qt>" + tr("Barcode incomplete") + "</qt>"); |
4762 | subik | 333 | okButton->setEnabled(false); |
334 | } |
||
4468 | subik | 335 | return retval; |
4460 | subik | 336 | } |
4468 | subik | 337 | |
338 | void BarcodeGenerator::resetButton_clicked() |
||
339 | { |
||
340 | useSamples = true; |
||
341 | bcComboChanged(); |
||
342 | } |