File indexing completed on 2024-05-12 04:52:55

0001 /*
0002     SPDX-FileCopyrightText: 2018 Jean-Baptiste Mardelle
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "urlparamwidget.hpp"
0007 #include "assets/model/assetparametermodel.hpp"
0008 
0009 #include <KUrlRequester>
0010 #include <kio_version.h>
0011 
0012 UrlParamWidget::UrlParamWidget(std::shared_ptr<AssetParameterModel> model, QModelIndex index, QWidget *parent)
0013     : AbstractParamWidget(std::move(model), index, parent)
0014 {
0015     setupUi(this);
0016 
0017     // setup the comment
0018     QString comment = m_model->data(m_index, AssetParameterModel::CommentRole).toString();
0019     labelComment->setText(comment);
0020     setToolTip(comment);
0021     labelComment->setHidden(true);
0022     QString filter = m_model->data(m_index, AssetParameterModel::FilterRole).toString();
0023     if (!filter.isEmpty()) {
0024 #if KIO_VERSION >= QT_VERSION_CHECK(5, 108, 0)
0025         urlwidget->setNameFilter(filter);
0026 #else
0027         // regular expression copied from QPlatformFileDialogHelper
0028         QStringList filters = filter.split(QStringLiteral(";;"), Qt::SkipEmptyParts);
0029         const QRegularExpression regexp(QStringLiteral("^(.*)\\(([a-zA-Z0-9_.,*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$"));
0030         QStringList result;
0031         for (const QString &filter : filters) {
0032             QRegularExpressionMatch match;
0033             filter.indexOf(regexp, 0, &match);
0034             if (match.hasMatch()) {
0035                 result.append(match.capturedView(2).trimmed() + QLatin1Char('|') + match.capturedView(1).trimmed());
0036             } else {
0037                 result.append(filter);
0038             }
0039         }
0040 
0041         urlwidget->setFilter(result.join(QLatin1Char('\n')));
0042 #endif
0043     }
0044     QString mode = m_model->data(m_index, AssetParameterModel::ModeRole).toString();
0045     if (mode == QLatin1String("save")) {
0046         urlwidget->setAcceptMode(QFileDialog::AcceptSave);
0047         urlwidget->setMode(KFile::File);
0048     }
0049     slotRefresh();
0050 
0051     // setup the name
0052     label->setText(m_model->data(m_index, Qt::DisplayRole).toString());
0053     setMinimumHeight(urlwidget->sizeHint().height());
0054 
0055     // set check state
0056     slotRefresh();
0057 
0058     // Q_EMIT the signal of the base class when appropriate
0059     connect(this->urlwidget, &KUrlRequester::textChanged, this, [this]() {
0060         QFileInfo info(urlwidget->url().toLocalFile());
0061         if (info.exists() && info.isFile()) {
0062             Q_EMIT valueChanged(m_index, this->urlwidget->url().toLocalFile(), true);
0063         }
0064     });
0065 }
0066 
0067 void UrlParamWidget::slotShowComment(bool show)
0068 {
0069     if (!labelComment->text().isEmpty()) {
0070         labelComment->setVisible(show);
0071     }
0072 }
0073 
0074 void UrlParamWidget::slotRefresh()
0075 {
0076     const QSignalBlocker bk(urlwidget);
0077     urlwidget->setUrl(QUrl::fromLocalFile(m_model->data(m_index, AssetParameterModel::ValueRole).toString()));
0078 }