File indexing completed on 2024-09-22 04:50:01

0001 /*
0002   SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003   SPDX-FileCopyrightText: 2010 Andras Mantia <andras@kdab.com>
0004   SPDX-FileCopyrightText: Marc Mutz <mutz@kde.org>
0005 
0006   based upon work by Stefan Taferner <taferner@kde.org>
0007 
0008   SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #pragma once
0012 
0013 #include "mailcommon_export.h"
0014 
0015 #include <Libkdepim/KWidgetLister>
0016 #include <QList>
0017 namespace MailCommon
0018 {
0019 class FilterAction;
0020 class FilterActionWidget;
0021 
0022 /**
0023  * @short A widget to edit a single MailCommon::FilterAction.
0024  *
0025  * This widgets allows to edit a single MailCommon::FilterAction (in fact
0026  * any derived class that is registered in
0027  * KMFilterActionDict). It consists of a combo box which allows to
0028  * select the type of actions this widget should act upon.
0029  *
0030  * You can load a MailCommon::FilterAction into this widget with setAction,
0031  * and retrieve the result of user action with action.
0032  * The widget will copy it's setting into the corresponding
0033  * parameter widget. For that, it internally creates an instance of
0034  * every MailCommon::FilterAction in KMFilterActionDict and asks each
0035  * one to create a parameter widget.
0036  *
0037  * @author Marc Mutz <mutz@kde.org>
0038  * @see MailCommon::FilterAction MailCommon::MailFilter FilterActionWidgetLister
0039  */
0040 class FilterActionWidget : public QWidget
0041 {
0042     Q_OBJECT
0043 
0044 public:
0045     /**
0046      * Creates a filter action widget with no type selected.
0047      *
0048      * @param parent The parent widget.
0049      */
0050     explicit FilterActionWidget(QWidget *parent = nullptr);
0051 
0052     /**
0053      * Destroys the filter action widget.
0054      */
0055     ~FilterActionWidget() override;
0056 
0057     /**
0058      * Sets the filter action.
0059      *
0060      * The action's type is determined and the corresponding widget
0061      * it loaded with @p action's parameters and then raised.
0062      *
0063      * If @p action is @c 0, the widget is cleared.
0064      * @note The widget takes ownership of the passed action.
0065      */
0066     void setAction(const MailCommon::FilterAction *action);
0067 
0068     /**
0069      * Returns the filter action.
0070      *
0071      * This method is necessary because the type of actions can
0072      * change during editing. Therefore the widget always creates a new
0073      * action object from the data in the combo box and returns that.
0074      */
0075     [[nodiscard]] MailCommon::FilterAction *action() const;
0076 
0077     void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled);
0078 
0079 Q_SIGNALS:
0080     void filterModified();
0081     void addFilterWidget(QWidget *);
0082     void removeFilterWidget(QWidget *);
0083 
0084 private:
0085     //@cond PRIVATE
0086     class FilterActionWidgetPrivate;
0087     std::unique_ptr<FilterActionWidgetPrivate> const d;
0088     //@endcond
0089 };
0090 
0091 /**
0092  * @short A container widget for a list of FilterActionWidgets.
0093  *
0094  * @author Marc Mutz <mutz@kde.org>
0095  * @see MailCommon::FilterAction MailCommon::MailFilter FilterActionWidget
0096  */
0097 class MAILCOMMON_EXPORT FilterActionWidgetLister : public KPIM::KWidgetLister
0098 {
0099     Q_OBJECT
0100 
0101 public:
0102     /**
0103      * Creates a new filter action widget lister.
0104      *
0105      * @param parent The parent widget.
0106      */
0107     explicit FilterActionWidgetLister(QWidget *parent = nullptr);
0108 
0109     /**
0110      * Destroys the filter action widget lister.
0111      */
0112     ~FilterActionWidgetLister() override;
0113 
0114     /**
0115      * Sets the @p list of filter actions, the lister will create FilterActionWidgets for.
0116      */
0117     void setActionList(QList<FilterAction *> *list);
0118 
0119     /**
0120      * Updates the action list according to the current action widget values.
0121      */
0122     void updateActionList();
0123 
0124     void reconnectWidget(FilterActionWidget *w);
0125 
0126 public Q_SLOTS:
0127     /**
0128      * Resets the action widgets.
0129      */
0130     void reset();
0131     void slotAddWidget(QWidget *);
0132     void slotRemoveWidget(QWidget *);
0133 
0134 Q_SIGNALS:
0135     void filterModified();
0136 
0137 protected:
0138     /**
0139      * @copydoc KPIM::KWidgetLister::clearWidget
0140      */
0141     void clearWidget(QWidget *) override;
0142 
0143     /**
0144      * @copydoc KPIM::KWidgetLister::createWidget
0145      */
0146     QWidget *createWidget(QWidget *) override;
0147 
0148     void updateAddRemoveButton();
0149 
0150 private:
0151     //@cond PRIVATE
0152     class FilterActionWidgetListerPrivate;
0153     std::unique_ptr<FilterActionWidgetListerPrivate> const d;
0154     //@endcond
0155     void connectWidget(QWidget *widget, FilterAction *filterAction);
0156 };
0157 }