File indexing completed on 2024-07-14 09:00:23

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andrey Batyiev <batyiev@gmail.com>
0003     SPDX-FileCopyrightText: 2015 Jan Grulich <jgrulich@redhat.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "editlistdialog.h"
0009 
0010 #include <QDialogButtonBox>
0011 #include <QLayout>
0012 
0013 #include <KLineEdit>
0014 
0015 EditListDialog::EditListDialog(QWidget *parent)
0016     : QDialog(parent)
0017     , editListWidget(new KEditListWidget(this))
0018 {
0019     editListWidget->setCheckAtEntering(true);
0020 
0021     auto buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0022     connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
0023     connect(buttons, &QDialogButtonBox::accepted, this, &EditListDialog::dialogAccepted);
0024     connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
0025 
0026     setLayout(new QVBoxLayout);
0027     layout()->addWidget(editListWidget);
0028     layout()->addWidget(buttons);
0029 }
0030 
0031 EditListDialog::~EditListDialog() = default;
0032 
0033 void EditListDialog::setItems(const QStringList &items)
0034 {
0035     editListWidget->setItems(items);
0036 }
0037 
0038 QStringList EditListDialog::items() const
0039 {
0040     return editListWidget->items();
0041 }
0042 
0043 void EditListDialog::removeEmptyItems(QStringList &list)
0044 {
0045     QStringList::iterator it = list.begin();
0046     const QStringList::iterator end = list.end();
0047     while (it != end) {
0048         if ((*it).trimmed().isEmpty()) {
0049             it = list.erase(it);
0050         } else {
0051             ++it;
0052         }
0053     }
0054 }
0055 
0056 void EditListDialog::dialogAccepted()
0057 {
0058     QStringList list = items();
0059     removeEmptyItems(list);
0060     Q_EMIT itemsEdited(list);
0061 }
0062 
0063 void EditListDialog::setValidator(const QValidator *validator)
0064 {
0065     editListWidget->lineEdit()->setValidator(validator);
0066 }
0067 
0068 const QValidator *EditListDialog::validator() const
0069 {
0070     return editListWidget->lineEdit()->validator();
0071 }
0072 
0073 void EditListDialog::setToolTip(const QString &toolTip)
0074 {
0075     editListWidget->lineEdit()->setToolTip(toolTip);
0076 }
0077 
0078 #include "moc_editlistdialog.cpp"