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
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "avatarselectorwidget.h"
00021
00022
00023 #include <QListWidget>
00024 #include <QListWidgetItem>
00025 #include <QIcon>
00026 #include <QPainter>
00027
00028
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
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
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
00109 d->mainWidget.buttonAddAvatar->setIcon( KIcon("list-add") );
00110 d->mainWidget.buttonRemoveAvatar->setIcon( KIcon("edit-delete") );
00111
00112
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
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
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
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
00182 if( !imageUrl.isLocalFile() )
00183 return;
00184
00185 QPixmap pixmap( imageUrl.path() );
00186 if ( pixmap.isNull() )
00187 return;
00188
00189
00190 QImage avatar = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 96, 96, this );
00191
00192 QString imageName = imageUrl.fileName();
00193
00194 Kopete::AvatarManager::AvatarEntry newEntry;
00195
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
00204
00205 return;
00206 }
00207
00208
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
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
00256
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
00268 foreach(QListWidgetItem *item, d->mainWidget.listUserAvatar->findItems("",Qt::MatchContains))
00269 {
00270
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
00286
00287 d->mainWidget.listUserAvatar->setCurrentRow( newRow );
00288
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
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 }
00315
00316 }
00317
00318 #include "avatarselectorwidget.moc"
00319 #endif // LIBKOPETE_UI/AVATARSELECTORWIDGET_CPP