diff options
author | Simon Rettberg | 2015-10-06 16:55:27 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-10-06 16:55:27 +0200 |
commit | f87a08c71516ab004ca7130e9cf05943c00bd09d (patch) | |
tree | f17ed80ef0d4041ddfde5706163bf6655d0ec943 | |
parent | [printergui] Fix memory leak (diff) | |
download | printergui-f87a08c71516ab004ca7130e9cf05943c00bd09d.tar.gz printergui-f87a08c71516ab004ca7130e9cf05943c00bd09d.tar.xz printergui-f87a08c71516ab004ca7130e9cf05943c00bd09d.zip |
[printergui] Refactor option parsing
-rw-r--r-- | src/maingui/printergui.cpp | 121 | ||||
-rw-r--r-- | src/maingui/printergui.h | 9 |
2 files changed, 52 insertions, 78 deletions
diff --git a/src/maingui/printergui.cpp b/src/maingui/printergui.cpp index b4f1473..feddb52 100644 --- a/src/maingui/printergui.cpp +++ b/src/maingui/printergui.cpp @@ -3,9 +3,13 @@ #include <QMessageBox> #include <unistd.h> #include <QTimer> +#include <QStatusBar> #include <pwd.h> #include <cups/ppd.h> +static QStringList knownColorOptions = QStringList() << "ColorModel" << "XRXColor"; +static QStringList knownDuplexOptions = QStringList() << "Duplex"; + // ____________________________________________________________________________ PrinterGui::PrinterGui(char *argv[], QWidget *parent) : QMainWindow(parent), @@ -55,6 +59,10 @@ void PrinterGui::initializeUI() { ui->setupUi(this); ui->horizontalLayoutButtons->setAlignment(Qt::AlignRight); + ui->comboBoxColor->setEnabled(false); + ui->comboBoxSides->setEnabled(false); + ui->label_color->setEnabled(false); + ui->label_duplex->setEnabled(false); /* Initialize Treeview */ @@ -97,18 +105,39 @@ void PrinterGui::initializeUI() desktopRect.height()/2-this->height()/2); this->setWindowTitle(QString::fromUtf8("Drucken - %1").arg(this->user)); - ui->comboBoxColor->hide(); - ui->comboBoxSides->hide(); - ui->label_color->hide(); - ui->label_duplex->hide(); - this->show(); this->showNormal(); this->raise(); this->activateWindow(); } - +void PrinterGui::enableOptionSelection(ppd_file_t *ppd, QStringList &nameList, QComboBox *combo, QLabel *label) +{ + // Check for each option if it matches an + QString matchedProperty; + + for (ppd_option_t *o = ppdFirstOption(ppd); o != NULL; o = ppdNextOption(ppd)) { + QString option(o->keyword); + for (QStringList::iterator it = nameList.begin(); it != nameList.end(); ++it) { + if(!option.contains(*it)) + continue; + // Matches, update combo + matchedProperty = option; + combo->clear(); + for (int i = 0; i < o->num_choices; ++i) { + combo->addItem(QString::fromUtf8(o->choices[i].text), QString::fromLatin1(o->choices[i].choice)); + if (strcmp(o->choices[i].choice, o->defchoice) == 0) { + combo->setCurrentIndex(i); + } + } + combo->adjustSize(); + break; + } + } + combo->setProperty("key", matchedProperty); + combo->setEnabled(!matchedProperty.isEmpty()); + label->setEnabled(!matchedProperty.isEmpty()); +} // ____________________________________________________________________________ void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) @@ -127,72 +156,12 @@ void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTr res = ::atoi(type); } -// ui->comboBoxSides->hide(); -// ui->comboBoxColor->hide(); -// ui->label_color->hide(); -// ui->label_duplex->hide(); - ppd_file_t *ppd = ppdOpenFile(cupsGetPPD2(CUPS_HTTP_DEFAULT, dest->name)); if (ppd != NULL) { // Check color capabilities - if (res & CUPS_PRINTER_COLOR) { - // Check for each option if it matches an - _colorOptionName.clear(); - QStringList knownColorOptions; - knownColorOptions << "ColorModel" << "XRXColor"; // TODO read from a file - - for (ppd_option_t *o = ppdFirstOption(ppd); o != NULL; o = ppdNextOption(ppd)) { - QString option(o->keyword); - for (QStringList::iterator it = knownColorOptions.begin(); - it != knownColorOptions.end(); ++it) { - if(option.contains(*it)) { - _colorOptionName = option; - // Add the choices to the combobox - ui->comboBoxColor->clear(); - for (int i = 0; i < o->num_choices; ++i) { - ui->comboBoxColor->addItem(o->choices[i].text, o->choices[i].choice); - if (strcmp(o->choices[i].choice, o->defchoice) == 0) { - ui->comboBoxColor->setCurrentIndex(i); - } - } - ui->comboBoxColor->show(); - ui->label_color->show(); - ui->comboBoxColor->adjustSize(); - break; - } - } - } - } - - // Check duplex capabilities - if (res & CUPS_PRINTER_DUPLEX) { - // Check for each option if it matches an - _duplexOptionName.clear(); - QStringList knownDuplexOptions; - knownDuplexOptions << "Duplex"; // TODO read from a file - - for (ppd_option_t *o = ppdFirstOption(ppd); o != NULL; o = ppdNextOption(ppd)) { - QString option(o->keyword); - for (QStringList::iterator it = knownDuplexOptions.begin(); - it != knownDuplexOptions.end(); ++it) { - if (option == *it) { - _duplexOptionName = option; - // Add the choices to the combobox - ui->comboBoxSides->clear(); - for (int i = 0; i < o->num_choices; ++i) { - ui->comboBoxSides->addItem(o->choices[i].text, o->choices[i].choice); - if (strcmp(o->choices[i].choice, o->defchoice) == 0) { - ui->comboBoxSides->setCurrentIndex(i); - } - } - ui->comboBoxSides->show(); - ui->label_duplex->show(); - ui->comboBoxSides->adjustSize(); - break; - } - } - } - } + //if (res & CUPS_PRINTER_COLOR) // No needed? Should just get disabled either way + enableOptionSelection(ppd, knownColorOptions, ui->comboBoxColor, ui->label_color); + enableOptionSelection(ppd, knownDuplexOptions, ui->comboBoxSides, ui->label_duplex); ppdClose(ppd); } else { qDebug() << "ppd is null"<< dest->name << cupsLastErrorString(); @@ -211,8 +180,10 @@ void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTr // Check availability if (res & CUPS_PRINTER_REJECTING) { ui->buttonPrint->setEnabled(false); + statusBar()->showMessage("Dieser Drucker nimmt zur Zeit keine Aufträge an"); } else { ui->buttonPrint->setEnabled(true); + statusBar()->clearMessage(); } } @@ -237,20 +208,22 @@ void PrinterGui::on_buttonPrint_clicked() dests); // Color - if (!_colorOptionName.isEmpty()) - dest->num_options = cupsAddOption (_colorOptionName.toStdString().c_str(), + QString colorKey = ui->comboBoxColor->property("key").toString(); + if (!colorKey.isEmpty()) + dest->num_options = cupsAddOption(colorKey.toUtf8().constData(), ui->comboBoxColor->itemData(ui->comboBoxColor->currentIndex()).toString().toStdString().c_str(), dest->num_options, &(dest->options)); // Duplex - if (!_duplexOptionName.isEmpty()) - dest->num_options = cupsAddOption (_duplexOptionName.toStdString().c_str(), + QString duplexKey = ui->comboBoxSides->property("key").toString(); + if (!duplexKey.isEmpty()) + dest->num_options = cupsAddOption(duplexKey.toUtf8().constData(), ui->comboBoxSides->itemData(ui->comboBoxSides->currentIndex()).toString().toStdString().c_str(), dest->num_options, &(dest->options)); // Kopien if (ui->lineEditCopies->isEnabled()) { - dest->num_options = cupsAddOption ("copies", + dest->num_options = cupsAddOption("copies", ui->lineEditCopies->text().toUtf8().constData(), dest->num_options, &(dest->options)); diff --git a/src/maingui/printergui.h b/src/maingui/printergui.h index 624b44c..17fc8e6 100644 --- a/src/maingui/printergui.h +++ b/src/maingui/printergui.h @@ -13,6 +13,10 @@ class PrinterGui; } class QTimer; +class QLabel; +class QComboBox; +class QStringList; +typedef struct ppd_file_s ppd_file_t; class PrinterGui : public QMainWindow { @@ -31,16 +35,13 @@ private slots: private: Ui::PrinterGui *ui; void initializeUI(); + void enableOptionSelection(ppd_file_t *ppd, QStringList &nameList, QComboBox *combo, QLabel *label); cups_dest_t *dests; int num_dests; char * user; char * file; QTimer * bgTimer; int bgTimeout; - - QString _colorOptionName; - QString _duplexOptionName; - }; #endif // MAINWINDOW_H |