Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / libkopete / ui / avatarselectorwidget.cpp
blob10b8191a75277a3abd3ebde8ea893e8fe52d8609
1 #ifndef LIBKOPETE_UI_AVATARSELECTORWIDGET_CPP
2 #define LIBKOPETE_UI_AVATARSELECTORWIDGET_CPP
3 /*
4 avatarselectorwidget.cpp - Widget to manage and select user avatar
6 Copyright (c) 2007 by Michaƫl Larouche <larouche@kde.org>
7 Copyright (c) 2007 Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
9 Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
11 *************************************************************************
12 * *
13 * This library is free software; you can redistribute it and/or *
14 * modify it under the terms of the GNU Lesser General Public *
15 * License as published by the Free Software Foundation; either *
16 * version 2 of the License, or (at your option) any later version. *
17 * *
18 *************************************************************************
20 #include "avatarselectorwidget.h"
22 // Qt includes
23 #include <QListWidget>
24 #include <QListWidgetItem>
25 #include <QIcon>
26 #include <QPainter>
28 // KDE includes
29 #include <kdebug.h>
30 #include <klocale.h>
31 #include <kurl.h>
32 #include <kfiledialog.h>
33 #include <kpixmapregionselectordialog.h>
35 #include "ui_avatarselectorwidget.h"
37 namespace Kopete
39 namespace UI
42 class AvatarSelectorWidgetItem : public QListWidgetItem
44 public:
45 AvatarSelectorWidgetItem(QListWidget *parent)
46 : QListWidgetItem(parent, QListWidgetItem::UserType)
49 void setAvatarEntry(Kopete::AvatarManager::AvatarEntry entry)
51 m_entry = entry;
53 QSize s(96,96);
55 if (listWidget())
56 s = listWidget()->iconSize();
58 QPixmap pix;
59 if (entry.path.isEmpty())
61 // draw a fake image telling there is no avatar
62 pix = QPixmap(s);
63 QPainter p(&pix);
64 p.fillRect(pix.rect(), Qt::white);
65 p.drawText(pix.rect(), Qt::TextWordWrap | Qt::AlignCenter, i18n("No Avatar"));
67 else
69 pix = QPixmap(entry.path).scaled(s);
72 // draw a border around the avatar
73 QPainter p(&pix);
74 p.setBrush(Qt::NoBrush);
75 p.drawRect(0,0,pix.width()-1,pix.height()-1);
77 setIcon(pix);
80 Kopete::AvatarManager::AvatarEntry avatarEntry() const
82 return m_entry;
85 private:
86 Kopete::AvatarManager::AvatarEntry m_entry;
89 class AvatarSelectorWidget::Private
91 public:
92 Private()
93 : selectedItem(0), noAvatarItem(0)
96 Ui::AvatarSelectorWidget mainWidget;
97 QListWidgetItem *selectedItem;
98 QString currentAvatar;
99 AvatarSelectorWidgetItem * noAvatarItem;
100 AvatarSelectorWidgetItem * addItem(Kopete::AvatarManager::AvatarEntry entry);
103 AvatarSelectorWidget::AvatarSelectorWidget(QWidget *parent)
104 : QWidget(parent), d(new Private)
106 d->mainWidget.setupUi(this);
108 // use icons on buttons
109 d->mainWidget.buttonAddAvatar->setIcon( KIcon("list-add") );
110 d->mainWidget.buttonRemoveAvatar->setIcon( KIcon("edit-delete") );
112 // Connect signals/slots
113 connect(d->mainWidget.buttonAddAvatar, SIGNAL(clicked()), this, SLOT(buttonAddAvatarClicked()));
114 connect(d->mainWidget.buttonRemoveAvatar, SIGNAL(clicked()), this, SLOT(buttonRemoveAvatarClicked()));
115 connect(d->mainWidget.listUserAvatar, SIGNAL(itemClicked(QListWidgetItem*)),
116 this, SLOT(listSelectionChanged(QListWidgetItem*)));
117 connect(Kopete::AvatarManager::self(), SIGNAL(avatarAdded(Kopete::AvatarManager::AvatarEntry)),
118 this, SLOT(avatarAdded(Kopete::AvatarManager::AvatarEntry)));
119 connect(Kopete::AvatarManager::self(), SIGNAL(avatarRemoved(Kopete::AvatarManager::AvatarEntry)),
120 this, SLOT(avatarRemoved(Kopete::AvatarManager::AvatarEntry)));
122 // Add a "No Avatar" option
123 Kopete::AvatarManager::AvatarEntry empty;
124 empty.name = i18n("No Avatar");
125 empty.contact = 0;
126 empty.category = Kopete::AvatarManager::User;
127 d->noAvatarItem = d->addItem(empty);
129 // List avatars of User category
130 Kopete::AvatarQueryJob *queryJob = new Kopete::AvatarQueryJob(this);
131 connect(queryJob, SIGNAL(result(KJob*)), this, SLOT(queryJobFinished(KJob*)));
132 queryJob->setQueryFilter( Kopete::AvatarManager::User );
134 queryJob->start();
137 AvatarSelectorWidget::~AvatarSelectorWidget()
139 delete d;
142 Kopete::AvatarManager::AvatarEntry AvatarSelectorWidget::selectedEntry() const
144 Kopete::AvatarManager::AvatarEntry result;
146 if( d->selectedItem )
148 result = static_cast<AvatarSelectorWidgetItem*>(d->selectedItem)->avatarEntry();
151 return result;
154 void AvatarSelectorWidget::setCurrentAvatar(const QString &path)
156 d->currentAvatar = path;
158 //try to find the avatar in the list
159 QList<QListWidgetItem*> itemList = d->mainWidget.listUserAvatar->findItems("", Qt::MatchContains);
160 QList<QListWidgetItem*>::iterator it = itemList.begin();
162 while (it != itemList.end())
164 AvatarSelectorWidgetItem *item = static_cast<AvatarSelectorWidgetItem*>(*it);
165 if (item->avatarEntry().path == path)
167 item->setSelected(true);
168 listSelectionChanged( item );
169 return;
171 ++it;
176 void AvatarSelectorWidget::buttonAddAvatarClicked()
178 KUrl imageUrl = KFileDialog::getImageOpenUrl( KUrl(), this );
179 if( !imageUrl.isEmpty() )
181 // TODO: Download image
182 if( !imageUrl.isLocalFile() )
183 return;
185 QPixmap pixmap( imageUrl.path() );
186 if ( pixmap.isNull() )
187 return;
189 // Crop the image
190 QImage avatar = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 96, 96, this );
192 QString imageName = imageUrl.fileName();
194 Kopete::AvatarManager::AvatarEntry newEntry;
195 // Remove extension from filename
196 newEntry.name = imageName.left( imageName.lastIndexOf('.') );
197 newEntry.image = avatar;
198 newEntry.category = Kopete::AvatarManager::User;
200 Kopete::AvatarManager::AvatarEntry addedEntry = Kopete::AvatarManager::self()->add( newEntry );
201 if( addedEntry.path.isEmpty() )
203 //TODO add a real error message
204 //d->mainWidget.labelErrorMessage->setText( i18n("Kopete cannot add this new avatar because it could not save the avatar image in user directory.") );
205 return;
208 // select the added entry and show the user tab
209 QList<QListWidgetItem *> foundItems = d->mainWidget.listUserAvatar->findItems( addedEntry.name, Qt::MatchContains );
210 if( !foundItems.isEmpty() )
212 AvatarSelectorWidgetItem *item = dynamic_cast<AvatarSelectorWidgetItem*>( foundItems.first() );
213 if ( !item )
214 return;
215 item->setSelected( true );
222 void AvatarSelectorWidget::buttonRemoveAvatarClicked()
224 // if no item was selected, just exit
225 if ( !d->mainWidget.listUserAvatar->selectedItems().count() )
226 return;
228 AvatarSelectorWidgetItem *selectedItem = dynamic_cast<AvatarSelectorWidgetItem*>( d->mainWidget.listUserAvatar->selectedItems().first() );
229 if( selectedItem )
231 if ( selectedItem != d->noAvatarItem )
233 if( !Kopete::AvatarManager::self()->remove( selectedItem->avatarEntry() ) )
235 kDebug(14010) << "Removing of avatar failed for unknown reason.";
241 void AvatarSelectorWidget::queryJobFinished(KJob *job)
243 Kopete::AvatarQueryJob *queryJob = static_cast<Kopete::AvatarQueryJob*>(job);
244 if( !queryJob->error() )
246 QList<Kopete::AvatarManager::AvatarEntry> avatarList = queryJob->avatarList();
247 foreach(const Kopete::AvatarManager::AvatarEntry &entry, avatarList)
249 d->addItem(entry);
252 else
254 //TODO add a real error message
255 //d->mainWidget.labelErrorMessage->setText( queryJob->errorText() );
259 void AvatarSelectorWidget::avatarAdded(Kopete::AvatarManager::AvatarEntry newEntry)
261 d->addItem(newEntry);
262 setCurrentAvatar(newEntry.path);
265 void AvatarSelectorWidget::avatarRemoved(Kopete::AvatarManager::AvatarEntry entryRemoved)
267 // Same here, avatar can be only removed from listUserAvatar
268 foreach(QListWidgetItem *item, d->mainWidget.listUserAvatar->findItems("",Qt::MatchContains))
270 // checks if this is the right item
271 AvatarSelectorWidgetItem *avatar = dynamic_cast<AvatarSelectorWidgetItem*>(item);
272 if (!avatar || avatar->avatarEntry().name != entryRemoved.name)
273 continue;
275 kDebug(14010) << "Removing " << entryRemoved.name << " from list.";
277 int deletedRow = d->mainWidget.listUserAvatar->row( item );
278 QListWidgetItem *removedItem = d->mainWidget.listUserAvatar->takeItem( deletedRow );
279 delete removedItem;
281 int newRow = --deletedRow;
282 if( newRow < 0 )
283 newRow = 0;
285 // Select the previous avatar in the list, thus selecting a new avatar
286 // and deselecting the avatar being removed.
287 d->mainWidget.listUserAvatar->setCurrentRow( newRow );
288 // Force update
289 listSelectionChanged( d->mainWidget.listUserAvatar->item(newRow) );
293 void AvatarSelectorWidget::listSelectionChanged(QListWidgetItem *item)
295 d->mainWidget.buttonRemoveAvatar->setEnabled( item != d->noAvatarItem );
296 d->selectedItem = item;
299 AvatarSelectorWidgetItem * AvatarSelectorWidget::Private::addItem(Kopete::AvatarManager::AvatarEntry entry)
301 kDebug(14010) << "Entry(" << entry.name << "): " << entry.category;
303 // only use User avatars
304 if( !(entry.category & Kopete::AvatarManager::User) )
305 return 0;
307 AvatarSelectorWidgetItem *item = new AvatarSelectorWidgetItem(mainWidget.listUserAvatar);
308 item->setAvatarEntry(entry);
309 if (entry.path == currentAvatar)
310 item->setSelected(true);
311 return item;
314 } // Namespace Kopete::UI
316 } // Namespace Kopete
318 #include "avatarselectorwidget.moc"
319 #endif // LIBKOPETE_UI/AVATARSELECTORWIDGET_CPP