1 /* ============================================================
2 * Date        : 2010-07-07
3 * Description : Save location settings dialog.
4 *
5 * SPDX-FileCopyrightText: 2010-2012 Kare Sars <kare.sars@iki.fi>
6 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
7 *
8 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
9 *
10 * ============================================================ */
11 
12 #include "SaveLocation.h"
13 #include "ui_SaveLocation.h"
14 
15 #include <QFileDialog>
16 #include <QPushButton>
17 #include <QComboBox>
18 #include <QShowEvent>
19 #include <QTimer>
20 
SaveLocation(QWidget * parent)21 SaveLocation::SaveLocation(QWidget *parent)
22     : QDialog(parent)
23 {
24     m_ui = new Ui_SaveLocation();
25     m_ui->setupUi(this);
26 
27     m_ui->u_urlRequester->setMode(KFile::Directory);
28     connect(m_ui->u_urlRequester, &KUrlRequester::textChanged, this, &SaveLocation::updateGui);
29     connect(m_ui->u_imgPrefix, &QLineEdit::textChanged, this, &SaveLocation::updateGui);
30     connect(m_ui->u_imgFormat, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::textActivated), this, &SaveLocation::updateGui);
31     connect(m_ui->u_numStartFrom, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &SaveLocation::updateGui);
32 }
33 
~SaveLocation()34 SaveLocation::~SaveLocation()
35 {
36 }
37 
updateGui()38 void SaveLocation::updateGui()
39 {
40     if (sender() != m_ui->u_numStartFrom) {
41         m_ui->u_numStartFrom->setValue(1); // Reset the counter whenever the directory or the prefix is changed
42     }
43     const QString name = QStringLiteral("%1%2.%3")
44     .arg(m_ui->u_imgPrefix->text())
45     .arg(m_ui->u_numStartFrom->value(), 4, 10, QLatin1Char('0'))
46     .arg(m_ui->u_imgFormat->currentText());
47 
48     QUrl folderUrl = m_ui->u_urlRequester->url();
49     folderUrl.setPath(folderUrl.path() + QLatin1Char('/'));
50     m_ui->u_resultValue->setText(folderUrl.toString(QUrl::PreferLocalFile | QUrl::NormalizePathSegments)+name);
51 }
52 
folderUrl() const53 QUrl SaveLocation::folderUrl() const
54 {
55     QUrl folderUrl = m_ui->u_urlRequester->url();
56     folderUrl.setPath(folderUrl.path() + QLatin1Char('/'));
57     return folderUrl.adjusted(QUrl::NormalizePathSegments);
58 }
59 
imagePrefix() const60 QString SaveLocation::imagePrefix() const
61 {
62     return m_ui->u_imgPrefix->text();
63 }
64 
imageMimetype() const65 QString SaveLocation::imageMimetype() const
66 {
67     return m_ui->u_imgFormat->currentData().toString();
68 }
69 
imageSuffix() const70 QString SaveLocation::imageSuffix() const
71 {
72     return m_ui->u_imgFormat->currentText().toLower();
73 }
74 
startNumber() const75 int SaveLocation::startNumber() const
76 {
77     return m_ui->u_numStartFrom->value();
78 }
79 
startNumberMax() const80 int SaveLocation::startNumberMax() const
81 {
82     return m_ui->u_numStartFrom->maximum();
83 }
84 
setFolderUrl(const QUrl & url)85 void SaveLocation::setFolderUrl(const QUrl &url)
86 {
87     m_ui->u_urlRequester->setUrl(url);
88 }
89 
setImagePrefix(const QString & prefix)90 void SaveLocation::setImagePrefix(const QString &prefix)
91 {
92     m_ui->u_imgPrefix->setText(prefix);
93 }
94 
addImageFormat(const QString & suffix,const QString & mimetype)95 void SaveLocation::addImageFormat(const QString &suffix, const QString &mimetype)
96 {
97     m_ui->u_imgFormat->addItem(suffix, mimetype);
98 }
99 
setImageFormatIndex(int index)100 void SaveLocation::setImageFormatIndex(int index)
101 {
102     m_ui->u_imgFormat->setCurrentIndex(index);
103 }
104 
setImageFormat(const QString & suffix)105 void SaveLocation::setImageFormat(const QString &suffix)
106 {
107     int index = m_ui->u_imgFormat->findText(suffix);
108     if (index >= 0) {
109         m_ui->u_imgFormat->setCurrentIndex(index);
110     }
111 }
112 
setStartNumber(int number)113 void SaveLocation::setStartNumber(int number)
114 {
115     m_ui->u_numStartFrom->setValue(number);
116 }
117 
setOpenRequesterOnShow(bool open)118 void SaveLocation::setOpenRequesterOnShow(bool open)
119 {
120     m_openRequesterOnShow = open;
121 }
122 
showEvent(QShowEvent * event)123 void SaveLocation::showEvent(QShowEvent *event)
124 {
125     QDialog::showEvent(event);
126 
127     if (m_openRequesterOnShow) {
128         QTimer::singleShot(0, this, [this]() { m_ui->u_urlRequester->button()->click(); });
129     }
130 }
131