blob: 96932173cc5534cc7fadf74a69c3f22607489c55 [file] [log] [blame]
Olivier SOLDANO9657fd12017-03-27 16:06:53 -04001/***************************************************************************
Anthony Léonard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 by Savoir-faire Linux *
Olivier SOLDANO9657fd12017-03-27 16:06:53 -04003 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> *
4 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 **************************************************************************/
19
20#include "currentaccountwidget.h"
21#include "ui_currentaccountwidget.h"
22#include "globalinstances.h"
Anthony Léonard2d99df02017-04-19 16:35:19 -040023#include "availableaccountmodel.h"
Olivier SOLDANO9657fd12017-03-27 16:06:53 -040024#include "account.h"
25#include "qstandarditemmodel.h"
26#include "profilemodel.h"
27#include "profile.h"
28#include "person.h"
29#include "utils.h"
Andreas Traczyk072291f2018-08-16 16:48:58 -040030#include "globalinstances.h"
Olivier SOLDANO9657fd12017-03-27 16:06:53 -040031
32CurrentAccountWidget::CurrentAccountWidget(QWidget *parent) :
33 QWidget(parent),
34 ui(new Ui::CurrentAccountWidget)
35{
36 ui->setupUi(this);
37 setup();
38}
39
40CurrentAccountWidget::~CurrentAccountWidget()
41{
42 delete ui;
43}
44
45void
46CurrentAccountWidget::setup()
47{
48 ui->accountsStatus->setText("No enabled account: impossible to communicate!");
49 ui->accountsStatus->hide();
Anthony Léonard2d99df02017-04-19 16:35:19 -040050 ui->currentAccountSelector->setModel(&AvailableAccountModel::instance());
Olivier SOLDANO9657fd12017-03-27 16:06:53 -040051 updateAccounts();
52 if (ui->currentAccountSelector->count() > 0) {
53 ui->currentAccountSelector->setCurrentIndex(0);
Olivier SOLDANO9657fd12017-03-27 16:06:53 -040054 qDebug() << "CurrentAccount : setup over";
55 } else {
56 qDebug() << "CurrentAccount : No account available";
57 }
58}
59
60void
61CurrentAccountWidget::update()
62{
63 updateAccounts();
64
65}
66
67void
68CurrentAccountWidget::updateAccounts()
69{
70 auto selector = ui->currentAccountSelector;
Olivier SOLDANO9657fd12017-03-27 16:06:53 -040071
72 if (selector->count() <= 1){
73 selector->hide();
74 if (selector->count() < 1) {
75 ui->accountsStatus->show();
76 setPhoto();
77 } else {
78 ui->accountsStatus->hide();
79 }
80 } else {
81 selector->show();
82 ui->accountsStatus->hide();
83 }
84}
85
Olivier SOLDANO9657fd12017-03-27 16:06:53 -040086void
87CurrentAccountWidget::setPhoto()
88{
89 auto selector = ui->currentAccountSelector;
90 if (selector->count() > 0) {
Anthony Léonard2d99df02017-04-19 16:35:19 -040091 if (ProfileModel::instance().selectedProfile()) {
Andreas Traczyk072291f2018-08-16 16:48:58 -040092 if (auto p = ProfileModel::instance().selectedProfile()->person()) {
93 QVariant avatarImage = ProfileModel::instance().selectedProfile()->person()->roleData(Qt::DecorationRole);
94 QImage image = Utils::getCirclePhoto(avatarImage.value<QImage>(), ui->idDisplayLayout->contentsRect().height());
95 ui->currentAccountPixmap->setPixmap(QPixmap::fromImage(image));
Anthony Léonard2d99df02017-04-19 16:35:19 -040096 qDebug() << "CurrentAccount : Photo set";
97 } else
98 qDebug() << "CurrentAccount : selected profile has no person";
99 } else
100 qDebug() << "CurrentAccount : Profilemodel: no selected profile";
Olivier SOLDANO9657fd12017-03-27 16:06:53 -0400101 } else {
102 qDebug() << "CurrentAccount : account not set";
103 ui->currentAccountPixmap->setPixmap(QPixmap());
104 }
105}
106
107void
108CurrentAccountWidget::on_currentAccountSelector_currentIndexChanged(int index)
109{
110 QModelIndex idx = ui->currentAccountSelector->model()->index(index,0);
111 Account* ac = AccountModel::instance().getAccountByModelIndex(idx);
112
113 if (ac) {
Anthony Léonard2d99df02017-04-19 16:35:19 -0400114 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
Olivier SOLDANO9657fd12017-03-27 16:06:53 -0400115 setPhoto();
116 } else {
117 qDebug() << "CurrentAccount : account not referenced correctly";
118 //null for now
119 }
120}
121
Anthony Léonard2d99df02017-04-19 16:35:19 -0400122void
123CurrentAccountWidget::changeSelectedIndex(int index)
124{
125 if (index != ui->currentAccountSelector->currentIndex()) {
126 ui->currentAccountSelector->setCurrentIndex(index);
127 }
128}
129