File indexing completed on 2024-04-14 12:03:11

0001 /*
0002  *  Print dialog
0003  *
0004  *  SPDX-FileCopyrightText: 2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 // application specific includes
0009 #include "smb4kprintdialog.h"
0010 #include "core/smb4kclient.h"
0011 #include "core/smb4ksettings.h"
0012 
0013 // Qt includes
0014 #include <QDialogButtonBox>
0015 #include <QDir>
0016 #include <QVBoxLayout>
0017 #include <QWindow>
0018 
0019 // KDE includes
0020 #include <KConfigGroup>
0021 #include <KFileItem>
0022 #include <KIconLoader>
0023 #include <KLocalizedString>
0024 #include <KWindowConfig>
0025 
0026 Smb4KPrintDialog::Smb4KPrintDialog(QWidget *parent)
0027     : QDialog(parent)
0028 {
0029     setWindowTitle(i18n("Print Dialog"));
0030     setAttribute(Qt::WA_DeleteOnClose, true);
0031 
0032     QVBoxLayout *layout = new QVBoxLayout(this);
0033 
0034     QWidget *descriptionWidget = new QWidget(this);
0035 
0036     QHBoxLayout *descriptionWidgetLayout = new QHBoxLayout(descriptionWidget);
0037 
0038     QLabel *descriptionPixmap = new QLabel(descriptionWidget);
0039     descriptionPixmap->setPixmap(KDE::icon(QStringLiteral("printer")).pixmap(KIconLoader::SizeHuge));
0040     descriptionPixmap->setAlignment(Qt::AlignBottom);
0041     descriptionPixmap->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0042 
0043     descriptionWidgetLayout->addWidget(descriptionPixmap);
0044 
0045     m_descriptionText = new QLabel(this);
0046     m_descriptionText->setText(i18n("Print a file."));
0047     m_descriptionText->setWordWrap(true);
0048     m_descriptionText->setAlignment(Qt::AlignBottom);
0049     m_descriptionText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0050 
0051     descriptionWidgetLayout->addWidget(m_descriptionText);
0052 
0053     layout->addWidget(descriptionWidget);
0054     layout->addSpacing(layout->spacing());
0055 
0056     QWidget *inputWidget = new QWidget(this);
0057 
0058     QGridLayout *inputWidgetLayout = new QGridLayout(inputWidget);
0059     inputWidgetLayout->setContentsMargins(0, 0, 0, 0);
0060 
0061     QLabel *fileLabel = new QLabel(i18n("File:"), inputWidget);
0062     m_fileInput = new KUrlRequester(QUrl::fromLocalFile(QDir::homePath() + QDir::separator()), inputWidget);
0063     m_fileInput->setMode(KFile::File | KFile::LocalOnly | KFile::ExistingOnly);
0064     connect(m_fileInput, &KUrlComboRequester::textChanged, this, &Smb4KPrintDialog::slotUrlChanged);
0065 
0066     inputWidgetLayout->addWidget(fileLabel, 0, 0);
0067     inputWidgetLayout->addWidget(m_fileInput, 0, 1);
0068 
0069     QLabel *copiesLabel = new QLabel(i18n("Copies:"), inputWidget);
0070     m_copiesInput = new QSpinBox(inputWidget);
0071     m_copiesInput->setValue(1);
0072     m_copiesInput->setMinimum(1);
0073     connect(m_copiesInput, &QSpinBox::valueChanged, this, &Smb4KPrintDialog::slotCopiesChanged);
0074 
0075     inputWidgetLayout->addWidget(copiesLabel, 1, 0);
0076     inputWidgetLayout->addWidget(m_copiesInput, 1, 1);
0077 
0078     layout->addWidget(inputWidget);
0079 
0080     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0081 
0082     m_cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
0083     m_cancelButton->setShortcut(QKeySequence::Cancel);
0084     m_cancelButton->setDefault(true);
0085     connect(m_cancelButton, &QPushButton::clicked, this, &Smb4KPrintDialog::reject);
0086 
0087     m_printButton = buttonBox->addButton(i18n("Print"), QDialogButtonBox::ActionRole);
0088     m_printButton->setShortcut(QKeySequence::Print);
0089     m_printButton->setEnabled(false);
0090     connect(m_printButton, &QPushButton::clicked, this, &Smb4KPrintDialog::slotPrintFile);
0091 
0092     layout->addWidget(buttonBox);
0093 
0094     setMinimumWidth(sizeHint().width() > 350 ? sizeHint().width() : 350);
0095 
0096     create();
0097 
0098     KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("PrintDialog"));
0099     QSize dialogSize;
0100 
0101     // FIXME: Insert completion objects?
0102 
0103     if (dialogGroup.exists()) {
0104         KWindowConfig::restoreWindowSize(windowHandle(), dialogGroup);
0105         dialogSize = windowHandle()->size();
0106     } else {
0107         dialogSize = sizeHint();
0108     }
0109 
0110     resize(dialogSize); // workaround for QTBUG-40584
0111 }
0112 
0113 Smb4KPrintDialog::~Smb4KPrintDialog()
0114 {
0115 }
0116 
0117 bool Smb4KPrintDialog::setPrinterShare(SharePtr printer)
0118 {
0119     if (!printer->isPrinter()) {
0120         return false;
0121     }
0122 
0123     m_descriptionText->setText(i18n("Print a file to printer <b>%1</b>.", printer->displayString()));
0124 
0125     m_printer = printer;
0126 
0127     return true;
0128 }
0129 
0130 void Smb4KPrintDialog::enablePrintButton()
0131 {
0132     QUrl pathUrl = m_fileInput->url();
0133     int copies = m_copiesInput->value();
0134 
0135     m_printButton->setEnabled(pathUrl.isValid() && copies > 0);
0136 }
0137 
0138 void Smb4KPrintDialog::slotPrintFile()
0139 {
0140     Smb4KClient::self()->printFile(m_printer, KFileItem(m_fileInput->url()), m_copiesInput->value());
0141 
0142     KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("PrintDialog"));
0143     KWindowConfig::saveWindowSize(windowHandle(), dialogGroup);
0144 
0145     // FIXME: Save completion objects?
0146 
0147     accept();
0148 }
0149 
0150 void Smb4KPrintDialog::slotUrlChanged(const QString &path)
0151 {
0152     Q_UNUSED(path)
0153     enablePrintButton();
0154 }
0155 
0156 void Smb4KPrintDialog::slotCopiesChanged(int copies)
0157 {
0158     Q_UNUSED(copies)
0159     enablePrintButton();
0160 }