Libksieve

selectbodytypewidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "selectbodytypewidget.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 
9 #include <KLocalizedString>
10 #include <Libkdepim/LineEditCatchReturnKey>
11 #include <QComboBox>
12 #include <QLineEdit>
13 
14 #include <QHBoxLayout>
15 
16 using namespace KSieveUi;
17 
18 SelectBodyTypeWidget::SelectBodyTypeWidget(QWidget *parent)
19  : QWidget(parent)
20 {
21  initialize();
22 }
23 
24 SelectBodyTypeWidget::~SelectBodyTypeWidget()
25 {
26 }
27 
28 void SelectBodyTypeWidget::initialize()
29 {
30  auto lay = new QHBoxLayout(this);
31  lay->setContentsMargins({});
32 
33  mBodyCombobox = new QComboBox(this);
34  lay->addWidget(mBodyCombobox);
35  mBodyCombobox->addItem(i18n("raw"), QStringLiteral(":raw"));
36  mBodyCombobox->addItem(i18n("content"), QStringLiteral(":content"));
37  mBodyCombobox->addItem(i18n("text"), QStringLiteral(":text"));
38  connect(mBodyCombobox, QOverload<int>::of(&QComboBox::activated), this, &SelectBodyTypeWidget::slotBodyTypeChanged);
39 
40  mBodyLineEdit = new QLineEdit(this);
41  new KPIM::LineEditCatchReturnKey(mBodyLineEdit, this);
42  connect(mBodyLineEdit, &QLineEdit::textChanged, this, &SelectBodyTypeWidget::valueChanged);
43  lay->addWidget(mBodyLineEdit);
44  mBodyLineEdit->hide();
45 }
46 
47 QString SelectBodyTypeWidget::code() const
48 {
49  QString value = mBodyCombobox->itemData(mBodyCombobox->currentIndex()).toString();
50  if (value == QLatin1String(":content")) {
51  value += QStringLiteral(" \"%1\"").arg(mBodyLineEdit->text());
52  }
53  return value;
54 }
55 
56 void SelectBodyTypeWidget::slotBodyTypeChanged(int index)
57 {
58  const QString value = mBodyCombobox->itemData(index).toString();
59  if (value == QLatin1String(":content")) {
60  mBodyLineEdit->show();
61  } else {
62  mBodyLineEdit->hide();
63  }
64  Q_EMIT valueChanged();
65 }
66 
67 void SelectBodyTypeWidget::setCode(const QString &type, const QString &content, const QString &name, QString &error)
68 {
69  const int index = mBodyCombobox->findData(type);
70  if (index != -1) {
71  mBodyCombobox->setCurrentIndex(index);
72  } else {
73  AutoCreateScriptUtil::comboboxItemNotFound(type, name, error);
74  mBodyCombobox->setCurrentIndex(0);
75  }
76  slotBodyTypeChanged(index);
77  mBodyLineEdit->setText(content);
78 }
KCRASH_EXPORT void initialize()
void textChanged(const QString &text)
void activated(int index)
QString i18n(const char *text, const TYPE &arg...)
char * toString(const T &value)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.