Mailcommon

searchpatternedit.h
1/*
2 SPDX-FileCopyrightText: Marc Mutz <mutz@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "mailcommon_export.h"
10#include "searchpattern.h"
11
12#include <Libkdepim/KWidgetLister>
13
14#include <QByteArray>
15#include <QGroupBox>
16
17class KComboBox;
18class QPushButton;
19
20class QAbstractButton;
21class QRadioButton;
22class QStackedWidget;
23
24namespace MailCommon
25{
26class SearchPatternEdit;
27
28/**
29 * This widget is intended to be used in the filter configuration as
30 * well as in the message search dialogs. It consists of a frame,
31 * inside which there are placed two radio buttons entitled "Match
32 * {all,any} of the following", followed by a vertical stack of
33 * MailCommon::SearchRuleWidgets (initially two) and two buttons to add
34 * and remove, resp., additional KMSearchWidget 's.
35 *
36 * To set the widget according to a given KMSearchPattern, use
37 * setSearchPattern; to initialize it (e.g. for a new, virgin
38 * rule), use setSearchPattern with a 0 argument. The widget
39 * operates directly on a shallow(!) copy of the search rule. So
40 * while you actually don't really need searchPattern, because
41 * you can always store a pointer to the current pattern yourself,
42 * you must not modify the currently-worked-on pattern yourself while
43 * this widget holds a reference to it. The only exceptions are:
44 *
45 * @li If you edit a derived class, you can change aspects of the
46 * class that don't interfere with the KMSearchPattern part. An
47 * example is KMFilter, whose actions you can still edit while the
48 * KMSearchPattern part of it is being acted upon by this widget.
49 *
50 * @li You can change the name of the pattern, but only using (this
51 * widget's) setName. You cannot change the pattern's name
52 * directly, although this widget in itself doesn't let the user
53 * change it. This is because it auto-names the pattern to
54 * "<$field>:$contents" iff the pattern begins with "<".
55 *
56 * @short A widget which allows editing a set of MailCommon::SearchRule's.
57 * @author Marc Mutz <mutz@kde.org>
58 */
59class SearchRuleWidgetLister;
60class MAILCOMMON_EXPORT SearchPatternEdit : public QWidget
61{
62 Q_OBJECT
63
64public:
65 enum SearchPatternEditOption {
66 None = 0,
67 HeadersOnly = 1,
68 NotShowAbsoluteDate = 2,
69 MatchAllMessages = 4,
70 NotShowSize = 8,
71 NotShowDate = 16,
72 NotShowTags = 32
73 };
74 Q_DECLARE_FLAGS(SearchPatternEditOptions, SearchPatternEditOption)
75
76 enum SearchModeType { StandardMode = 0, BalooMode = 1 };
77
78 /**
79 * Constructor. The parent parameter is passed to the underlying
80 * QGroupBox, as usual.
81 */
82 explicit SearchPatternEdit(QWidget *parent = nullptr,
83 SearchPatternEditOptions options = (SearchPatternEditOptions)(None),
84 SearchModeType modeType = StandardMode);
85
86 ~SearchPatternEdit() override;
87
88 void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
89
90 /**
91 * Sets the search pattern. Rules are inserted regardless of the
92 * return value of each rules' MailCommon::SearchRule::isEmpty.
93 * This widget makes a shallow copy of @p aPattern and operates
94 * directly on it.
95 */
96 void setSearchPattern(MailCommon::SearchPattern *aPattern);
97
98 /**
99 * Updates the search pattern according to the current widget values.
100 */
101 void updateSearchPattern();
102
103public Q_SLOTS:
104 /**
105 * Called when the widget should let go of the currently referenced
106 * filter and disable itself.
107 */
108 void reset();
109
110Q_SIGNALS:
111 /**
112 * This signal is emitted whenever the name of the processed
113 * search pattern may have changed.
114 */
115 void maybeNameChanged();
116
117 /**
118 * This signal is emitted wherenever the search pattern changes in some way.
119 */
120 void patternChanged();
121
122 void returnPressed();
123
124private:
125 MAILCOMMON_NO_EXPORT void slotRuleAdded(QWidget *widget);
126 MAILCOMMON_NO_EXPORT void slotAutoNameHack();
127 MAILCOMMON_NO_EXPORT void slotRadioClicked(QAbstractButton *aRBtn);
128 MAILCOMMON_NO_EXPORT void initLayout(SearchPatternEditOptions options, SearchModeType modeType);
129 MailCommon::SearchPattern *mPattern = nullptr;
130 QRadioButton *mAllRBtn = nullptr;
131 QRadioButton *mAnyRBtn = nullptr;
132 QRadioButton *mAllMessageRBtn = nullptr;
133 SearchRuleWidgetLister *mRuleLister = nullptr;
134};
135
136/**
137 * A widget to edit a single MailCommon::SearchRule.
138 * It consists of an editable KComboBox for the field,
139 * a read-only KComboBox for the function and
140 * a QLineEdit for the content or the pattern (in case of regexps).
141 * It manages the i18n itself, so field name should be in it's english form.
142 *
143 * To use, you essentially give it the reference to a MailCommon::SearchRule and
144 * it does the rest. It will never delete the rule itself, as it assumes
145 * that something outside of it manages this.
146 *
147 * @short A widget to edit a single MailCommon::SearchRule.
148 * @author Marc Mutz <mutz@kde.org>
149 */
151{
153
154public:
155 /**
156 * Constructor. You can give a MailCommon::SearchRule as parameter,
157 * which will be used to initialize the widget.
158 */
159 explicit SearchRuleWidget(QWidget *parent = nullptr,
162 SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
163
164 enum { Message, Body, AnyHeader, Recipients, Size, AgeInDays, Status, Tag, Subject, From, To, CC, ReplyTo, Organization, Date, Encryption };
165
166 /**
167 * Sets the rule. The rule is accepted regardless of the return
168 * value of MailCommon::SearchRule::isEmpty. This widget makes a shallow
169 * copy of @p aRule and operates directly on it. If @p aRule is 0,
170 * resets itself, takes user input, but does essentially nothing.
171 * If you pass 0, you should probably disable it.
172 */
174
175 /**
176 * Returns a reference to the currently-worked-on MailCommon::SearchRule.
177 */
179
180 /**
181 * Resets the rule currently worked on and updates the widget accordingly.
182 */
183 void reset();
184
185 static int ruleFieldToId(const QString &i18nVal);
186
187 void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled);
188
189 void setPatternEditOptions(MailCommon::SearchPatternEdit::SearchPatternEditOptions options);
190
191public Q_SLOTS:
192 void slotFunctionChanged();
193 void slotValueChanged();
194 void slotReturnPressed();
195
197 /**
198 * This signal is emitted whenever the user alters the field.
199 * The pseudo-headers <...> are returned in their i18n form, but
200 * stored in their English form in the rule.
201 */
202 void fieldChanged(const QString &);
203
204 /**
205 * This signal is emitted whenever the user alters the contents/value
206 * of the rule.
207 */
209
210 void returnPressed();
211
212 void addWidget(QWidget *);
213 void removeWidget(QWidget *);
214
215protected:
216 /**
217 * Used internally to translate i18n-ized pseudo-headers back to English.
218 */
219 static QByteArray ruleFieldToEnglish(const QString &i18nVal);
220
221 /**
222 * Used internally to find the corresponding index into the field
223 * ComboBox. Returns the index if found or -1 if the search failed,
224 */
225 int indexOfRuleField(const QByteArray &aName) const;
226
227protected Q_SLOTS:
228 void slotRuleFieldChanged(const QString &);
229 void slotAddWidget();
230 void slotRemoveWidget();
231
232private:
233 void initWidget(SearchPatternEdit::SearchModeType modeType);
235
236 QStringList mFilterFieldList;
237 KComboBox *mRuleField = nullptr;
238 QStackedWidget *mFunctionStack = nullptr;
239 QStackedWidget *mValueStack = nullptr;
240 QPushButton *mAdd = nullptr;
241 QPushButton *mRemove = nullptr;
242};
243
244class SearchRuleWidgetLister : public KPIM::KWidgetLister
245{
247
248 friend class SearchPatternEdit;
249
250public:
251 explicit SearchRuleWidgetLister(QWidget *parent = nullptr,
253 SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
254
255 ~SearchRuleWidgetLister() override;
256
257 void setRuleList(QList<MailCommon::SearchRule::Ptr> *aList);
258
259 void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
260
261public Q_SLOTS:
262 void reset();
263 void slotAddWidget(QWidget *);
264 void slotRemoveWidget(QWidget *);
265
266protected:
267 void clearWidget(QWidget *aWidget) override;
268 QWidget *createWidget(QWidget *parent) override;
269
270private:
271 MAILCOMMON_NO_EXPORT void reconnectWidget(SearchRuleWidget *w);
272 MAILCOMMON_NO_EXPORT void updateAddRemoveButton();
273 MAILCOMMON_NO_EXPORT void regenerateRuleListFromWidgets();
274 QList<MailCommon::SearchRule::Ptr> *mRuleList = nullptr;
276 SearchPatternEdit::SearchModeType mTypeMode;
277};
278}
This class is an abstraction of a search over messages.
A widget to edit a single MailCommon::SearchRule.
void reset()
Resets the rule currently worked on and updates the widget accordingly.
SearchRuleWidget(QWidget *parent=nullptr, MailCommon::SearchRule::Ptr aRule=MailCommon::SearchRule::Ptr(), SearchPatternEdit::SearchPatternEditOptions options=(SearchPatternEdit::SearchPatternEditOptions)(SearchPatternEdit::None), SearchPatternEdit::SearchModeType modeType=SearchPatternEdit::StandardMode)
Constructor.
void contentsChanged(const QString &)
This signal is emitted whenever the user alters the contents/value of the rule.
MailCommon::SearchRule::Ptr rule() const
Returns a reference to the currently-worked-on MailCommon::SearchRule.
void fieldChanged(const QString &)
This signal is emitted whenever the user alters the field.
int indexOfRuleField(const QByteArray &aName) const
Used internally to find the corresponding index into the field ComboBox.
void setRule(MailCommon::SearchRule::Ptr aRule)
Sets the rule.
static QByteArray ruleFieldToEnglish(const QString &i18nVal)
Used internally to translate i18n-ized pseudo-headers back to English.
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
KGuiItem reset()
The filter dialog.
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:01 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.