1 /***************************************************************************
2  *   Copyright (C) 2020 by Friedrich W. H. Kossebau - kossebau@kde.org     *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  ***************************************************************************/
9 
10 #include "imageexportdlg.h"
11 
12 #include <KComboBox>
13 #include <KUrlRequester>
14 #include <KLocalizedString>
15 
16 #include <QMimeDatabase>
17 #include <QDialogButtonBox>
18 #include <QPushButton>
19 #include <QCheckBox>
20 #include <QVBoxLayout>
21 #include <QFormLayout>
22 #include <QString>
23 
24 
ImageExportDialog(QWidget * parent)25 ImageExportDialog::ImageExportDialog(QWidget *parent)
26     : QDialog(parent)
27     , m_mimeTypeNames({
28         QStringLiteral("image/png"),
29         QStringLiteral("image/bmp"),
30         QStringLiteral("image/svg+xml"),
31     })
32 {
33     setWindowTitle(i18n("Export As Image"));
34     setModal(true);
35 
36     QVBoxLayout *layout = new QVBoxLayout;
37     setLayout(layout);
38 
39     QFormLayout *formLayout = new QFormLayout;
40 
41     m_formatSelect = new KComboBox(this);
42     QMimeDatabase mimeDb;
43     for (auto& mimeTypeName : qAsConst(m_mimeTypeNames)) {
44         m_formatSelect->addItem(mimeDb.mimeTypeForName(mimeTypeName).comment());
45     }
46     formLayout->addRow(i18n("Format:"), m_formatSelect);
47 
48     m_filePathEdit = new KUrlRequester(QUrl(), this);
49     m_filePathEdit->setAcceptMode(QFileDialog::AcceptSave);
50     m_filePathEdit->setMode(KFile::File | KFile::LocalOnly);
51 
52     formLayout->addRow(i18n("File name:"), m_filePathEdit);
53 
54     m_cropCheck = new QCheckBox(this);
55     m_cropCheck->setObjectName( "cropCheck" );
56     m_cropCheck->setChecked(true); // yes by default?
57 
58     formLayout->addRow(i18n("Crop image:"), m_cropCheck);
59     layout->addLayout(formLayout);
60 
61     layout->addStretch();
62 
63     m_buttonBox = new QDialogButtonBox(this);
64     m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
65     m_exportButton = m_buttonBox->addButton(i18n("Export"), QDialogButtonBox::AcceptRole);
66     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
67     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
68     layout->addWidget(m_buttonBox);
69 
70     connect(m_formatSelect, QOverload<int>::of(&KComboBox::currentIndexChanged),
71             this, &ImageExportDialog::handleFormatIndexChanged);
72     connect(m_filePathEdit, &KUrlRequester::textChanged,
73             this, &ImageExportDialog::updateExportButton);
74 
75     handleFormatIndexChanged(m_formatSelect->currentIndex());
76 }
77 
filePath() const78 QString ImageExportDialog::filePath() const
79 {
80     return m_filePathEdit->text();
81 }
82 
formatType() const83 QString ImageExportDialog::formatType() const
84 {
85     const int formatIndex = m_formatSelect->currentIndex();
86     return
87         (formatIndex == 0) ? QStringLiteral("PNG") :
88         (formatIndex == 1) ? QStringLiteral("BMP") :
89         (formatIndex == 2) ? QStringLiteral("SVG") :
90         QString();
91 }
92 
isCropSelected() const93 bool ImageExportDialog::isCropSelected() const
94 {
95     return m_cropCheck->isChecked();
96 }
97 
handleFormatIndexChanged(int index)98 void ImageExportDialog::handleFormatIndexChanged(int index)
99 {
100     m_filePathEdit->setMimeTypeFilters((index != -1) ? QStringList{m_mimeTypeNames.at(index)} : QStringList());
101 
102     updateExportButton();
103 }
104 
updateExportButton()105 void ImageExportDialog::updateExportButton()
106 {
107     const bool acceptable = !m_filePathEdit->text().isEmpty() && (m_formatSelect->currentIndex() != -1);
108 
109     m_exportButton->setEnabled(acceptable);
110 }
111