1 /****************************************************************************************
2  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "SimpleImporterConfigWidget.h"
18 
19 #include "core/support/Debug.h"
20 
21 #include <QLineEdit>
22 #include <KLocalizedString>
23 
24 #include <QBoxLayout>
25 #include <QGridLayout>
26 #include <QLabel>
27 #include <QSpacerItem>
28 
29 using namespace StatSyncing;
30 
SimpleImporterConfigWidget(const QString & targetName,const QVariantMap & config,QWidget * parent,Qt::WindowFlags f)31 SimpleImporterConfigWidget::SimpleImporterConfigWidget( const QString &targetName,
32                                                         const QVariantMap &config,
33                                                         QWidget *parent,
34                                                         Qt::WindowFlags f )
35     : ProviderConfigWidget( parent, f )
36     , m_config( config )
37     , m_layout( new QGridLayout )
38 {
39     m_layout->setColumnMinimumWidth( 0, 100 );
40     m_layout->setColumnMinimumWidth( 1, 250 );
41     m_layout->setColumnStretch( 0, 0 );
42     m_layout->setColumnStretch( 1, 1 );
43 
44     QBoxLayout *mainLayout = new QBoxLayout( QBoxLayout::TopToBottom );
45     mainLayout->addLayout( m_layout, 0 );
46     mainLayout->addStretch( 1 );
47     setLayout( mainLayout );
48 
49     addField( QStringLiteral("name"), i18nc( "Name of the synchronization target", "Target name" ),
50               new QLineEdit( targetName ),  QStringLiteral("text") );
51 }
52 
~SimpleImporterConfigWidget()53 SimpleImporterConfigWidget::~SimpleImporterConfigWidget()
54 {
55 }
56 
57 void
addField(const QString & configName,const QString & label,QWidget * const field,const QString & property)58 SimpleImporterConfigWidget::addField( const QString &configName, const QString &label,
59                                       QWidget * const field, const QString &property )
60 {
61     if( !field )
62     {
63         warning() << __PRETTY_FUNCTION__ << "Attempted to add null field";
64         return;
65     }
66 
67     QLabel *lwidget = new QLabel( label );
68     lwidget->setBuddy( field );
69 
70     const int row = m_layout->rowCount();
71     m_layout->addWidget( lwidget, row, 0 );
72     m_layout->addWidget( field, row, 1 );
73 
74     // Populate field with previously configured value
75     if( m_config.contains( configName ) )
76     {
77         const QByteArray propertyName = property.toLocal8Bit();
78         field->setProperty( propertyName.constData(), m_config.value( configName ) );
79     }
80 
81     m_fieldForName.insert( configName, qMakePair( field, property ) );
82 }
83 
84 QVariantMap
config() const85 SimpleImporterConfigWidget::config() const
86 {
87     QVariantMap cfg( m_config );
88 
89     foreach( const QString &key, m_fieldForName.keys() )
90     {
91         const QPair<QWidget*, QString> val = m_fieldForName.value( key );
92         const QByteArray propertyName = val.second.toLocal8Bit();
93         cfg.insert( key, val.first->property( propertyName.constData() ) );
94     }
95 
96     return cfg;
97 }
98