Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Local includes
- #include "avatarimage.h"
- // Global includes
- #include <QPainter>
- #include <QPixmapCache>
- #include <QContactManager>
- #include <QContact>
- #include <QContactFetchRequest>
- #include <QContactLocalIdFilter>
- #include <QContactThumbnail>
- #include <QContactAvatar>
- #include <QContactName>
- #include <QContactDisplayLabel>
- #include <QTimer>
- #include <QSet>
- #include <QDebug>
- QTM_USE_NAMESPACE
- class AvatarImageShared : public QObject
- {
- Q_OBJECT
- public:
- AvatarImageShared()
- : defaultPixmap("/usr/share/themes/blanco/meegotouch/icons/icon-m-content-avatar-placeholder.png")
- , defaultPixmapInverted("/usr/share/themes/blanco/meegotouch/icons/icon-m-content-avatar-placeholder-dialog.png")
- , maskPixmap("/usr/share/themes/blanco/meegotouch/images/theme/basement/meegotouch-avatar/meegotouch-avatar-placeholder-background.png")
- , manager("tracker")
- {
- fetchTimer.setSingleShot(true);
- fetchTimer.setInterval(20);
- }
- QPixmap defaultPixmap;
- QPixmap defaultPixmapInverted;
- QPixmap maskPixmap;
- /// Get the image from a contactId
- QPixmap image(quint32 contactId);
- signals:
- void avatarsLoaded();
- private slots:
- void startLoading();
- void contactsFetched();
- protected:
- QString cacheKey(quint32 contactId) const;
- void queueContact(quint32 contactId);
- QPixmap loadImage(const QContact& contact);
- protected:
- QContactManager manager;
- QTimer fetchTimer;
- QSet<quint32> ids;
- };
- Q_GLOBAL_STATIC(AvatarImageShared, avatarShared)
- void AvatarImageShared::startLoading()
- {
- qDebug() << Q_FUNC_INFO << ids.size();
- // Setup the id filter
- QContactLocalIdFilter idFilter;
- idFilter.setIds( ids.toList() );
- // Create a fetch request
- QContactFetchRequest* request = new QContactFetchRequest;
- connect(request, SIGNAL(resultsAvailable()), this, SLOT(contactsFetched()));
- request->setFilter(idFilter);
- request->setManager(&manager);
- request->start();
- }
- void AvatarImageShared::contactsFetched()
- {
- qDebug() << Q_FUNC_INFO;
- // Get the sending request
- QContactFetchRequest* request = qobject_cast<QContactFetchRequest*>( sender() );
- // Create a image for each contact
- foreach(const QContact& contact, request->contacts()) {
- // Get the contactId
- quint32 contactId = contact.localId();
- // Create the image
- QPixmap pixmap = loadImage(contact);
- ids.remove(contactId);
- // Add it to the cache
- QPixmapCache::insert(cacheKey(contactId), pixmap);
- }
- // Free the memory
- request->deleteLater();
- emit avatarsLoaded();
- }
- QString AvatarImageShared::cacheKey(quint32 contactId) const
- {
- return QString("avatarimage_%1").arg(contactId);
- }
- void AvatarImageShared::queueContact(quint32 contactId)
- {
- // Do not add twice
- if ( ids.contains(contactId) )
- return;
- // Add the contactId to our queue
- ids.insert(contactId);
- // Prepare the timer
- fetchTimer.start();
- }
- QPixmap AvatarImageShared::image(quint32 contactId)
- {
- QPixmap pixmap;
- // Search the pixmap in the cache
- if (QPixmapCache::find(cacheKey(contactId), &pixmap))
- return pixmap;
- else {
- // Queue the contact
- queueContact(contactId);
- return QPixmap();
- }
- }
- QPixmap AvatarImageShared::loadImage(const QContact &contact)
- {
- // First try to find the thumbnail image
- QContactThumbnail thumbnail = contact.detail<QContactThumbnail>();
- QPixmap thumbnailImage = QPixmap::fromImage(thumbnail.thumbnail());
- if ( thumbnailImage.isNull() == false )
- //maskImage(image, thumbnailImage);
- return thumbnailImage;
- else {
- // Get the avatar image if we don't have a thumbnail
- QContactAvatar avatar = contact.detail<QContactAvatar>();
- QString path = avatar.imageUrl().toLocalFile();
- if ( path.isEmpty() == false ) {
- //QImage tmp(path);
- //maskImage(image, tmp.scaled(image.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
- return QPixmap(path);
- }
- else {
- // Get the contact name
- QContactName name = contact.detail<QContactName>();
- QString initials;
- QString firstName = name.firstName();
- QString lastName = name.lastName();
- if ( firstName.isEmpty() && lastName.isEmpty() )
- return avatarShared()->defaultPixmap;
- else {
- // Construct the initials
- if ( firstName.isEmpty() )
- initials = lastName.left(1);
- else if ( lastName.isEmpty() )
- initials = firstName.left(1);
- else
- initials = firstName.left(1) + lastName.left(1);
- // Load the default image
- //image.load("/usr/share/themes/blanco/meegotouch/images/theme/basement/meegotouch-avatar/meegotouch-avatar-placeholder-background.png");
- QPixmap pm = avatarShared()->maskPixmap;
- QPainter painter(&pm);
- QFont font("Nokia Pure", 24, QFont::Bold);
- painter.setFont(font);
- painter.setPen(Qt::white);
- QRect rect(QPoint(), pm.size());
- painter.drawText(rect, Qt::AlignCenter, initials);
- return pm;
- }
- }
- }
- return QPixmap();
- }
- class AvatarImagePrivate : public QObject
- {
- Q_OBJECT
- public:
- AvatarImagePrivate()
- : contactId(-1)
- {
- // Connect to the shared avatar loader
- connect(avatarShared(), SIGNAL(avatarsLoaded()), this, SLOT(updatePixmap()));
- }
- // Properties
- int contactId;
- QPixmap pixmap;
- // Functions
- public slots:
- void updatePixmap();
- };
- void AvatarImagePrivate::updatePixmap()
- {
- QPixmap pixmap;
- // Load the image or get one from cache
- if ( contactId > 0 )
- pixmap = avatarShared()->image(contactId);
- // Load the default pixmap
- if ( pixmap.isNull() )
- pixmap = avatarShared()->defaultPixmap;
- // Assign the new pixmap
- this->pixmap = pixmap;
- }
- AvatarImage::AvatarImage(QDeclarativeItem *parent)
- : QDeclarativeItem(parent)
- , d(new AvatarImagePrivate)
- {
- // We want to paint this item
- setFlag(ItemHasNoContents, false);
- }
- AvatarImage::~AvatarImage()
- {
- delete d;
- }
- void AvatarImage::setContactId(int contactId)
- {
- if ( d->contactId == contactId )
- return;
- d->contactId = contactId;
- emit contactIdChanged();
- d->updatePixmap();
- }
- int AvatarImage::contactId() const
- {
- return d->contactId;
- }
- void AvatarImage::componentComplete()
- {
- // Base class may do something more
- QDeclarativeItem::componentComplete();
- }
- void AvatarImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
- {
- Q_UNUSED(option);
- Q_UNUSED(widget);
- // Simply draw the pixmap
- painter->drawPixmap(0, 0, d->pixmap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement