File indexing completed on 2022-10-04 17:30:20

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "numberdialog.h"
0005 
0006 #include <QDialogButtonBox>
0007 #include <QVBoxLayout>
0008 
0009 NumberDialog::NumberDialog(int start, int step, bool reset, const QList<int> &skip, QWidget *parent)
0010     : QDialog(parent)
0011 {
0012     QVBoxLayout *layout = new QVBoxLayout(this);
0013     QWidget     *widget = new QWidget(this);
0014 
0015     m_widget.setupUi(widget);
0016     m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
0017 
0018     layout->addWidget(widget);
0019     layout->addWidget(m_buttons);
0020 
0021     connect(m_buttons, &QDialogButtonBox::accepted,
0022             this, &NumberDialog::accept);
0023     connect(m_buttons, &QDialogButtonBox::rejected,
0024             this, &NumberDialog::reject);
0025 
0026     connect(m_widget.buttonAdd, &QPushButton::clicked,
0027             this, &NumberDialog::slotAddNumber);
0028     connect(m_widget.buttonRemove, &QPushButton::clicked, this,
0029             &NumberDialog::slotRemoveNumber);
0030     connect(m_widget.listSkip, &QListWidget::itemSelectionChanged,
0031             this, &NumberDialog::slotEnableControls);
0032     m_widget.spinStart->setValue(start);
0033     m_widget.spinStep->setValue(step);
0034     m_widget.checkReset->setChecked(reset);
0035     m_widget.listSkip->setSortingEnabled(true);   // TODO: sort numeric
0036 
0037     QList<int>::ConstIterator it = skip.constBegin();
0038     while (it != skip.constEnd()) {
0039         m_widget.listSkip->addItem(QString::number(*it));
0040         ++it;
0041     }
0042 }
0043 
0044 QList<int> NumberDialog::skipNumbers() const
0045 {
0046     QList<int> list;
0047 
0048     for (int i = 0; i < m_widget.listSkip->count(); i++) {
0049         QListWidgetItem *item = m_widget.listSkip->item(i);
0050         list.append(item->data(Qt::DisplayRole).toInt());
0051 
0052     }
0053     return list;
0054 }
0055 
0056 void NumberDialog::slotEnableControls()
0057 {
0058     QList<QListWidgetItem *> selected = m_widget.listSkip->selectedItems();
0059     m_widget.buttonRemove->setEnabled(selected.size());
0060 }
0061 
0062 void NumberDialog::slotAddNumber()
0063 {
0064     m_widget.listSkip->addItem(QString::number(m_widget.spinNumber->value()));
0065 }
0066 
0067 void NumberDialog::slotRemoveNumber()
0068 {
0069     delete m_widget.listSkip->takeItem(m_widget.listSkip->currentRow());
0070 }