KoLineStyleSelector.cpp 2.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* This file is part of the KDE project
 * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "KoLineStyleSelector.h"
21 22
#include "KoLineStyleModel_p.h"
#include "KoLineStyleItemDelegate_p.h"
23 24 25 26

#include <QPen>
#include <QPainter>

27
class Q_DECL_HIDDEN KoLineStyleSelector::Private
28 29
{
public:
30 31
    Private(QWidget *parent)
    : model(new KoLineStyleModel(parent))
32 33 34
    {
    }

35
    KoLineStyleModel *model;
36 37
};

38 39
KoLineStyleSelector::KoLineStyleSelector(QWidget *parent)
    : QComboBox(parent), d(new Private(this))
40
{
41 42
    setModel(d->model);
    setItemDelegate(new KoLineStyleItemDelegate(this));
43 44 45 46 47 48 49
}

KoLineStyleSelector::~KoLineStyleSelector()
{
    delete d;
}

50
void KoLineStyleSelector::paintEvent(QPaintEvent *pe)
51
{
52
    QComboBox::paintEvent(pe);
53 54

    QStyleOptionComboBox option;
55
    option.initFrom(this);
56
    option.frame = hasFrame();
57
    QRect r = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
58 59
    if (!option.frame) // frameless combo boxes have smaller margins but styles do not take this into account
        r.adjust(-14, 0, 14, 1);
60

61
    QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
62

63 64
    QPainter painter(this);
    painter.setPen(pen);
65 66 67
    if (!(option.state & QStyle::State_Enabled)) {
        painter.setOpacity(0.5);
    }
68
    painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y());
69 70
}

71
bool KoLineStyleSelector::addCustomStyle(const QVector<qreal> &style)
72
{
73
    return d->model->addCustomStyle(style);
74 75
}

76
void KoLineStyleSelector::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes)
77
{
78 79 80
    int index = d->model->setLineStyle(style, dashes);
    if (index >= 0)
        setCurrentIndex(index);
81 82 83 84
}

Qt::PenStyle KoLineStyleSelector::lineStyle() const
{
85
    QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
86 87 88 89 90
    return pen.style();
}

QVector<qreal> KoLineStyleSelector::lineDashes() const
{
91
    QPen pen = itemData(currentIndex(), Qt::DecorationRole).value<QPen>();
92 93
    return pen.dashPattern();
}