| 1 | #include "StationeryDialog.h" |
| 2 | #include "ui_StationeryDialog.h" |
| 3 | |
| 4 | #include "FieldTip.h" |
| 5 | #include "Environment.h" |
| 6 | #include "PagePrinter.h" |
| 7 | #include "HerculesStudio.h" |
| 8 | #include "ConfigurationEditor.h" |
| 9 | #include "Preferences.h" |
| 10 | |
| 11 | #include <QSpinBox> |
| 12 | #include <QToolTip> |
| 13 | #include <QPainter> |
| 14 | #include <QMessageBox> |
| 15 | |
| 16 | #include <sstream> |
| 17 | |
| 18 | //TODO: fix paper sizes by qt's sizes and add 1 or 2 fanfold |
| 19 | |
| 20 | const char *paperHeight[] = |
| 21 | { |
| 22 | "11", |
| 23 | "14", |
| 24 | "17", |
| 25 | "11", |
| 26 | "11", |
| 27 | "14.875", //Std FanFold |
| 28 | "46.81", //A0 |
| 29 | "33.11", //A1 |
| 30 | "23.39", //A2 |
| 31 | "16.54", //A3 |
| 32 | "11.69", //A4 |
| 33 | "8.27", //A5 |
| 34 | "0", |
| 35 | }; |
| 36 | |
| 37 | const char *paperWidth[] = |
| 38 | { |
| 39 | "8.5", |
| 40 | "8.5", |
| 41 | "11", |
| 42 | "9.5", |
| 43 | "15", |
| 44 | "11", //Std FanFold |
| 45 | "33.11", //A0 |
| 46 | "23.39", //A1 |
| 47 | "16.54", //A2 |
| 48 | "11.69", //A3 |
| 49 | "8.27", //A4 |
| 50 | "5.83", //A5 |
| 51 | "0", |
| 52 | }; |
| 53 | |
| 54 | const char *paperHeightMm[] = |
| 55 | { |
| 56 | "279.4", |
| 57 | "266.7", |
| 58 | "432", |
| 59 | "279.4", |
| 60 | "279.4", |
| 61 | "377.8", |
| 62 | "1189", // A0 |
| 63 | "841", // A1 |
| 64 | "495", // A2 |
| 65 | "420", // A3 |
| 66 | "297", // A4 |
| 67 | "210", // A5 |
| 68 | "0", |
| 69 | }; |
| 70 | |
| 71 | const char *paperWidthMm[] = |
| 72 | { |
| 73 | "215.9", |
| 74 | "215.9", |
| 75 | "279", |
| 76 | "241.3", |
| 77 | "381", |
| 78 | "279.4", |
| 79 | "841", // A0 |
| 80 | "495", // A1 |
| 81 | "420", // A2 |
| 82 | "297", // A3 |
| 83 | "210", // A4 |
| 84 | "148", // A5 |
| 85 | "0", |
| 86 | }; |
| 87 | |
| 88 | StationeryDialog::StationeryDialog(QWidget *parent, const QString &item) : |
| 89 | QDialog(parent), |
| 90 | ui(new Ui::PaperDialog) |
| 91 | { |
| 92 | ui->setupUi(this); |
| 93 | unsigned int i; |
| 94 | |
| 95 | QStringList items = Preferences::getInstance().stationeryList(); |
| 96 | if (items.count() > 0) ui->stationeryNameComboBox->insertItems(0,items); |
| 97 | |
| 98 | for (i=0; i<Stationery::paperSizes(); i++) |
| 99 | ui->paperSizeComboBox->addItem(Stationery::paperSize(i)); |
| 100 | |
| 101 | ui->inchesRadioButton->setChecked(true); |
| 102 | ui->landscapeRadioButton->setChecked(true); |
| 103 | ui->paperSizeComboBox->setCurrentIndex(0); |
| 104 | paperSizeChanged(ui->paperSizeComboBox->currentText()); |
| 105 | ui->barsColorComboBox->setCurrentIndex(0); |
| 106 | ui->decorationCheckBox->setChecked(true); |
| 107 | ui->customColor->setEnabled(false); |
| 108 | |
| 109 | for (i=0; strcmp(Stationery::mBarsColorNames[i],"Custom") != 0; i++) |
| 110 | { |
| 111 | ui->barsColorComboBox->addItem(Stationery::mBarsColorNames[i]); |
| 112 | } |
| 113 | ui->barsColorComboBox->addItem("Custom"); |
| 114 | |
| 115 | QRegExp validation("^#[A-Fa-f0-9]{6}$"); |
| 116 | QRegExpValidator *validator = new QRegExpValidator(validation); |
| 117 | ui->customColor->setValidator(validator); |
| 118 | |
| 119 | mTopMarginSpinBox = new InchesSpinBox(ui->paperTab); |
| 120 | mTopMarginSpinBox->setObjectName(QStringLiteral("topMarginSpinBox")); |
| 121 | mTopMarginSpinBox->setGeometry(QRect(140, 350, 120, 31)); |
| 122 | mTopMarginSpinBox->setSingleStep(1); |
| 123 | |
| 124 | QString icon1 = Environment::getIconsPath().c_str(); |
| 125 | icon1 += "/landscape.png"; |
| 126 | QPixmap * pixmap1 = new QPixmap(icon1); |
| 127 | ui->orientationLabel->setPixmap(*pixmap1); |
| 128 | |
| 129 | mPaperPreview = new PaperPreview(this, ui->styleTab); |
| 130 | mPaperPreview->setGeometry(20,190,450,230); |
| 131 | repaint(); |
| 132 | |
| 133 | if (item.length() > 0) |
| 134 | { |
| 135 | ui->stationeryNameComboBox->setCurrentText(item); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | ui->stationeryNameComboBox->setCurrentIndex(0); |
| 140 | } |
| 141 | populate(); |
| 142 | |
| 143 | connect(ui->stationeryNameComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(stationeryNameChanged(QString))); |
| 144 | connect(ui->stationeryNameComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(stationeryNameEdited(QString))); |
| 145 | connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked())); |
| 146 | connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked())); |
| 147 | connect(ui->paperSizeComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(paperSizeChanged(QString))); |
| 148 | connect(ui->inchesRadioButton, SIGNAL(toggled(bool)), this, SLOT(inchesMm(bool))); |
| 149 | connect(ui->landscapeRadioButton, SIGNAL(toggled(bool)), this, SLOT(landscape(bool))); |
| 150 | connect(ui->barsColorComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(barsColorChanged(QString))); |
| 151 | connect(ui->decorationCheckBox, SIGNAL(stateChanged(int)), this, SLOT(decorationToggeled())); |
| 152 | connect(ui->feedHolesCheckBox, SIGNAL(stateChanged(int)), this, SLOT(feedHolesToggeled())); |
| 153 | connect(ui->customColor, SIGNAL(textChanged(QString)), this, SLOT(customColorChanged())); |
| 154 | |
| 155 | if (Preferences::getInstance().balloonStationery() && builtinStationery(ui->stationeryNameComboBox->currentText())) |
| 156 | { |
| 157 | Preferences::getInstance().setBalloonStationery(true); |
| 158 | FieldTip *baloon = new FieldTip(this, true); |
| 159 | baloon->setOwner(ui->stationeryNameComboBox); |
| 160 | baloon->showMessage("Change this name to define you own stationery", 10000); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | StationeryDialog::~StationeryDialog() |
| 165 | { |
| 166 | delete ui; |
| 167 | } |
| 168 | |
| 169 | void StationeryDialog::paperSizeChanged(QString value) |
| 170 | { |
| 171 | if (value.compare("Custom") == 0) |
| 172 | { |
| 173 | ui->customSizeH->setEnabled(true); |
| 174 | ui->customSizeW->setEnabled(true); |
| 175 | ui->orientationGroupBox->setEnabled(false); |
| 176 | |
| 177 | FieldTip *balloon = new FieldTip(ui->paperTab); |
| 178 | balloon->setOwner(ui->customSizeH); |
| 179 | balloon->showMessage("Enter width and height of paper",5000); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | hOutDebug(3,"i " << ui->paperSizeComboBox->currentIndex() << " v " << ui->customSizeW->text().toUtf8().data()); |
| 184 | if (ui->inchesRadioButton->isChecked()) |
| 185 | { |
| 186 | ui->customSizeW->setText(paperWidth[ui->paperSizeComboBox->currentIndex()]); |
| 187 | ui->customSizeH->setText(paperHeight[ui->paperSizeComboBox->currentIndex()]); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | ui->customSizeW->setText(paperWidthMm[ui->paperSizeComboBox->currentIndex()]); |
| 192 | ui->customSizeH->setText(paperHeightMm[ui->paperSizeComboBox->currentIndex()]); |
| 193 | } |
| 194 | ui->customSizeH->setEnabled(false); |
| 195 | ui->customSizeW->setEnabled(false); |
| 196 | ui->orientationGroupBox->setEnabled(true); |
| 197 | } |
| 198 | repaint(); |
| 199 | } |
| 200 | |
| 201 | void StationeryDialog::barsColorChanged(QString value) |
| 202 | { |
| 203 | if (value.compare("Custom") == 0) |
| 204 | { |
| 205 | ui->customColor->setEnabled(true); |
| 206 | FieldTip *balloon = new FieldTip(ui->styleTab); |
| 207 | balloon->setOwner(ui->customColor); |
| 208 | balloon->showMessage("Enter RGB value in html format (e.g. #f0fff0)",5000); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | ui->customColor->setEnabled(false); |
| 213 | StationeryPtr stationery = createRecord(); |
| 214 | int rgbSet[3]; |
| 215 | stationery->getBarsColor(rgbSet); |
| 216 | QString rgb("#"); |
| 217 | char hexBuf[3]; |
| 218 | for (int i=0; i<3 ; i++) |
| 219 | { |
| 220 | sprintf(hexBuf,"%2x", rgbSet[i]); |
| 221 | rgb += hexBuf; |
| 222 | } |
| 223 | ui->customColor->setText(rgb); |
| 224 | if (mPaperPreview != NULL) mPaperPreview->repaint(); |
| 225 | } |
| 226 | ui->decorationCheckBox->setDisabled( (value.compare("None") == 0) ); |
| 227 | repaint(); |
| 228 | } |
| 229 | |
| 230 | void StationeryDialog::inchesMm(bool) |
| 231 | { |
| 232 | if (ui->paperSizeComboBox->currentText().compare("Custom") == 0) |
| 233 | { |
| 234 | |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | paperSizeChanged(ui->paperSizeComboBox->currentText()); |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | void StationeryDialog::landscape(bool checked) |
| 244 | { |
| 245 | QString icon1 = Environment::getIconsPath().c_str(); |
| 246 | if (checked) |
| 247 | { |
| 248 | icon1 += "/landscape.png"; |
| 249 | QPixmap * pixmap1 = new QPixmap(icon1); |
| 250 | ui->orientationLabel->setPixmap(*pixmap1); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | icon1 += "/portrait.png"; |
| 255 | QPixmap * pixmap1 = new QPixmap(icon1); |
| 256 | ui->orientationLabel->setPixmap(*pixmap1); |
| 257 | |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void StationeryDialog::save(QString name) |
| 262 | { |
| 263 | //TODO - sort(??) |
| 264 | if (name.length() == 0) return; |
| 265 | if (name.indexOf("\"") >= 0) //TODO: here is too late (in case combo box was changed |
| 266 | { |
| 267 | QMessageBox::warning(this, "stationery", "Stationery name cannot contain quotes", |
| 268 | QMessageBox::Ok, QMessageBox::NoButton); |
| 269 | ui->stationeryNameComboBox->setFocus(); |
| 270 | return; |
| 271 | } |
| 272 | QSharedPointer<Stationery> st(createRecord()); |
| 273 | Preferences::getInstance().setStationery(name, *st); |
| 274 | } |
| 275 | |
| 276 | void StationeryDialog::stationeryNameChanged(QString) |
| 277 | { |
| 278 | save(mCurrentName); |
| 279 | mCurrentName = ui->stationeryNameComboBox->currentText(); |
| 280 | populate(); |
| 281 | repaint(); |
| 282 | } |
| 283 | |
| 284 | void StationeryDialog::stationeryNameEdited(QString name) |
| 285 | { |
| 286 | setProtected(builtinStationery(name)); |
| 287 | } |
| 288 | |
| 289 | void StationeryDialog::cancelClicked() //TODO allow exit when there is no name |
| 290 | { |
| 291 | if (ui->stationeryNameComboBox->currentText().length() == 0) |
| 292 | { |
| 293 | QMessageBox::warning(this, "stationery", "Please enter stationery name", |
| 294 | QMessageBox::Ok, QMessageBox::NoButton); |
| 295 | FieldTip *balloon = new FieldTip(this, true); |
| 296 | balloon->setOwner(ui->stationeryNameComboBox); |
| 297 | balloon->showMessage("Give a name to this stationery",2000); |
| 298 | ui->stationeryNameComboBox->setFocus(); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | save(ui->stationeryNameComboBox->currentText()); |
| 303 | deleteLater(); |
| 304 | } |
| 305 | |
| 306 | bool StationeryDialog::builtinStationery(QString stationeryName) |
| 307 | { |
| 308 | if (stationeryName == "Legacy Green") |
| 309 | return true; |
| 310 | else |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | void StationeryDialog::deleteClicked() |
| 315 | { |
| 316 | QString stationeryToDelete = ui->stationeryNameComboBox->currentText(); |
| 317 | mCurrentName = ""; |
| 318 | Preferences::getInstance().deleteStationery(stationeryToDelete); |
| 319 | ui->stationeryNameComboBox->clear(); |
| 320 | QStringList items = Preferences::getInstance().stationeryList(); |
| 321 | if (items.count() > 0) ui->stationeryNameComboBox->insertItems(0,items); |
| 322 | else ui->stationeryNameComboBox->clear(); |
| 323 | ui->stationeryNameComboBox->setCurrentIndex(0); |
| 324 | } |
| 325 | |
| 326 | StationeryPtr StationeryDialog::createRecord() |
| 327 | { |
| 328 | QSharedPointer<Stationery> ret(new Stationery(ui->stationeryNameComboBox->currentText(), |
| 329 | ui->paperSizeComboBox->currentText(), |
| 330 | ui->customSizeW->text(), |
| 331 | ui->customSizeH->text(), |
| 332 | mTopMarginSpinBox->value(), |
| 333 | ui->mmRadioButton->isChecked(), |
| 334 | ui->portraitRadioButton->isChecked(), |
| 335 | ui->barsColorComboBox->currentText(), |
| 336 | ui->customColor->text(), |
| 337 | ui->decorationCheckBox->isChecked(), |
| 338 | ui->feedHolesCheckBox->isChecked())); |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | void StationeryDialog::populate() |
| 344 | { |
| 345 | QSharedPointer<Stationery> st = QSharedPointer<Stationery>(Preferences::getInstance().stationery(ui->stationeryNameComboBox->currentText())); |
| 346 | ui->paperSizeComboBox->setCurrentText(st->mPaperSize); |
| 347 | ui->customSizeW->setText(st->mSize1); |
| 348 | ui->customSizeH->setText(st->mSize2); |
| 349 | mTopMarginSpinBox->setValue(st->mTopMargin); |
| 350 | ui->mmRadioButton->setChecked(st->mMillimeters); |
| 351 | ui->portraitRadioButton->setChecked(st->mPortrait); |
| 352 | ui->landscapeRadioButton->setChecked(!st->mPortrait); |
| 353 | ui->barsColorComboBox->setCurrentText(st->mBarsColor); |
| 354 | ui->customColor->setText(st->mBarsRGB); |
| 355 | ui->decorationCheckBox->setChecked(st->mDecorated); |
| 356 | ui->feedHolesCheckBox->setChecked(st->mTractorFeedHoles); |
| 357 | |
| 358 | if (ui->paperSizeComboBox->currentText().compare("Custom") == 0) |
| 359 | { |
| 360 | ui->customSizeH->setEnabled(true); |
| 361 | ui->customSizeW->setEnabled(true); |
| 362 | } |
| 363 | barsColorChanged(ui->barsColorComboBox->currentText()); |
| 364 | paperSizeChanged(st->mPaperSize); |
| 365 | bool disabled = builtinStationery(ui->stationeryNameComboBox->currentText()); |
| 366 | setProtected(disabled); |
| 367 | hOutDebug(0,ui->paperSizeComboBox->currentText().toUtf8().data()); |
| 368 | if (ui->paperSizeComboBox->currentText().compare("Custom") == 0) |
| 369 | { |
| 370 | ui->orientationGroupBox->setEnabled(false); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void StationeryDialog::setProtected(bool disabled) |
| 375 | { |
| 376 | ui->paperSizeComboBox->setEnabled(!disabled); |
| 377 | ui->customSizeW->setEnabled(!disabled); |
| 378 | ui->customSizeH->setEnabled(!disabled); |
| 379 | mTopMarginSpinBox->setEnabled(!disabled); |
| 380 | ui->orientationGroupBox->setEnabled(!disabled); |
| 381 | ui->groupBox_2->setEnabled(!disabled); |
| 382 | ui->feedHolesCheckBox->setEnabled(!disabled); |
| 383 | ui->decorationCheckBox->setEnabled(!disabled); |
| 384 | ui->barsColorComboBox->setEnabled(!disabled); |
| 385 | ui->deleteButton->setEnabled(!disabled); |
| 386 | } |
| 387 | |
| 388 | void StationeryDialog::feedHolesToggeled() |
| 389 | { |
| 390 | repaint(); |
| 391 | } |
| 392 | |
| 393 | void StationeryDialog::decorationToggeled() |
| 394 | { |
| 395 | repaint(); |
| 396 | } |
| 397 | |
| 398 | void StationeryDialog::customColorChanged() |
| 399 | { |
| 400 | if (ui->customColor->text().length() != 7) |
| 401 | return; |
| 402 | mPaperPreview->setBarsRgb(ConfigurationEditor::parseNum(ui->customColor->text().right(6).toStdString(), 16)); |
| 403 | repaint(); |
| 404 | } |
| 405 | |
| 406 | // |
| 407 | // InchesSpinBox |
| 408 | // |
| 409 | |
| 410 | InchesSpinBox::InchesSpinBox(QWidget *parent) : QSpinBox(parent) |
| 411 | { |
| 412 | mNumerator = 8; |
| 413 | setRange(1,32); |
| 414 | setSingleStep(1); |
| 415 | setValue(mNumerator); |
| 416 | } |
| 417 | |
| 418 | void InchesSpinBox::setValue(int val) |
| 419 | { |
| 420 | mNumerator = val; |
| 421 | QSpinBox::setValue(mNumerator); |
| 422 | } |
| 423 | |
| 424 | int InchesSpinBox::value() |
| 425 | { |
| 426 | valueFromText(this->text()); |
| 427 | return mNumerator; |
| 428 | } |
| 429 | |
| 430 | int InchesSpinBox::valueFromText(const QString &text) const |
| 431 | { |
| 432 | int slashPos = text.indexOf('/'); |
| 433 | bool ok; |
| 434 | |
| 435 | int numerator = text.left(slashPos).toInt(&ok); |
| 436 | if (!ok) return 8; |
| 437 | |
| 438 | int denominator = text.mid(slashPos+1).toInt(&ok); |
| 439 | hOutDebug(3, "den=" << denominator << " OK=" << (ok? "Y" : "N")); |
| 440 | if (!ok) return 8; |
| 441 | |
| 442 | if (denominator%2) return mNumerator; |
| 443 | if (denominator == 2) mNumerator = numerator * 8; |
| 444 | else if (denominator == 4) mNumerator = numerator * 4; |
| 445 | else if (denominator == 8) mNumerator = numerator * 2; |
| 446 | else mNumerator = numerator; |
| 447 | return mNumerator; |
| 448 | } |
| 449 | |
| 450 | QString InchesSpinBox::textFromValue(int val) const |
| 451 | { |
| 452 | std::stringstream ss; |
| 453 | |
| 454 | if ((val%16) == 0) |
| 455 | { |
| 456 | ss << (val/16) ; |
| 457 | } |
| 458 | else if ((val%8) == 0) |
| 459 | { |
| 460 | ss << (val/8) << "/" << "2"; |
| 461 | } |
| 462 | else if ((val%4) == 0) |
| 463 | { |
| 464 | ss << (val/4) << "/" << "4"; |
| 465 | } |
| 466 | else if ((val%2) == 0) |
| 467 | { |
| 468 | ss << (val/2) << "/" << "8"; |
| 469 | } |
| 470 | else\ |
| 471 | { |
| 472 | ss << val << "/" << "16"; |
| 473 | } |
| 474 | //ss << " (" << val << "/16)"; |
| 475 | |
| 476 | hOutDebug(7, "text=" << ss.str()); |
| 477 | return ss.str().c_str(); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | |
| 482 | // |
| 483 | // PaperPreview |
| 484 | // |
| 485 | |
| 486 | |
| 487 | PaperPreview::PaperPreview(StationeryDialog *dialog, QWidget *parent) : |
| 488 | QWidget(parent), mDialog(dialog) |
| 489 | { |
| 490 | |
| 491 | } |
| 492 | |
| 493 | void PaperPreview::paintEvent(QPaintEvent *) |
| 494 | { |
| 495 | |
| 496 | QPainter p(this); |
| 497 | StationeryPtr s = mDialog->createRecord(); |
| 498 | PagePrinter pp(s, p); |
| 499 | pp.preparePage(true); |
| 500 | } |
| 501 |