File indexing completed on 2024-04-28 09:45:39

0001 //  SPDX-FileCopyrightText: 1998-2005 Matthias Hoelzer <hoelzer@physik.uni-wuerzburg.de>
0002 //  SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "klistboxdialog.h"
0005 
0006 #include <QLabel>
0007 #include <QVBoxLayout>
0008 #include <QDialogButtonBox>
0009 
0010 KListBoxDialog::KListBoxDialog(const QString &text, QWidget *parent)
0011     : QDialog(parent)
0012 {
0013     setModal(true);
0014     auto vLayout = new QVBoxLayout(this);
0015 
0016     label = new QLabel(text, this);
0017     vLayout->addWidget(label);
0018     label->setAlignment(Qt::AlignCenter);
0019 
0020     table = new QListWidget(this);
0021     vLayout->addWidget(table);
0022     table->setFocus();
0023 
0024     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0025     vLayout->addWidget(buttonBox);
0026 
0027     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0028     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0029 }
0030 
0031 void KListBoxDialog::insertItem(const QString &item)
0032 {
0033     table->addItem(item);
0034     table->setCurrentItem(nullptr);
0035 }
0036 
0037 void KListBoxDialog::setCurrentItem(const QString &item)
0038 {
0039     for (int i = 0; i < table->count(); ++i) {
0040         if (table->item(i)->text() == item) {
0041             table->setCurrentItem(table->item(i));
0042             break;
0043         }
0044     }
0045 }
0046 
0047 int KListBoxDialog::currentItem() const
0048 {
0049     return table->currentRow();
0050 }
0051 
0052 #include "moc_klistboxdialog.cpp"