Mailcommon

invitationwidgethandler.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "invitationwidgethandler.h"
8#include <KLazyLocalizedString>
9#include <QComboBox>
10#include <QLabel>
11#include <QStackedWidget>
12using namespace MailCommon;
13
14static const struct {
16 const KLazyLocalizedString displayName;
17} InvitationFunctions[] = {{SearchRule::FuncEquals, kli18n("has")}, {SearchRule::FuncNotEqual, kli18n("has not")}};
18static const int InvitationFunctionCount = sizeof(InvitationFunctions) / sizeof(*InvitationFunctions);
19
20InvitationWidgetHandler::InvitationWidgetHandler()
22{
23}
24
25InvitationWidgetHandler::~InvitationWidgetHandler() = default;
26
27QWidget *InvitationWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isBalooSearch) const
28{
29 Q_UNUSED(isBalooSearch)
30
31 if (number != 0) {
32 return nullptr;
33 }
34
35 auto combo = new QComboBox(functionStack);
36 combo->setMinimumWidth(50);
37 combo->setObjectName(QLatin1StringView("invitationRuleFuncCombo"));
38 for (int i = 0; i < InvitationFunctionCount; ++i) {
39 combo->addItem(InvitationFunctions[i].displayName.toString());
40 }
41 combo->adjustSize();
42 QObject::connect(combo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
43 return combo;
44}
45
46QWidget *InvitationWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
47{
48 Q_UNUSED(receiver)
49
50 if (number != 0) {
51 return nullptr;
52 }
53
54 auto lbl = new QLabel(i18nc("@label:textbox", "invitation"), valueStack);
55 lbl->setObjectName(QLatin1StringView("invitationRuleValueLabel"));
56 return lbl;
57}
58
59SearchRule::Function InvitationWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
60{
61 if (!handlesField(field)) {
62 return SearchRule::FuncNone;
63 }
64
65 const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("invitationRuleFuncCombo"));
66 if (combo && combo->currentIndex() >= 0) {
67 return InvitationFunctions[combo->currentIndex()].id;
68 }
69
70 return SearchRule::FuncNone;
71}
72
73QString InvitationWidgetHandler::value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
74{
75 Q_UNUSED(functionStack)
76 Q_UNUSED(valueStack)
77 if (!handlesField(field)) {
78 return {};
79 }
80 return QStringLiteral("has invitation"); // dummy value
81}
82
83QString InvitationWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
84{
85 Q_UNUSED(functionStack)
86 Q_UNUSED(valueStack)
87 if (!handlesField(field)) {
88 return {};
89 }
90
91 return i18n("has invitation");
92}
93
94bool InvitationWidgetHandler::handlesField(const QByteArray &field) const
95{
96 return field == "<invitation>";
97}
98
99void InvitationWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
100{
101 const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("invitationRuleFuncCombo"));
102 if (combo) {
103 const bool blocked = combo->blockSignals(true);
104 combo->setCurrentIndex(0);
105 combo->blockSignals(blocked);
106 }
107
108 Q_UNUSED(valueStack)
109}
110
111bool InvitationWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isBalooSearch) const
112{
113 Q_UNUSED(isBalooSearch)
114 if (!rule || !handlesField(rule->field())) {
115 reset(functionStack, valueStack);
116 return false;
117 }
118 update("<invitation>", functionStack, valueStack);
119
120 const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("invitationRuleFuncCombo"));
121 if (combo) {
122 const bool blocked = combo->blockSignals(true);
123 for (int i = 0; i < InvitationFunctionCount; ++i) {
124 if (InvitationFunctions[i].id == rule->function()) {
125 combo->setCurrentIndex(i);
126 break;
127 }
128 }
129 combo->blockSignals(blocked);
130 return true;
131 }
132
133 return true;
134}
135
136bool InvitationWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
137{
138 if (!handlesField(field)) {
139 return false;
140 }
141
142 functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("invitationRuleFuncCombo")));
143 valueStack->setCurrentWidget(valueStack->findChild<QWidget *>(QStringLiteral("invitationRuleValueLabel")));
144
145 return true;
146}
QString toString() const
An interface to filter/search rule widget handlers.
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
Function
Describes operators for comparison of field and contents.
Definition searchrule.h:40
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
void setCurrentWidget(QWidget *widget)
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.