Skip to content

Commit e9907bc

Browse files
committedNov 21, 2017
AvatarJob improvements
* Drop AvatarJob2 * Allow AvatarJob to retrieve different sizes and users * Make creating a circular avatar into a function (maybe all avatars should be made into that shape in the first place)
1 parent 6b9b525 commit e9907bc

9 files changed

+47
-184
lines changed
 

‎src/gui/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ set(client_SRCS
4747
accountmanager.cpp
4848
accountsettings.cpp
4949
application.cpp
50-
avatarjob.cpp
5150
folder.cpp
5251
folderman.cpp
5352
folderstatusmodel.cpp

‎src/gui/avatarjob.cpp

-52
This file was deleted.

‎src/gui/avatarjob.h

-63
This file was deleted.

‎src/gui/settingsdialog.cpp

+2-19
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,6 @@ namespace OCC {
5757

5858
#include "settingsdialogcommon.cpp"
5959

60-
static QIcon circleMask(const QImage &avatar)
61-
{
62-
int dim = avatar.width();
63-
64-
QPixmap fixedImage(dim, dim);
65-
fixedImage.fill(Qt::transparent);
66-
67-
QPainter imgPainter(&fixedImage);
68-
QPainterPath clip;
69-
clip.addEllipse(0, 0, dim, dim);
70-
imgPainter.setClipPath(clip);
71-
imgPainter.drawImage(0, 0, avatar);
72-
imgPainter.end();
73-
74-
return QIcon(fixedImage);
75-
}
76-
7760
//
7861
// Whenever you change something here check both settingsdialog.cpp and settingsdialogmac.cpp !
7962
//
@@ -232,7 +215,7 @@ void SettingsDialog::accountAdded(AccountState *s)
232215
accountAction = createColorAwareAction(QLatin1String(":/client/resources/account.png"),
233216
actionText);
234217
} else {
235-
QIcon icon = circleMask(avatar);
218+
QIcon icon(QPixmap::fromImage(AvatarJob::makeCircularAvatar(avatar)));
236219
accountAction = createActionWithIcon(icon, actionText);
237220
}
238221

@@ -265,7 +248,7 @@ void SettingsDialog::slotAccountAvatarChanged()
265248
if (action) {
266249
QImage pix = account->avatar();
267250
if (!pix.isNull()) {
268-
action->setIcon(circleMask(pix));
251+
action->setIcon(QPixmap::fromImage(AvatarJob::makeCircularAvatar(pix)));
269252
}
270253
}
271254
}

‎src/gui/shareusergroupwidget.cpp

+10-41
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "thumbnailjob.h"
2727
#include "sharee.h"
2828
#include "sharemanager.h"
29-
#include "avatarjob.h"
3029

3130
#include "QProgressIndicator.h"
3231
#include <QBuffer>
@@ -470,53 +469,23 @@ void ShareUserLine::loadAvatar()
470469

471470
// We can only fetch avatars for local users currently
472471
if (_share->getShareWith()->type() == Sharee::User) {
473-
AvatarJob2 *job = new AvatarJob2(_share->getShareWith()->shareWith(), 48, _share->account(), this);
474-
connect(job, SIGNAL(avatarReady(QByteArray, QString)), SLOT(slotAvatarLoaded(QByteArray, QString)));
472+
AvatarJob *job = new AvatarJob(_share->account(), _share->getShareWith()->shareWith(), 48, this);
473+
connect(job, &AvatarJob::avatarPixmap, this, &ShareUserLine::slotAvatarLoaded);
475474
job->start();
476475
}
477476
}
478477

479-
void ShareUserLine::slotAvatarLoaded(const QByteArray &data, const QString &mimeType)
478+
void ShareUserLine::slotAvatarLoaded(QImage avatar)
480479
{
481-
QPixmap p;
482-
bool valid = false;
483-
if (mimeType == "image/png") {
484-
valid = p.loadFromData(data, "PNG");
485-
} else if (mimeType == "image/jpeg") {
486-
valid = p.loadFromData(data, "JPG");
487-
} else {
488-
// Guess the filetype
489-
valid = p.loadFromData(data);
490-
}
480+
if (avatar.isNull())
481+
return;
491482

492-
// If the image was loaded succesfully set it!
493-
if (valid) {
483+
avatar = AvatarJob::makeCircularAvatar(avatar);
494484

495-
/*
496-
* We want round avatars so create a new pixmap to draw
497-
* the round avatar on and set a transparent background
498-
*/
499-
QPixmap avatar(p.width(), p.height());
500-
avatar.fill(QColor(0,0,0,0));
501-
502-
// Initialise our painer
503-
QPainter painter(&avatar);
504-
painter.setRenderHint(QPainter::Antialiasing);
505-
506-
// Set to draw only a cricle
507-
QPainterPath path;
508-
path.addEllipse(0,0,p.width(),p.height());
509-
painter.setClipPath(path);
510-
511-
// Draw the round avatar
512-
painter.drawPixmap(0,0,p.width(),p.width(),p);
513-
514-
// Set the avatar
515-
_ui->avatar->setPixmap(avatar);
516-
517-
// Remove the stylesheet
518-
_ui->avatar->setStyleSheet("");
519-
}
485+
_ui->avatar->setPixmap(QPixmap::fromImage(avatar));
486+
487+
// Remove the stylesheet for the fallback avatar
488+
_ui->avatar->setStyleSheet("");
520489
}
521490

