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);
48 rangeEdit = new QLineEdit();
49 // Page ranges in format 1-3,5,8-10
50 // Valid characters: 0123456789 , +
51 QRegExpValidator* rangeSensor = new QRegExpValidator( QRegExp("[0-9\\-,]+"), this );
52 rangeEdit->setValidator( rangeSensor);
55 copiesSpin = new QSpinBox();
56 copiesSpin->setRange(1,99);
57 copiesSpin->setValue(1);
60 persheetCombo = new QComboBox();
61 persheetCombo->insertItem(0,"1",1);
62 persheetCombo->insertItem(1,"2",2);
63 persheetCombo->insertItem(2,"4",4);
65 printButton = new QPushButton( tr("Print"));
66 printButton->setEnabled(false);
67 connect( printButton, SIGNAL(clicked()), this, SLOT(doPrint()));
69 statusLabel = new QLabel();
70 progressBar = new QProgressBar();
71 progressBar->setRange(0,0);
72 cancelButton = new QPushButton(tr("Cancel"));
73 connect(cancelButton,SIGNAL(clicked()),this,SIGNAL(cancel()));
75 reconnectButton = new QPushButton( tr("Reconnect"));
76 connect(reconnectButton, SIGNAL(clicked()), this, SIGNAL(reconnect()));
79 QVBoxLayout* mainLayout = new QVBoxLayout();
80 QHBoxLayout* fileLayout = new QHBoxLayout();
81 fileLayout->addWidget( new QLabel( tr ("File") ));
82 fileLayout->addWidget(filenameEdit);
83 fileLayout->addWidget(browseButton);
84 mainLayout->addLayout(fileLayout);
86 QHBoxLayout* printerLayout = new QHBoxLayout();
87 printerLayout->addWidget(new QLabel( tr("Printer")));
88 printerLayout->addWidget(printerCombo);
89 printerLayout->addStretch();
90 mainLayout->addLayout(printerLayout);
92 QHBoxLayout* rangeLayout = new QHBoxLayout();
93 rangeLayout->addWidget(new QLabel(tr("Page range")));
94 rangeLayout->addWidget(rangeEdit);
95 mainLayout->addLayout(rangeLayout);
97 QHBoxLayout* sheetLayout = new QHBoxLayout();
98 sheetLayout->addWidget( new QLabel(tr("Pages per sheet")) );
99 sheetLayout->addWidget(persheetCombo);
100 sheetLayout->addStretch();
101 sheetLayout->addWidget(new QLabel(tr("Copies")));
102 sheetLayout->addWidget(copiesSpin);
103 mainLayout->addLayout(sheetLayout);
105 mainLayout->addStretch();
107 mainLayout->addWidget(statusLabel);
108 QHBoxLayout* barLayout = new QHBoxLayout;
109 barLayout->addWidget(progressBar);
110 barLayout->addStretch();
111 barLayout->addWidget(cancelButton);
112 barLayout->addWidget(reconnectButton);
113 barLayout->addWidget(printButton);
114 mainLayout->addLayout(barLayout);
116 setLayout(mainLayout);
118 progressBar->setVisible(false);
122 void PrintWidget::setStatus(QString message, bool busy)
124 statusLabel->setText(message);
127 // Busy: show progress bar, enable Cancel, disable others.
128 progressBar->setVisible(true);
129 cancelButton->setEnabled(true);
130 reconnectButton->setEnabled(false);
134 progressBar->setVisible(false);
135 reconnectButton->setEnabled(true);
136 cancelButton->setEnabled(false);
140 void PrintWidget::setPrinters(QStringList printers)
142 printerCombo->clear();
143 if(printers.isEmpty())
145 printerCombo->setEnabled(false);
149 printerCombo->addItems(printers);
150 printerCombo->setEnabled(true);
151 printerCombo->setCurrentIndex(0);
155 void PrintWidget::setReady(bool ready)
159 setStatus( tr("Ready"), false);
160 printButton->setEnabled(true);
163 printButton->setEnabled(false);
167 void PrintWidget::browseFile()
169 QString path = QFileDialog::getOpenFileName(this, tr("Print file"));
171 filenameEdit->setText(path);
175 void PrintWidget::doPrint()
177 // Make cups lp options
178 QString options = QString("-d %1 -o number-up=").arg(printerCombo->currentText());
179 options.append(persheetCombo->currentText());
180 if( !rangeEdit->text().isEmpty())
181 options += " -o page-ranges=" + rangeEdit->text();
182 if( copiesSpin->value() > 1)
183 options += QString(" -n %1").arg(copiesSpin->value());
184 emit print(filenameEdit->text(), options);