File indexing completed on 2024-05-12 05:09:29

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "configwidget.h"
0026 
0027 #include <KLocalizedString>
0028 #include <KAcceleratorManager>
0029 #include <KConfigGroup>
0030 
0031 #include <QGroupBox>
0032 #include <QHBoxLayout>
0033 
0034 using Tellico::Fetch::ConfigWidget;
0035 
0036 ConfigWidget::ConfigWidget(QWidget* parent_) : QWidget(parent_), m_modified(false), m_accepted(false) {
0037   QHBoxLayout* boxLayout = new QHBoxLayout(this);
0038   boxLayout->setSpacing(10);
0039   boxLayout->setMargin(0);
0040 
0041   QGroupBox* gvbox = new QGroupBox(i18n("Source Options"), this);
0042   boxLayout->addWidget(gvbox, 10 /*stretch*/);
0043 
0044   QVBoxLayout* vbox = new QVBoxLayout();
0045   m_optionsWidget = new QWidget(gvbox);
0046   vbox->addWidget(m_optionsWidget);
0047   vbox->addStretch(1);
0048   gvbox->setLayout(vbox);
0049 }
0050 
0051 bool ConfigWidget::shouldSave() const {
0052   return m_modified && m_accepted;
0053 }
0054 
0055 void ConfigWidget::setAccepted(bool accepted_) {
0056   m_accepted = accepted_;
0057 }
0058 
0059 void ConfigWidget::slotSetModified() {
0060   m_modified = true;
0061 }
0062 
0063 void ConfigWidget::addFieldsWidget(const Tellico::StringHash& customFields_, const QStringList& fieldsToAdd_) {
0064   if(customFields_.isEmpty()) {
0065     return;
0066   }
0067 
0068   QGroupBox* gbox = new QGroupBox(i18n("Available Fields"), this);
0069   static_cast<QBoxLayout*>(layout())->addWidget(gbox);
0070 
0071   QVBoxLayout* vbox = new QVBoxLayout();
0072   for(StringHash::ConstIterator it = customFields_.begin(); it != customFields_.end(); ++it) {
0073     QCheckBox* cb = new QCheckBox(it.value(), gbox);
0074     m_fields.insert(it.key(), cb);
0075     if(fieldsToAdd_.contains(it.key())) {
0076       cb->setChecked(true);
0077     }
0078     connect(cb, &QAbstractButton::clicked, this, &ConfigWidget::slotSetModified);
0079     vbox->addWidget(cb);
0080   }
0081   vbox->addStretch(1);
0082   gbox->setLayout(vbox);
0083 
0084   KAcceleratorManager::manage(this);
0085 }
0086 
0087 void ConfigWidget::saveConfig(KConfigGroup& config_) {
0088   QStringList fields;
0089   QHash<QString, QCheckBox*>::const_iterator it = m_fields.constBegin();
0090   for( ; it != m_fields.constEnd(); ++it) {
0091     if(it.value()->isChecked()) {
0092       fields << it.key();
0093     }
0094   }
0095   config_.writeEntry(QStringLiteral("Custom Fields"), fields);
0096   saveConfigHook(config_);
0097   m_modified = false;
0098 }
0099 
0100 QWidget* ConfigWidget::optionsWidget() {
0101   return m_optionsWidget;
0102 }