1 /**************************************************************************
5 Unix Remote Printing Operation
6 Copyright (c) Arto Hyvättinen 2010
8 This file is part of URPO.
10 URPO 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.
15 URPO 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.
21 **************************************************************************/
23 #include "printwidget.h"
28 #include <QPushButton>
30 #include <QProgressBar>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
33 #include <QFileDialog>
37 PrintWidget::PrintWidget(QWidget *parent) :
41 filenameEdit = new QLineEdit();
42 browseButton = new QPushButton( tr("Browse"));
43 connect(browseButton, SIGNAL(clicked()), this, SLOT(browseFile()));
45 printerCombo = new QComboBox();
46 printerCombo->setEnabled(false);
47 printerCombo->setEditable(true);
49 rangeEdit = new QLineEdit();
50 // Page ranges in format 1-3,5,8-10
51 // Valid characters: 0123456789 , +
52 QRegExpValidator* rangeSensor = new QRegExpValidator( QRegExp("[0-9\\-,]+"), this );
53 rangeEdit->setValidator( rangeSensor);
56 copiesSpin = new QSpinBox();
57 copiesSpin->setRange(1,99);
58 copiesSpin->setValue(1);
61 persheetCombo = new QComboBox();
62 persheetCombo->insertItem(0,"1",1);
63 persheetCombo->insertItem(1,"2",2);
64 persheetCombo->insertItem(2,"4",4);
66 printButton = new QPushButton( tr("Print"));
67 printButton->setEnabled(false);
68 connect( printButton, SIGNAL(clicked()), this, SLOT(doPrint()));
70 statusLabel = new QLabel();
71 progressBar = new QProgressBar();
72 progressBar->setRange(0,0);
73 cancelButton = new QPushButton(tr("Cancel"));
74 connect(cancelButton,SIGNAL(clicked()),this,SIGNAL(cancel()));
76 reconnectButton = new QPushButton( tr("Reconnect"));
77 connect(reconnectButton, SIGNAL(clicked()), this, SIGNAL(reconnect()));
80 QVBoxLayout* mainLayout = new QVBoxLayout();
81 QHBoxLayout* fileLayout = new QHBoxLayout();
82 fileLayout->addWidget( new QLabel( tr ("File") ));
83 fileLayout->addWidget(filenameEdit);
84 fileLayout->addWidget(browseButton);
85 mainLayout->addLayout(fileLayout);
87 QHBoxLayout* printerLayout = new QHBoxLayout();
88 printerLayout->addWidget(new QLabel( tr("Printer")));
89 printerLayout->addWidget(printerCombo);
90 printerLayout->addStretch();
91 mainLayout->addLayout(printerLayout);
93 QHBoxLayout* rangeLayout = new QHBoxLayout();
94 rangeLayout->addWidget(new QLabel(tr("Page range")));
95 rangeLayout->addWidget(rangeEdit);
96 mainLayout->addLayout(rangeLayout);
98 QHBoxLayout* sheetLayout = new QHBoxLayout();
99 sheetLayout->addWidget( new QLabel(tr("Pages per sheet")) );
100 sheetLayout->addWidget(persheetCombo);
101 sheetLayout->addStretch();
102 sheetLayout->addWidget(new QLabel(tr("Copies")));
103 sheetLayout->addWidget(copiesSpin);
104 mainLayout->addLayout(sheetLayout);
106 mainLayout->addStretch();
108 mainLayout->addWidget(statusLabel);
109 QHBoxLayout* barLayout = new QHBoxLayout;
110 barLayout->addWidget(progressBar);
111 barLayout->addStretch();
112 barLayout->addWidget(cancelButton);
113 barLayout->addWidget(reconnectButton);
114 barLayout->addWidget(printButton);
115 mainLayout->addLayout(barLayout);
117 setLayout(mainLayout);
119 progressBar->setVisible(false);
123 void PrintWidget::setStatus(QString message, bool busy)
125 statusLabel->setText(message);
128 // Busy: show progress bar, enable Cancel, disable others.
129 progressBar->setVisible(true);
130 cancelButton->setEnabled(true);
131 reconnectButton->setEnabled(false);
135 progressBar->setVisible(false);
136 reconnectButton->setEnabled(true);
137 cancelButton->setEnabled(false);
141 void PrintWidget::setPrinters(QStringList printers)
143 printerCombo->clear();
144 if(printers.isEmpty())
146 printerCombo->setEnabled(false);
150 printerCombo->addItems(printers);
151 printerCombo->setEnabled(true);
152 printerCombo->setCurrentIndex(0);
156 void PrintWidget::setReady(bool ready)
160 setStatus( tr("Ready"), false);
161 printButton->setEnabled(true);
164 printButton->setEnabled(false);
168 void PrintWidget::browseFile()
170 QString path = QFileDialog::getOpenFileName(this, tr("Print file"));
172 filenameEdit->setText(path);
176 void PrintWidget::doPrint()
178 // Make cups lp options
179 QString options = QString("-d %1 -o number-up=").arg(printerCombo->currentText());
180 options.append(persheetCombo->currentText());
181 if( !rangeEdit->text().isEmpty())
182 options += " -o page-ranges=" + rangeEdit->text();
183 if( copiesSpin->value() > 1)
184 options += QString(" -n %1").arg(copiesSpin->value());
185 emit print(filenameEdit->text(), options);