11#include "keditlistwidget.h"
13#include <QApplication>
21#include <QStringListModel>
24class KEditListWidgetPrivate
27 KEditListWidgetPrivate(KEditListWidget *parent)
31 QListView *listView =
nullptr;
32 QPushButton *servUpButton =
nullptr;
33 QPushButton *servDownButton =
nullptr;
34 QPushButton *servNewButton =
nullptr;
35 QPushButton *servRemoveButton =
nullptr;
36 QLineEdit *lineEdit =
nullptr;
37 QWidget *editingWidget =
nullptr;
38 QVBoxLayout *mainLayout =
nullptr;
39 QVBoxLayout *btnsLayout =
nullptr;
40 QStringListModel *model =
nullptr;
45 void init(
bool check =
false,
KEditListWidget::Buttons buttons = KEditListWidget::All, QWidget *representationWidget =
nullptr);
46 void setEditor(QLineEdit *lineEdit, QWidget *representationWidget =
nullptr);
47 void updateButtonState();
48 QModelIndex selectedIndex();
51 KEditListWidget *
const q;
56 checkAtEntering = check;
58 servNewButton = servRemoveButton = servUpButton = servDownButton =
nullptr;
61 mainLayout =
new QVBoxLayout(q);
62 mainLayout->setContentsMargins(0, 0, 0, 0);
64 QHBoxLayout *subLayout =
new QHBoxLayout;
65 btnsLayout =
new QVBoxLayout;
66 btnsLayout->addStretch();
68 model =
new QStringListModel(q);
69 listView =
new QListView(q);
70 listView->setModel(model);
75 mainLayout->addLayout(subLayout);
77 setEditor(lineEdit, representationWidget);
80 q->setButtons(newButtons);
85void KEditListWidgetPrivate::setEditor(
QLineEdit *newLineEdit,
QWidget *representationWidget)
87 if (editingWidget != lineEdit && editingWidget != representationWidget) {
90 if (lineEdit != newLineEdit) {
93 lineEdit = newLineEdit ? newLineEdit :
new QLineEdit(q);
94 editingWidget = representationWidget ? representationWidget : lineEdit;
96 if (representationWidget) {
100 mainLayout->insertWidget(0, editingWidget);
108 q->typedSomething(lineEdit->text());
111 q->setTabOrder(editingWidget, listView);
112 QWidget *w = listView;
114 q->setTabOrder(w, servNewButton);
117 if (servRemoveButton) {
118 q->setTabOrder(w, servRemoveButton);
119 w = servRemoveButton;
122 q->setTabOrder(w, servUpButton);
125 if (servDownButton) {
126 q->setTabOrder(w, servDownButton);
131void KEditListWidgetPrivate::updateButtonState()
133 const bool hasSelectedItem = selectedIndex().isValid();
136 QPushButton *
const buttons[3] = {servUpButton, servDownButton, servRemoveButton};
138 for (QPushButton *button : buttons) {
141 if (!hasSelectedItem && button->hasFocus()) {
144 button->setEnabled(hasSelectedItem);
151 QItemSelectionModel *selection = listView->selectionModel();
153 if (!selectedIndexes.isEmpty() && selectedIndexes[0].isValid()) {
154 return selectedIndexes[0];
156 return QModelIndex();
160class KEditListWidgetCustomEditorPrivate
163 KEditListWidgetCustomEditorPrivate(KEditListWidget::CustomEditor *qq)
165 , representationWidget(nullptr)
170 KEditListWidget::CustomEditor *q;
171 QWidget *representationWidget;
175KEditListWidget::CustomEditor::CustomEditor()
176 : d(new KEditListWidgetCustomEditorPrivate(this))
180KEditListWidget::CustomEditor::CustomEditor(
QWidget *repWidget,
QLineEdit *edit)
181 : d(new KEditListWidgetCustomEditorPrivate(this))
183 d->representationWidget = repWidget;
187KEditListWidget::CustomEditor::CustomEditor(
QComboBox *combo)
188 : d(new KEditListWidgetCustomEditorPrivate(this))
190 d->representationWidget = combo;
192 Q_ASSERT(d->lineEdit);
195KEditListWidget::CustomEditor::~CustomEditor() =
default;
197void KEditListWidget::CustomEditor::setRepresentationWidget(
QWidget *repWidget)
199 d->representationWidget = repWidget;
202void KEditListWidget::CustomEditor::setLineEdit(QLineEdit *edit)
207QWidget *KEditListWidget::CustomEditor::representationWidget()
const
209 return d->representationWidget;
212QLineEdit *KEditListWidget::CustomEditor::lineEdit()
const
219 , d(new KEditListWidgetPrivate(this))
226 , d(new KEditListWidgetPrivate(this))
228 d->lineEdit = custom.lineEdit();
229 d->init(checkAtEntering, buttons, custom.representationWidget());
232KEditListWidget::~KEditListWidget() =
default;
236 d->setEditor(editor.lineEdit(), editor.representationWidget());
251 return d->servNewButton;
256 return d->servRemoveButton;
261 return d->servUpButton;
266 return d->servDownButton;
271 return int(d->model->rowCount());
276 if (d->buttons == buttons) {
280 if ((buttons & Add) && !d->servNewButton) {
282 d->servNewButton->setEnabled(
false);
283 d->servNewButton->show();
286 d->btnsLayout->insertWidget(0, d->servNewButton);
287 }
else if ((buttons & Add) == 0 && d->servNewButton) {
288 delete d->servNewButton;
289 d->servNewButton =
nullptr;
292 if ((buttons & Remove) && !d->servRemoveButton) {
294 d->servRemoveButton->setEnabled(
false);
295 d->servRemoveButton->show();
298 d->btnsLayout->insertWidget(1, d->servRemoveButton);
299 }
else if ((buttons & Remove) == 0 && d->servRemoveButton) {
300 delete d->servRemoveButton;
301 d->servRemoveButton =
nullptr;
304 if ((buttons & UpDown) && !d->servUpButton) {
306 d->servUpButton->setEnabled(
false);
307 d->servUpButton->show();
311 d->servDownButton->setEnabled(
false);
312 d->servDownButton->show();
315 d->btnsLayout->insertWidget(2, d->servUpButton);
316 d->btnsLayout->insertWidget(3, d->servDownButton);
317 }
else if ((buttons & UpDown) == 0 && d->servUpButton) {
318 delete d->servUpButton;
319 d->servUpButton =
nullptr;
320 delete d->servDownButton;
321 d->servDownButton =
nullptr;
324 d->buttons = buttons;
329 d->checkAtEntering = check;
332bool KEditListWidget::checkAtEntering()
334 return d->checkAtEntering;
337void KEditListWidget::typedSomething(
const QString &text)
344 bool block = d->listView->signalsBlocked();
345 d->listView->blockSignals(
true);
346 QModelIndex currentIndex = d->selectedIndex();
348 d->model->setData(currentIndex,
text);
350 d->listView->blockSignals(block);
355 if (!d->servNewButton) {
359 if (!d->lineEdit->hasAcceptableInput()) {
360 d->servNewButton->setEnabled(
false);
364 if (!d->checkAtEntering) {
365 d->servNewButton->setEnabled(!
text.isEmpty());
367 if (
text.isEmpty()) {
368 d->servNewButton->setEnabled(
false);
370 QStringList
list = d->model->stringList();
372 d->servNewButton->setEnabled(enable);
377void KEditListWidget::moveItemUp()
379 if (!d->listView->isEnabled()) {
384 QModelIndex index = d->selectedIndex();
386 if (index.
row() == 0) {
391 QModelIndex aboveIndex = d->model->index(index.
row() - 1, index.
column());
395 d->model->setData(index, tmp);
404void KEditListWidget::moveItemDown()
406 if (!d->listView->isEnabled()) {
411 QModelIndex index = d->selectedIndex();
413 if (index.
row() == d->model->rowCount() - 1) {
418 QModelIndex belowIndex = d->model->index(index.
row() + 1, index.
column());
422 d->model->setData(index, tmp);
431void KEditListWidget::addItem()
436 if (!d->servNewButton || !d->servNewButton->isEnabled()) {
440 QModelIndex currentIndex = d->selectedIndex();
442 const QString ¤tTextLE = d->lineEdit->text();
443 bool alreadyInList(
false);
445 if (!d->checkAtEntering) {
448 if (d->model->data(currentIndex,
Qt::DisplayRole).toString() == currentTextLE) {
449 alreadyInList =
true;
452 alreadyInList = d->model->stringList().contains(currentTextLE,
Qt::CaseSensitive);
455 if (d->servNewButton) {
461 if (d->servNewButton->hasFocus()) {
464 d->servNewButton->setEnabled(
false);
467 bool block = d->lineEdit->signalsBlocked();
468 d->lineEdit->blockSignals(
true);
469 d->lineEdit->clear();
470 d->lineEdit->blockSignals(block);
474 if (!alreadyInList) {
475 block = d->listView->signalsBlocked();
478 d->model->setData(currentIndex, currentTextLE);
481 lst << currentTextLE;
482 lst << d->model->stringList();
483 d->model->setStringList(lst);
489 d->updateButtonState();
496 return selectedIndex.
row();
502void KEditListWidget::removeItem()
509 if (currentIndex.
row() >= 0) {
514 if (d->servRemoveButton && d->servRemoveButton->hasFocus()) {
518 QString removedText = d->model->data(currentIndex,
Qt::DisplayRole).toString();
520 d->model->removeRows(currentIndex.
row(), 1);
522 d->listView->selectionModel()->clear();
529 d->updateButtonState();
534 int index = newIndex.
row();
541 bool moveEnabled = d->servUpButton && d->servDownButton;
544 if (d->model->rowCount() <= 1) {
545 d->servUpButton->setEnabled(
false);
546 d->servDownButton->setEnabled(
false);
547 }
else if (index == (d->model->rowCount() - 1)) {
548 d->servUpButton->setEnabled(
true);
549 d->servDownButton->setEnabled(
false);
550 }
else if (index == 0) {
551 d->servUpButton->setEnabled(
false);
552 d->servDownButton->setEnabled(
true);
554 d->servUpButton->setEnabled(
true);
555 d->servDownButton->setEnabled(
true);
559 if (d->servRemoveButton) {
560 d->servRemoveButton->setEnabled(
true);
566 d->lineEdit->clear();
577 for (
int i = 0, j = index; i < list.
count(); ++i, ++j) {
578 content.
insert(j, list[i]);
582 d->model->setStringList(content);
595 d->model->setStringList(list);
617 return d->model->stringList();
622 d->model->setStringList(items);
632 d->updateButtonState();
645 return ((
QObject *)d->listView)->event(e);
654#include "moc_keditlistwidget.cpp"
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void addLayout(QLayout *layout, int stretch)
QLineEdit * lineEdit() const const
QIcon fromTheme(const QString &name)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void textChanged(const QString &text)
void append(QList< T > &&value)
qsizetype count() const const
iterator insert(const_iterator before, parameter_type value)
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void installEventFilter(QObject *filterObj)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const