File indexing completed on 2024-11-10 04:50:05

0001 /*
0002  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "filteractionwithurl.h"
0009 
0010 #include <KLocalizedString>
0011 #include <KUrlRequester>
0012 #include <QUrl>
0013 
0014 #include <QHBoxLayout>
0015 #include <QWhatsThis>
0016 
0017 using namespace MailCommon;
0018 
0019 FilterActionWithUrlHelpButton::FilterActionWithUrlHelpButton(QWidget *parent)
0020     : QToolButton(parent)
0021 {
0022     setToolTip(i18n("Help"));
0023     setIcon(QIcon::fromTheme(QStringLiteral("help-hint")));
0024 }
0025 
0026 FilterActionWithUrlHelpButton::~FilterActionWithUrlHelpButton() = default;
0027 
0028 FilterActionWithUrl::FilterActionWithUrl(const QString &name, const QString &label, QObject *parent)
0029     : FilterAction(name, label, parent)
0030     , mHelpButton(nullptr)
0031 {
0032 }
0033 
0034 FilterActionWithUrl::~FilterActionWithUrl() = default;
0035 
0036 bool FilterActionWithUrl::isEmpty() const
0037 {
0038     return mParameter.trimmed().isEmpty();
0039 }
0040 
0041 QWidget *FilterActionWithUrl::createParamWidget(QWidget *parent) const
0042 {
0043     auto widget = new QWidget(parent);
0044     auto layout = new QHBoxLayout;
0045     layout->setContentsMargins({});
0046     layout->setSpacing(0);
0047     widget->setLayout(layout);
0048     auto requester = new KUrlRequester(parent);
0049     requester->setUrl(QUrl::fromLocalFile(mParameter));
0050     requester->setObjectName(QLatin1StringView("requester"));
0051     layout->addWidget(requester);
0052     mHelpButton = new FilterActionWithUrlHelpButton(parent);
0053     mHelpButton->setObjectName(QLatin1StringView("helpbutton"));
0054     connect(mHelpButton, &QAbstractButton::clicked, this, &FilterActionWithUrl::slotHelp);
0055     layout->addWidget(mHelpButton);
0056 
0057     connect(requester, &KUrlRequester::textChanged, this, &FilterActionWithUrl::filterActionModified);
0058 
0059     return widget;
0060 }
0061 
0062 void FilterActionWithUrl::slotHelp()
0063 {
0064     const QString fullWhatsThis = i18n("You can get specific header when you use %{headername}.");
0065     QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton);
0066 }
0067 
0068 void FilterActionWithUrl::applyParamWidgetValue(QWidget *paramWidget)
0069 {
0070     auto requester = paramWidget->findChild<KUrlRequester *>(QStringLiteral("requester"));
0071     Q_ASSERT(requester);
0072 
0073     mParameter = requester->text();
0074 }
0075 
0076 void FilterActionWithUrl::setParamWidgetValue(QWidget *paramWidget) const
0077 {
0078     auto requester = paramWidget->findChild<KUrlRequester *>(QStringLiteral("requester"));
0079     Q_ASSERT(requester);
0080 
0081     requester->setText(mParameter);
0082 }
0083 
0084 void FilterActionWithUrl::clearParamWidget(QWidget *paramWidget) const
0085 {
0086     auto requester = paramWidget->findChild<KUrlRequester *>(QStringLiteral("requester"));
0087     Q_ASSERT(requester);
0088     requester->clear();
0089 }
0090 
0091 void FilterActionWithUrl::argsFromString(const QString &argsStr)
0092 {
0093     mParameter = argsStr;
0094 }
0095 
0096 QString FilterActionWithUrl::argsAsString() const
0097 {
0098     return mParameter;
0099 }
0100 
0101 QString FilterActionWithUrl::displayString() const
0102 {
0103     return label() + QLatin1StringView(" \"") + argsAsString().toHtmlEscaped() + QLatin1StringView("\"");
0104 }
0105 
0106 #include "moc_filteractionwithurl.cpp"