Edric Milaret | 5d61a06 | 2015-06-12 11:16:08 -0400 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015 by Savoir-Faire Linux * |
| 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 | |
| 24 | AccountStateDelegate::AccountStateDelegate(QObject *parent) : |
| 25 | QStyledItemDelegate(parent) |
| 26 | {} |
| 27 | |
| 28 | void |
| 29 | AccountStateDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 30 | { |
| 31 | QStyleOptionViewItemV4 opt = option; |
| 32 | initStyleOption(&opt, index); |
| 33 | if (index.column() == 0) { |
| 34 | auto name = index.model()->data(index, Qt::DisplayRole).toString(); |
| 35 | opt.text = ""; |
| 36 | QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); |
| 37 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
| 38 | auto rect = opt.rect; |
| 39 | QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? |
| 40 | QPalette::Normal : QPalette::Disabled; |
| 41 | if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) |
| 42 | cg = QPalette::Inactive; |
| 43 | auto font = painter->font() ; |
| 44 | font.setBold(true); |
| 45 | painter->setFont(font); |
| 46 | painter->setPen(AccountModel::instance()-> |
| 47 | getAccountByModelIndex(index)->stateColorName()); |
| 48 | painter->setOpacity(1.0); |
| 49 | painter->drawText(QRect(rect.left()+25, rect.top(), |
| 50 | rect.width(), rect.height()), |
| 51 | opt.displayAlignment, name); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | QSize |
| 56 | AccountStateDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 57 | { |
| 58 | QSize result = QStyledItemDelegate::sizeHint(option, index); |
| 59 | return result; |
| 60 | } |