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

0001 /***************************************************************************
0002  *  Copyright (C) 2021 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software 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 "taglistdelegate.h"
0021 
0022 #include <QDebug>
0023 #include <QFontMetrics>
0024 #include <QPainter>
0025 
0026 TagListDelegate::TagListDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
0027 
0028 TagListDelegate::~TagListDelegate() {}
0029 
0030 void TagListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0031 {
0032     QStyledItemDelegate::paint(painter, option, index);
0033     auto tags= index.data().toStringList();
0034 
0035     int margex= 4;
0036     int margey= 4;
0037 
0038     // qDebug() << option.palette.highlightedText() << option.palette.brightText();
0039 
0040     if(option.state & QStyle::State_Selected)
0041     {
0042         painter->setPen(option.palette.highlightedText().color());
0043     }
0044     else
0045     {
0046         painter->setPen(option.palette.text().color());
0047     }
0048 
0049     int startx= option.rect.x() + margex;
0050     int starty= option.rect.y() + margey;
0051     auto fontmetrics= option.fontMetrics;
0052     for(auto const& tag : qAsConst(tags))
0053     {
0054         auto rect= fontmetrics.boundingRect(tag);
0055 
0056         if(startx + rect.width() > option.rect.x() + option.rect.width())
0057         {
0058             startx= option.rect.x() + margex;
0059             starty+= rect.height() + margey;
0060         }
0061 
0062         rect.translate(startx, starty - rect.y());
0063         painter->drawText(rect, tag);
0064         painter->drawRoundedRect(rect, 2, 2);
0065         startx+= rect.width() + margex;
0066     }
0067 }
0068 
0069 QSize TagListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0070 {
0071     QSize ret= QStyledItemDelegate::sizeHint(option, index);
0072     ret= ret * 1.3; // add some more padding between items
0073     return ret;
0074 }