blob: b64db0aad9a2ac5b8cc2d28babdbbb55db48947e [file] [log] [blame]
Edric Milaret5d61a062015-06-12 11:16:08 -04001/***************************************************************************
Anthony LĂ©onard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 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;
Nicolas Jager74fe46f2016-02-29 14:55:09 -050041 auto font = painter->font();
42 font.setPointSize(12);
Edric Milaret5d61a062015-06-12 11:16:08 -040043 painter->setFont(font);
Edric Milaret5d61a062015-06-12 11:16:08 -040044 painter->setOpacity(1.0);
Nicolas Jager74fe46f2016-02-29 14:55:09 -050045 opt.displayAlignment = Qt::AlignTop;
46
47 painter->setPen(Qt::black);
48 painter->drawText(QRect(rect.left() + 25, rect.top(),
Edric Milaret5d61a062015-06-12 11:16:08 -040049 rect.width(), rect.height()),
50 opt.displayAlignment, name);
Nicolas Jager74fe46f2016-02-29 14:55:09 -050051
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 Milaret5d61a062015-06-12 11:16:08 -040064 }
65}
66
67QSize
Nicolas Jager74fe46f2016-02-29 14:55:09 -050068AccountStateDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
Edric Milaret5d61a062015-06-12 11:16:08 -040069{
70 QSize result = QStyledItemDelegate::sizeHint(option, index);
71 return result;
72}