File indexing completed on 2024-04-28 05:50:39
0001 /* 0002 SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com> 0003 SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 // Own 0009 #include "ColorSchemeViewDelegate.h" 0010 0011 // Konsole 0012 #include "../config-konsole.h" 0013 #include "ColorScheme.h" 0014 0015 // KDE 0016 #include <KLocalizedString> 0017 0018 // Qt 0019 #include <QApplication> 0020 #include <QPainter> 0021 0022 using namespace Konsole; 0023 0024 ColorSchemeViewDelegate::ColorSchemeViewDelegate(std::function<bool()> compositingActiveHelper, QObject *parent) 0025 : QAbstractItemDelegate(parent) 0026 , m_compositingActive(std::move(compositingActiveHelper)) 0027 { 0028 Q_ASSERT(m_compositingActive); 0029 } 0030 0031 void ColorSchemeViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0032 { 0033 std::shared_ptr<const ColorScheme> scheme = index.data(Qt::UserRole + 1).value<std::shared_ptr<const ColorScheme>>(); 0034 QFont profileFont = index.data(Qt::UserRole + 2).value<QFont>(); 0035 Q_ASSERT(scheme); 0036 if (scheme == nullptr) { 0037 return; 0038 } 0039 0040 painter->setRenderHint(QPainter::Antialiasing); 0041 0042 // Draw background 0043 QStyle *style = option.widget != nullptr ? option.widget->style() : QApplication::style(); 0044 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget); 0045 0046 // Draw name 0047 QPalette::ColorRole textColor = ((option.state & QStyle::State_Selected) != 0) ? QPalette::HighlightedText : QPalette::Text; 0048 painter->setPen(option.palette.color(textColor)); 0049 painter->setFont(option.font); 0050 0051 // Determine width of sample text using profile's font 0052 const QString sampleText = i18n("AaZz09..."); 0053 QFontMetrics profileFontMetrics(profileFont); 0054 const int sampleTextWidth = profileFontMetrics.boundingRect(sampleText).width(); 0055 0056 painter->drawText(option.rect.adjusted(sampleTextWidth + 15, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, index.data(Qt::DisplayRole).toString()); 0057 0058 // Draw the preview 0059 const int x = option.rect.left(); 0060 const int y = option.rect.top(); 0061 0062 QRect previewRect(x + 4, y + 4, sampleTextWidth + 8, option.rect.height() - 8); 0063 0064 if (m_compositingActive()) { 0065 painter->save(); 0066 QColor color = scheme->backgroundColor(); 0067 color.setAlphaF(scheme->opacity()); 0068 painter->setPen(Qt::NoPen); 0069 painter->setCompositionMode(QPainter::CompositionMode_Source); 0070 painter->setBrush(color); 0071 painter->drawRect(previewRect); 0072 painter->restore(); 0073 } else { 0074 painter->setPen(Qt::NoPen); 0075 painter->setBrush(scheme->backgroundColor()); 0076 painter->drawRect(previewRect); 0077 } 0078 0079 // draw color scheme name using scheme's foreground color 0080 QPen pen(scheme->foregroundColor()); 0081 painter->setPen(pen); 0082 0083 // TODO: respect antialias setting 0084 painter->setFont(profileFont); 0085 painter->drawText(previewRect, Qt::AlignCenter, sampleText); 0086 } 0087 0088 QSize ColorSchemeViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const 0089 { 0090 const int width = 200; 0091 const int margin = 5; 0092 const int colorWidth = width / TABLE_COLORS; 0093 const int heightForWidth = (colorWidth * 2) + option.fontMetrics.height() + margin; 0094 0095 // temporary 0096 return {width, heightForWidth}; 0097 } 0098 0099 #include "moc_ColorSchemeViewDelegate.cpp"