Libksieve

selectflagswidget.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 "selectflagswidget.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8
9#include <KConfigGroup>
10#include <KLineEditEventHandler>
11#include <KLocalizedString>
12#include <KSharedConfig>
13#include <QLineEdit>
14
15#include <KWindowConfig>
16#include <QDialogButtonBox>
17#include <QHBoxLayout>
18#include <QPointer>
19#include <QPushButton>
20#include <QToolButton>
21#include <QVBoxLayout>
22#include <QWindow>
23
24using namespace KSieveUi;
25namespace
26{
27static const char mySelectFlagsListDialogGroupName[] = "SelectFlagsListDialog";
28}
29SelectFlagsListDialog::SelectFlagsListDialog(QWidget *parent)
30 : QDialog(parent)
31 , mListWidget(new SelectFlagsListWidget(this))
32{
33 setWindowTitle(i18nc("@title:window", "Flags"));
34 auto mainLayout = new QVBoxLayout(this);
35 mainLayout->addWidget(mListWidget);
36
38 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
39 okButton->setDefault(true);
41 connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectFlagsListDialog::accept);
42 connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectFlagsListDialog::reject);
43 mainLayout->addWidget(buttonBox);
44 okButton->setFocus();
45 readConfig();
46}
47
48SelectFlagsListDialog::~SelectFlagsListDialog()
49{
50 writeConfig();
51}
52
53void SelectFlagsListDialog::readConfig()
54{
55 create(); // ensure a window is created
56 windowHandle()->resize(QSize(300, 200));
57 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySelectFlagsListDialogGroupName));
59 resize(windowHandle()->size()); // workaround for QTBUG-40584
60}
61
62void SelectFlagsListDialog::writeConfig()
63{
64 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySelectFlagsListDialogGroupName));
66 group.sync();
67}
68
69void SelectFlagsListDialog::setFlags(const QStringList &list)
70{
71 mListWidget->setFlags(list);
72}
73
74QStringList SelectFlagsListDialog::flags() const
75{
76 return mListWidget->flags();
77}
78
79SelectFlagsListWidget::SelectFlagsListWidget(QWidget *parent)
80 : QListWidget(parent)
81{
82 init();
83}
84
85SelectFlagsListWidget::~SelectFlagsListWidget() = default;
86
87void SelectFlagsListWidget::init()
88{
89 auto item = new QListWidgetItem(i18n("Deleted"), this);
90 item->setData(FlagsRealName, QStringLiteral("\\\\Deleted"));
92 item = new QListWidgetItem(i18n("Answered"), this);
93 item->setData(FlagsRealName, QStringLiteral("\\\\Answered"));
95 item = new QListWidgetItem(i18n("Flagged"), this);
96 item->setData(FlagsRealName, QStringLiteral("\\\\Flagged"));
98 item = new QListWidgetItem(i18n("Seen"), this);
99 item->setData(FlagsRealName, QStringLiteral("\\\\Seen"));
101 // item = new QListWidgetItem(QStringLiteral("\\\\Recent"), this);
102 // item->setCheckState(Qt::Unchecked);
103 item = new QListWidgetItem(i18n("Draft"), this);
104 item->setData(FlagsRealName, QStringLiteral("\\\\Draft"));
106}
107
108void SelectFlagsListWidget::setFlags(const QStringList &list)
109{
110 const int numberOfItem = count();
111 for (int i = 0; i < numberOfItem; ++i) {
112 QListWidgetItem *it = item(i);
113 if (list.contains(it->data(FlagsRealName).toString())) {
115 }
116 }
117}
118
119QStringList SelectFlagsListWidget::flags() const
120{
121 QStringList result;
122 const int numberOfItem = count();
123 for (int i = 0; i < numberOfItem; ++i) {
124 QListWidgetItem *it = item(i);
125 if (it->checkState() == Qt::Checked) {
126 result << it->data(FlagsRealName).toString();
127 }
128 }
129 return result;
130}
131
132SelectFlagsWidget::SelectFlagsWidget(QWidget *parent)
133 : QWidget(parent)
134{
135 auto lay = new QHBoxLayout(this);
136 lay->setContentsMargins({});
137 mEdit = new QLineEdit(this);
139 mEdit->setReadOnly(true);
140 mEdit->setPlaceholderText(i18nc("@info:placeholder", "Click on button for selecting flags…"));
141 connect(mEdit, &QLineEdit::textChanged, this, &SelectFlagsWidget::valueChanged);
142 lay->addWidget(mEdit);
143 auto selectFlags = new QToolButton(this);
144 selectFlags->setText(i18n("…"));
145 selectFlags->setToolTip(i18nc("@info:tooltip", "Select Flags"));
146 connect(selectFlags, &QPushButton::clicked, this, &SelectFlagsWidget::slotSelectFlags);
147 lay->addWidget(selectFlags);
148}
149
150SelectFlagsWidget::~SelectFlagsWidget() = default;
151
152void SelectFlagsWidget::slotSelectFlags()
153{
154 QPointer<SelectFlagsListDialog> dialog = new SelectFlagsListDialog(this);
155 dialog->setFlags(AutoCreateScriptUtil::createListFromString(mEdit->text()));
156 if (dialog->exec()) {
157 const QStringList lstFlags = dialog->flags();
158 QString flags;
159 if (!lstFlags.isEmpty()) {
160 flags = AutoCreateScriptUtil::createList(lstFlags);
161 }
162 mEdit->setText(flags);
163 }
164 delete dialog;
165}
166
167void SelectFlagsWidget::setFlags(const QStringList &flags)
168{
169 mEdit->setText(AutoCreateScriptUtil::createList(flags, true, true));
170}
171
172QString SelectFlagsWidget::code() const
173{
174 return mEdit->text();
175}
176
177#include "moc_selectflagswidget.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
QCA_EXPORT void init()
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
void textChanged(const QString &text)
bool isEmpty() const const
QListWidgetItem * item(int row) const const
Qt::CheckState checkState() const const
virtual QVariant data(int role) const const
void setCheckState(Qt::CheckState state)
virtual void setData(int role, const QVariant &value)
void setDefault(bool)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
Unchecked
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setFocus()
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.