File indexing completed on 2024-05-05 04:48:33

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "SimpleImporterConfigWidget.h"
0018 
0019 #include "core/support/Debug.h"
0020 
0021 #include <QLineEdit>
0022 #include <KLocalizedString>
0023 
0024 #include <QBoxLayout>
0025 #include <QGridLayout>
0026 #include <QLabel>
0027 #include <QSpacerItem>
0028 
0029 using namespace StatSyncing;
0030 
0031 SimpleImporterConfigWidget::SimpleImporterConfigWidget( const QString &targetName,
0032                                                         const QVariantMap &config,
0033                                                         QWidget *parent,
0034                                                         Qt::WindowFlags f )
0035     : ProviderConfigWidget( parent, f )
0036     , m_config( config )
0037     , m_layout( new QGridLayout )
0038 {
0039     m_layout->setColumnMinimumWidth( 0, 100 );
0040     m_layout->setColumnMinimumWidth( 1, 250 );
0041     m_layout->setColumnStretch( 0, 0 );
0042     m_layout->setColumnStretch( 1, 1 );
0043 
0044     QBoxLayout *mainLayout = new QBoxLayout( QBoxLayout::TopToBottom );
0045     mainLayout->addLayout( m_layout, 0 );
0046     mainLayout->addStretch( 1 );
0047     setLayout( mainLayout );
0048 
0049     addField( QStringLiteral("name"), i18nc( "Name of the synchronization target", "Target name" ),
0050               new QLineEdit( targetName ),  QStringLiteral("text") );
0051 }
0052 
0053 SimpleImporterConfigWidget::~SimpleImporterConfigWidget()
0054 {
0055 }
0056 
0057 void
0058 SimpleImporterConfigWidget::addField( const QString &configName, const QString &label,
0059                                       QWidget * const field, const QString &property )
0060 {
0061     if( !field )
0062     {
0063         warning() << __PRETTY_FUNCTION__ << "Attempted to add null field";
0064         return;
0065     }
0066 
0067     QLabel *lwidget = new QLabel( label );
0068     lwidget->setBuddy( field );
0069 
0070     const int row = m_layout->rowCount();
0071     m_layout->addWidget( lwidget, row, 0 );
0072     m_layout->addWidget( field, row, 1 );
0073 
0074     // Populate field with previously configured value
0075     if( m_config.contains( configName ) )
0076     {
0077         const QByteArray propertyName = property.toLocal8Bit();
0078         field->setProperty( propertyName.constData(), m_config.value( configName ) );
0079     }
0080 
0081     m_fieldForName.insert( configName, qMakePair( field, property ) );
0082 }
0083 
0084 QVariantMap
0085 SimpleImporterConfigWidget::config() const
0086 {
0087     QVariantMap cfg( m_config );
0088 
0089     foreach( const QString &key, m_fieldForName.keys() )
0090     {
0091         const QPair<QWidget*, QString> val = m_fieldForName.value( key );
0092         const QByteArray propertyName = val.second.toLocal8Bit();
0093         cfg.insert( key, val.first->property( propertyName.constData() ) );
0094     }
0095 
0096     return cfg;
0097 }