File indexing completed on 2024-05-12 16:02:09

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KoLineStyleSelector.h"
0008 #include "KoLineStyleModel_p.h"
0009 #include "KoLineStyleItemDelegate_p.h"
0010 
0011 #include <QPen>
0012 #include <QPainter>
0013 
0014 class Q_DECL_HIDDEN KoLineStyleSelector::Private
0015 {
0016 public:
0017     Private(QWidget *parent)
0018     : model(new KoLineStyleModel(parent))
0019     {
0020     }
0021 
0022     KoLineStyleModel *model;
0023 };
0024 
0025 KoLineStyleSelector::KoLineStyleSelector(QWidget *parent)
0026     : QComboBox(parent), d(new Private(this))
0027 {
0028     setModel(d->model);
0029     setItemDelegate(new KoLineStyleItemDelegate(this));
0030 }
0031 
0032 KoLineStyleSelector::~KoLineStyleSelector()
0033 {
0034     delete d;
0035 }
0036 
0037 void KoLineStyleSelector::paintEvent(QPaintEvent *pe)
0038 {
0039     QComboBox::paintEvent(pe);
0040 
0041     QStyleOptionComboBox option;
0042     option.initFrom(this);
0043     option.frame = hasFrame();
0044     QRect r = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
0045     if (!option.frame) // frameless combo boxes have smaller margins but styles do not take this into account
0046         r.adjust(-14, 0, 14, 1);
0047 
0048     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0049 
0050     QPainter painter(this);
0051     painter.setPen(pen);
0052     if (!(option.state & QStyle::State_Enabled)) {
0053         painter.setOpacity(0.5);
0054     }
0055     painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y());
0056 }
0057 
0058 bool KoLineStyleSelector::addCustomStyle(const QVector<qreal> &style)
0059 {
0060     return d->model->addCustomStyle(style);
0061 }
0062 
0063 void KoLineStyleSelector::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes)
0064 {
0065     int index = d->model->setLineStyle(style, dashes);
0066     if (index >= 0)
0067         setCurrentIndex(index);
0068 }
0069 
0070 Qt::PenStyle KoLineStyleSelector::lineStyle() const
0071 {
0072     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0073     return pen.style();
0074 }
0075 
0076 QVector<qreal> KoLineStyleSelector::lineDashes() const
0077 {
0078     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0079     return pen.dashPattern();
0080 }