Libksieve

selectbodytypewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "selectbodytypewidget.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8
9#include <KLineEditEventHandler>
10#include <KLocalizedString>
11#include <QComboBox>
12#include <QLineEdit>
13
14#include <QHBoxLayout>
15
16using namespace KSieveUi;
17
18SelectBodyTypeWidget::SelectBodyTypeWidget(QWidget *parent)
19 : QWidget(parent)
20{
21 initialize();
22}
23
24SelectBodyTypeWidget::~SelectBodyTypeWidget() = default;
25
26void SelectBodyTypeWidget::initialize()
27{
28 auto lay = new QHBoxLayout(this);
29 lay->setContentsMargins({});
30
31 mBodyCombobox = new QComboBox(this);
32 lay->addWidget(mBodyCombobox);
33 mBodyCombobox->addItem(i18n("raw"), QStringLiteral(":raw"));
34 mBodyCombobox->addItem(i18n("content"), QStringLiteral(":content"));
35 mBodyCombobox->addItem(i18n("text"), QStringLiteral(":text"));
36 connect(mBodyCombobox, &QComboBox::activated, this, &SelectBodyTypeWidget::slotBodyTypeChanged);
37
38 mBodyLineEdit = new QLineEdit(this);
40 connect(mBodyLineEdit, &QLineEdit::textChanged, this, &SelectBodyTypeWidget::valueChanged);
41 lay->addWidget(mBodyLineEdit);
42 mBodyLineEdit->hide();
43}
44
45QString SelectBodyTypeWidget::code() const
46{
47 QString value = mBodyCombobox->itemData(mBodyCombobox->currentIndex()).toString();
48 if (value == QLatin1StringView(":content")) {
49 value += QStringLiteral(" \"%1\"").arg(mBodyLineEdit->text());
50 }
51 return value;
52}
53
54void SelectBodyTypeWidget::slotBodyTypeChanged(int index)
55{
56 const QString value = mBodyCombobox->itemData(index).toString();
57 if (value == QLatin1StringView(":content")) {
58 mBodyLineEdit->show();
59 } else {
60 mBodyLineEdit->hide();
61 }
62 Q_EMIT valueChanged();
63}
64
65void SelectBodyTypeWidget::setCode(const QString &type, const QString &content, const QString &name, QString &error)
66{
67 const int index = mBodyCombobox->findData(type);
68 if (index != -1) {
69 mBodyCombobox->setCurrentIndex(index);
70 } else {
71 AutoCreateScriptUtil::comboboxItemNotFound(type, name, error);
72 mBodyCombobox->setCurrentIndex(0);
73 }
74 slotBodyTypeChanged(index);
75 mBodyLineEdit->setText(content);
76}
77
78#include "moc_selectbodytypewidget.cpp"
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void initialize(StandardShortcut id)
void activated(int index)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
void textChanged(const QString &text)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
QString toString() const const
void hide()
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.