File indexing completed on 2025-01-05 05:09:28
0001 /* 0002 SPDX-FileCopyrightText: 2010-2018 Daniel Nicoletti <dantti12@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "ClassListWidget.h" 0008 0009 #include "SelectMakeModel.h" 0010 0011 #include "KCupsRequest.h" 0012 #include "NoSelectionRectDelegate.h" 0013 0014 #include <QFileDialog> 0015 #include <QPointer> 0016 0017 #include <KConfigDialogManager> 0018 #include <KIconLoader> 0019 #include <KPixmapSequence> 0020 0021 ClassListWidget::ClassListWidget(QWidget *parent) 0022 : QListView(parent) 0023 { 0024 m_model = new QStandardItemModel(this); 0025 setModel(m_model); 0026 setItemDelegate(new NoSelectionRectDelegate(this)); 0027 0028 // Setup the busy cursor 0029 m_busySeq = new KPixmapSequenceOverlayPainter(this); 0030 m_busySeq->setSequence(KPixmapSequence(QLatin1String("process-working"), KIconLoader::SizeSmallMedium)); 0031 m_busySeq->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); 0032 m_busySeq->setWidget(viewport()); 0033 0034 connect(m_model, &QStandardItemModel::dataChanged, this, &ClassListWidget::modelChanged); 0035 0036 m_delayedInit.setInterval(0); 0037 m_delayedInit.setSingleShot(true); 0038 connect(&m_delayedInit, &QTimer::timeout, this, &ClassListWidget::init); 0039 m_delayedInit.start(); 0040 } 0041 0042 ClassListWidget::~ClassListWidget() 0043 { 0044 } 0045 0046 void ClassListWidget::init() 0047 { 0048 m_busySeq->start(); // Start spinning 0049 m_model->clear(); 0050 0051 QStringList att; 0052 att << KCUPS_PRINTER_NAME; 0053 att << KCUPS_PRINTER_URI_SUPPORTED; 0054 // Get destinations with these masks 0055 m_request = new KCupsRequest; 0056 connect(m_request, &KCupsRequest::finished, this, &ClassListWidget::loadFinished); 0057 if (m_showClasses) { 0058 m_request->getPrinters(att); 0059 } else { 0060 m_request->getPrinters(att, CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT); 0061 } 0062 } 0063 0064 void ClassListWidget::loadFinished(KCupsRequest *request) 0065 { 0066 // If we have an old request running discard it's result and get a new one 0067 if (m_request != request) { 0068 request->deleteLater(); 0069 return; 0070 } 0071 0072 m_busySeq->stop(); // Stop spinning 0073 0074 const KCupsPrinters printers = request->printers(); 0075 request->deleteLater(); 0076 m_request = nullptr; 0077 0078 for (const KCupsPrinter &printer : printers) { 0079 QString destName = printer.name(); 0080 if (destName != m_printerName) { 0081 auto item = new QStandardItem; 0082 item->setText(destName); 0083 item->setCheckable(true); 0084 item->setEditable(false); 0085 item->setData(printer.uriSupported()); 0086 updateItemState(item); 0087 0088 m_model->appendRow(item); 0089 } 0090 } 0091 0092 modelChanged(); 0093 } 0094 0095 void ClassListWidget::modelChanged() 0096 { 0097 QStringList currentMembers = currentSelected(false); 0098 0099 m_changed = m_selectedPrinters != currentMembers; 0100 0101 Q_EMIT changed(m_changed); 0102 } 0103 0104 QStringList ClassListWidget::currentSelected(bool uri) const 0105 { 0106 QStringList currentMembers; 0107 for (int i = 0; i < m_model->rowCount(); i++) { 0108 QStandardItem *item = m_model->item(i); 0109 if (item && item->checkState() == Qt::Checked) { 0110 if (uri) { 0111 currentMembers << item->data().toString(); 0112 } else { 0113 currentMembers << item->text(); 0114 } 0115 } 0116 } 0117 currentMembers.sort(); 0118 return currentMembers; 0119 } 0120 0121 void ClassListWidget::updateItemState(QStandardItem *item) const 0122 { 0123 if (m_selectedPrinters.contains(item->text())) { 0124 item->setCheckState(Qt::Checked); 0125 } else { 0126 item->setCheckState(Qt::Unchecked); 0127 } 0128 } 0129 0130 bool ClassListWidget::hasChanges() 0131 { 0132 return m_changed; 0133 } 0134 0135 void ClassListWidget::setPrinter(const QString &printer) 0136 { 0137 if (m_printerName != printer) { 0138 m_printerName = printer; 0139 m_delayedInit.start(); 0140 } 0141 } 0142 0143 QString ClassListWidget::selectedPrinters() const 0144 { 0145 return currentSelected(false).join(QLatin1String("|")); 0146 } 0147 0148 void ClassListWidget::setSelectedPrinters(const QString &selected) 0149 { 0150 m_selectedPrinters = selected.split(QLatin1Char('|')); 0151 m_selectedPrinters.sort(); 0152 m_delayedInit.start(); 0153 } 0154 0155 bool ClassListWidget::showClasses() const 0156 { 0157 return m_showClasses; 0158 } 0159 0160 void ClassListWidget::setShowClasses(bool enable) 0161 { 0162 if (m_showClasses != enable) { 0163 m_showClasses = enable; 0164 m_delayedInit.start(); 0165 } 0166 } 0167 0168 #include "moc_ClassListWidget.cpp"