simplestringlisteditor.cpp 16.3 KB
Newer Older
1 2 3 4 5 6
/*
    simplestringlisteditor.cpp

    This file is part of KMail, the KDE mail client.
    Copyright (c) 2001 Marc Mutz <mutz@kde.org>

Laurent Montel's avatar
Laurent Montel committed
7
    Copyright (C) 2013-2019 Laurent Montel <montel@kde.org>
8 9

    KMail is free software; you can redistribute it and/or modify it
10 11 12
    under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

    KMail is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include "simplestringlisteditor.h"

Laurent Montel's avatar
Laurent Montel committed
37
#include <QInputDialog>
38 39
#include <QIcon>
#include <KLocalizedString>
40
#include <KMessageBox>
41 42 43 44 45 46 47 48 49 50 51
#include "pimcommon_debug.h"
#include <QPushButton>
#include <QMenu>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QListWidget>

//********************************************************
// SimpleStringListEditor
//********************************************************
using namespace PimCommon;
Laurent Montel's avatar
Laurent Montel committed
52 53 54 55 56 57 58 59

class PimCommon::SimpleStringListEditorPrivate
{
public:
    SimpleStringListEditorPrivate()
    {
    }

Laurent Montel's avatar
Laurent Montel committed
60 61 62 63 64 65 66 67 68 69 70 71
    QList<QListWidgetItem *> selectedItems() const
    {
        QList<QListWidgetItem *> listWidgetItem;
        const int numberOfFilters = mListBox->count();
        for (int i = 0; i < numberOfFilters; ++i) {
            if (mListBox->item(i)->isSelected()) {
                listWidgetItem << mListBox->item(i);
            }
        }
        return listWidgetItem;
    }

Laurent Montel's avatar
Laurent Montel committed
72 73 74 75 76 77
    QListWidget *mListBox = nullptr;
    QPushButton *mAddButton = nullptr;
    QPushButton *mRemoveButton = nullptr;
    QPushButton *mModifyButton = nullptr;
    QPushButton *mUpButton = nullptr;
    QPushButton *mDownButton = nullptr;
Laurent Montel's avatar
Laurent Montel committed
78
    QPushButton *mCustomButton = nullptr;
Laurent Montel's avatar
Laurent Montel committed
79
    QVBoxLayout *mButtonLayout = nullptr;
Laurent Montel's avatar
Laurent Montel committed
80
    QString mAddDialogLabel;
81
    QString mRemoveDialogLabel;
Laurent Montel's avatar
Laurent Montel committed
82 83
};

Laurent Montel's avatar
Laurent Montel committed
84 85 86
SimpleStringListEditor::SimpleStringListEditor(QWidget *parent, ButtonCode buttons, const QString &addLabel, const QString &removeLabel, const QString &modifyLabel, const QString &addDialogLabel)
    : QWidget(parent)
    , d(new SimpleStringListEditorPrivate)
87 88 89 90
{
    setAddDialogLabel(addDialogLabel);
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    QHBoxLayout *hlay = new QHBoxLayout(this);
91
    hlay->setContentsMargins(0, 0, 0, 0);
92

Laurent Montel's avatar
Laurent Montel committed
93
    d->mListBox = new QListWidget(this);
94

Laurent Montel's avatar
Laurent Montel committed
95 96
    d->mListBox->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(d->mListBox, &QListWidget::customContextMenuRequested, this, &SimpleStringListEditor::slotContextMenu);
97

Laurent Montel's avatar
Laurent Montel committed
98 99
    d->mListBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
    hlay->addWidget(d->mListBox, 1);
100 101 102

    if (buttons == None) {
        qCDebug(PIMCOMMON_LOG) << "SimpleStringListBox called with no buttons."
Laurent Montel's avatar
Laurent Montel committed
103
                                  "Consider using a plain QListBox instead!";
104 105
    }

Laurent Montel's avatar
Laurent Montel committed
106 107
    d->mButtonLayout = new QVBoxLayout(); // inherits spacing
    hlay->addLayout(d->mButtonLayout);
108 109 110

    if (buttons & Add) {
        if (addLabel.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
111
            d->mAddButton = new QPushButton(i18n("&Add..."), this);
112
        } else {
Laurent Montel's avatar
Laurent Montel committed
113
            d->mAddButton = new QPushButton(addLabel, this);
114
        }
Laurent Montel's avatar
Laurent Montel committed
115 116 117
        d->mAddButton->setAutoDefault(false);
        d->mButtonLayout->addWidget(d->mAddButton);
        connect(d->mAddButton, &QPushButton::clicked, this, &SimpleStringListEditor::slotAdd);
118 119 120 121
    }

    if (buttons & Modify) {
        if (modifyLabel.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
122
            d->mModifyButton = new QPushButton(i18n("&Modify..."), this);
123
        } else {
Laurent Montel's avatar
Laurent Montel committed
124
            d->mModifyButton = new QPushButton(modifyLabel, this);
125
        }
Laurent Montel's avatar
Laurent Montel committed
126 127 128 129 130
        d->mModifyButton->setAutoDefault(false);
        d->mModifyButton->setEnabled(false);   // no selection yet
        d->mButtonLayout->addWidget(d->mModifyButton);
        connect(d->mModifyButton, &QPushButton::clicked, this, &SimpleStringListEditor::slotModify);
        connect(d->mListBox, &QListWidget::itemDoubleClicked, this, &SimpleStringListEditor::slotModify);
131 132
    }

Laurent Montel's avatar
Laurent Montel committed
133

134 135
    if (buttons & Remove) {
        if (removeLabel.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
136
            d->mRemoveButton = new QPushButton(i18n("&Remove"), this);
137
        } else {
Laurent Montel's avatar
Laurent Montel committed
138
            d->mRemoveButton = new QPushButton(removeLabel, this);
139
        }
Laurent Montel's avatar
Laurent Montel committed
140 141 142 143
        d->mRemoveButton->setAutoDefault(false);
        d->mRemoveButton->setEnabled(false);   // no selection yet
        d->mButtonLayout->addWidget(d->mRemoveButton);
        connect(d->mRemoveButton, &QPushButton::clicked, this, &SimpleStringListEditor::slotRemove);
144 145 146 147 148
    }

    if (buttons & Up) {
        if (!(buttons & Down)) {
            qCDebug(PIMCOMMON_LOG) << "Are you sure you want to use an Up button"
Laurent Montel's avatar
Laurent Montel committed
149
                                      "without a Down button??";
150
        }
Laurent Montel's avatar
Laurent Montel committed
151 152 153 154 155 156
        d->mUpButton = new QPushButton(QString(), this);
        d->mUpButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
        d->mUpButton->setAutoDefault(false);
        d->mUpButton->setEnabled(false);   // no selection yet
        d->mButtonLayout->addWidget(d->mUpButton);
        connect(d->mUpButton, &QPushButton::clicked, this, &SimpleStringListEditor::slotUp);
157 158 159 160 161
    }

    if (buttons & Down) {
        if (!(buttons & Up)) {
            qCDebug(PIMCOMMON_LOG) << "Are you sure you want to use a Down button"
Laurent Montel's avatar
Laurent Montel committed
162
                                      "without an Up button??";
163
        }
Laurent Montel's avatar
Laurent Montel committed
164 165 166 167 168 169
        d->mDownButton = new QPushButton(QString(), this);
        d->mDownButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
        d->mDownButton->setAutoDefault(false);
        d->mDownButton->setEnabled(false);   // no selection yet
        d->mButtonLayout->addWidget(d->mDownButton);
        connect(d->mDownButton, &QPushButton::clicked, this, &SimpleStringListEditor::slotDown);
170 171
    }

Laurent Montel's avatar
Laurent Montel committed
172 173 174 175 176 177 178
    if (buttons & Custom) {
        d->mCustomButton = new QPushButton(i18n("&Customize..."), this);
        d->mCustomButton->setAutoDefault(false);
        d->mButtonLayout->addWidget(d->mCustomButton);
        connect(d->mCustomButton, &QPushButton::clicked, this, &SimpleStringListEditor::slotCustomize);
    }

Laurent Montel's avatar
Laurent Montel committed
179
    d->mButtonLayout->addStretch(1);   // spacer
180

Laurent Montel's avatar
Laurent Montel committed
181 182 183 184 185 186 187
    connect(d->mListBox, &QListWidget::currentItemChanged, this, &SimpleStringListEditor::slotSelectionChanged);
    connect(d->mListBox, &QListWidget::itemSelectionChanged, this, &SimpleStringListEditor::slotSelectionChanged);
}

SimpleStringListEditor::~SimpleStringListEditor()
{
    delete d;
188 189 190 191
}

void SimpleStringListEditor::setUpDownAutoRepeat(bool b)
{
Laurent Montel's avatar
Laurent Montel committed
192 193
    if (d->mUpButton) {
        d->mUpButton->setAutoRepeat(b);
194
    }
Laurent Montel's avatar
Laurent Montel committed
195 196
    if (d->mDownButton) {
        d->mDownButton->setAutoRepeat(b);
197 198 199 200 201
    }
}

void SimpleStringListEditor::setStringList(const QStringList &strings)
{
Laurent Montel's avatar
Laurent Montel committed
202 203
    d->mListBox->clear();
    d->mListBox->addItems(strings);
204 205 206 207
}

void SimpleStringListEditor::appendStringList(const QStringList &strings)
{
Laurent Montel's avatar
Laurent Montel committed
208
    d->mListBox->addItems(strings);
209 210 211 212 213
}

QStringList SimpleStringListEditor::stringList() const
{
    QStringList result;
Laurent Montel's avatar
Laurent Montel committed
214
    const int numberOfItem(d->mListBox->count());
215 216
    result.reserve(numberOfItem);
    for (int i = 0; i < numberOfItem; ++i) {
Laurent Montel's avatar
Laurent Montel committed
217
        result << (d->mListBox->item(i)->text());
218 219 220 221 222 223
    }
    return result;
}

bool SimpleStringListEditor::containsString(const QString &str)
{
Laurent Montel's avatar
Laurent Montel committed
224
    const int numberOfItem(d->mListBox->count());
225
    for (int i = 0; i < numberOfItem; ++i) {
Laurent Montel's avatar
Laurent Montel committed
226
        if (d->mListBox->item(i)->text() == str) {
227 228 229 230 231 232 233 234 235 236
            return true;
        }
    }
    return false;
}

void SimpleStringListEditor::setButtonText(ButtonCode button, const QString &text)
{
    switch (button) {
    case Add:
Laurent Montel's avatar
Laurent Montel committed
237
        if (!d->mAddButton) {
238 239
            break;
        }
Laurent Montel's avatar
Laurent Montel committed
240
        d->mAddButton->setText(text);
241 242
        return;
    case Remove:
Laurent Montel's avatar
Laurent Montel committed
243
        if (!d->mRemoveButton) {
244 245
            break;
        }
Laurent Montel's avatar
Laurent Montel committed
246
        d->mRemoveButton->setText(text);
247 248
        return;
    case Modify:
Laurent Montel's avatar
Laurent Montel committed
249
        if (!d->mModifyButton) {
250 251
            break;
        }
Laurent Montel's avatar
Laurent Montel committed
252
        d->mModifyButton->setText(text);
253
        return;
Laurent Montel's avatar
Laurent Montel committed
254 255 256 257 258 259
    case Custom:
        if (!d->mCustomButton) {
            break;
        }
        d->mCustomButton->setText(text);
        return;
260 261 262
    case Up:
    case Down:
        qCDebug(PIMCOMMON_LOG) << "SimpleStringListEditor: Cannot change text of"
Laurent Montel's avatar
Laurent Montel committed
263
                                  "Up and Down buttons: they don't contains text!";
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        return;
    default:
        if (button & All) {
            qCDebug(PIMCOMMON_LOG) << "No such button!";
        } else {
            qCDebug(PIMCOMMON_LOG) << "Can only set text for one button at a time!";
        }
        return;
    }

    qCDebug(PIMCOMMON_LOG) << "The requested button has not been created!";
}

void SimpleStringListEditor::addNewEntry()
{
    bool ok = false;
    const QString newEntry = QInputDialog::getText(this, i18n("New Value"),
Laurent Montel's avatar
Laurent Montel committed
281 282
                                                   d->mAddDialogLabel, QLineEdit::Normal, QString(),
                                                   &ok);
283 284 285 286 287 288 289 290 291 292 293
    if (ok && !newEntry.trimmed().isEmpty()) {
        insertNewEntry(newEntry);
    }
}

void SimpleStringListEditor::insertNewEntry(const QString &entry)
{
    QString newEntry = entry;
    // let the user verify the string before adding
    Q_EMIT aboutToAdd(newEntry);
    if (!newEntry.isEmpty() && !containsString(newEntry)) {
Laurent Montel's avatar
Laurent Montel committed
294
        d->mListBox->addItem(newEntry);
295 296 297 298 299 300 301 302 303 304
        slotSelectionChanged();
        Q_EMIT changed();
    }
}

void SimpleStringListEditor::slotAdd()
{
    addNewEntry();
}

Laurent Montel's avatar
Laurent Montel committed
305 306 307 308 309 310 311 312 313 314
void SimpleStringListEditor::slotCustomize()
{
    customEntry();
}

void SimpleStringListEditor::customEntry()
{
    //TODO
}

315 316
void SimpleStringListEditor::slotRemove()
{
Laurent Montel's avatar
Laurent Montel committed
317
    const QList<QListWidgetItem *> selectedItems = d->mListBox->selectedItems();
318 319 320
    if (selectedItems.isEmpty()) {
        return;
    }
321
    if (KMessageBox::Yes == KMessageBox::warningYesNo(this, d->mRemoveDialogLabel, i18n("Remove"))) {
322 323 324 325 326
        for (QListWidgetItem *item : selectedItems) {
            delete d->mListBox->takeItem(d->mListBox->row(item));
        }
        slotSelectionChanged();
        Q_EMIT changed();
327 328 329 330 331 332 333
    }
}

QString SimpleStringListEditor::modifyEntry(const QString &text)
{
    bool ok = false;
    QString newText = QInputDialog::getText(this, i18n("Change Value"),
Laurent Montel's avatar
Laurent Montel committed
334
                                            d->mAddDialogLabel, QLineEdit::Normal, text,
335 336 337 338 339 340 341 342 343 344 345 346
                                            &ok);
    Q_EMIT aboutToAdd(newText);

    if (!ok || newText.trimmed().isEmpty() || newText == text) {
        return QString();
    }

    return newText;
}

void SimpleStringListEditor::slotModify()
{
Laurent Montel's avatar
Laurent Montel committed
347
    QListWidgetItem *item = d->mListBox->currentItem();
348 349 350 351 352 353 354 355 356 357
    if (!item) {
        return;
    }
    const QString newText = modifyEntry(item->text());
    if (!newText.isEmpty()) {
        item->setText(newText);
        Q_EMIT changed();
    }
}

358 359 360 361 362
void SimpleStringListEditor::setRemoveDialogLabel(const QString &removeDialogLabel)
{
    d->mRemoveDialogLabel = removeDialogLabel.isEmpty() ? i18n("Do you want to remove selected text?") : removeDialogLabel;
}

363 364
void SimpleStringListEditor::setAddDialogLabel(const QString &addDialogLabel)
{
Laurent Montel's avatar
Laurent Montel committed
365
    d->mAddDialogLabel = addDialogLabel.isEmpty() ? i18n("New entry:") : addDialogLabel;
366 367 368 369
}

void SimpleStringListEditor::slotUp()
{
Laurent Montel's avatar
Laurent Montel committed
370
    QList<QListWidgetItem *> listWidgetItem = d->selectedItems();
371 372 373 374 375
    if (listWidgetItem.isEmpty()) {
        return;
    }

    const int numberOfItem(listWidgetItem.count());
Laurent Montel's avatar
Laurent Montel committed
376
    const int currentRow = d->mListBox->currentRow();
377 378 379 380 381 382 383
    if ((numberOfItem == 1) && (currentRow == 0)) {
        qCDebug(PIMCOMMON_LOG) << "Called while the _topmost_ filter is selected, ignoring.";
        return;
    }
    bool wasMoved = false;

    for (int i = 0; i < numberOfItem; ++i) {
Laurent Montel's avatar
Laurent Montel committed
384
        const int posItem = d->mListBox->row(listWidgetItem.at(i));
385 386 387
        if (posItem == i) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
388 389
        QListWidgetItem *item = d->mListBox->takeItem(posItem);
        d->mListBox->insertItem(posItem - 1, item);
390 391 392 393 394

        wasMoved = true;
    }
    if (wasMoved) {
        Q_EMIT changed();
Laurent Montel's avatar
Laurent Montel committed
395
        d->mListBox->setCurrentRow(currentRow - 1);
396 397 398 399 400
    }
}

void SimpleStringListEditor::slotDown()
{
Laurent Montel's avatar
Laurent Montel committed
401
    QList<QListWidgetItem *> listWidgetItem = d->selectedItems();
402 403 404 405
    if (listWidgetItem.isEmpty()) {
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
406
    const int numberOfElement(d->mListBox->count());
407
    const int numberOfItem(listWidgetItem.count());
Laurent Montel's avatar
Laurent Montel committed
408
    const int currentRow = d->mListBox->currentRow();
409 410 411 412 413 414 415 416
    if ((numberOfItem == 1) && (currentRow == numberOfElement - 1)) {
        qCDebug(PIMCOMMON_LOG) << "Called while the _last_ filter is selected, ignoring.";
        return;
    }

    int j = 0;
    bool wasMoved = false;
    for (int i = numberOfItem - 1; i >= 0; --i, j++) {
Laurent Montel's avatar
Laurent Montel committed
417
        const int posItem = d->mListBox->row(listWidgetItem.at(i));
418 419 420
        if (posItem == (numberOfElement - 1 - j)) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
421 422
        QListWidgetItem *item = d->mListBox->takeItem(posItem);
        d->mListBox->insertItem(posItem + 1, item);
423 424 425 426
        wasMoved = true;
    }
    if (wasMoved) {
        Q_EMIT changed();
Laurent Montel's avatar
Laurent Montel committed
427
        d->mListBox->setCurrentRow(currentRow + 1);
428 429 430 431 432
    }
}

void SimpleStringListEditor::slotSelectionChanged()
{
Laurent Montel's avatar
Laurent Montel committed
433
    const QList<QListWidgetItem *> lstSelectedItems = d->mListBox->selectedItems();
434 435
    const int numberOfItemSelected(lstSelectedItems.count());
    const bool uniqItemSelected = (numberOfItemSelected == 1);
Laurent Montel's avatar
Laurent Montel committed
436
    const bool aItemIsSelected = !lstSelectedItems.isEmpty();
437 438
    // if there is one, item will be non-null (ie. true), else 0
    // (ie. false):
Laurent Montel's avatar
Laurent Montel committed
439
    if (d->mRemoveButton) {
Laurent Montel's avatar
Laurent Montel committed
440
        d->mRemoveButton->setEnabled(aItemIsSelected);
441 442
    }

Laurent Montel's avatar
Laurent Montel committed
443 444
    if (d->mModifyButton) {
        d->mModifyButton->setEnabled(uniqItemSelected);
445 446
    }

Laurent Montel's avatar
Laurent Montel committed
447
    const int currentIndex = d->mListBox->currentRow();
448

Laurent Montel's avatar
Laurent Montel committed
449 450
    const bool allItemSelected = (d->mListBox->count() == numberOfItemSelected);
    const bool theLast = (currentIndex >= d->mListBox->count() - 1);
451 452
    const bool theFirst = (currentIndex == 0);

Laurent Montel's avatar
Laurent Montel committed
453
    if (d->mUpButton) {
Laurent Montel's avatar
Laurent Montel committed
454 455
        d->mUpButton->setEnabled(aItemIsSelected && ((uniqItemSelected && !theFirst)
                                                     || (!uniqItemSelected)) && !allItemSelected);
456
    }
Laurent Montel's avatar
Laurent Montel committed
457
    if (d->mDownButton) {
Laurent Montel's avatar
Laurent Montel committed
458 459 460
        d->mDownButton->setEnabled(aItemIsSelected
                                   && ((uniqItemSelected && !theLast)
                                       || (!uniqItemSelected)) && !allItemSelected);
461 462 463 464 465
    }
}

void SimpleStringListEditor::slotContextMenu(const QPoint &pos)
{
Laurent Montel's avatar
Laurent Montel committed
466
    QList<QListWidgetItem *> lstSelectedItems = d->mListBox->selectedItems();
467
    const bool hasItemsSelected = !lstSelectedItems.isEmpty();
Laurent Montel's avatar
Laurent Montel committed
468
    QMenu menu(this);
Laurent Montel's avatar
Laurent Montel committed
469
    if (d->mAddButton) {
Laurent Montel's avatar
Laurent Montel committed
470 471
        QAction *act = menu.addAction(d->mAddButton->text(), this, &SimpleStringListEditor::slotAdd);
        act->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
472
    }
Laurent Montel's avatar
Laurent Montel committed
473
    if (d->mModifyButton && (lstSelectedItems.count() == 1)) {
Laurent Montel's avatar
Laurent Montel committed
474 475
        QAction *act = menu.addAction(d->mModifyButton->text(), this, &SimpleStringListEditor::slotModify);
        act->setIcon(QIcon::fromTheme(QStringLiteral("document-edit")));
476
    }
Laurent Montel's avatar
Laurent Montel committed
477
    if (d->mRemoveButton && hasItemsSelected) {
Laurent Montel's avatar
Laurent Montel committed
478
        menu.addSeparator();
Laurent Montel's avatar
Laurent Montel committed
479 480
        QAction *act = menu.addAction(d->mRemoveButton->text(), this, &SimpleStringListEditor::slotRemove);
        act->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
Laurent Montel's avatar
Laurent Montel committed
481 482 483
    }
    if (!menu.isEmpty()) {
        menu.exec(d->mListBox->mapToGlobal(pos));
484 485 486 487 488 489 490 491 492
    }
}

QSize SimpleStringListEditor::sizeHint() const
{
    // Override height because we want the widget to be tall enough to fit the
    // button columns, but we want to allow it to be made smaller than list
    // sizeHint().height()
    QSize sh = QWidget::sizeHint();
Laurent Montel's avatar
Laurent Montel committed
493
    sh.setHeight(d->mButtonLayout->minimumSize().height());
494 495
    return sh;
}