Libksieve

selectbodytypewidget.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "selectbodytypewidget.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 
22 #include <KLocalizedString>
23 #include <QComboBox>
24 #include <QLineEdit>
25 
26 #include <QHBoxLayout>
27 
28 using namespace KSieveUi;
29 
30 SelectBodyTypeWidget::SelectBodyTypeWidget(QWidget *parent)
31  : QWidget(parent)
32 {
33  initialize();
34 }
35 
36 SelectBodyTypeWidget::~SelectBodyTypeWidget()
37 {
38 }
39 
40 void SelectBodyTypeWidget::initialize()
41 {
42  QHBoxLayout *lay = new QHBoxLayout(this);
43  lay->setContentsMargins(0, 0, 0, 0);
44 
45  mBodyCombobox = new QComboBox(this);
46  lay->addWidget(mBodyCombobox);
47  mBodyCombobox->addItem(i18n("raw"), QStringLiteral(":raw"));
48  mBodyCombobox->addItem(i18n("content"), QStringLiteral(":content"));
49  mBodyCombobox->addItem(i18n("text"), QStringLiteral(":text"));
50  connect(mBodyCombobox, QOverload<int>::of(&QComboBox::activated), this, &SelectBodyTypeWidget::slotBodyTypeChanged);
51 
52  mBodyLineEdit = new QLineEdit(this);
53  connect(mBodyLineEdit, &QLineEdit::textChanged, this, &SelectBodyTypeWidget::valueChanged);
54  lay->addWidget(mBodyLineEdit);
55  mBodyLineEdit->hide();
56 }
57 
58 QString SelectBodyTypeWidget::code() const
59 {
60  QString value = mBodyCombobox->itemData(mBodyCombobox->currentIndex()).toString();
61  if (value == QLatin1String(":content")) {
62  value += QStringLiteral(" \"%1\"").arg(mBodyLineEdit->text());
63  }
64  return value;
65 }
66 
67 void SelectBodyTypeWidget::slotBodyTypeChanged(int index)
68 {
69  const QString value = mBodyCombobox->itemData(index).toString();
70  if (value == QLatin1String(":content")) {
71  mBodyLineEdit->show();
72  } else {
73  mBodyLineEdit->hide();
74  }
75  Q_EMIT valueChanged();
76 }
77 
78 void SelectBodyTypeWidget::setCode(const QString &type, const QString &content, const QString &name, QString &error)
79 {
80  const int index = mBodyCombobox->findData(type);
81  if (index != -1) {
82  mBodyCombobox->setCurrentIndex(index);
83  } else {
84  AutoCreateScriptUtil::comboboxItemNotFound(type, name, error);
85  mBodyCombobox->setCurrentIndex(0);
86  }
87  slotBodyTypeChanged(index);
88  mBodyLineEdit->setText(content);
89 }
void setContentsMargins(int left, int top, int right, int bottom)
KCRASH_EXPORT void initialize()
void textChanged(const QString &text)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
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-2020 The KDE developers.
Generated on Fri Aug 7 2020 23:06:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.