blob: 09c063492718fa2a55502d176c2b99e149dcf6cc [file] [log] [blame]
Edric Milaret5d61a062015-06-12 11:16:08 -04001/***************************************************************************
Edric Milaretbab169d2016-01-07 15:13:33 -05002 * Copyright (C) 2015-2016 by Savoir-faire Linux *
Edric Milaret5d61a062015-06-12 11:16:08 -04003 * 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 Jager74fe46f2016-02-29 14:55:09 -050024AccountStateDelegate::AccountStateDelegate(QObject* parent) :
Edric Milaret5d61a062015-06-12 11:16:08 -040025 QStyledItemDelegate(parent)
26{}
27
28void
Nicolas Jager74fe46f2016-02-29 14:55:09 -050029AccountStateDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
Edric Milaret5d61a062015-06-12 11:16:08 -040030{
Nicolas Jager74fe46f2016-02-29 14:55:09 -050031 painter->setRenderHint(QPainter::Antialiasing);
Edric Milaret5d61a062015-06-12 11:16:08 -040032 QStyleOptionViewItemV4 opt = option;
33 initStyleOption(&opt, index);
34 if (index.column() == 0) {
Nicolas Jager74fe46f2016-02-29 14:55:09 -050035 // name & checkbox
Edric Milaret5d61a062015-06-12 11:16:08 -040036 auto name = index.model()->data(index, Qt::DisplayRole).toString();
Edric Milareted0b2802015-10-01 15:10:02 -040037 opt.text = QString();
Nicolas Jager74fe46f2016-02-29 14:55:09 -050038 QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
Edric Milaret5d61a062015-06-12 11:16:08 -040039 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
40 auto rect = opt.rect;
41 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ?
42 QPalette::Normal : QPalette::Disabled;
43 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
44 cg = QPalette::Inactive;
Nicolas Jager74fe46f2016-02-29 14:55:09 -050045 auto font = painter->font();
46 font.setPointSize(12);
Edric Milaret5d61a062015-06-12 11:16:08 -040047 painter->setFont(font);
Edric Milaret5d61a062015-06-12 11:16:08 -040048 painter->setOpacity(1.0);
Nicolas Jager74fe46f2016-02-29 14:55:09 -050049 opt.displayAlignment = Qt::AlignTop;
50
51 painter->setPen(Qt::black);
52 painter->drawText(QRect(rect.left() + 25, rect.top(),
Edric Milaret5d61a062015-06-12 11:16:08 -040053 rect.width(), rect.height()),
54 opt.displayAlignment, name);
Nicolas Jager74fe46f2016-02-29 14:55:09 -050055
56 // status
57 auto account = AccountModel::instance().getAccountByModelIndex(index);
58 QString stateColor(account->stateColorName());
59 QString accountStatus = account->toHumanStateName();
60
61 painter->setPen(stateColor);
62
63 opt.displayAlignment = Qt::AlignBottom|Qt::AlignLeft;
64
65 painter->drawText(QRect(rect.left() + 25, rect.top(),
66 rect.width(), rect.height()),
67 opt.displayAlignment, accountStatus);
Edric Milaret5d61a062015-06-12 11:16:08 -040068 }
69}
70
71QSize
Nicolas Jager74fe46f2016-02-29 14:55:09 -050072AccountStateDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
Edric Milaret5d61a062015-06-12 11:16:08 -040073{
74 QSize result = QStyledItemDelegate::sizeHint(option, index);
75 return result;
76}