From d466a059151fec1ed883ea124e6bc7dcf9a2457d Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Sun, 22 Jan 2017 13:58:36 +0100 Subject: [PATCH] SettingsDialog: Display the user avatar as action icon if available. The avatar image is fetched from the server async, thus connect a signal from the account if the avatar changes. Server feature https://github.com/owncloud/core/pull/26872 is needed. --- src/gui/settingsdialog.cpp | 48 +++++++++++++++++++++++++++++++++----- src/gui/settingsdialog.h | 7 ++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index 499fb3e0a..665738cd4 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -196,8 +196,17 @@ void SettingsDialog::accountAdded(AccountState *s) bool brandingSingleAccount = !Theme::instance()->multiAccount(); - auto accountAction = createColorAwareAction(QLatin1String(":/client/resources/account.png"), - brandingSingleAccount ? tr("Account") : s->account()->displayName()); + QAction *accountAction; + QPixmap avatar = s->account()->avatar(); + const QString actionText = brandingSingleAccount ? tr("Account") : s->account()->displayName(); + if(avatar.isNull()) { + accountAction = createColorAwareAction(QLatin1String(":/client/resources/account.png"), + actionText); + } else { + QIcon icon(avatar); + accountAction = createActionWithIcon(icon, actionText); + } + if (!brandingSingleAccount) { accountAction->setToolTip(s->account()->displayName()); accountAction->setIconText(s->shortDisplayNameForSettings(height * buttonSizeRatio)); @@ -207,14 +216,30 @@ void SettingsDialog::accountAdded(AccountState *s) _ui->stack->insertWidget(0 , accountSettings); _actionGroup->addAction(accountAction); _actionGroupWidgets.insert(accountAction, accountSettings); + _actionForAccount.insert(s->account().data(), accountAction); connect( accountSettings, SIGNAL(folderChanged()), _gui, SLOT(slotFoldersChanged())); connect( accountSettings, SIGNAL(openFolderAlias(const QString&)), _gui, SLOT(slotFolderOpenAction(QString))); + connect(s->account().data(), SIGNAL(accountChangedAvatar()), SLOT(slotAccountAvatarChanged())); slotRefreshActivity(s); } +void SettingsDialog::slotAccountAvatarChanged() +{ + Account *account = static_cast(sender()); + if( account && _actionForAccount.contains(account)) { + QAction *action = _actionForAccount[account]; + if( action ) { + QPixmap pix = account->avatar(); + if( !pix.isNull() ) { + action->setIcon( QIcon(pix) ); + } + } + } +} + void SettingsDialog::accountRemoved(AccountState *s) { for (auto it = _actionGroupWidgets.begin(); it != _actionGroupWidgets.end(); ++it) { @@ -236,6 +261,9 @@ void SettingsDialog::accountRemoved(AccountState *s) } } + if( _actionForAccount.contains(s->account().data()) ) { + _actionForAccount.remove(s->account().data()); + } _activitySettings->slotRemoveAccount(s); // Hide when the last account is deleted. We want to enter the same @@ -306,14 +334,22 @@ public: } }; +QAction *SettingsDialog::createActionWithIcon(const QIcon& icon, const QString& text, const QString& iconPath) +{ + QAction *action = new ToolButtonAction(icon, text, this); + action->setCheckable(true); + if(!iconPath.isEmpty()) { + action->setProperty("iconPath", iconPath); + } + return action; + +} + QAction *SettingsDialog::createColorAwareAction(const QString &iconPath, const QString &text) { // all buttons must have the same size in order to keep a good layout QIcon coloredIcon = createColorAwareIcon(iconPath); - QAction *action = new ToolButtonAction(coloredIcon, text, this); - action->setCheckable(true); - action->setProperty("iconPath", iconPath); - return action; + return createActionWithIcon(coloredIcon, text, iconPath); } void SettingsDialog::slotRefreshActivity( AccountState* accountState ) diff --git a/src/gui/settingsdialog.h b/src/gui/settingsdialog.h index dd4ffd448..39f2595a1 100644 --- a/src/gui/settingsdialog.h +++ b/src/gui/settingsdialog.h @@ -58,6 +58,7 @@ public slots: void showActivityPage(); void slotSwitchPage(QAction *action); void slotRefreshActivity(AccountState *accountState ); + void slotAccountAvatarChanged(); protected: void reject() Q_DECL_OVERRIDE; @@ -73,12 +74,18 @@ private: QIcon createColorAwareIcon(const QString &name); QAction *createColorAwareAction(const QString &iconName, const QString &fileName); + QAction *createActionWithIcon(const QIcon& icon, const QString& text, const QString& iconPath = QString()); + Ui::SettingsDialog * const _ui; QActionGroup* _actionGroup; // Maps the actions from the action group to the corresponding widgets QHash _actionGroupWidgets; + // Maps the action in the dialog to their according account. Needed in + // case the account avatar changes + QHash _actionForAccount; + QToolBar* _toolBar; ActivitySettings *_activitySettings;