File indexing completed on 2024-04-28 09:38:49

0001 /***************************************************************************
0002  *   Copyright (C) 2020 by Friedrich W. H. Kossebau - kossebau@kde.org     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #include "imageexportdlg.h"
0011 
0012 #include <KComboBox>
0013 #include <KLocalizedString>
0014 #include <KUrlRequester>
0015 
0016 #include <QCheckBox>
0017 #include <QDialogButtonBox>
0018 #include <QFormLayout>
0019 #include <QMimeDatabase>
0020 #include <QPushButton>
0021 #include <QString>
0022 #include <QVBoxLayout>
0023 
0024 ImageExportDialog::ImageExportDialog(QWidget *parent)
0025     : QDialog(parent)
0026     , m_mimeTypeNames({
0027           QStringLiteral("image/png"),
0028           QStringLiteral("image/bmp"),
0029           QStringLiteral("image/svg+xml"),
0030       })
0031 {
0032     setWindowTitle(i18n("Export As Image"));
0033     setModal(true);
0034 
0035     QVBoxLayout *layout = new QVBoxLayout;
0036     setLayout(layout);
0037 
0038     QFormLayout *formLayout = new QFormLayout;
0039 
0040     m_formatSelect = new KComboBox(this);
0041     QMimeDatabase mimeDb;
0042     for (auto &mimeTypeName : qAsConst(m_mimeTypeNames)) {
0043         m_formatSelect->addItem(mimeDb.mimeTypeForName(mimeTypeName).comment());
0044     }
0045     formLayout->addRow(i18n("Format:"), m_formatSelect);
0046 
0047     m_filePathEdit = new KUrlRequester(QUrl(), this);
0048     m_filePathEdit->setAcceptMode(QFileDialog::AcceptSave);
0049     m_filePathEdit->setMode(KFile::File | KFile::LocalOnly);
0050 
0051     formLayout->addRow(i18n("File name:"), m_filePathEdit);
0052 
0053     m_cropCheck = new QCheckBox(this);
0054     m_cropCheck->setObjectName("cropCheck");
0055     m_cropCheck->setChecked(true); // yes by default?
0056 
0057     formLayout->addRow(i18n("Crop image:"), m_cropCheck);
0058     layout->addLayout(formLayout);
0059 
0060     layout->addStretch();
0061 
0062     m_buttonBox = new QDialogButtonBox(this);
0063     m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
0064     m_exportButton = m_buttonBox->addButton(i18n("Export"), QDialogButtonBox::AcceptRole);
0065     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0066     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0067     layout->addWidget(m_buttonBox);
0068 
0069     connect(m_formatSelect, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &ImageExportDialog::handleFormatIndexChanged);
0070     connect(m_filePathEdit, &KUrlRequester::textChanged, this, &ImageExportDialog::updateExportButton);
0071 
0072     handleFormatIndexChanged(m_formatSelect->currentIndex());
0073 }
0074 
0075 QString ImageExportDialog::filePath() const
0076 {
0077     return m_filePathEdit->text();
0078 }
0079 
0080 QString ImageExportDialog::formatType() const
0081 {
0082     const int formatIndex = m_formatSelect->currentIndex();
0083     return (formatIndex == 0) ? QStringLiteral("PNG") : (formatIndex == 1) ? QStringLiteral("BMP") : (formatIndex == 2) ? QStringLiteral("SVG") : QString();
0084 }
0085 
0086 bool ImageExportDialog::isCropSelected() const
0087 {
0088     return m_cropCheck->isChecked();
0089 }
0090 
0091 void ImageExportDialog::handleFormatIndexChanged(int index)
0092 {
0093     m_filePathEdit->setMimeTypeFilters((index != -1) ? QStringList {m_mimeTypeNames.at(index)} : QStringList());
0094 
0095     updateExportButton();
0096 }
0097 
0098 void ImageExportDialog::updateExportButton()
0099 {
0100     const bool acceptable = !m_filePathEdit->text().isEmpty() && (m_formatSelect->currentIndex() != -1);
0101 
0102     m_exportButton->setEnabled(acceptable);
0103 }
0104 
0105 #include "moc_imageexportdlg.cpp"