12#include "simplestringlisteditor.h"
14#include "pimcommon_debug.h"
15#include <KLocalizedString>
19#include <QInputDialog>
30class PimCommon::SimpleStringListEditorPrivate
33 SimpleStringListEditorPrivate() =
default;
38 const int numberOfFilters = mListBox->
count();
39 for (
int i = 0; i < numberOfFilters; ++i) {
41 listWidgetItem << mListBox->
item(i);
44 return listWidgetItem;
59 QString mRemoveDialogLabel =
i18n(
"Do you want to remove selected text?");
69 , d(new SimpleStringListEditorPrivate)
71 setAddDialogLabel(addDialogLabel);
74 hlay->setContentsMargins({});
82 hlay->addWidget(d->mListBox, 1);
84 if (buttons == None) {
85 qCDebug(PIMCOMMON_LOG) <<
"SimpleStringListBox called with no buttons."
86 "Consider using a plain QListBox instead!";
90 hlay->addLayout(d->mButtonLayout);
98 d->mAddButton->setAutoDefault(
false);
99 d->mButtonLayout->addWidget(d->mAddButton);
103 if (buttons & Modify) {
105 d->mModifyButton =
new QPushButton(
i18nc(
"@action:button",
"&Modify..."),
this);
107 d->mModifyButton =
new QPushButton(modifyLabel,
this);
109 d->mModifyButton->setAutoDefault(
false);
110 d->mModifyButton->setEnabled(
false);
111 d->mButtonLayout->addWidget(d->mModifyButton);
116 if (buttons & Remove) {
118 d->mRemoveButton =
new QPushButton(
i18nc(
"@action:button",
"&Remove"),
this);
120 d->mRemoveButton =
new QPushButton(removeLabel,
this);
122 d->mRemoveButton->setAutoDefault(
false);
123 d->mRemoveButton->setEnabled(
false);
124 d->mButtonLayout->addWidget(d->mRemoveButton);
129 if (!(buttons & Down)) {
130 qCDebug(PIMCOMMON_LOG) <<
"Are you sure you want to use an Up button"
131 "without a Down button??";
135 d->mUpButton->setAutoDefault(
false);
136 d->mUpButton->setEnabled(
false);
137 d->mButtonLayout->addWidget(d->mUpButton);
141 if (buttons & Down) {
142 if (!(buttons & Up)) {
143 qCDebug(PIMCOMMON_LOG) <<
"Are you sure you want to use a Down button"
144 "without an Up button??";
148 d->mDownButton->setAutoDefault(
false);
149 d->mDownButton->setEnabled(
false);
150 d->mButtonLayout->addWidget(d->mDownButton);
154 if (buttons & Custom) {
155 d->mCustomButton =
new QPushButton(
i18nc(
"@action:button",
"&Customize..."),
this);
156 d->mCustomButton->setAutoDefault(
false);
157 d->mCustomButton->setEnabled(
false);
158 d->mButtonLayout->addWidget(d->mCustomButton);
162 d->mButtonLayout->addStretch(1);
168SimpleStringListEditor::~SimpleStringListEditor() =
default;
170void SimpleStringListEditor::setUpDownAutoRepeat(
bool b)
173 d->mUpButton->setAutoRepeat(b);
175 if (d->mDownButton) {
176 d->mDownButton->setAutoRepeat(b);
182 d->mListBox->clear();
183 d->mListBox->addItems(strings);
188 d->mListBox->addItems(strings);
194 const int numberOfItem(d->mListBox->count());
196 for (
int i = 0; i < numberOfItem; ++i) {
197 result << (d->mListBox->item(i)->text());
202bool SimpleStringListEditor::containsString(
const QString &str)
204 const int numberOfItem(d->mListBox->count());
205 for (
int i = 0; i < numberOfItem; ++i) {
206 if (d->mListBox->item(i)->text() == str) {
217 if (!d->mAddButton) {
220 d->mAddButton->setText(text);
223 if (!d->mRemoveButton) {
226 d->mRemoveButton->setText(text);
229 if (!d->mModifyButton) {
232 d->mModifyButton->setText(text);
235 if (!d->mCustomButton) {
238 d->mCustomButton->setText(text);
242 qCDebug(PIMCOMMON_LOG) <<
"SimpleStringListEditor: Cannot change text of"
243 "Up and Down buttons: they don't contains text!";
247 qCDebug(PIMCOMMON_LOG) <<
"No such button!";
249 qCDebug(PIMCOMMON_LOG) <<
"Can only set text for one button at a time!";
254 qCDebug(PIMCOMMON_LOG) <<
"The requested button has not been created!";
257void SimpleStringListEditor::addNewEntry()
262 insertNewEntry(newEntry);
266void SimpleStringListEditor::insertNewEntry(
const QString &entry)
271 if (!newEntry.
isEmpty() && !containsString(newEntry)) {
272 d->mListBox->addItem(newEntry);
273 slotSelectionChanged();
278void SimpleStringListEditor::slotAdd()
283void SimpleStringListEditor::slotCustomize()
302void SimpleStringListEditor::slotRemove()
310 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
312 delete d->mListBox->takeItem(d->mListBox->row(item));
314 slotSelectionChanged();
332void SimpleStringListEditor::slotModify()
345void SimpleStringListEditor::setRemoveDialogLabel(
const QString &removeDialogLabel)
347 d->mRemoveDialogLabel = removeDialogLabel;
350void SimpleStringListEditor::setAddDialogLabel(
const QString &addDialogLabel)
352 d->mAddDialogLabel = addDialogLabel;
355void SimpleStringListEditor::setAddDialogTitle(
const QString &str)
357 d->mAddDialogTitle = str;
360void SimpleStringListEditor::setModifyDialogTitle(
const QString &str)
362 d->mModifyDialogTitle = str;
365void SimpleStringListEditor::setModifyDialogLabel(
const QString &str)
367 d->mModifyDialogLabel = str;
370void SimpleStringListEditor::slotUp()
373 if (listWidgetItem.
isEmpty()) {
377 const int numberOfItem(listWidgetItem.
count());
378 const int currentRow = d->mListBox->currentRow();
379 if ((numberOfItem == 1) && (currentRow == 0)) {
380 qCDebug(PIMCOMMON_LOG) <<
"Called while the _topmost_ filter is selected, ignoring.";
383 bool wasMoved =
false;
385 for (
int i = 0; i < numberOfItem; ++i) {
386 const int posItem = d->mListBox->row(listWidgetItem.
at(i));
391 d->mListBox->insertItem(posItem - 1, item);
397 d->mListBox->setCurrentRow(currentRow - 1);
401void SimpleStringListEditor::slotDown()
404 if (listWidgetItem.
isEmpty()) {
408 const int numberOfElement(d->mListBox->count());
409 const int numberOfItem(listWidgetItem.
count());
410 const int currentRow = d->mListBox->currentRow();
411 if ((numberOfItem == 1) && (currentRow == numberOfElement - 1)) {
412 qCDebug(PIMCOMMON_LOG) <<
"Called while the _last_ filter is selected, ignoring.";
417 bool wasMoved =
false;
418 for (
int i = numberOfItem - 1; i >= 0; --i, j++) {
419 const int posItem = d->mListBox->row(listWidgetItem.
at(i));
420 if (posItem == (numberOfElement - 1 - j)) {
424 d->mListBox->insertItem(posItem + 1, item);
429 d->mListBox->setCurrentRow(currentRow + 1);
433void SimpleStringListEditor::slotSelectionChanged()
436 const int numberOfItemSelected(lstSelectedItems.
count());
437 const bool uniqItemSelected = (numberOfItemSelected == 1);
438 const bool aItemIsSelected = !lstSelectedItems.
isEmpty();
441 if (d->mRemoveButton) {
442 d->mRemoveButton->setEnabled(aItemIsSelected);
445 if (d->mModifyButton) {
446 d->mModifyButton->setEnabled(uniqItemSelected);
449 const int currentIndex = d->mListBox->currentRow();
451 const bool allItemSelected = (d->mListBox->count() == numberOfItemSelected);
452 const bool theLast = (currentIndex >= d->mListBox->count() - 1);
453 const bool theFirst = (currentIndex == 0);
455 if (d->mCustomButton) {
456 d->mCustomButton->setEnabled(uniqItemSelected);
460 d->mUpButton->setEnabled(aItemIsSelected && ((uniqItemSelected && !theFirst) || (!uniqItemSelected)) && !allItemSelected);
462 if (d->mDownButton) {
463 d->mDownButton->setEnabled(aItemIsSelected && ((uniqItemSelected && !theLast) || (!uniqItemSelected)) && !allItemSelected);
467void SimpleStringListEditor::slotContextMenu(
const QPoint &pos)
470 const bool hasItemsSelected = !lstSelectedItems.
isEmpty();
473 QAction *act = menu.addAction(d->mAddButton->text(),
this, &SimpleStringListEditor::slotAdd);
476 if (d->mModifyButton && (lstSelectedItems.
count() == 1)) {
477 QAction *act = menu.addAction(d->mModifyButton->text(),
this, &SimpleStringListEditor::slotModify);
480 if (d->mRemoveButton && hasItemsSelected) {
482 QAction *act = menu.addAction(d->mRemoveButton->text(),
this, &SimpleStringListEditor::slotRemove);
485 if (!menu.isEmpty()) {
486 menu.exec(d->mListBox->mapToGlobal(
pos));
490QSize SimpleStringListEditor::sizeHint()
const
496 sh.
setHeight(d->mButtonLayout->minimumSize().height());
500#include "moc_simplestringlisteditor.cpp"
SimpleStringListEditor(QWidget *parent=nullptr, ButtonCode buttons=Unsorted, const QString &addLabel=QString(), const QString &removeLabel=QString(), const QString &modifyLabel=QString(), const QString &addDialogLabel=QString())
Constructor.
void setStringList(const QStringList &strings)
Sets the list of strings displayed to strings.
void appendStringList(const QStringList &strings)
Adds strings to the list of displayed strings.
void aboutToAdd(QString &)
Connected slots can alter the argument to be added or set the argument to QString() to suppress addin...
void setButtonText(ButtonCode button, const QString &text)
Sets the text of button button to text.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
ButtonCode warningTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
void setIcon(const QIcon &icon)
QIcon fromTheme(const QString &name)
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
const_reference at(qsizetype i) const const
qsizetype count() const const
bool isEmpty() const const
void reserve(qsizetype size)
void setText(const QString &text)
QString text() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setHeight(int height)
bool isEmpty() const const
QString trimmed() const const