Corrected: urpo_id.pub in help file for public key file name
[urpo] / src / printwidget.cpp
1 /**************************************************************************
2
3     URPO
4
5     Unix Remote Printing Operation
6     Copyright (c) Arto Hyvättinen 2010
7
8     This file is part of URPO.
9
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.
14
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.
19
20
21 **************************************************************************/
22
23 #include "printwidget.h"
24
25
26 #include <QLineEdit>
27 #include <QComboBox>
28 #include <QPushButton>
29 #include <QLabel>
30 #include <QProgressBar>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
33 #include <QFileDialog>
34 #include <QSpinBox>
35
36
37 PrintWidget::PrintWidget(QWidget *parent) :
38     QWidget(parent)
39 {
40
41     filenameEdit = new QLineEdit();
42     browseButton = new QPushButton( tr("Browse"));
43     connect(browseButton, SIGNAL(clicked()), this, SLOT(browseFile()));
44
45     printerCombo = new QComboBox();
46     printerCombo->setEnabled(false);
47
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);
53
54     // Copies: Spin 0..99
55     copiesSpin = new QSpinBox();
56     copiesSpin->setRange(1,99);
57     copiesSpin->setValue(1);
58
59     // Pages per sheet
60     persheetCombo = new QComboBox();
61     persheetCombo->insertItem(0,"1",1);
62     persheetCombo->insertItem(1,"2",2);
63     persheetCombo->insertItem(2,"4",4);
64
65     printButton = new QPushButton( tr("Print"));
66     printButton->setEnabled(false);
67     connect( printButton, SIGNAL(clicked()), this, SLOT(doPrint()));
68
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()));
74
75     reconnectButton = new QPushButton( tr("Reconnect"));
76     connect(reconnectButton, SIGNAL(clicked()), this, SIGNAL(reconnect()));
77
78
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);
85
86     QHBoxLayout* printerLayout = new QHBoxLayout();
87     printerLayout->addWidget(new QLabel( tr("Printer")));
88     printerLayout->addWidget(printerCombo);
89     printerLayout->addStretch();
90     mainLayout->addLayout(printerLayout);
91
92     QHBoxLayout* rangeLayout = new QHBoxLayout();
93     rangeLayout->addWidget(new QLabel(tr("Page range")));
94     rangeLayout->addWidget(rangeEdit);
95     mainLayout->addLayout(rangeLayout);
96
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);
104
105     mainLayout->addStretch();
106
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);
115
116     setLayout(mainLayout);
117
118     progressBar->setVisible(false);
119
120 }
121
122 void PrintWidget::setStatus(QString message, bool busy)
123 {
124     statusLabel->setText(message);
125     if(busy)
126     {
127         // Busy: show progress bar, enable Cancel, disable others.
128         progressBar->setVisible(true);
129         cancelButton->setEnabled(true);
130         reconnectButton->setEnabled(false);
131     }
132     else
133     {
134         progressBar->setVisible(false);
135         reconnectButton->setEnabled(true);
136         cancelButton->setEnabled(false);
137     }
138 }
139
140 void PrintWidget::setPrinters(QStringList printers)
141 {
142     printerCombo->clear();
143     if(printers.isEmpty())
144     {
145         printerCombo->setEnabled(false);
146     }
147     else
148     {
149         printerCombo->addItems(printers);
150         printerCombo->setEnabled(true);
151         printerCombo->setCurrentIndex(0);
152     }
153 }
154
155 void PrintWidget::setReady(bool ready)
156 {
157     if(ready)
158     {
159         setStatus( tr("Ready"), false);
160         printButton->setEnabled(true);
161     }
162     else
163         printButton->setEnabled(false);
164 }
165
166
167 void PrintWidget::browseFile()
168 {
169     QString path = QFileDialog::getOpenFileName(this, tr("Print file"));
170     if(!path.isNull())
171         filenameEdit->setText(path);
172
173 }
174
175 void PrintWidget::doPrint()
176 {
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);
185 }