File indexing completed on 2024-05-05 05:40:57

0001 /***************************************************************************
0002  *     Copyright (C) 2009 by Renaud Guezennec                             *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "userlistdelegate.h"
0021 #include <QDebug>
0022 #include <QImage>
0023 #include <QPainter>
0024 
0025 #include "data/person.h"
0026 
0027 UserListDelegate::UserListDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
0028 void UserListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0029 {
0030     if(!index.isValid())
0031     {
0032         return;
0033     }
0034     Person* p= static_cast<Person*>(index.internalPointer());
0035     if(p == nullptr)
0036         return;
0037     painter->save();
0038 
0039     QRect tmp= option.rect;
0040     if(option.state & QStyle::State_Selected)
0041     {
0042         painter->fillRect(option.rect, option.palette.highlight());
0043     }
0044 
0045     painter->fillRect(option.rect.x(), option.rect.y(), option.decorationSize.width(), option.rect.height(),
0046                       p->getColor());
0047     QRectF target(option.rect.x(), option.rect.y(), option.decorationSize.width(), option.rect.height());
0048     auto img= QImage::fromData(p->avatar());
0049     painter->drawImage(target, img, img.rect());
0050     tmp.adjust(option.decorationSize.width(), 1, 1, 1);
0051     painter->drawText(tmp, Qt::AlignVCenter, p->name());
0052 
0053     painter->restore();
0054 }
0055 QSize UserListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0056 {
0057     QSize returnValue;
0058     Person* p= static_cast<Person*>(index.internalPointer());
0059     if(p != nullptr)
0060     {
0061         returnValue.setWidth(option.fontMetrics.horizontalAdvance(p->name()));
0062         returnValue.setHeight(/*option.fontMetrics.height()*/ option.decorationSize.height());
0063     }
0064 
0065     return returnValue;
0066 }