File indexing completed on 2024-04-14 04:36:12
0001 /* This file is part of the KDE project 0002 Copyright (C) 2010-2015 Jarosław Staniek <staniek@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "linestyleedit.h" 0021 #include "KPropertyWidgetsFactory.h" 0022 #include "combobox.h" 0023 #include "KPropertyLineStyleItemDelegate_p.h" 0024 #include "KPropertyUtils_p.h" 0025 0026 #include <QApplication> 0027 #include <QPen> 0028 0029 class Q_DECL_HIDDEN KPropertyLineStyleComboEditor::Private 0030 { 0031 public: 0032 Private() 0033 { 0034 } 0035 }; 0036 0037 KPropertyLineStyleComboEditor::KPropertyLineStyleComboEditor(QWidget *parent) 0038 : KPropertyLineStyleSelector(parent), d(new Private) 0039 { 0040 connect(this, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int))); 0041 0042 int paddingTop = 1; 0043 int paddingLeft = 0; 0044 const QString style(parent->style()->objectName()); 0045 if (!KPropertyUtilsPrivate::gridLineColor(this).isValid()) { 0046 setFrame(false); 0047 paddingTop = 0; 0048 } 0049 if (style == QLatin1String("windows") || style == QLatin1String("fusion")) { 0050 paddingLeft = 3; 0051 } else if (style == QLatin1String("windowsvista")) { 0052 paddingLeft = 2; 0053 } 0054 QString styleSheet = QString::fromLatin1("KPropertyLineStyleSelector { \ 0055 %1 \ 0056 padding-top: %2px; padding-left: %3px; }").arg(KPropertyComboBoxEditor::borderSheet(this)) 0057 .arg(paddingTop).arg(paddingLeft); 0058 setStyleSheet(styleSheet); 0059 } 0060 0061 KPropertyLineStyleComboEditor::~KPropertyLineStyleComboEditor() 0062 { 0063 delete d; 0064 } 0065 0066 QVariant KPropertyLineStyleComboEditor::value() const 0067 { 0068 return int(lineStyle()); 0069 } 0070 0071 static bool hasVisibleStyle(const QVariant &value) 0072 { 0073 return !value.isNull() && value.canConvert(QVariant::Int) && value.toInt() < Qt::CustomDashLine 0074 && value.toInt() >= Qt::NoPen; 0075 } 0076 0077 void KPropertyLineStyleComboEditor::setValue(const QVariant &value) 0078 { 0079 if (!hasVisibleStyle(value)) { 0080 setLineStyle(Qt::NoPen); 0081 return; 0082 } 0083 setLineStyle(static_cast<Qt::PenStyle>(value.toInt())); 0084 } 0085 0086 void KPropertyLineStyleComboEditor::slotValueChanged(int) 0087 { 0088 emit commitData(this); 0089 } 0090 0091 KPropertyLineStyleComboDelegate::KPropertyLineStyleComboDelegate() 0092 { 0093 options()->setBordersVisible(true); 0094 } 0095 0096 QWidget * KPropertyLineStyleComboDelegate::createEditor( int type, QWidget *parent, 0097 const QStyleOptionViewItem & option, const QModelIndex & index ) const 0098 { 0099 Q_UNUSED(type) 0100 Q_UNUSED(option) 0101 Q_UNUSED(index) 0102 return new KPropertyLineStyleComboEditor(parent); 0103 } 0104 0105 void KPropertyLineStyleComboDelegate::paint( QPainter * painter, 0106 const QStyleOptionViewItem & option, const QModelIndex & index ) const 0107 { 0108 const KPropertyUtilsPrivate::PainterSaver saver(painter); 0109 Qt::PenStyle penStyle = Qt::NoPen; 0110 if (hasVisibleStyle(index.data(Qt::EditRole))) { 0111 penStyle = static_cast<Qt::PenStyle>(index.data(Qt::EditRole).toInt()); 0112 } 0113 const QWidget *paintedWidget = dynamic_cast<QWidget*>(painter->device()); 0114 const QStyle *style = paintedWidget ? paintedWidget->style() : qApp->style(); 0115 QStyleOptionComboBox cbOption; 0116 cbOption.rect = option.rect; 0117 QRect r = style->subControlRect(QStyle::CC_ComboBox, &cbOption, QStyle::SC_ComboBoxEditField, nullptr); 0118 r.setRight(option.rect.right() - (r.left() - option.rect.left())); 0119 KPropertyLineStyleItemDelegate::paintItem(painter, QPen(penStyle), r, option); 0120 } 0121 0122 QString KPropertyLineStyleComboDelegate::valueToString(const QVariant& value, const QLocale &locale) const 0123 { 0124 Qt::PenStyle style = (value.isNull() || !value.canConvert(QVariant::Int) || value.toInt() > Qt::CustomDashLine 0125 || value.toInt() < Qt::NoPen) ? Qt::NoPen : static_cast<Qt::PenStyle>(value.toInt()); 0126 return KPropertyLineStyleItemDelegate::styleName(style, locale); 0127 }