File: PrinterDescription.cpp

package info (click to toggle)
print-manager 4:20.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,628 kB
  • sloc: cpp: 9,788; xml: 192; makefile: 4; sh: 4
file content (271 lines) | stat: -rw-r--r-- 9,275 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/***************************************************************************
 *   Copyright (C) 2010-2018 by Daniel Nicoletti                           *
 *   dantti12@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; see the file COPYING. If not, write to       *
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 *   Boston, MA 02110-1301, USA.                                           *
 ***************************************************************************/

#include "PrinterDescription.h"
#include "ui_PrinterDescription.h"

#include <KCupsPrinter.h>

#include <QPainter>
#include <QPointer>
#include <QProgressBar>
#include <QLabel>
#include <QMenu>
#include <QProcess>

#include <KIO/CommandLauncherJob>

#define PRINTER_ICON_SIZE 128

Q_DECLARE_METATYPE(QList<int>)

PrinterDescription::PrinterDescription(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PrinterDescription)
{
    ui->setupUi(this);
    m_layoutEnd = ui->formLayout->count();

    // loads the standard key icon
    m_printerIcon = QIcon::fromTheme(QStringLiteral("printer")).pixmap(PRINTER_ICON_SIZE);
    ui->iconL->setPixmap(m_printerIcon);

    auto menu = new QMenu(ui->maintenancePB);
    menu->addAction(ui->actionPrintTestPage);
    menu->addAction(ui->actionPrintSelfTestPage);
    menu->addAction(ui->actionCleanPrintHeads);
    ui->actionCleanPrintHeads->setVisible(false);
    ui->actionPrintSelfTestPage->setVisible(false);
    ui->maintenancePB->setMenu(menu);
    ui->errorMessage->setWordWrap(true);
    ui->errorMessage->setMessageType(KMessageWidget::Error);
    ui->errorMessage->hide();
}

PrinterDescription::~PrinterDescription()
{
    delete ui;
}

void PrinterDescription::on_openQueuePB_clicked()
{
    auto job = new KIO::CommandLauncherJob(QStringLiteral("kde-print-queue"), { m_destName });
    job->start();
}

void PrinterDescription::on_defaultCB_clicked()
{
    ui->defaultCB->setDisabled(true);
    auto request = new KCupsRequest;
    connect(request, &KCupsRequest::finished, this, &PrinterDescription::requestFinished);
    request->setDefaultPrinter(m_destName);
}

void PrinterDescription::on_sharedCB_clicked()
{
    ui->sharedCB->setDisabled(true);
    auto request = new KCupsRequest;
    connect(request, &KCupsRequest::finished, this, &PrinterDescription::requestFinished);
    request->setShared(m_destName, m_isClass, ui->sharedCB->isChecked());
}

void PrinterDescription::on_rejectPrintJobsCB_clicked()
{
    ui->rejectPrintJobsCB->setDisabled(true);
    auto request = new KCupsRequest;
    connect(request, &KCupsRequest::finished, this, &PrinterDescription::requestFinished);
    if (ui->rejectPrintJobsCB->isChecked()) {
        request->rejectJobs(m_destName);
    } else {
        request->acceptJobs(m_destName);
    }
}

void PrinterDescription::setPrinterIcon(const QIcon &icon)
{
    ui->iconL->setPixmap(icon.pixmap(PRINTER_ICON_SIZE, PRINTER_ICON_SIZE));
}

void PrinterDescription::setDestName(const QString &name, const QString &description, bool isClass, bool singlePrinter)
{
    m_destName = name;

    m_markerData.clear();
    if (m_isClass != isClass) {
        m_isClass = isClass;
        ui->sharedCB->setText(m_isClass ? i18n("Share this class") : i18n("Share this printer"));
    }

    ui->nameMsgL->setText(name);
    if (!description.isEmpty() && description != ui->printerNameL->text()) {
        ui->printerNameL->setText(description);
    } else if (description.isEmpty() && name != ui->printerNameL->text()) {
        ui->printerNameL->setText(name);
    }

    ui->statusL->setVisible(singlePrinter);
    ui->nameL->setVisible(singlePrinter);
    ui->nameMsgL->setVisible(singlePrinter);
}

void PrinterDescription::setDestStatus(const QString &status)
{
    ui->statusL->setText(status);
}

void PrinterDescription::setLocation(const QString &location)
{
    ui->locationMsgL->setText(location);
}

void PrinterDescription::setKind(const QString &kind)
{
    ui->kindMsgL->setText(kind);
}

