File indexing completed on 2024-12-01 04:36:37

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "commandcompletiondelegate.h"
0008 
0009 #include "common/delegatepaintutil.h"
0010 #include "model/commandsmodel.h"
0011 
0012 #include <QPainter>
0013 
0014 CommandCompletionDelegate::CommandCompletionDelegate(QObject *parent)
0015     : QItemDelegate(parent)
0016 {
0017 }
0018 
0019 CommandCompletionDelegate::~CommandCompletionDelegate() = default;
0020 
0021 void CommandCompletionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0022 {
0023     // command <parameter> description at end
0024     drawBackground(painter, option, index);
0025 
0026     if (option.state & QStyle::State_Selected) {
0027         painter->fillRect(option.rect, option.palette.highlight());
0028     }
0029 
0030     const int margin = DelegatePaintUtil::margin();
0031     const QFont oldFont = painter->font();
0032     const QFontMetrics commandFontMetrics(oldFont);
0033     const QString commandText = index.data(CommandsModel::CommandName).toString();
0034     const int commandWidth = commandFontMetrics.horizontalAdvance(commandText);
0035 
0036     const int defaultCharHeight = option.rect.y() + commandFontMetrics.ascent();
0037 
0038     painter->drawText(margin, defaultCharHeight, commandText);
0039 
0040     QFont italicFont = oldFont;
0041     italicFont.setItalic(true);
0042     painter->setFont(italicFont);
0043 
0044     const QPen oldPen = painter->pen();
0045     QColor col = painter->pen().color();
0046     col.setAlpha(128);
0047     painter->setPen(col);
0048 
0049     const QString parameter = index.data(CommandsModel::TranslatedParams).toString();
0050     const int xText = option.rect.x() + 3 * margin + commandWidth;
0051     painter->drawText(xText, defaultCharHeight, parameter);
0052 
0053     painter->setFont(oldFont);
0054 
0055     const QString description = index.data(CommandsModel::TranslatedDescription).toString();
0056     const int descriptionWidth = commandFontMetrics.horizontalAdvance(description);
0057 
0058     painter->drawText(option.rect.width() - descriptionWidth - margin, defaultCharHeight, description);
0059 
0060     painter->setPen(oldPen);
0061 }