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" |
| 30 | |
| 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 | |
| 86 | |
| 87 | |
| 88 | void |
| 89 | CurrentAccountWidget::setPhoto() |
| 90 | { |
| 91 | auto selector = ui->currentAccountSelector; |
| 92 | if (selector->count() > 0) { |
Anthony Léonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 93 | if (ProfileModel::instance().selectedProfile()) { |
| 94 | if (ProfileModel::instance().selectedProfile()->person()){ |
| 95 | QImage img = Utils::getCirclePhoto(ProfileModel::instance().selectedProfile()->person()->photo().value<QImage>(), |
| 96 | ui->idDisplayLayout->contentsRect().height()); |
| 97 | ui->currentAccountPixmap->setPixmap(QPixmap::fromImage(img)); |
| 98 | qDebug() << "CurrentAccount : Photo set"; |
| 99 | } else |
| 100 | qDebug() << "CurrentAccount : selected profile has no person"; |
| 101 | } else |
| 102 | qDebug() << "CurrentAccount : Profilemodel: no selected profile"; |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 103 | } else { |
| 104 | qDebug() << "CurrentAccount : account not set"; |
| 105 | ui->currentAccountPixmap->setPixmap(QPixmap()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | CurrentAccountWidget::on_currentAccountSelector_currentIndexChanged(int index) |
| 111 | { |
| 112 | QModelIndex idx = ui->currentAccountSelector->model()->index(index,0); |
| 113 | Account* ac = AccountModel::instance().getAccountByModelIndex(idx); |
| 114 | |
| 115 | if (ac) { |
Anthony Léonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 116 | AvailableAccountModel::instance().selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect); |
Olivier SOLDANO | 9657fd1 | 2017-03-27 16:06:53 -0400 | [diff] [blame] | 117 | setPhoto(); |
| 118 | } else { |
| 119 | qDebug() << "CurrentAccount : account not referenced correctly"; |
| 120 | //null for now |
| 121 | } |
| 122 | } |
| 123 | |
Anthony Léonard | 2d99df0 | 2017-04-19 16:35:19 -0400 | [diff] [blame] | 124 | void |
| 125 | CurrentAccountWidget::changeSelectedIndex(int index) |
| 126 | { |
| 127 | if (index != ui->currentAccountSelector->currentIndex()) { |
| 128 | ui->currentAccountSelector->setCurrentIndex(index); |
| 129 | } |
| 130 | } |
| 131 | |