Libksieve

selectflagswidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 <KLocalizedString>
11 #include <KSharedConfig>
12 #include <Libkdepim/LineEditCatchReturnKey>
13 #include <QLineEdit>
14 
15 #include <QDialogButtonBox>
16 #include <QHBoxLayout>
17 #include <QPointer>
18 #include <QPushButton>
19 #include <QToolButton>
20 #include <QVBoxLayout>
21 
22 using namespace KSieveUi;
23 
24 SelectFlagsListDialog::SelectFlagsListDialog(QWidget *parent)
25  : QDialog(parent)
26  , mListWidget(new SelectFlagsListWidget(this))
27 {
28  setWindowTitle(i18nc("@title:window", "Flags"));
29  auto mainLayout = new QVBoxLayout(this);
30  mainLayout->addWidget(mListWidget);
31 
33  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
34  okButton->setDefault(true);
35  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
36  connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectFlagsListDialog::accept);
37  connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectFlagsListDialog::reject);
38  mainLayout->addWidget(buttonBox);
39  okButton->setFocus();
40  readConfig();
41 }
42 
43 SelectFlagsListDialog::~SelectFlagsListDialog()
44 {
45  writeConfig();
46 }
47 
48 void SelectFlagsListDialog::readConfig()
49 {
50  KConfigGroup group(KSharedConfig::openStateConfig(), "SelectFlagsListDialog");
51  const QSize sizeDialog = group.readEntry("Size", QSize(300, 200));
52  if (sizeDialog.isValid()) {
53  resize(sizeDialog);
54  }
55 }
56 
57 void SelectFlagsListDialog::writeConfig()
58 {
59  KConfigGroup group(KSharedConfig::openStateConfig(), "SelectFlagsListDialog");
60  group.writeEntry("Size", size());
61 }
62 
63 void SelectFlagsListDialog::setFlags(const QStringList &list)
64 {
65  mListWidget->setFlags(list);
66 }
67 
68 QStringList SelectFlagsListDialog::flags() const
69 {
70  return mListWidget->flags();
71 }
72 
73 SelectFlagsListWidget::SelectFlagsListWidget(QWidget *parent)
74  : QListWidget(parent)
75 {
76  init();
77 }
78 
79 SelectFlagsListWidget::~SelectFlagsListWidget()
80 {
81 }
82 
83 void SelectFlagsListWidget::init()
84 {
85  auto item = new QListWidgetItem(i18n("Deleted"), this);
86  item->setData(FlagsRealName, QStringLiteral("\\\\Deleted"));
87  item->setCheckState(Qt::Unchecked);
88  item = new QListWidgetItem(i18n("Answered"), this);
89  item->setData(FlagsRealName, QStringLiteral("\\\\Answered"));
90  item->setCheckState(Qt::Unchecked);
91  item = new QListWidgetItem(i18n("Flagged"), this);
92  item->setData(FlagsRealName, QStringLiteral("\\\\Flagged"));
93  item->setCheckState(Qt::Unchecked);
94  item = new QListWidgetItem(i18n("Seen"), this);
95  item->setData(FlagsRealName, QStringLiteral("\\\\Seen"));
96  item->setCheckState(Qt::Unchecked);
97  // item = new QListWidgetItem(QStringLiteral("\\\\Recent"), this);
98  // item->setCheckState(Qt::Unchecked);
99  item = new QListWidgetItem(i18n("Draft"), this);
100  item->setData(FlagsRealName, QStringLiteral("\\\\Draft"));
101  item->setCheckState(Qt::Unchecked);
102 }
103 
104 void SelectFlagsListWidget::setFlags(const QStringList &list)
105 {
106  const int numberOfItem = count();
107  for (int i = 0; i < numberOfItem; ++i) {
108  QListWidgetItem *it = item(i);
109  if (list.contains(it->data(FlagsRealName).toString())) {
111  }
112  }
113 }
114 
115 QStringList SelectFlagsListWidget::flags() const
116 {
117  QStringList result;
118  const int numberOfItem = count();
119  for (int i = 0; i < numberOfItem; ++i) {
120  QListWidgetItem *it = item(i);
121  if (it->checkState() == Qt::Checked) {
122  result << it->data(FlagsRealName).toString();
123  }
124  }
125  return result;
126 }
127 
128 SelectFlagsWidget::SelectFlagsWidget(QWidget *parent)
129  : QWidget(parent)
130 {
131  auto lay = new QHBoxLayout(this);
132  lay->setContentsMargins({});
133  mEdit = new QLineEdit(this);
134  new KPIM::LineEditCatchReturnKey(mEdit, this);
135  mEdit->setReadOnly(true);
136  mEdit->setPlaceholderText(i18n("Click on button for selecting flags..."));
137  connect(mEdit, &QLineEdit::textChanged, this, &SelectFlagsWidget::valueChanged);
138  lay->addWidget(mEdit);
139  auto selectFlags = new QToolButton(this);
140  selectFlags->setText(i18n("..."));
141  selectFlags->setToolTip(i18n("Select Flags"));
142  connect(selectFlags, &QPushButton::clicked, this, &SelectFlagsWidget::slotSelectFlags);
143  lay->addWidget(selectFlags);
144 }
145 
146 SelectFlagsWidget::~SelectFlagsWidget()
147 {
148 }
149 
150 void SelectFlagsWidget::slotSelectFlags()
151 {
152  QPointer<SelectFlagsListDialog> dialog = new SelectFlagsListDialog(this);
153  dialog->setFlags(AutoCreateScriptUtil::createListFromString(mEdit->text()));
154  if (dialog->exec()) {
155  const QStringList lstFlags = dialog->flags();
156  QString flags;
157  if (!lstFlags.isEmpty()) {
158  flags = AutoCreateScriptUtil::createList(lstFlags);
159  }
160  mEdit->setText(flags);
161  }
162  delete dialog;
163 }
164 
165 void SelectFlagsWidget::setFlags(const QStringList &flags)
166 {
167  mEdit->setText(AutoCreateScriptUtil::createList(flags, true, true));
168 }
169 
170 QString SelectFlagsWidget::code() const
171 {
172  return mEdit->text();
173 }
bool isValid() const const
Qt::CheckState checkState() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void textChanged(const QString &text)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setFocus()
bool isEmpty() const const
void clicked(bool checked)
virtual QVariant data(int role) const const
void setShortcut(const QKeySequence &key)
void setCheckState(Qt::CheckState state)
QCA_EXPORT void init()
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
Unchecked
void setDefault(bool)
QString toString() const const
Key_Return
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.