Warning, file /office/calligra/libs/widgets/KoLineStyleSelector.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
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 "KoLineStyleSelector.h"
0021 #include "KoLineStyleModel_p.h"
0022 #include "KoLineStyleItemDelegate_p.h"
0023 
0024 #include <QPen>
0025 #include <QPainter>
0026 
0027 class Q_DECL_HIDDEN KoLineStyleSelector::Private
0028 {
0029 public:
0030     Private(QWidget *parent)
0031     : model(new KoLineStyleModel(parent))
0032     {
0033     }
0034 
0035     KoLineStyleModel *model;
0036 };
0037 
0038 KoLineStyleSelector::KoLineStyleSelector(QWidget *parent)
0039     : QComboBox(parent), d(new Private(this))
0040 {
0041     setModel(d->model);
0042     setItemDelegate(new KoLineStyleItemDelegate(this));
0043 }
0044 
0045 KoLineStyleSelector::~KoLineStyleSelector()
0046 {
0047     delete d;
0048 }
0049 
0050 void KoLineStyleSelector::paintEvent(QPaintEvent *pe)
0051 {
0052     QComboBox::paintEvent(pe);
0053 
0054     QStyleOptionComboBox option;
0055     option.initFrom(this);
0056     option.frame = hasFrame();
0057     QRect r = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
0058     if (!option.frame) // frameless combo boxes have smaller margins but styles do not take this into account
0059         r.adjust(-14, 0, 14, 1);
0060 
0061     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0062     pen.setBrush(option.palette.text()); // use the view-specific palette; the model hardcodes this to black
0063 
0064     QPainter painter(this);
0065     painter.setPen(pen);
0066     painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y());
0067 }
0068 
0069 bool KoLineStyleSelector::addCustomStyle(const QVector<qreal> &style)
0070 {
0071     return d->model->addCustomStyle(style);
0072 }
0073 
0074 void KoLineStyleSelector::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 KoLineStyleSelector::lineStyle() const
0082 {
0083     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0084     return pen.style();
0085 }
0086 
0087 QVector<qreal> KoLineStyleSelector::lineDashes() const
0088 {
0089     QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
0090     return pen.dashPattern();
0091 }