void PrinterDescription::setIsDefault(bool isDefault)
{
    ui->defaultCB->setEnabled(!isDefault);
    ui->defaultCB->setChecked(isDefault);
}

void PrinterDescription::setIsShared(bool isShared)
{
    m_isShared = isShared;
    if (m_globalShared) {
        ui->sharedCB->setChecked(isShared);
    } else {
        ui->sharedCB->setChecked(false);
    }
    ui->sharedCB->setEnabled(m_globalShared);
}

void PrinterDescription::setAcceptingJobs(bool accepting)
{
    ui->rejectPrintJobsCB->setEnabled(true);
    ui->rejectPrintJobsCB->setChecked(!accepting);
}

void PrinterDescription::setCommands(const QStringList &commands)
{
    // On the first time this method runs the list
    // can be empty, so keep all objects initialized on the
    // constructor
    if (m_commands != commands) {
        m_commands = commands;

        ui->actionCleanPrintHeads->setVisible(commands.contains(QLatin1String("Clean")));
        ui->actionPrintSelfTestPage->setVisible(commands.contains(QLatin1String("PrintSelfTestPage")));

        // TODO if the printer supports ReportLevels
        // we should probably probe for them
        // commands.contains("ReportLevels")
    }
}

void PrinterDescription::setMarkers(const QVariantHash &data)
{
    // Remove old progress bars
    while (ui->formLayout->count() > m_layoutEnd) {
        ui->formLayout->takeAt(ui->formLayout->count() - 1)->widget()->deleteLater();
    }

    int size = data[KCUPS_MARKER_NAMES].toStringList().size();
    if (size != data[KCUPS_MARKER_LEVELS].value<QList<int> >().size() ||
            size != data[KCUPS_MARKER_COLORS].toStringList().size() ||
            size != data[KCUPS_MARKER_TYPES].toStringList().size()) {
        return;
    }

    // Create a colored progress bar for each marker
    for (int i = 0; i < size; i++) {
        if (data[KCUPS_MARKER_TYPES].toStringList().at(i) == QLatin1String("unknown")) {
            continue;
        }
        auto pogressBar = new QProgressBar;
        pogressBar->setValue(data[KCUPS_MARKER_LEVELS].value<QList<int> >().at(i));
        pogressBar->setTextVisible(false);
        pogressBar->setMaximumHeight(15);
        QPalette palette = pogressBar->palette();
        palette.setColor(QPalette::Active,
                         QPalette::Highlight,
                         QColor(data[KCUPS_MARKER_COLORS].toStringList().at(i)));
        palette.setColor(QPalette::Inactive,
                         QPalette::Highlight,
                         QColor(data[KCUPS_MARKER_COLORS].toStringList().at(i)).lighter());
        pogressBar->setPalette(palette);
        auto label = new QLabel(data[KCUPS_MARKER_NAMES].toStringList().at(i), this);
        ui->formLayout->addRow(label, pogressBar);
    }
}

void PrinterDescription::on_actionPrintTestPage_triggered(bool checked)
{
    Q_UNUSED(checked)

    auto request = new KCupsRequest;
    connect(request, &KCupsRequest::finished, this, &PrinterDescription::requestFinished);
    request->printTestPage(m_destName, m_isClass);
}

void PrinterDescription::on_actionCleanPrintHeads_triggered(bool checked)
{
    Q_UNUSED(checked)

    auto request = new KCupsRequest;
    connect(request, &KCupsRequest::finished, this, &PrinterDescription::requestFinished);
    request->printCommand(m_destName, QLatin1String("Clean all"), i18n("Clean Print Heads"));
}

void PrinterDescription::on_actionPrintSelfTestPage_triggered(bool checked)
{
    Q_UNUSED(checked)

    auto request = new KCupsRequest;
    connect(request, &KCupsRequest::finished, this, &PrinterDescription::requestFinished);
    request->printCommand(m_destName, QLatin1String("PrintSelfTestPage"), i18n("Print Self-Test Page"));
}

void PrinterDescription::requestFinished(KCupsRequest *request)
{
    if (request && request->hasError()) {
        ui->errorMessage->setText(i18n("Failed to perform request: %1", request->errorMsg()));
        ui->errorMessage->animatedShow();
        emit updateNeeded();
    }
}

QString PrinterDescription::destName() const
{
    return m_destName;
}

void PrinterDescription::enableShareCheckBox(bool enable)
{
    m_globalShared = enable;
    setIsShared(m_isShared);
}

void PrinterDescription::on_configurePB_clicked()
{
    QProcess::startDetached(QLatin1String("configure-printer"), {m_destName});
}

#include "moc_PrinterDescription.cpp"