Libksieve

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

KDE's Doxygen guidelines are available online.