File indexing completed on 2024-04-21 15:30:31

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
0003  * Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library 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 GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KPropertyLineStyleItemDelegate_p.h"
0022 #include "KPropertyCoreUtils_p.h"
0023 #include "KPropertyUtils_p.h"
0024 
0025 #include <QPen>
0026 
0027 KPropertyLineStyleItemDelegate::KPropertyLineStyleItemDelegate(QObject * parent)
0028     : QAbstractItemDelegate(parent)
0029 {
0030 }
0031 
0032 KPropertyLineStyleItemDelegate::~KPropertyLineStyleItemDelegate()
0033 {
0034 }
0035 
0036 class PenStyleData : public QHash<Qt::PenStyle, QString>
0037 {
0038 public:
0039     PenStyleData() {
0040         insert(Qt::NoPen, QObject::tr("None", "No Line"));
0041         insert(Qt::SolidLine, QObject::tr("Solid Line"));
0042         insert(Qt::DashLine, QObject::tr("Dash Line"));
0043         insert(Qt::DotLine, QObject::tr("Dot Line"));
0044         insert(Qt::DashDotLine, QObject::tr("Dash-Dot Line"));
0045         insert(Qt::DashDotDotLine, QObject::tr("Dash-Dot-Dot Line"));
0046         insert(Qt::CustomDashLine, QObject::tr("Custom Dash Line"));
0047     }
0048 };
0049 
0050 Q_GLOBAL_STATIC(PenStyleData, g_penStyleData)
0051 
0052 //static
0053 QString KPropertyLineStyleItemDelegate::styleName(Qt::PenStyle style, const QLocale &locale)
0054 {
0055     if (locale.language() == QLocale::C) {
0056         return KPropertyUtils::keyForEnumValue("PenStyle", style);
0057     }
0058     return g_penStyleData->value(style);
0059 }
0060 
0061 //static
0062 void KPropertyLineStyleItemDelegate::paintItem(QPainter *painter, const QPen &pen_,
0063                                                const QRect &rect, const QStyleOption &option)
0064 {
0065     const KPropertyUtilsPrivate::PainterSaver saver(painter);
0066     QPen pen(pen_);
0067     pen.setBrush((option.state & QStyle::State_Selected)
0068                  ? option.palette.highlightedText() : option.palette.text());
0069     if (pen.style() == Qt::NoPen) {
0070         pen.setWidth(0);
0071         pen.setStyle(Qt::SolidLine);
0072         painter->setPen(pen);
0073         painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, g_penStyleData->value(Qt::NoPen));
0074     } else {
0075         pen.setWidth(3);
0076         painter->setPen(pen);
0077         painter->drawLine(rect.left(), rect.center().y(), rect.right(), rect.center().y());
0078     }
0079 }
0080 
0081 void KPropertyLineStyleItemDelegate::paint(QPainter *painter,
0082                                            const QStyleOptionViewItem &option,
0083                                            const QModelIndex &index) const
0084 {
0085     const KPropertyUtilsPrivate::PainterSaver saver(painter);
0086     if (option.state & QStyle::State_Selected) {
0087         painter->fillRect(option.rect, option.palette.highlight());
0088     }
0089 
0090     QPen pen = index.data(Qt::DecorationRole).value<QPen>();
0091     paintItem(painter, pen, option.rect, option);
0092 }
0093 
0094 QSize KPropertyLineStyleItemDelegate::sizeHint(const QStyleOptionViewItem &option,
0095                                                const QModelIndex &index) const
0096 {
0097     Q_UNUSED(option);
0098     Q_UNUSED(index);
0099     return QSize(100, 15);
0100 }