522491
void ShareUserLine::on_deleteShareButton_clicked()

‎src/gui/shareusergroupwidget.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private slots:
131131
void slotShareDeleted();
132132
void slotPermissionsSet();
133133

134-
void slotAvatarLoaded(const QByteArray &data, const QString &mimeType);
134+
void slotAvatarLoaded(QImage avatar);
135135

136136
private:
137137
void displayPermissions();

‎src/libsync/connectionvalidator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void ConnectionValidator::slotUserFetched(const QJsonDocument &json)
334334
if (!user.isEmpty()) {
335335
_account->setDavUser(user);
336336

337-
AvatarJob *job = new AvatarJob(_account, this);
337+
AvatarJob *job = new AvatarJob(_account, _account->davUser(), 128, this);
338338
job->setTimeout(20 * 1000);
339339
QObject::connect(job, &AvatarJob::avatarPixmap, this, &ConnectionValidator::slotAvatarImage);
340340

‎src/libsync/networkjobs.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <QPixmap>
3131
#include <QJsonDocument>
3232
#include <QJsonObject>
33+
#include <QPainter>
3334

3435
#include "networkjobs.h"
3536
#include "account.h"
@@ -626,10 +627,10 @@ bool PropfindJob::finished()
626627

627628
/*********************************************************************************************/
628629

629-
AvatarJob::AvatarJob(AccountPtr account, QObject *parent)
630+
AvatarJob::AvatarJob(AccountPtr account, const QString &userId, int size, QObject *parent)
630631
: AbstractNetworkJob(account, QString(), parent)
631632
{
632-
_avatarUrl = Utility::concatUrlPath(account->url(), QString("remote.php/dav/avatars/%1/128.png").arg(account->davUser()));
633+
_avatarUrl = Utility::concatUrlPath(account->url(), QString("remote.php/dav/avatars/%1/%2.png").arg(userId, QString::number(size)));
633634
}
634635

635636
void AvatarJob::start()
@@ -639,6 +640,26 @@ void AvatarJob::start()
639640
AbstractNetworkJob::start();
640641
}
641642

643+
QImage AvatarJob::makeCircularAvatar(const QImage &baseAvatar)
644+
{
645+
int dim = baseAvatar.width();
646+
647+
QImage avatar(dim, dim, baseAvatar.format());
648+
avatar.fill(Qt::transparent);
649+
650+
QPainter painter(&avatar);
651+
painter.setRenderHint(QPainter::Antialiasing);
652+
653+
QPainterPath path;
654+
path.addEllipse(0, 0, dim, dim);
655+
painter.setClipPath(path);
656+
657+
painter.drawImage(0, 0, baseAvatar);
658+
painter.end();
659+
660+
return avatar;
661+
}
662+
642663
bool AvatarJob::finished()
643664
{
644665
int http_result_code = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

‎src/libsync/networkjobs.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ private slots:
137137

138138

139139
/**
140-
* @brief The AvatarJob class
141-
*
142-
* Retrieves the account users avatar from the server using a GET request.
140+
* @brief Retrieves the account users avatar from the server using a GET request.
143141
*
144142
* If the server does not have the avatar, the result Pixmap is empty.
145143
*
@@ -149,9 +147,17 @@ class OWNCLOUDSYNC_EXPORT AvatarJob : public AbstractNetworkJob
149147
{
150148
Q_OBJECT
151149
public:
152-
explicit AvatarJob(AccountPtr account, QObject *parent = 0);
150+
/**
151+
* @param userId The user for which to obtain the avatar
152+
* @param size The size of the avatar (square so size*size)
153+
*/
154+
explicit AvatarJob(AccountPtr account, const QString &userId, int size, QObject *parent = 0);
155+
153156
void start() Q_DECL_OVERRIDE;
154157

158+
/** The retrieved avatar images don't have the circle shape by default */
159+
static QImage makeCircularAvatar(const QImage &baseAvatar);
160+
155161
signals:
156162
/**
157163
* @brief avatarPixmap - returns either a valid pixmap or not.

0 commit comments

Comments
 (0)
Please sign in to comment.