File indexing completed on 2024-04-14 04:36:15

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 "KPropertyLineStyleSelector_p.h"
0022 #include "KPropertyLineStyleModel_p.h"
0023 #include "KPropertyLineStyleItemDelegate_p.h"
0024 #include "KPropertyWidgetsFactory.h"
0025 
0026 #include <QPen>
0027 #include <QPainter>
0028 
0029 class Q_DECL_HIDDEN KPropertyLineStyleSelector::Private
0030 {
0031 public:
0032     Private(QWidget *parent)
0033     : model(new KPropertyLineStyleModel(parent))
0034     {
0035     }
0036 
0037     KPropertyLineStyleModel *model;
0038 };
0039 
0040 KPropertyLineStyleSelector::KPropertyLineStyleSelector(QWidget *parent)
0041     : QComboBox(parent), d(new Private(this))
0042 {
0043     setModel(d->model);
0044     setItemDelegate(new KPropertyLineStyleItemDelegate(this));
0045     setEditable(false);
0046     setInsertPolicy(QComboBox::NoInsert);
0047     setContextMenuPolicy(Qt::NoContextMenu);
0048 }
0049 
0050 KPropertyLineStyleSelector::~KPropertyLineStyleSelector()
0051 {
0052     delete d;
0053 }
0054 
0055 void KPropertyLineStyleSelector::paintEvent(QPaintEvent *pe)
0056 {
0057     QComboBox::paintEvent(pe);
0058 
0059     QStyleOptionComboBox option;
0060     option.initFrom(this);
0061     option.frame = hasFrame();
0062     QRect r = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
0063     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0064     QPainter painter(this);
0065     KPropertyLineStyleItemDelegate::paintItem(&painter, pen, r, option);
0066     KPropertyWidgetsFactory::paintTopGridLine(this);
0067 }
0068 
0069 bool KPropertyLineStyleSelector::addCustomStyle(const QVector<qreal> &style)
0070 {
0071     return d->model->addCustomStyle(style);
0072 }
0073 
0074 void KPropertyLineStyleSelector::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes)
0075 {
0076     int index = d->model->setLineStyle(style, dashes);
0077     if (index >= 0)
0078         setCurrentIndex(index);
0079 }
0080 
0081 Qt::PenStyle KPropertyLineStyleSelector::lineStyle() const
0082 {
0083     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0084     return pen.style();
0085 }
0086 
0087 QVector<qreal> KPropertyLineStyleSelector::lineDashes() const
0088 {
0089     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0090     return pen.dashPattern();
0091 }