Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Anthony Léonard | 2fde81d | 2017-04-17 10:06:55 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 3 | * 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éonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 23 | #include "availableaccountmodel.h" |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 24 | #include "account.h" |
| 25 | #include "qstandarditemmodel.h" |
| 26 | #include "profilemodel.h" |
| 27 | #include "profile.h" |
| 28 | #include "person.h" |
| 29 | #include "utils.h" |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame] | 30 | #include "globalinstances.h" |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 31 | |
| 32 | CurrentAccountWidget::CurrentAccountWidget(QWidget *parent) : |
| 33 | QWidget(parent), |
| 34 | ui(new Ui::CurrentAccountWidget) |
| 35 | { |
| 36 | ui->setupUi(this); |
| 37 | setup(); |
| 38 | } |
| 39 | |
| 40 | CurrentAccountWidget::~CurrentAccountWidget() |
| 41 | { |
| 42 | delete ui; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | CurrentAccountWidget::setup() |
| 47 | { |
| 48 | ui->accountsStatus->setText("No enabled account: impossible to communicate!"); |
| 49 | ui->accountsStatus->hide(); |
Anthony Léonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 50 | ui->currentAccountSelector->setModel(&AvailableAccountModel::instance()); |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 51 | updateAccounts(); |
| 52 | if (ui->currentAccountSelector->count() > 0) { |
| 53 | ui->currentAccountSelector->setCurrentIndex(0); |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 54 | qDebug() << "CurrentAccount : setup over"; |
| 55 | } else { |
| 56 | qDebug() << "CurrentAccount : No account available"; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | CurrentAccountWidget::update() |
| 62 | { |
| 63 | updateAccounts(); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | CurrentAccountWidget::updateAccounts() |
| 69 | { |
| 70 | auto selector = ui->currentAccountSelector; |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 71 | |
| 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 SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 86 | void |
| 87 | CurrentAccountWidget::setPhoto() |
| 88 | { |
| 89 | auto selector = ui->currentAccountSelector; |
| 90 | if (selector->count() > 0) { |
Anthony Léonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 91 | if (ProfileModel::instance().selectedProfile()) { |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame] | 92 | 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éonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 96 | qDebug() << "CurrentAccount : Photo set"; |
| 97 | } else |
| 98 | qDebug() << "CurrentAccount : selected profile has no person"; |
| 99 | } else |
| 100 | qDebug() << "CurrentAccount : Profilemodel: no selected profile"; |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 101 | } else { |
| 102 | qDebug() << "CurrentAccount : account not set"; |
| 103 | ui->currentAccountPixmap->setPixmap(QPixmap()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | CurrentAccountWidget::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éonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 114 | AvailableAccountModel::instance().selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect); |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 115 | setPhoto(); |
| 116 | } else { |
| 117 | qDebug() << "CurrentAccount : account not referenced correctly"; |
| 118 | //null for now |
| 119 | } |
| 120 | } |
| 121 | |
Anthony Léonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 122 | void |
| 123 | CurrentAccountWidget::changeSelectedIndex(int index) |
| 124 | { |
| 125 | if (index != ui->currentAccountSelector->currentIndex()) { |
| 126 | ui->currentAccountSelector->setCurrentIndex(index); |
| 127 | } |
| 128 | } |
| 129 | |