Modified printer combo editable, identity key dir /home/user/.ssh
[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     printerCombo->setEditable(true);
48
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);
54
55     // Copies: Spin 0..99
56     copiesSpin = new QSpinBox();
57     copiesSpin->setRange(1,99);
58     copiesSpin->setValue(1);
59
60     // Pages per sheet
61     persheetCombo = new QComboBox();
62     persheetCombo->insertItem(0,"1",1);
63     persheetCombo->insertItem(1,"2",2);
64     persheetCombo->insertItem(2,"4",4);
65
66     printButton = new QPushButton( tr("Print"));
67     printButton->setEnabled(false);
68     connect( printButton, SIGNAL(clicked()), this, SLOT(doPrint()));
69
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()));
75
76     reconnectButton = new QPushButton( tr("Reconnect"));
77     connect(reconnectButton, SIGNAL(clicked()), this, SIGNAL(reconnect()));
78
79
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);
86
87     QHBoxLayout* printerLayout = new QHBoxLayout();
88     printerLayout->addWidget(new QLabel( tr("Printer")));
89     printerLayout->addWidget(printerCombo);
90     printerLayout->addStretch();
91     mainLayout->addLayout(printerLayout);
92
93     QHBoxLayout* rangeLayout = new QHBoxLayout();
94     rangeLayout->addWidget(new QLabel(tr("Page range")));
95     rangeLayout->addWidget(rangeEdit);
96     mainLayout->addLayout(rangeLayout);
97
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);
105
106     mainLayout->addStretch();
107
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);
116
117     setLayout(mainLayout);
118
119     progressBar->setVisible(false);
120
121 }
122
123 void PrintWidget::setStatus(QString message, bool busy)
124 {
125     statusLabel->setText(message);
126     if(busy)
127     {
128         // Busy: show progress bar, enable Cancel, disable others.
129         progressBar->setVisible(true);
130         cancelButton->setEnabled(true);
131         reconnectButton->setEnabled(false);
132     }
133     else
134     {
135         progressBar->setVisible(false);
136         reconnectButton->setEnabled(true);
137         cancelButton->setEnabled(false);
138     }
139 }
140
141 void PrintWidget::setPrinters(QStringList printers)
142 {
143     printerCombo->clear();
144     if(printers.isEmpty())
145     {
146         printerCombo->setEnabled(false);
147     }
148     else
149     {
150         printerCombo->addItems(printers);
151         printerCombo->setEnabled(true);
152         printerCombo->setCurrentIndex(0);
153     }
154 }
155
156 void PrintWidget::setReady(bool ready)
157 {
158     if(ready)
159     {
160         setStatus( tr("Ready"), false);
161         printButton->setEnabled(true);
162     }
163     else
164         printButton->setEnabled(false);
165 }
166
167
168 void PrintWidget::browseFile()
169 {
170     QString path = QFileDialog::getOpenFileName(this, tr("Print file"));
171     if(!path.isNull())
172         filenameEdit->setText(path);
173
174 }
175
176 void PrintWidget::doPrint()
177 {
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);
186 }