| 1 | /* |
| 2 | * File: PrinterProperties.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "PrinterProperties.h" |
| 26 | |
| 27 | #include <QMessageBox> |
| 28 | #include <QFileDialog> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <sstream> |
| 32 | |
| 33 | PrinterProperties::PrinterProperties(ConfigLine& line, QWidget *parent) |
| 34 | : GenericDeviceProperties(line, parent) |
| 35 | { |
| 36 | ui.setupUi(this); |
| 37 | |
| 38 | ui.filename->setValidator(new PrinterFileNameValidator(this)); |
| 39 | |
| 40 | if (!line.isNew()) |
| 41 | { |
| 42 | ui.deviceNumber->setText(mLine.getToken(0).c_str()); |
| 43 | ui.printerCombo->setCurrentIndex( (mLine.getToken(1) == "3211") ? 1 : 0); |
| 44 | if (mLine.size() > 2) |
| 45 | { |
| 46 | ui.filename->setText(mLine.getToken(2).c_str()); |
| 47 | if (mLine.size() > 3) |
| 48 | { |
| 49 | std::string crlf = mLine.getToken(3); |
| 50 | std::transform(crlf.begin(), crlf.end(), crlf.begin(), ::tolower); |
| 51 | ui.crlfCheckBox->setChecked(crlf == "crlf"); |
| 52 | } |
| 53 | } |
| 54 | else |
| 55 | ui.crlfCheckBox->setEnabled(false); |
| 56 | } |
| 57 | |
| 58 | connect(ui.OKButton, SIGNAL(clicked()), this, SLOT(ok())); |
| 59 | connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); |
| 60 | connect(ui.filenameBrowseButton, SIGNAL(clicked()), this, SLOT(fileNameBrowse())); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | PrinterProperties::~PrinterProperties() |
| 65 | { |
| 66 | |
| 67 | } |
| 68 | |
| 69 | void PrinterProperties::ok() |
| 70 | { |
| 71 | if (!deviceNumberValidator(ui.deviceNumber)) |
| 72 | return; |
| 73 | |
| 74 | if (ui.filename->text().isEmpty()) |
| 75 | { |
| 76 | QMessageBox::warning(this, "File name is empty", "Please provide file name", QMessageBox::Ok); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | std::stringstream newLineBuff; |
| 82 | bool crlf = (ui.filename->text().isEmpty() == false) && (ui.crlfCheckBox->isChecked()); |
| 83 | |
| 84 | newLineBuff << ui.deviceNumber->text().toStdString() << " " |
| 85 | << ui.printerCombo->currentText().toStdString() << " " |
| 86 | << ui.filename->text().toStdString() << " " |
| 87 | << (crlf ? "crlf" : " ") |
| 88 | << "\n"; |
| 89 | hOutDebug(5,"new line:" << newLineBuff.str()); |
| 90 | mLine.replaceLine(newLineBuff.str()); |
| 91 | emit updateLine(true); |
| 92 | this->deleteLater(); |
| 93 | } |
| 94 | |
| 95 | void PrinterProperties::cancel() |
| 96 | { |
| 97 | emit updateLine(false); |
| 98 | this->deleteLater(); |
| 99 | } |
| 100 | |
| 101 | void PrinterProperties::setCrlfEnabled(bool checked) |
| 102 | { |
| 103 | ui.crlfCheckBox->setEnabled(checked); |
| 104 | } |
| 105 | |
| 106 | void PrinterProperties::fileNameBrowse() |
| 107 | { |
| 108 | std::string s = QFileDialog::getOpenFileName(this, |
| 109 | "Set Printer File", |
| 110 | "~/", |
| 111 | tr("All files(*)")).toUtf8().data(); |
| 112 | ui.filename->setText(s.c_str()); |
| 113 | } |
| 114 | |
| 115 | PrinterFileNameValidator::PrinterFileNameValidator(QObject * parent) |
| 116 | : QValidator(parent), mParent(dynamic_cast<PrinterProperties *>(parent)) |
| 117 | { |
| 118 | |
| 119 | } |
| 120 | |
| 121 | QValidator::State PrinterFileNameValidator::validate(QString & v, int & ) const |
| 122 | { |
| 123 | mParent->setCrlfEnabled(v.length()>0); |
| 124 | return QValidator::Acceptable; |
| 125 | } |