File indexing completed on 2022-12-06 18:50:18

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2011 Martin Kuettler <martin.kuettler@gmail.com>
0004     SPDX-FileCopyrightText: 2016-2021 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #include "imagesettingsdialog.h"
0008 
0009 #include <QImageReader>
0010 #include <QFileDialog>
0011 #include <QWindow>
0012 #include <QUrl>
0013 
0014 #include <KCompletion>
0015 #include <KConfigGroup>
0016 #include <KLocalizedString>
0017 #include <KSharedConfig>
0018 #include <KWindowConfig>
0019 #include <KUrlCompletion>
0020 
0021 ImageSettingsDialog::ImageSettingsDialog(QWidget* parent) : QDialog(parent)
0022 {
0023     setWindowTitle(i18nc("@title:window", "Image Settings"));
0024     setWindowIcon(QIcon::fromTheme(QLatin1String("viewimage")));
0025     setAttribute(Qt::WA_DeleteOnClose);
0026 
0027     auto* w = new QWidget(this);
0028     m_ui.setupUi(w);
0029 
0030     auto* vLayout = new QVBoxLayout(this);
0031     vLayout->setSpacing(0);
0032     vLayout->setContentsMargins(0,0,0,0);
0033     vLayout->addWidget(w);
0034 
0035     m_ui.buttonBox->button(QDialogButtonBox::Ok)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogOkButton));
0036     m_ui.buttonBox->button(QDialogButtonBox::Apply)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogApplyButton));
0037     m_ui.buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCancelButton));
0038     m_ui.openDialogButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon));
0039 
0040     m_unitNames << i18n("(auto)") << i18n("px") << i18n("%");
0041 
0042     m_ui.displayWidthCombo->addItems(m_unitNames);
0043     m_ui.displayHeightCombo->addItems(m_unitNames);
0044     m_ui.printWidthCombo->addItems(m_unitNames);
0045     m_ui.printHeightCombo->addItems(m_unitNames);
0046 
0047     auto* completer = new KUrlCompletion(KUrlCompletion::FileCompletion);
0048     completer->setCompletionMode(KCompletion::CompletionMan);
0049     m_ui.pathEdit->setCompletionObject(completer);
0050     m_ui.pathEdit->setAutoDeleteCompletionObject( true );
0051 
0052     m_ui.displayWidthInput->setMinimum(0);
0053     m_ui.displayHeightInput->setMinimum(0);
0054     m_ui.printWidthInput->setMinimum(0);
0055     m_ui.printHeightInput->setMinimum(0);
0056     m_ui.displayWidthInput->setSingleStep(1);
0057     m_ui.displayHeightInput->setSingleStep(1);
0058     m_ui.printWidthInput->setSingleStep(1);
0059     m_ui.printHeightInput->setSingleStep(1);
0060 
0061     connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &ImageSettingsDialog::sendChangesAndClose);
0062     connect(m_ui.buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &ImageSettingsDialog::sendChanges);
0063     connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &ImageSettingsDialog::close);
0064 
0065     connect(m_ui.openDialogButton, &QPushButton::clicked, this, &ImageSettingsDialog::openDialog);
0066     connect(m_ui.pathEdit, &QLineEdit::textChanged, this, &ImageSettingsDialog::updatePreview);
0067 
0068     connect(m_ui.displayWidthCombo, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &ImageSettingsDialog::updateInputWidgets);
0069     connect(m_ui.displayHeightCombo, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &ImageSettingsDialog::updateInputWidgets);
0070     connect(m_ui.printWidthCombo, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &ImageSettingsDialog::updateInputWidgets);
0071     connect(m_ui.printHeightCombo, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &ImageSettingsDialog::updateInputWidgets);
0072 
0073     connect(m_ui.useDisplaySize, &QCheckBox::stateChanged, this, &ImageSettingsDialog::updatePrintingGroup);
0074 }
0075 
0076 ImageSettingsDialog::~ImageSettingsDialog() {
0077     KConfigGroup dialogConfig = KSharedConfig::openConfig()->group("ImageSettingsDialog");
0078     KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
0079 }
0080 
0081 void ImageSettingsDialog::setData(const QString& file, const ImageSize& displaySize, const ImageSize& printSize, bool useDisplaySizeForPrinting)
0082 {
0083     m_ui.pathEdit->setText(file);
0084     if (displaySize.width >= 0)
0085         m_ui.displayWidthInput->setValue(displaySize.width);
0086 
0087     if (displaySize.height >= 0)
0088         m_ui.displayHeightInput->setValue(displaySize.height);
0089 
0090     if (printSize.width >= 0)
0091         m_ui.printWidthInput->setValue(printSize.width);
0092 
0093     if (printSize.height >= 0)
0094         m_ui.printHeightInput->setValue(printSize.height);
0095 
0096     m_ui.displayWidthCombo->setCurrentIndex(displaySize.widthUnit);
0097     m_ui.displayHeightCombo->setCurrentIndex(displaySize.heightUnit);
0098     m_ui.printWidthCombo->setCurrentIndex(printSize.widthUnit);
0099     m_ui.printHeightCombo->setCurrentIndex(printSize.heightUnit);
0100 
0101     if (useDisplaySizeForPrinting)
0102         m_ui.useDisplaySize->setCheckState(Qt::Checked);
0103     else
0104         m_ui.useDisplaySize->setCheckState(Qt::Unchecked);
0105 
0106     updatePreview();
0107     updatePrintingGroup(useDisplaySizeForPrinting);
0108     //updateInputWidgets();
0109 
0110     //restore saved settings if available
0111     create(); // ensure there's a window created
0112     KConfigGroup conf(KSharedConfig::openConfig(), "ImageSettingsDialog");
0113     if (conf.exists()) {
0114         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0115         resize(windowHandle()->size()); // workaround for QTBUG-40584
0116     } else
0117         resize(QSize(0, 0).expandedTo(minimumSize()));
0118 }
0119 
0120 void ImageSettingsDialog::sendChangesAndClose()
0121 {
0122     sendChanges();
0123     close();
0124 }
0125 
0126 void ImageSettingsDialog::sendChanges()
0127 {
0128     ImageSize displaySize, printSize;
0129     displaySize.width = m_ui.displayWidthInput->value();
0130     displaySize.height = m_ui.displayHeightInput->value();
0131     displaySize.widthUnit = m_ui.displayWidthCombo->currentIndex();
0132     displaySize.heightUnit = m_ui.displayHeightCombo->currentIndex();
0133     printSize.width = m_ui.printWidthInput->value();
0134     printSize.height = m_ui.printHeightInput->value();
0135     printSize.widthUnit = m_ui.printWidthCombo->currentIndex();
0136     printSize.heightUnit = m_ui.printHeightCombo->currentIndex();
0137 
0138     emit dataChanged(m_ui.pathEdit->text(), displaySize, printSize, m_ui.useDisplaySize->isChecked());
0139 }
0140 
0141 void ImageSettingsDialog::openDialog()
0142 {
0143     KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("ImageSettingsDialog"));
0144     const QString& dir = conf.readEntry(QLatin1String("LastImageDir"), QString());
0145 
0146     QString formats;
0147     for (const QByteArray& format : QImageReader::supportedImageFormats())
0148         formats += QLatin1String("*.") + QLatin1String(format.constData()) + QLatin1Char(' ');
0149 
0150     const QString& path = QFileDialog::getOpenFileName(this,
0151                                                       i18n("Open image file"),
0152                                                       dir,
0153                                                       i18n("Images (%1)", formats));
0154     if (path.isEmpty())
0155         return; //cancel was clicked in the file-dialog
0156 
0157     //save the last used directory, if changed
0158     const int pos = path.lastIndexOf(QLatin1String("/"));
0159     if (pos != -1) {
0160         const QString& newDir = path.left(pos);
0161         if (newDir != dir)
0162             conf.writeEntry(QLatin1String("LastImageDir"), newDir);
0163     }
0164 
0165     m_ui.pathEdit->setText(path);
0166     updatePreview();
0167 }
0168 
0169 void ImageSettingsDialog::updatePreview()
0170 {
0171     m_ui.imagePreview->clearPreview();
0172     if (!m_ui.pathEdit->text().isEmpty())
0173         m_ui.imagePreview->showPreview(QUrl::fromLocalFile(m_ui.pathEdit->text()));
0174 }
0175 
0176 void ImageSettingsDialog::updateInputWidgets()
0177 {
0178     if (m_ui.displayWidthCombo->currentIndex() == 0)
0179         m_ui.displayWidthInput->setEnabled(false);
0180     else
0181         m_ui.displayWidthInput->setEnabled(true);
0182 
0183     if (m_ui.displayHeightCombo->currentIndex() == 0)
0184         m_ui.displayHeightInput->setEnabled(false);
0185     else
0186         m_ui.displayHeightInput->setEnabled(true);
0187 
0188     if (m_ui.printWidthCombo->currentIndex() == 0 || !m_ui.printWidthCombo->isEnabled())
0189         m_ui.printWidthInput->setEnabled(false);
0190     else
0191         m_ui.printWidthInput->setEnabled(true);
0192 
0193     if (m_ui.printHeightCombo->currentIndex() == 0 || !m_ui.printHeightCombo->isEnabled())
0194         m_ui.printHeightInput->setEnabled(false);
0195     else
0196         m_ui.printHeightInput->setEnabled(true);
0197 }
0198 
0199 void ImageSettingsDialog::updatePrintingGroup(int b)
0200 {
0201     m_ui.printWidthCombo->setEnabled(!b);
0202     m_ui.printHeightCombo->setEnabled(!b);
0203 
0204     updateInputWidgets();
0205 }