• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdenetwork
  • Sitemap
  • Contact Us
 

kopete/libkopete

avatarselectorwidget.cpp

Go to the documentation of this file.
00001 #ifndef LIBKOPETE_UI_AVATARSELECTORWIDGET_CPP
00002 #define LIBKOPETE_UI_AVATARSELECTORWIDGET_CPP
00003 /*
00004     avatarselectorwidget.cpp - Widget to manage and select user avatar
00005 
00006     Copyright (c) 2007      by Michaƫl Larouche       <larouche@kde.org>
00007     Copyright (c) 2007         Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
00008 
00009     Kopete    (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
00010 
00011     *************************************************************************
00012     *                                                                       *
00013     * This library is free software; you can redistribute it and/or         *
00014     * modify it under the terms of the GNU Lesser General Public            *
00015     * License as published by the Free Software Foundation; either          *
00016     * version 2 of the License, or (at your option) any later version.      *
00017     *                                                                       *
00018     *************************************************************************
00019 */
00020 #include "avatarselectorwidget.h"
00021 
00022 // Qt includes
00023 #include <QListWidget>
00024 #include <QListWidgetItem>
00025 #include <QIcon>
00026 #include <QPainter>
00027 
00028 // KDE includes
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <kurl.h>
00032 #include <kfiledialog.h>
00033 #include <kpixmapregionselectordialog.h>
00034 
00035 #include "ui_avatarselectorwidget.h"
00036 
00037 namespace Kopete
00038 {
00039 namespace UI
00040 {
00041 
00042 class AvatarSelectorWidgetItem : public QListWidgetItem
00043 {
00044 public:
00045     AvatarSelectorWidgetItem(QListWidget *parent)
00046      : QListWidgetItem(parent, QListWidgetItem::UserType)
00047     {}
00048 
00049     void setAvatarEntry(Kopete::AvatarManager::AvatarEntry entry)
00050     {
00051         m_entry = entry;
00052         
00053         QSize s(96,96);
00054 
00055         if (listWidget())
00056             s = listWidget()->iconSize();
00057 
00058         QPixmap pix;
00059         if (entry.path.isEmpty())
00060         {
00061             // draw a fake image telling there is no avatar
00062             pix = QPixmap(s);
00063             QPainter p(&pix);
00064             p.fillRect(pix.rect(), Qt::white);
00065             p.drawText(pix.rect(), Qt::TextWordWrap | Qt::AlignCenter, i18n("No Avatar"));
00066         }
00067         else
00068         {
00069             pix = QPixmap(entry.path).scaled(s);
00070         }
00071 
00072         // draw a border around the avatar
00073         QPainter p(&pix);
00074         p.setBrush(Qt::NoBrush);
00075         p.drawRect(0,0,pix.width()-1,pix.height()-1);
00076 
00077         setIcon(pix);
00078     }
00079 
00080     Kopete::AvatarManager::AvatarEntry avatarEntry() const
00081     {
00082         return m_entry;
00083     }
00084 
00085 private:
00086     Kopete::AvatarManager::AvatarEntry m_entry;
00087 };
00088 
00089 class AvatarSelectorWidget::Private
00090 {
00091 public:
00092     Private()
00093      : selectedItem(0), noAvatarItem(0)
00094     {}
00095 
00096     Ui::AvatarSelectorWidget mainWidget;
00097     QListWidgetItem *selectedItem;
00098     QString currentAvatar;
00099     AvatarSelectorWidgetItem * noAvatarItem;
00100     AvatarSelectorWidgetItem * addItem(Kopete::AvatarManager::AvatarEntry entry);
00101 };
00102 
00103 AvatarSelectorWidget::AvatarSelectorWidget(QWidget *parent)
00104  : QWidget(parent), d(new Private)
00105 {
00106     d->mainWidget.setupUi(this);
00107 
00108     // use icons on buttons
00109     d->mainWidget.buttonAddAvatar->setIcon( KIcon("list-add") );
00110     d->mainWidget.buttonRemoveAvatar->setIcon( KIcon("edit-delete") );
00111 
00112     // Connect signals/slots
00113     connect(d->mainWidget.buttonAddAvatar, SIGNAL(clicked()), this, SLOT(buttonAddAvatarClicked()));
00114     connect(d->mainWidget.buttonRemoveAvatar, SIGNAL(clicked()), this, SLOT(buttonRemoveAvatarClicked()));
00115     connect(d->mainWidget.listUserAvatar, SIGNAL(itemClicked(QListWidgetItem*)),
00116             this, SLOT(listSelectionChanged(QListWidgetItem*)));
00117     connect(Kopete::AvatarManager::self(), SIGNAL(avatarAdded(Kopete::AvatarManager::AvatarEntry)),
00118             this, SLOT(avatarAdded(Kopete::AvatarManager::AvatarEntry)));
00119     connect(Kopete::AvatarManager::self(), SIGNAL(avatarRemoved(Kopete::AvatarManager::AvatarEntry)),
00120             this, SLOT(avatarRemoved(Kopete::AvatarManager::AvatarEntry)));
00121 
00122     // Add a "No Avatar" option
00123     Kopete::AvatarManager::AvatarEntry empty;
00124     empty.name = i18n("No Avatar");
00125     empty.contact = 0;
00126     empty.category = Kopete::AvatarManager::User;
00127     d->noAvatarItem = d->addItem(empty);
00128 
00129     // List avatars of User category
00130     Kopete::AvatarQueryJob *queryJob = new Kopete::AvatarQueryJob(this);
00131     connect(queryJob, SIGNAL(result(KJob*)), this, SLOT(queryJobFinished(KJob*)));
00132     queryJob->setQueryFilter( Kopete::AvatarManager::User );
00133 
00134     queryJob->start();
00135 }
00136 
00137 AvatarSelectorWidget::~AvatarSelectorWidget()
00138 {
00139     delete d;
00140 }
00141 
00142 Kopete::AvatarManager::AvatarEntry AvatarSelectorWidget::selectedEntry() const
00143 {
00144     Kopete::AvatarManager::AvatarEntry result;
00145 
00146     if( d->selectedItem )
00147     {
00148         result = static_cast<AvatarSelectorWidgetItem*>(d->selectedItem)->avatarEntry();
00149     }
00150 
00151     return result;
00152 }
00153 
00154 void AvatarSelectorWidget::setCurrentAvatar(const QString &path)
00155 {
00156     d->currentAvatar = path;
00157 
00158     //try to find the avatar in the list
00159     QList<QListWidgetItem*> itemList = d->mainWidget.listUserAvatar->findItems("", Qt::MatchContains);
00160     QList<QListWidgetItem*>::iterator it = itemList.begin();
00161     
00162     while (it != itemList.end())
00163     {
00164         AvatarSelectorWidgetItem *item = static_cast<AvatarSelectorWidgetItem*>(*it);
00165         if (item->avatarEntry().path == path)
00166         {
00167             item->setSelected(true);
00168             listSelectionChanged( item );
00169             return;
00170         }
00171         ++it;
00172     }
00173 
00174 }
00175 
00176 void AvatarSelectorWidget::buttonAddAvatarClicked()
00177 {
00178     KUrl imageUrl = KFileDialog::getImageOpenUrl( KUrl(), this );
00179     if( !imageUrl.isEmpty() )
00180     {
00181         // TODO: Download image
00182         if( !imageUrl.isLocalFile() )
00183             return;
00184 
00185         QPixmap pixmap( imageUrl.path() );
00186         if ( pixmap.isNull() )
00187             return;
00188 
00189         // Crop the image
00190         QImage avatar = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 96, 96, this );
00191 
00192         QString imageName = imageUrl.fileName();
00193 
00194         Kopete::AvatarManager::AvatarEntry newEntry;
00195         // Remove extension from filename
00196         newEntry.name = imageName.left( imageName.lastIndexOf('.') );
00197         newEntry.image = avatar;
00198         newEntry.category = Kopete::AvatarManager::User;
00199 
00200         Kopete::AvatarManager::AvatarEntry addedEntry = Kopete::AvatarManager::self()->add( newEntry );
00201         if( addedEntry.path.isEmpty() )
00202         {
00203             //TODO add a real error message
00204             //d->mainWidget.labelErrorMessage->setText( i18n("Kopete cannot add this new avatar because it could not save the avatar image in user directory.") );
00205             return;
00206         }
00207 
00208         // select the added entry and show the user tab
00209         QList<QListWidgetItem *> foundItems = d->mainWidget.listUserAvatar->findItems( addedEntry.name, Qt::MatchContains );
00210         if( !foundItems.isEmpty() )
00211         {
00212             AvatarSelectorWidgetItem *item = dynamic_cast<AvatarSelectorWidgetItem*>( foundItems.first() );
00213             if ( !item )
00214                 return;
00215             item->setSelected( true );  
00216         }
00217 
00218 
00219     }
00220 }
00221 
00222 void AvatarSelectorWidget::buttonRemoveAvatarClicked()
00223 {
00224     // if no item was selected, just exit
00225     if ( !d->mainWidget.listUserAvatar->selectedItems().count() )
00226         return;
00227 
00228     AvatarSelectorWidgetItem *selectedItem = dynamic_cast<AvatarSelectorWidgetItem*>( d->mainWidget.listUserAvatar->selectedItems().first() );
00229     if( selectedItem )
00230     {
00231         if ( selectedItem != d->noAvatarItem )
00232         {
00233             if( !Kopete::AvatarManager::self()->remove( selectedItem->avatarEntry() ) )
00234             {
00235                 kDebug(14010) << "Removing of avatar failed for unknown reason.";
00236             }
00237         }
00238     }
00239 }
00240 
00241 void AvatarSelectorWidget::queryJobFinished(KJob *job)
00242 {
00243     Kopete::AvatarQueryJob *queryJob = static_cast<Kopete::AvatarQueryJob*>(job);
00244     if( !queryJob->error() )
00245     {
00246         QList<Kopete::AvatarManager::AvatarEntry> avatarList = queryJob->avatarList();
00247         Kopete::AvatarManager::AvatarEntry entry;
00248         foreach(entry, avatarList)
00249         {
00250             d->addItem(entry);
00251         }
00252     }
00253     else
00254     {
00255         //TODO add a real error message
00256         //d->mainWidget.labelErrorMessage->setText( queryJob->errorText() );
00257     }
00258 }
00259 
00260 void AvatarSelectorWidget::avatarAdded(Kopete::AvatarManager::AvatarEntry newEntry)
00261 {
00262     d->addItem(newEntry);
00263 }
00264 
00265 void AvatarSelectorWidget::avatarRemoved(Kopete::AvatarManager::AvatarEntry entryRemoved)
00266 {
00267     // Same here, avatar can be only removed from listUserAvatar
00268     foreach(QListWidgetItem *item, d->mainWidget.listUserAvatar->findItems("",Qt::MatchContains))
00269     {
00270         // checks if this is the right item
00271         AvatarSelectorWidgetItem *avatar = dynamic_cast<AvatarSelectorWidgetItem*>(item);
00272         if (!avatar || avatar->avatarEntry().name != entryRemoved.name)
00273             continue;
00274 
00275         kDebug(14010) << "Removing " << entryRemoved.name << " from list.";
00276 
00277         int deletedRow = d->mainWidget.listUserAvatar->row( item );
00278         QListWidgetItem *removedItem = d->mainWidget.listUserAvatar->takeItem( deletedRow );
00279         delete removedItem;
00280 
00281         int newRow = --deletedRow;
00282         if( newRow < 0 )
00283             newRow = 0;
00284 
00285         // Select the previous avatar in the list, thus selecting a new avatar
00286         // and deselecting the avatar being removed.
00287         d->mainWidget.listUserAvatar->setCurrentRow( newRow );
00288         // Force update
00289         listSelectionChanged( d->mainWidget.listUserAvatar->item(newRow) );
00290     }
00291 }
00292 
00293 void AvatarSelectorWidget::listSelectionChanged(QListWidgetItem *item)
00294 {
00295     d->mainWidget.buttonRemoveAvatar->setEnabled( item != d->noAvatarItem );
00296     d->selectedItem = item;
00297 }
00298 
00299 AvatarSelectorWidgetItem * AvatarSelectorWidget::Private::addItem(Kopete::AvatarManager::AvatarEntry entry)
00300 {
00301     kDebug(14010) << "Entry(" << entry.name << "): " << entry.category;
00302 
00303     // only use User avatars
00304     if( !(entry.category & Kopete::AvatarManager::User) )
00305         return 0;
00306 
00307     AvatarSelectorWidgetItem *item = new AvatarSelectorWidgetItem(mainWidget.listUserAvatar);
00308     item->setAvatarEntry(entry);
00309     if (entry.path == currentAvatar)
00310         item->setSelected(true);
00311     return item;
00312 }
00313 
00314 } // Namespace Kopete::UI
00315 
00316 } // Namespace Kopete
00317 
00318 #include "avatarselectorwidget.moc"
00319 #endif // LIBKOPETE_UI/AVATARSELECTORWIDGET_CPP

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal