File indexing completed on 2024-04-14 05:39:33

0001 /*
0002  * Copyright (C) 2010-2018 Daniel Nicoletti <dantti12@gmail.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB. If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "DebconfMultiselect.h"
0021 
0022 #include <QStandardItemModel>
0023 #include <QStandardItem>
0024 
0025 using namespace DebconfKde;
0026 
0027 DebconfMultiselect::DebconfMultiselect(const QString &name, QWidget *parent)
0028  : DebconfElement(name, parent)
0029 {
0030     setupUi(this);
0031 
0032     m_model = new QStandardItemModel(this);
0033     multiselectLV->setModel(m_model);
0034 }
0035 
0036 DebconfMultiselect::~DebconfMultiselect()
0037 {
0038 }
0039 
0040 QString DebconfMultiselect::value() const
0041 {
0042     QStringList checked;
0043     for (int i = 0; i < m_model->rowCount(); i++) {
0044         int state;
0045         state = m_model->data(m_model->index(i, 0), Qt::CheckStateRole).toInt();
0046         if (state == Qt::Checked) {
0047             checked << m_model->data(m_model->index(i, 0), Qt::DisplayRole).toString();
0048         }
0049     }
0050     return checked.join(QLatin1String(", "));
0051 }
0052 
0053 void DebconfMultiselect::setMultiselect(const QString &extended_description,
0054                                         const QString &description,
0055                                         const QStringList &default_choices,
0056                                         const QStringList &choices)
0057 {
0058     extendedDescriptionL->setText(extended_description);
0059     descriptionL->setText(description);
0060 
0061     m_model->clear();
0062     for (const QString &choice : choices) {
0063         auto item = new QStandardItem(choice);
0064         item->setSelectable(false);
0065         item->setCheckable(true);
0066         if (default_choices.contains(choice)) {
0067             item->setCheckState(Qt::Checked);
0068         } else {
0069             item->setCheckState(Qt::Unchecked);
0070         }
0071         m_model->appendRow(item);
0072     }
0073 }
0074 
0075 #include "moc_DebconfMultiselect.cpp"