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 {
77 StandardMode = 0,
78 BalooMode = 1,
79 };
80
81 /**
82 * Constructor. The parent parameter is passed to the underlying
83 * QGroupBox, as usual.
84 */
85 explicit SearchPatternEdit(QWidget *parent = nullptr,
86 SearchPatternEditOptions options = (SearchPatternEditOptions)(None),
87 SearchModeType modeType = StandardMode);
88
89 ~SearchPatternEdit() override;
90
91 void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
92
93 /**
94 * Sets the search pattern. Rules are inserted regardless of the
95 * return value of each rules' MailCommon::SearchRule::isEmpty.
96 * This widget makes a shallow copy of @p aPattern and operates
97 * directly on it.
98 */
99 void setSearchPattern(MailCommon::SearchPattern *aPattern);
100
101 /**
102 * Updates the search pattern according to the current widget values.
103 */
104 void updateSearchPattern();
105
106public Q_SLOTS:
107 /**
108 * Called when the widget should let go of the currently referenced
109 * filter and disable itself.
110 */
111 void reset();
112
113Q_SIGNALS:
114 /**
115 * This signal is emitted whenever the name of the processed
116 * search pattern may have changed.
117 */
118 void maybeNameChanged();
119
120 /**
121 * This signal is emitted wherenever the search pattern changes in some way.
122 */
123 void patternChanged();
124
125 void returnPressed();
126
127private:
128 MAILCOMMON_NO_EXPORT void slotRuleAdded(QWidget *widget);
129 MAILCOMMON_NO_EXPORT void slotAutoNameHack();
130 MAILCOMMON_NO_EXPORT void slotRadioClicked(QAbstractButton *aRBtn);
131 MAILCOMMON_NO_EXPORT void initLayout(SearchPatternEditOptions options, SearchModeType modeType);
132 MailCommon::SearchPattern *mPattern = nullptr;
133 QRadioButton *mAllRBtn = nullptr;
134 QRadioButton *mAnyRBtn = nullptr;
135 QRadioButton *mAllMessageRBtn = nullptr;
136 SearchRuleWidgetLister *mRuleLister = nullptr;
137};
138
139/**
140 * A widget to edit a single MailCommon::SearchRule.
141 * It consists of an editable KComboBox for the field,
142 * a read-only KComboBox for the function and
143 * a QLineEdit for the content or the pattern (in case of regexps).
144 * It manages the i18n itself, so field name should be in it's english form.
145 *
146 * To use, you essentially give it the reference to a MailCommon::SearchRule and
147 * it does the rest. It will never delete the rule itself, as it assumes
148 * that something outside of it manages this.
149 *
150 * @short A widget to edit a single MailCommon::SearchRule.
151 * @author Marc Mutz <mutz@kde.org>
152 */
154{
156
157public:
158 /**
159 * Constructor. You can give a MailCommon::SearchRule as parameter,
160 * which will be used to initialize the widget.
161 */
162 explicit SearchRuleWidget(QWidget *parent = nullptr,
165 SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
166
167 enum {
168 Message,
169 Body,
170 AnyHeader,
171 Recipients,
172 Size,
173 AgeInDays,
174 Status,
175 Tag,
176 Subject,
177 From,
178 To,
179 CC,
180 ReplyTo,
181 Organization,
182 Date,
183 Encryption,
184 Attachment,
185 Invitation,
186 };
187
188 /**
189 * Sets the rule. The rule is accepted regardless of the return
190 * value of MailCommon::SearchRule::isEmpty. This widget makes a shallow
191 * copy of @p aRule and operates directly on it. If @p aRule is 0,
192 * resets itself, takes user input, but does essentially nothing.
193 * If you pass 0, you should probably disable it.
194 */
196
197 /**
198 * Returns a reference to the currently-worked-on MailCommon::SearchRule.
199 */
201
202 /**
203 * Resets the rule currently worked on and updates the widget accordingly.
204 */
205 void reset();
206
207 static int ruleFieldToId(const QString &i18nVal);
208
209 void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled);
210
211 void setPatternEditOptions(MailCommon::SearchPatternEdit::SearchPatternEditOptions options);
212
213public Q_SLOTS:
214 void slotFunctionChanged();
215 void slotValueChanged();
216 void slotReturnPressed();
217
219 /**
220 * This signal is emitted whenever the user alters the field.
221 * The pseudo-headers <...> are returned in their i18n form, but
222 * stored in their English form in the rule.
223 */
224 void fieldChanged(const QString &);
225
226 /**
227 * This signal is emitted whenever the user alters the contents/value
228 * of the rule.
229 */
231
232 void returnPressed();
233
234 void addWidget(QWidget *);
235 void removeWidget(QWidget *);
236
237protected:
238 /**
239 * Used internally to translate i18n-ized pseudo-headers back to English.
240 */
241 static QByteArray ruleFieldToEnglish(const QString &i18nVal);
242
243 /**
244 * Used internally to find the corresponding index into the field
245 * ComboBox. Returns the index if found or -1 if the search failed,
246 */
247 int indexOfRuleField(const QByteArray &aName) const;
248
249protected Q_SLOTS:
250 void slotRuleFieldChanged(const QString &);
251 void slotAddWidget();
252 void slotRemoveWidget();
253
254private:
255 void initWidget(SearchPatternEdit::SearchModeType modeType);
257
258 QStringList mFilterFieldList;
259 KComboBox *mRuleField = nullptr;
260 QStackedWidget *mFunctionStack = nullptr;
261 QStackedWidget *mValueStack = nullptr;
262 QPushButton *mAdd = nullptr;
263 QPushButton *mRemove = nullptr;
264};
265
266class SearchRuleWidgetLister : public KPIM::KWidgetLister
267{
269
270 friend class SearchPatternEdit;
271
272public:
273 explicit SearchRuleWidgetLister(QWidget *parent = nullptr,
275 SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
276
277 ~SearchRuleWidgetLister() override;
278
279 void setRuleList(QList<MailCommon::SearchRule::Ptr> *aList);
280
281 void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
282
283public Q_SLOTS:
284 void reset();
285 void slotAddWidget(QWidget *);
286 void slotRemoveWidget(QWidget *);
287
288protected:
289 void clearWidget(QWidget *aWidget) override;
290 QWidget *createWidget(QWidget *parent) override;
291
292private:
293 MAILCOMMON_NO_EXPORT void reconnectWidget(SearchRuleWidget *w);
294 MAILCOMMON_NO_EXPORT void updateAddRemoveButton();
295 MAILCOMMON_NO_EXPORT void regenerateRuleListFromWidgets();
296 QList<MailCommon::SearchRule::Ptr> *mRuleList = nullptr;
298 SearchPatternEdit::SearchModeType mTypeMode;
299};
300}
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 Sat Dec 21 2024 17:00:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.