Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

providercombobox.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 - 2008 by Martin Mueller                           *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "providercombobox.h"
00021 #include "providercombobox.moc"
00022 
00023 #include <klocale.h>
00024 
00025 #include <QIcon>
00026 
00027 class Blokkal::Ui::ProviderInformation::Private {
00028 public:
00029         Private( const QString & id, const QString & name, const QIcon & icon ):
00030         id( id ),
00031         name( name ),
00032         icon( icon ) {}
00033 
00034         QString id;
00035         QString name;
00036         QIcon icon;
00037 };
00038 
00039 Blokkal::Ui::ProviderInformation::ProviderInformation( const QString & id,      const QString & name ):
00040 d( new Private( id, name, QIcon() ) )
00041 {
00042 }
00043 
00044 Blokkal::Ui::ProviderInformation::ProviderInformation( const QString & id, const QString & name, const QIcon & icon ):
00045 d( new Private( id, name, icon ) )
00046 {
00047 }
00048 
00049 Blokkal::Ui::ProviderInformation::ProviderInformation( const Blokkal::Ui::ProviderInformation & provider ):
00050 d( new Private( provider.id(), provider.name(), provider.icon() ) )
00051 {
00052 }
00053 
00054 Blokkal::Ui::ProviderInformation::~ProviderInformation( void )
00055 {
00056         delete d;
00057 }
00058 
00059 const QString & Blokkal::Ui::ProviderInformation::id( void ) const
00060 {
00061         return d->id;
00062 }
00063 
00064 const QIcon & Blokkal::Ui::ProviderInformation::icon( void ) const
00065 {
00066         return d->icon;
00067 }
00068 
00069 const QString & Blokkal::Ui::ProviderInformation::name( void ) const
00070 {
00071         return d->name;
00072 }
00073 
00074 Blokkal::Ui::ProviderInformation & Blokkal::Ui::ProviderInformation::operator =( const Blokkal::Ui::ProviderInformation & provider )
00075 {
00076         if( this != &provider ) {
00077                 delete d;
00078                 d = new Private( provider.id(), provider.name(), provider.icon() );
00079         }
00080         return *this;
00081 }
00082 
00083 class Blokkal::Ui::ProviderComboBox::Private {
00084 public:
00085         Private( void ) {}
00086 
00087         QMap<unsigned int, ProviderInformation> providerMap;
00088 };
00089 
00090 Blokkal::Ui::ProviderComboBox::ProviderComboBox( QWidget * parent ):
00091 KComboBox( parent ),
00092 d( new Private() )
00093 {
00094         connect( this, SIGNAL( activated( int ) ),
00095                  this, SLOT( emitProviderSelected( int ) ) );
00096 }
00097 
00098 Blokkal::Ui::ProviderComboBox::~ProviderComboBox( void )
00099 {
00100         delete d;
00101 }
00102 
00103 void Blokkal::Ui::ProviderComboBox::setProviders( const ProviderList & providers )
00104 {
00105         d->providerMap.clear();
00106         clear();
00107         
00108         addProviders( providers );
00109 }
00110 
00111 void Blokkal::Ui::ProviderComboBox::addProviders( const ProviderList & providers )
00112 {
00113         if( count() > 0
00114             && d->providerMap.count() > 0
00115             && d->providerMap[count()-1].id().isNull() )
00116         {
00117                 d->providerMap.remove( count() - 1 );
00118                 removeItem( count() -1 );
00119         }
00120 
00121         for( ProviderList::ConstIterator it = providers.begin();
00122              it != providers.end();
00123              ++it )
00124         {
00125                 d->providerMap.insert( count(), *it );
00126                 addItem( (*it).icon(), (*it).name() );
00127         }
00128         d->providerMap.insert( count(), ProviderInformation( QString::null, i18n( "Custom" ) ) );
00129         addItem( i18n( "Custom" ) );
00130 }
00131 
00132 QString Blokkal::Ui::ProviderComboBox::currentProvider( void ) const
00133 {
00134         if( currentIndex() < 0 ) {
00135                 return QString::null;
00136         }
00137 
00138         return d->providerMap[currentIndex()].id();
00139 }
00140 
00141 void Blokkal::Ui::ProviderComboBox::setCurrentProvider( const QString & id )
00142 {
00143         for( QMap<unsigned int, ProviderInformation>::ConstIterator it = d->providerMap.begin();
00144              it != d->providerMap.end();
00145              ++it )
00146         {
00147                 if( it.value().id() == id ) {
00148                         setCurrentIndex( it.key() );
00149                         return;
00150                 }
00151         }
00152 
00153         if( count() ) {
00154                 setCurrentIndex( count() - 1 );
00155         }
00156 }
00157 
00158 void Blokkal::Ui::ProviderComboBox::emitProviderSelected( int index )
00159 {
00160         if( d->providerMap.contains( index ) ) {
00161                 emit providerSelected( d->providerMap[index].id() );
00162         }
00163 }