Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -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 * |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "accountstatedelegate.h" |
| 20 | |
| 21 | #include "accountmodel.h" |
| 22 | #include "account.h" |
| 23 | |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 24 | AccountStateDelegate::AccountStateDelegate(QObject* parent) : |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 25 | QStyledItemDelegate(parent) |
| 26 | {} |
| 27 | |
| 28 | void |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 29 | AccountStateDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 30 | { |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 31 | painter->setRenderHint(QPainter::Antialiasing); |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 32 | QStyleOptionViewItemV4 opt = option; |
| 33 | initStyleOption(&opt, index); |
| 34 | if (index.column() == 0) { |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 35 | // name & checkbox |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 36 | auto name = index.model()->data(index, Qt::DisplayRole).toString(); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 37 | opt.text = QString(); |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 38 | QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 39 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
| 40 | auto rect = opt.rect; |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 41 | auto font = painter->font(); |
| 42 | font.setPointSize(12); |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 43 | painter->setFont(font); |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 44 | painter->setOpacity(1.0); |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 45 | opt.displayAlignment = Qt::AlignTop; |
| 46 | |
| 47 | painter->setPen(Qt::black); |
| 48 | painter->drawText(QRect(rect.left() + 25, rect.top(), |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 49 | rect.width(), rect.height()), |
| 50 | opt.displayAlignment, name); |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 51 | |
| 52 | // status |
| 53 | auto account = AccountModel::instance().getAccountByModelIndex(index); |
| 54 | QString stateColor(account->stateColorName()); |
| 55 | QString accountStatus = account->toHumanStateName(); |
| 56 | |
| 57 | painter->setPen(stateColor); |
| 58 | |
| 59 | opt.displayAlignment = Qt::AlignBottom|Qt::AlignLeft; |
| 60 | |
| 61 | painter->drawText(QRect(rect.left() + 25, rect.top(), |
| 62 | rect.width(), rect.height()), |
| 63 | opt.displayAlignment, accountStatus); |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | QSize |
Nicolas Jager | 74fe46f | 2016-02-29 14:55:09 -0500 | [diff] [blame] | 68 | AccountStateDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const |
Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame] | 69 | { |
| 70 | QSize result = QStyledItemDelegate::sizeHint(option, index); |
| 71 | return result; |
| 72 | } |